public void update(int offset, int replacedTextLength, String newText) { if (!isLoaded()) { throw new IllegalStateException("You can't update an unloaded resource."); } try { isUpdating = true; IParseResult oldParseResult = parseResult; ReplaceRegion replaceRegion = new ReplaceRegion(new TextRegion(offset, replacedTextLength), newText); IParseResult newParseResult = parser.reparse(oldParseResult, replaceRegion); updateInternalState(oldParseResult, newParseResult); } finally { isUpdating = false; } }
/** * @return the cross referenced EObject under, right or left to the cursor (in that order) or * <code>null</code> if there is no cross referenced object next to the offset. */ public EObject resolveCrossReferencedElementAt(XtextResource resource, int offset) { INode node = getCrossReferenceNode(resource, new TextRegion(offset, 0)); if (node != null) return getCrossReferencedElement(node); return null; }
public ITextRegion addTrailingWhitespace(ITextRegion textRegion, XtextResource resource) { String text = resource.getParseResult().getRootNode().getText(); int offset = textRegion.getOffset(); int length = textRegion.getLength(); while(offset + length < text.length() && Character.isWhitespace(text.charAt(offset+length))) { ++length; } return new TextRegion(offset, length); }
public IOutlineNode createRoot(IXtextDocument document, ImageDescriptor imageDescriptor, Object text, IOutlineTreeStructureProvider provider) { DocumentRootNode documentNode = new DocumentRootNode(imageDescriptor, text, document, provider); documentNode.setTextRegion(new TextRegion(0, document.getLength())); return documentNode; }
public ITextRegion addTrailingSingleWhitespace(ITextRegion textRegion, String lineSeparator, XtextResource resource) { String text = resource.getParseResult().getRootNode().getText(); String theFollowing = text.substring(textRegion.getOffset() + textRegion.getLength()); if(!isEmpty(theFollowing)) { if(theFollowing.startsWith(lineSeparator)) return new TextRegion(textRegion.getOffset(), textRegion.getLength() + lineSeparator.length()); else if(Character.isWhitespace(theFollowing.charAt(0))) return new TextRegion(textRegion.getOffset(), textRegion.getLength() + 1); } return textRegion; } }
@Override public ITextRegion merge(ITextRegion other) { if (contains(other)) return this; if (other.contains(this)) return other; int newOffset = Math.min(offset, other.getOffset()); int newLength = Math.max(offset + length, other.getOffset() + other.getLength()) - newOffset; return new TextRegion(newOffset, newLength); }
public ITextRegion merge(ITextRegion other) { if (contains(other)) return this; if (other.contains(this)) return other; int newOffset = Math.min(offset, other.getOffset()); int newLength = Math.max(offset + length, other.getOffset() + other.getLength()) - newOffset; return new TextRegion(newOffset, newLength); }
public ITextRegion addLeadingWhitespace(ITextRegion textRegion, XtextResource resource) { String text = resource.getParseResult().getRootNode().getText(); int offset = textRegion.getOffset(); int length = textRegion.getLength(); while(offset > 0 && Character.isWhitespace(text.charAt(offset-1)) ){ --offset; ++length; } return new TextRegion(offset, length); }
protected void addSectionToAppend(IAcceptor<ReplaceRegion> acceptor) { StringBuilder importDeclarationsToAppend = getImportDeclarationsToAppend(); if (importDeclarationsToAppend.length() == 0) return; importRegion = regionUtil.addLeadingWhitespace(importRegion, resource); importRegion = regionUtil.addTrailingSingleWhitespace(importRegion, lineSeparator, resource); int insertOffset = importRegion.getOffset() + importRegion.getLength(); if (insertOffset != 0 && originalImportDeclarations.isEmpty()) importDeclarationsToAppend.insert(0, lineSeparator); importDeclarationsToAppend.append(lineSeparator); int insertLength = -importRegion.getLength(); insertLength += regionUtil.addTrailingWhitespace(importRegion, resource).getLength(); ReplaceRegion appendDeclarations = new ReplaceRegion(new TextRegion(insertOffset, insertLength), importDeclarationsToAppend.toString()); acceptor.accept(appendDeclarations); }
public ITextRegion computeRegion(XtextResource resource) { XImportSection xImportSection = config.getImportSection(resource); // try to obtain the region from the text if it is not a synthetic region. if (xImportSection != null && xImportSection.eResource() != null) { INode node = NodeModelUtils.findActualNodeFor(xImportSection); if(node == null) LOG.error("Cannot detect node for original import section"); else return node.getTextRegion(); } return new TextRegion(config.getImportSectionOffset(resource), 0); }
public void createHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) { INode crossRefNode = eObjectAtOffsetHelper.getCrossReferenceNode(resource, new TextRegion(offset, 0)); if (crossRefNode == null) return; EObject crossLinkedEObject = eObjectAtOffsetHelper.getCrossReferencedElement(crossRefNode); if (crossLinkedEObject != null && !crossLinkedEObject.eIsProxy()) { createHyperlinksTo(resource, crossRefNode, crossLinkedEObject, acceptor); } }
protected ITextRegion computeInitialFineGrainedSelection(INode node, ITextRegion currentEditorSelection) { if (node.getText().trim().length() > 0 && currentEditorSelection.getOffset() >= node.getOffset() && getEndOffset(currentEditorSelection) < node.getOffset() + node.getLength()) { //TODO enhance to just select a single word in a comment or string literal. return new TextRegion(node.getOffset(), node.getLength()); } return null; }
public IOutlineNode createRoot(IXtextDocument document) { DocumentRootNode documentNode = new DocumentRootNode(labelProvider.getImage(document), labelProvider.getText(document), document, this); documentNode.setTextRegion(new TextRegion(0, document.getLength())); return documentNode; }
public ReplaceRegion getTokenReplaceRegion(String changedText, ReplaceRegion replaceRegion) { int lengthDelta = replaceRegion.getText().length() - replaceRegion.getLength(); ITextRegion tokenRegion = getTokenRegion(changedText, new TextRegion(replaceRegion.getOffset(), replaceRegion .getText().length())); if (tokenRegion.getOffset() == replaceRegion.getOffset() && tokenRegion.getLength() == replaceRegion.getText().length()) return replaceRegion; else return new ReplaceRegion(tokenRegion.getOffset(), tokenRegion.getLength() - lengthDelta, changedText.substring(tokenRegion.getOffset(), tokenRegion.getOffset() + tokenRegion.getLength())); }
@Override public void run() { ITextSelection selection = (ITextSelection) xtextEditor.getSelectionProvider().getSelection(); ITextRegion currentEditorSelection = new TextRegion(selection.getOffset(), selection.getLength()); ITextRegion nextSelection = xtextEditor.getDocument().readOnly(createTextSelectionWork(currentEditorSelection)); if (nextSelection == null || nextSelection == ITextRegion.EMPTY_REGION || nextSelection.equals(currentEditorSelection)) { return; } xtextEditor.selectAndReveal(nextSelection.getOffset(), nextSelection.getLength()); }
public EObjectNode createEObjectNode(IOutlineNode parentNode, EObject modelElement, ImageDescriptor imageDescriptor, Object text, boolean isLeaf) { EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode, imageDescriptor, text, isLeaf); ICompositeNode parserNode = NodeModelUtils.getNode(modelElement); if (parserNode != null) eObjectNode.setTextRegion(new TextRegion(parserNode.getOffset(), parserNode.getLength())); if(isLocalElement(parentNode, modelElement)) eObjectNode.setShortTextRegion(locationInFileProvider.getSignificantTextRegion(modelElement)); return eObjectNode; }
protected INodeModelFormatter.IFormattedRegion format1(final XtextResource resource, final ITextRegion selection) { final IParseResult parseResult = resource.getParseResult(); if ((parseResult == null)) { return null; } final ICompositeNode rootNode = parseResult.getRootNode(); ITextRegion region = selection; if ((region == null)) { int _offset = rootNode.getOffset(); int _length = rootNode.getLength(); TextRegion _textRegion = new TextRegion(_offset, _length); region = _textRegion; } return this.formatter1.format(rootNode, region.getOffset(), region.getLength()); }
protected void selectInTreeView(ISelection selection) { if (selection instanceof ITextSelection && !treeViewer.getTree().isDisposed()) { ITextSelection textSelection = (ITextSelection) selection; ITextRegion selectedTextRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength()); Object input = treeViewer.getInput(); if (input instanceof IOutlineNode) { IOutlineNode nodeToBeSelected = findBestNode((IOutlineNode) input, selectedTextRegion); if (nodeToBeSelected != null) treeViewer.setSelection(new StructuredSelection(nodeToBeSelected)); } } }
/** * @since 2.1 */ protected EObjectNode createEObjectNode(IOutlineNode parentNode, EObject modelElement, Image image, Object text, boolean isLeaf) { EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode, image, text, isLeaf); ICompositeNode parserNode = NodeModelUtils.getNode(modelElement); if (parserNode != null) eObjectNode.setTextRegion(new TextRegion(parserNode.getOffset(), parserNode.getLength())); if (isLocalElement(parentNode, modelElement)) eObjectNode.setShortTextRegion(locationInFileProvider.getSignificantTextRegion(modelElement)); return eObjectNode; }
protected ITextRegion getRegion(Pair<EObject, EObject> selection) { final ITextRegion firstRegion = getTextRegion(selection.getFirst()); if (selection.getFirst() == selection.getSecond()) { return firstRegion; } ITextRegion secondRegion = getTextRegion(selection.getSecond()); return new TextRegion(firstRegion.getOffset(), getEndOffset(secondRegion) - firstRegion.getOffset()); }