Java Code Examples for org.eclipse.xtext.util.ITextRegion#getLength()
The following examples show how to use
org.eclipse.xtext.util.ITextRegion#getLength() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XtendProposalProvider.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
public void completeInRichString(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { INode node = context.getCurrentNode(); ITextRegion textRegion = node.getTextRegion(); int offset = textRegion.getOffset(); int length = textRegion.getLength(); String currentNodeText = node.getText(); if (currentNodeText.startsWith("\u00BB") && offset + 1 <= context.getOffset() || currentNodeText.startsWith("'''") && offset + 3 <= context.getOffset()) { if (context.getOffset() > offset && context.getOffset() < offset + length) addGuillemotsProposal(context, acceptor); } else if (currentNodeText.startsWith("\u00AB\u00AB")) { try { IDocument document = context.getViewer().getDocument(); int nodeLine = document.getLineOfOffset(offset); int completionLine = document.getLineOfOffset(context.getOffset()); if (completionLine > nodeLine) { addGuillemotsProposal(context, acceptor); } } catch (BadLocationException e) { // ignore } } }
Example 2
Source File: AbstractFormatter2.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected boolean isInRequestedRange(EObject obj) { Collection<ITextRegion> regions = request.getRegions(); if (regions.isEmpty()) return true; ITextRegionAccess access = request.getTextRegionAccess(); IEObjectRegion objRegion = access.regionForEObject(obj); if (objRegion == null) return false; IHiddenRegion previousHidden = objRegion.getPreviousHiddenRegion(); IHiddenRegion nextHidden = objRegion.getNextHiddenRegion(); int objOffset = previousHidden != null ? previousHidden.getOffset() : 0; int objEnd = nextHidden != null ? nextHidden.getEndOffset() : access.regionForRootEObject().getEndOffset(); for (ITextRegion region : regions) { int regionOffset = region.getOffset(); int regionEnd = regionOffset + region.getLength(); if (regionOffset <= objEnd && regionEnd >= objOffset) return true; } return false; }
Example 3
Source File: DefaultHierarchyNodeLocationProvider.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected ITextRegionWithLineInformation toTextRegionWithLineInformation(EObject obj, ITextRegion textRegion) { if (textRegion == null) { return ITextRegionWithLineInformation.EMPTY_REGION; } if (textRegion instanceof ITextRegionWithLineInformation) { return (ITextRegionWithLineInformation) textRegion; } ICompositeNode node = NodeModelUtils.getNode(obj); if (node == null) { return new TextRegionWithLineInformation(textRegion.getOffset(), textRegion.getLength(), 0, 0); } int startLine = NodeModelUtils.getLineAndColumn(node, textRegion.getOffset()).getLine() - 1; int endLine = NodeModelUtils.getLineAndColumn(node, textRegion.getOffset() + textRegion.getLength()).getLine() - 1; return new TextRegionWithLineInformation(textRegion.getOffset(), textRegion.getLength(), startLine, endLine); }
Example 4
Source File: TokenRegionProviderTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testTokenRegionContainsRegion() throws Exception { String modelAsString = "a1 / /* comment */ b2"; List<CommonToken> tokens = getTokens(modelAsString); for(int length=0; length < modelAsString.length(); ++length) { for(int offset = 0; offset + length < modelAsString.length(); ++offset) { ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(modelAsString, new TextRegion(offset, length)); // System.out.println(offset + ":" + length + " -> " + tokenRegion); CommonToken firstToken = findTokenStartingAt(tokenRegion.getOffset(), tokens); assertTrue(firstToken.getStartIndex() <= offset); if(tokenRegion.getLength() != 0) { CommonToken lastToken = findTokenStopingAt(tokenRegion.getOffset() + tokenRegion.getLength()-1, tokens); assertTrue(lastToken.getStopIndex() >= offset + length -1); } } } }
Example 5
Source File: RegionNodeModelFormatter.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
@Override public IFormattedRegion format(final ICompositeNode root, final int offset, final int length) { final TokenStringBuffer buf = new TokenStringBuffer(); final ITokenStream out = offset == 0 ? buf : new FilterStream(buf); final ITokenStream formatStream = formatter.createFormatterStream(null, out, false); if (!(formatStream instanceof IDelegatingTokenStream)) { return super.format(root, offset, length); } try { ITextRegion range; if (offset > 0) { int noffset = root.getText().lastIndexOf('\n', offset) + 1; // Keep the new line int nlength = length + (offset - noffset); // Always start in the beginning of the line range = nodeModelStreamer.feedTokenStream(formatStream, root, noffset, nlength); } else { range = nodeModelStreamer.feedTokenStream(formatStream, root, offset, length); } return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString()); } catch (IOException e) { // this should never happen since TokenStringBuffer doesn't throw IOEs. throw new RuntimeException(e); // NOPMD } }
Example 6
Source File: StringBasedTextRegionAccessDiffBuilder.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Override public boolean isModified(ITextRegion region) { int offset = region.getOffset(); int endOffset = offset + region.getLength(); for (Rewrite action : rewrites) { int rwOffset = action.originalFirst.getOffset(); int rwEndOffset = action.originalLast.getEndOffset(); if (rwOffset <= offset && offset < rwEndOffset) { return true; } if (rwOffset < endOffset && endOffset <= rwEndOffset) { return true; } } return false; }
Example 7
Source File: FlexTokenRegionProvider.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
@Override public ITextRegion getTokenRegion(String text, ITextRegion region) { try { InternalFlexer flexer = flexerFactory.createFlexer(new StringReader(text)); int token = flexer.advance(); int prevStart = 0; int nextStart = flexer.getTokenLength(); final int regionStartOffset = region.getOffset(); final int regionEnd = regionStartOffset + region.getLength(); while (token != Token.EOF && nextStart <= regionStartOffset) { prevStart = nextStart; token = flexer.advance(); nextStart += flexer.getTokenLength(); } while (token != Token.EOF && nextStart < regionEnd) { token = flexer.advance(); nextStart += flexer.getTokenLength(); } return new TextRegion(prevStart, nextStart - prevStart); } catch(IOException e) { throw new RuntimeException(e); // cannot happen since StringReader doesn't throw IOException } }
Example 8
Source File: ParserBasedContentAssistContextFactory.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
public boolean doComputePrefix(ICompositeNode node, StringBuilder result) { List<ILeafNode> hiddens = Lists.newArrayListWithCapacity(2); for (INode child : node.getChildren()) { if (child instanceof ICompositeNode) { if (!doComputePrefix((ICompositeNode) child, result)) return false; } else { ILeafNode leaf = (ILeafNode) child; ITextRegion leafRegion = leaf.getTextRegion(); if (leafRegion.getOffset() > completionOffset) return false; if (leaf.isHidden()) { if (result.length() != 0) hiddens.add((ILeafNode) child); } else { Iterator<ILeafNode> iter = hiddens.iterator(); while (iter.hasNext()) { result.append(iter.next().getText()); } hiddens.clear(); result.append(getNodeTextUpToCompletionOffset(leaf)); if (leafRegion.getOffset() + leafRegion.getLength() > completionOffset) return false; } } } return true; }
Example 9
Source File: DefaultFoldingRegionAcceptor.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
/** * @return a freshly created {@link FoldedPosition} * * @since 2.8 * @deprecated use / override {@link #newFoldedPosition(IRegion, ITextRegion)} instead */ @Deprecated protected FoldedPosition newFoldedPosition(IRegion region, ITextRegion significantRegion, boolean initiallyFolded) { if (region == null) return null; if (significantRegion != null) return new DefaultFoldedPosition(region.getOffset(), region.getLength(), significantRegion.getOffset() - region.getOffset(), significantRegion.getLength(), initiallyFolded); return new DefaultFoldedPosition(region.getOffset(), region.getLength(), DefaultFoldedPosition.UNSET, DefaultFoldedPosition.UNSET, initiallyFolded); }
Example 10
Source File: AstSelectionProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected ITextRegion computeInitialFineGrainedSelection(INode node, ITextRegion currentEditorSelection) { if (node.getText().trim().length() > 0) { ITextRegion textRegion = node.getTextRegion(); if (currentEditorSelection.getOffset() >= textRegion.getOffset() && getEndOffset(currentEditorSelection) < textRegion.getOffset() + textRegion.getLength()) { //TODO enhance to just select a single word in a comment or string literal. return textRegion; } } return null; }
Example 11
Source File: ValidatingRichStringAcceptor.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
protected void resetCurrentOffset(/* @Nullable */ RichStringLiteral origin) { if (root == null) root = origin; if (origin != null && origin != recent) { if (wasCurrentOffset == -1) wasCurrentOffset = currentOffset; int diff = lastOffsetOfLiteral - currentOffset; // no actions are involved, we are interested in the real node recent = origin; List<INode> featureNodes = NodeModelUtils.findNodesForFeature(origin, XbasePackage.Literals.XSTRING_LITERAL__VALUE); if (featureNodes.size() == 1) { INode node = featureNodes.get(0); ITextRegion textRegion = node.getTextRegion(); currentOffset = textRegion.getOffset(); String nodeText = node.getText(); if (nodeText.endsWith("'''")) { lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 3; } else if (nodeText.endsWith("''")) { lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 2; } else if (nodeText.endsWith("'") || nodeText.endsWith("\u00AB")) { lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 1; } if (nodeText.charAt(0) == '\'') { currentOffset += 3; } else { currentOffset += 1; } } currentOffset -= diff; } }
Example 12
Source File: AbstractSourceView.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected void setSelection(ITextRegion textSelection, boolean reveal) { StyledText text = getSourceViewer().getTextWidget(); if (text.getText().length() >= textSelection.getOffset() + textSelection.getLength()) { getSourceViewer().setSelection(new TextSelection(textSelection.getOffset(), textSelection.getLength()), reveal); } }
Example 13
Source File: EcorePackageRenameStrategy.java From sarl with Apache License 2.0 | 5 votes |
/** Replies the text update for the rename. * * @param newName the new package name. * @param resourceSet the set of resources. * @return the text update. */ protected TextEdit getDeclarationTextEdit(String newName, ResourceSet resourceSet) { final EObject object = resourceSet.getEObject(this.uriProvider.apply(resourceSet), true); if (object instanceof SarlScript) { final ITextRegion region = getOriginalPackageRegion((SarlScript) object); if (region != null) { return new ReplaceEdit(region.getOffset(), region.getLength(), newName); } } throw new RefactoringException("SARL script not loaded."); //$NON-NLS-1$ }
Example 14
Source File: ImportSectionRegionUtil.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
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); }
Example 15
Source File: ContentAssistContextFactory.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
public boolean doComputePrefix(ICompositeNode node, StringBuilder result) { List<ILeafNode> hiddens = Lists.newArrayListWithCapacity(2); for (INode child : node.getChildren()) { if (child instanceof ICompositeNode) { if (!doComputePrefix((ICompositeNode) child, result)) return false; } else { ILeafNode leaf = (ILeafNode) child; ITextRegion leafRegion = leaf.getTextRegion(); if (leafRegion.getOffset() > completionOffset) return false; if (leaf.isHidden()) { if (result.length() != 0) hiddens.add((ILeafNode) child); } else { Iterator<ILeafNode> iter = hiddens.iterator(); while (iter.hasNext()) { result.append(iter.next().getText()); } hiddens.clear(); result.append(getNodeTextUpToCompletionOffset(leaf)); if (leafRegion.getOffset() + leafRegion.getLength() > completionOffset) return false; } } } return true; }
Example 16
Source File: XtextHyperlinkHelper.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
protected Region toRegion(ITextRegion location) { return new Region(location.getOffset(), location.getLength()); }
Example 17
Source File: AbstractTextSegment.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Override public boolean contains(ITextRegion other) { int offset = getOffset(); int length = getLength(); return (other.getOffset() + other.getLength() <= offset + length && other.getOffset() >= offset); }
Example 18
Source File: ParameterContextInformationProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
@Override public void getContextInformation(ContentAssistContext context, IContextInformationAcceptor acceptor) { XExpression containerCall = getContainerCall(eObjectAtOffsetHelper.resolveContainedElementAt(context.getResource(), context.getOffset())); LightweightTypeReferenceFactory factory = proposalProvider.getTypeConverter(context.getResource()); if (containerCall != null) { ICompositeNode containerCallNode = NodeModelUtils.findActualNodeFor(containerCall); ITextRegion containerCallRegion = containerCallNode.getTextRegion(); if(containerCallRegion.getOffset() > context.getOffset() || containerCallRegion.getOffset() + containerCallRegion.getLength() < context.getOffset()) return; JvmIdentifiableElement calledFeature = getCalledFeature(containerCall); if (calledFeature instanceof JvmExecutable) { if(getParameterListOffset(containerCall) > context.getOffset()) return; ParameterData parameterData = new ParameterData(); IScope scope = getScope(containerCall); QualifiedName qualifiedName = QualifiedName.create(getCalledFeatureName(containerCall)); boolean candidatesFound = false; for (IEObjectDescription element : scope.getElements(qualifiedName)) { if (element instanceof IIdentifiableElementDescription) { IIdentifiableElementDescription featureDescription = (IIdentifiableElementDescription) element; JvmIdentifiableElement featureCandidate = featureDescription.getElementOrProxy(); if (featureCandidate instanceof JvmExecutable) { JvmExecutable executable = (JvmExecutable) featureCandidate; if(!executable.getParameters().isEmpty()) { StyledString styledString = new StyledString(); proposalProvider.appendParameters(styledString, executable, featureDescription.getNumberOfIrrelevantParameters(), factory); parameterData.addOverloaded(styledString.toString(), executable.isVarArgs()); candidatesFound = true; } } } } if (candidatesFound) { StyledString displayString = proposalProvider.getStyledDisplayString((JvmExecutable) calledFeature, true, 0, qualifiedNameConverter.toString(qualifiedNameProvider.getFullyQualifiedName(calledFeature)), calledFeature.getSimpleName(), factory); ParameterContextInformation parameterContextInformation = new ParameterContextInformation( parameterData, displayString.toString(), getParameterListOffset(containerCall), context.getOffset()); acceptor.accept(parameterContextInformation); } } } }
Example 19
Source File: ExpressionUtil.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
protected boolean contains(ITextRegion textRegion, ITextSelection selection) { return textRegion.getOffset() <= selection.getOffset() && textRegion.getOffset() + textRegion.getLength() >= selection.getOffset() + selection.getLength(); }
Example 20
Source File: HyperlinkHelper.java From xtext-eclipse with Eclipse Public License 2.0 | 2 votes |
/** * Produces hyperlinks for the given {@code node} which is associated with a cross reference * that points to the referenced {@code target}. * * @see #createHyperlinksTo(XtextResource, Region, EObject, IHyperlinkAcceptor) * * @since 2.4 */ protected void createHyperlinksTo(XtextResource resource, INode node, EObject target, IHyperlinkAcceptor acceptor) { ITextRegion textRegion = node.getTextRegion(); Region region = new Region(textRegion.getOffset(), textRegion.getLength()); createHyperlinksTo(resource, region, target, acceptor); }