Java Code Examples for org.eclipse.xtext.nodemodel.ICompositeNode#getLeafNodes()
The following examples show how to use
org.eclipse.xtext.nodemodel.ICompositeNode#getLeafNodes() .
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: CheckHighlightingCalculator.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
@Override protected void highlightSpecialIdentifiers(final IHighlightedPositionAcceptor acceptor, final ICompositeNode root) { TerminalRule idRule = grammarAccess.getIDRule(); for (ILeafNode leaf : root.getLeafNodes()) { if (commentProvider.isJavaDocComment(leaf)) { // not really a special identifier, but we don't want to iterate over the leaf nodes twice, do we? acceptor.addPosition(leaf.getOffset(), leaf.getLength(), CheckHighlightingConfiguration.JAVADOC_ID); } else if (!leaf.isHidden()) { if (leaf.getGrammarElement() instanceof Keyword) { // Check if it is a keyword used as an identifier. ParserRule rule = GrammarUtil.containingParserRule(leaf.getGrammarElement()); if (FEATURE_CALL_ID_RULE_NAME.equals(rule.getName())) { acceptor.addPosition(leaf.getOffset(), leaf.getLength(), DefaultHighlightingConfiguration.DEFAULT_ID); } } else { highlightSpecialIdentifiers(leaf, acceptor, idRule); } } } }
Example 2
Source File: SingleLineCommentDocumentationProvider.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** * Finds trailing comment for a given context object. I.e. the comment after / on the same line as the context object. * * @param context * the object * @return the documentation string */ protected String findTrailingComment(final EObject context) { StringBuilder returnValue = new StringBuilder(); ICompositeNode node = NodeModelUtils.getNode(context); if (node != null) { final int contextEndLine = node.getEndLine(); // process all leaf nodes first for (ILeafNode leave : node.getLeafNodes()) { addComment(returnValue, leave, contextEndLine); } // we also need to process siblings (leave nodes only) due to the fact that the last comment after // a given element is not a leaf node of that element anymore. INode sibling = node.getNextSibling(); while (sibling instanceof ILeafNode) { addComment(returnValue, (ILeafNode) sibling, contextEndLine); sibling = sibling.getNextSibling(); } } return returnValue.toString(); }
Example 3
Source File: XtextLabelProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
private String getLiteralName(EnumLiteralDeclaration declaration) { if (declaration.getEnumLiteral() != null) { return declaration.getEnumLiteral().getName(); } ICompositeNode node = NodeModelUtils.getNode(declaration); String literalName = UNKNOWN; if (node != null) { for (ILeafNode leaf : node.getLeafNodes()) { if (!leaf.isHidden()) { literalName = leaf.getText(); break; } } } return literalName; }
Example 4
Source File: XtextLabelProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
private String getLabel(RuleCall ruleCall) { if (ruleCall.getRule() != null) { return ruleCall.getRule().getName(); } ICompositeNode node = NodeModelUtils.getNode(ruleCall); String ruleName = UNKNOWN; if (node != null) { for (ILeafNode leaf : node.getLeafNodes()) { if (!leaf.isHidden()) { ruleName = leaf.getText(); break; } } } return ruleName; }
Example 5
Source File: SingleLineCommentDocumentationProvider.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** {@inheritDoc} */ @Override public List<INode> getDocumentationNodes(final EObject object) { ICompositeNode node = NodeModelUtils.getNode(object); if (node == null) { return Collections.emptyList(); } // get all single line comments before a non hidden leaf node List<INode> result = Lists.newArrayList(); for (ILeafNode leaf : node.getLeafNodes()) { if (!leaf.isHidden()) { break; } EObject grammarElement = leaf.getGrammarElement(); if (grammarElement instanceof AbstractRule && ruleName.equals(((AbstractRule) grammarElement).getName())) { String comment = leaf.getText(); if (getCommentPattern().matcher(comment).matches() && !comment.matches(ignore)) { result.add(leaf); } } } return result; }
Example 6
Source File: FormatterTester.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
protected ArrayList<TextReplacement> createMissingEditReplacements(XtextResource res, Collection<TextReplacement> edits, int offset, int length) { Set<Integer> offsets = IterableExtensions .toSet(Iterables.transform(edits, (TextReplacement it) -> Integer.valueOf(it.getOffset()))); ArrayList<TextReplacement> result = new ArrayList<>(); int lastOffset = 0; IParseResult parseResult = res.getParseResult(); if (parseResult != null) { ICompositeNode rootNode = parseResult.getRootNode(); if (rootNode != null) { for (ILeafNode leaf : rootNode.getLeafNodes()) { if (!leaf.isHidden() || !StringExtensions.isNullOrEmpty(leaf.getText().trim())) { ITextRegion leafRegion = leaf.getTextRegion(); if (lastOffset >= offset && leafRegion.getOffset() <= offset + length && !offsets.contains(Integer.valueOf(lastOffset))) { result.add(new TextReplacement(lastOffset, leafRegion.getOffset() - lastOffset, "!!")); } lastOffset = leafRegion.getOffset() + leafRegion.getLength(); } } } } return result; }
Example 7
Source File: XtendQuickfixProvider.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
protected void replaceKeyword(Keyword keyword, String replacement, EObject container, IXtextDocument document) throws BadLocationException { ICompositeNode node = NodeModelUtils.findActualNodeFor(container); if (node != null) { for (ILeafNode leafNode : node.getLeafNodes()) { if (leafNode.getGrammarElement() == keyword) { ITextRegion leafRegion = leafNode.getTextRegion(); String actualReplacement = replacement; if (!Character.isWhitespace(document.getChar(leafRegion.getOffset() - 1))) { actualReplacement = " " + replacement; } document.replace(leafRegion.getOffset(), leafRegion.getLength(), actualReplacement); } } } }
Example 8
Source File: XtendQuickfixProvider.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
protected void internalDoAddAbstractKeyword(EObject element, IModificationContext context) throws BadLocationException { if (element instanceof XtendFunction) { element = element.eContainer(); } if (element instanceof XtendClass) { XtendClass clazz = (XtendClass) element; IXtextDocument document = context.getXtextDocument(); ICompositeNode clazzNode = NodeModelUtils.findActualNodeFor(clazz); if (clazzNode == null) throw new IllegalStateException("Cannot determine node for clazz " + clazz.getName()); int offset = -1; for (ILeafNode leafNode : clazzNode.getLeafNodes()) { if (leafNode.getText().equals("class")) { offset = leafNode.getOffset(); break; } } ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) clazz.eResource(), offset, 0); appendable.append("abstract "); appendable.commitChanges(); } }
Example 9
Source File: DefaultNodeModelFormatter.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected String getIndentation(ICompositeNode root, int fromOffset) { if (fromOffset == 0) return ""; List<ILeafNode> r = new ArrayList<ILeafNode>(); // add all nodes until fromOffset for(ILeafNode l: root.getLeafNodes()) { if (l.getOffset() >= fromOffset) break; else r.add(l); } // go backwards until first linewrap Pattern p = Pattern.compile("(\\n|\\r)([ \\t]*)"); for (int i = r.size() - 1; i >= 0; i--) { Matcher m = p.matcher(r.get(i).getText()); if (m.find()) { String ind = m.group(2); while (m.find()) ind = m.group(2); return ind; } } return ""; }
Example 10
Source File: XtendHighlightingCalculator.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
protected void highlightDeprecatedXtendAnnotationTarget(IHighlightedPositionAcceptor acceptor, XtendAnnotationTarget target, XAnnotation annotation){ JvmType annotationType = annotation.getAnnotationType(); if(annotationType instanceof JvmAnnotationType && DeprecationUtil.isDeprecatedAnnotation((JvmAnnotationType) annotationType)){ if (target instanceof XtendConstructor) { ICompositeNode compositeNode = NodeModelUtils.getNode(target); for(ILeafNode leaf: compositeNode.getLeafNodes()) { if (leaf.getGrammarElement() == xtendGrammarAccess.getMemberAccess().getNewKeyword_2_2_2()) { highlightNode(acceptor, leaf, XbaseHighlightingStyles.DEPRECATED_MEMBERS); highlightNode(acceptor, leaf, HighlightingStyles.KEYWORD_ID); return; } } } else { EStructuralFeature nameFeature = target.eClass().getEStructuralFeature("name"); if (nameFeature!=null) { highlightFeature(acceptor, target, nameFeature, XbaseHighlightingStyles.DEPRECATED_MEMBERS); } } } }
Example 11
Source File: MultiLineCommentDocumentationProvider.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
/** * Returns the nearest multi line comment node that precedes the given object. * @since 2.3 * @return a list with exactly one node or an empty list if the object is undocumented. */ /* @NonNull */ @Override public List<INode> getDocumentationNodes(/* @NonNull */ EObject object) { ICompositeNode node = NodeModelUtils.getNode(object); List<INode> result = Collections.emptyList(); if (node != null) { // get the last multi line comment before a non hidden leaf node for (ILeafNode leafNode : node.getLeafNodes()) { if (!leafNode.isHidden()) break; if (leafNode.getGrammarElement() instanceof TerminalRule && ruleName.equalsIgnoreCase(((TerminalRule) leafNode.getGrammarElement()).getName())) { String comment = leafNode.getText(); if (commentStartTagRegex.matcher(comment).matches()) { result = Collections.<INode>singletonList(leafNode); } } } } return result; }
Example 12
Source File: CreateMemberQuickfixes.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
/** * @since 2.3 */ protected int getFirstOffsetOfKeyword(EObject object, String keyword) { int offset = -1; ICompositeNode node = NodeModelUtils.getNode(object); if (node != null) { for (ILeafNode leafNode : node.getLeafNodes()) { if (leafNode.getGrammarElement() instanceof Keyword && equal(keyword, ((Keyword) leafNode.getGrammarElement()).getValue())) { return leafNode.getOffset() + 1; } } } return offset; }
Example 13
Source File: LeafNodeBug_234132_Test.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testLeafNodeBug() throws Exception { with(ReferenceGrammarTestLanguageStandaloneSetup.class); String model = readFileIntoString("org/eclipse/xtext/reference/leafNodeBug_234132.tst"); ICompositeNode rootNode = getRootNodeAndExpect(model, 1); for(ILeafNode leafNode: rootNode.getLeafNodes()) { assertTrue(leafNode.getTotalLength() + leafNode.getTotalOffset() <= model.length()); assertEquals(model.substring(leafNode.getTotalOffset(), leafNode.getTotalOffset() + leafNode.getTotalLength()), leafNode.getText()); } }
Example 14
Source File: ASTGraphProvider.java From n4js with Eclipse Public License 1.0 | 5 votes |
private String getDocumentation(/* @NonNull */EObject object) { if (object.eContainer() == null) { // if a comment is at the beginning of the file it will be returned for // the root element (e.g. Script in N4JS) as well -> avoid this! return null; } ICompositeNode node = NodeModelUtils.getNode(object); if (node != null) { // get the last multi line comment before a non hidden leaf node for (ILeafNode leafNode : node.getLeafNodes()) { if (!leafNode.isHidden()) break; EObject grammarElem = leafNode.getGrammarElement(); if (grammarElem instanceof TerminalRule && "ML_COMMENT".equalsIgnoreCase(((TerminalRule) grammarElem).getName())) { String comment = leafNode.getText(); if (commentStartTagRegex.matcher(comment).matches()) { return leafNode.getText(); } } } } return null; }
Example 15
Source File: InsertionOffsets.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
public int inEmpty(EObject element) { ICompositeNode compositeNode = NodeModelUtils.findActualNodeFor(element); for (ILeafNode leafNode : compositeNode.getLeafNodes()) { if ("{".equals(leafNode.getText())) { return leafNode.getOffset() + 1; } } return compositeNode.getEndOffset(); }
Example 16
Source File: XtextProposalProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
@Override public void completeTypeRef_Classifier(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { Grammar grammar = GrammarUtil.getGrammar(model); ContentAssistContext.Builder myContextBuilder = context.copy(); myContextBuilder.setMatcher(new ClassifierPrefixMatcher(context.getMatcher(), classifierQualifiedNameConverter)); if (model instanceof TypeRef) { ICompositeNode node = NodeModelUtils.getNode(model); if (node != null) { int offset = node.getOffset(); Region replaceRegion = new Region(offset, context.getReplaceRegion().getLength() + context.getReplaceRegion().getOffset() - offset); myContextBuilder.setReplaceRegion(replaceRegion); myContextBuilder.setLastCompleteNode(node); StringBuilder availablePrefix = new StringBuilder(4); for (ILeafNode leaf : node.getLeafNodes()) { if (leaf.getGrammarElement() != null && !leaf.isHidden()) { if ((leaf.getTotalLength() + leaf.getTotalOffset()) < context.getOffset()) availablePrefix.append(leaf.getText()); else availablePrefix.append(leaf.getText().substring(0, context.getOffset() - leaf.getTotalOffset())); } if (leaf.getTotalOffset() >= context.getOffset()) break; } myContextBuilder.setPrefix(availablePrefix.toString()); } } ContentAssistContext myContext = myContextBuilder.toContext(); for (AbstractMetamodelDeclaration declaration : grammar.getMetamodelDeclarations()) { if (declaration.getEPackage() != null) { createClassifierProposals(declaration, model, myContext, acceptor); } } }
Example 17
Source File: XbaseHighlightingCalculator.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected void highlightSpecialIdentifiers(IHighlightedPositionAcceptor acceptor, ICompositeNode root) { TerminalRule idRule = getIDRule(); for (ILeafNode leaf : root.getLeafNodes()) { if (!leaf.isHidden()) { highlightSpecialIdentifiers(leaf, acceptor, idRule); } } }
Example 18
Source File: FeatureCallCompiler.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected boolean isDeclaredInNewLine(XExpression obj) { ICompositeNode node = NodeModelUtils.getNode(obj); if (node != null) { int line = -1; for(ILeafNode n : node.getLeafNodes()) { if (n.isHidden() && line == -1) line = n.getStartLine(); if (!n.isHidden() && line != -1) return line != n.getStartLine(); } } return false; }
Example 19
Source File: XtendLocationInFileProvider.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
@Override public ITextRegion getSignificantTextRegion(EObject element) { if (element instanceof RichStringLiteral) { ICompositeNode elementNode = findNodeFor(element); if (elementNode == null) { return ITextRegion.EMPTY_REGION; } ITextRegion result = ITextRegion.EMPTY_REGION; for (INode node : elementNode.getLeafNodes()) { if (isHidden(node)) { continue; } EObject grammarElement = node.getGrammarElement(); if (!(grammarElement instanceof RuleCall)) { continue; } RuleCall ruleCall = (RuleCall) grammarElement; ITextRegionWithLineInformation region = node.getTextRegionWithLineInformation(); int offset = region.getOffset(); int length = region.getLength(); if (grammarAccess.getRICH_TEXTRule() == ruleCall.getRule()) { offset += 3; length -= 6; } else if (grammarAccess.getRICH_TEXT_STARTRule() == ruleCall.getRule()) { offset += 3; length -= 4; } else if (grammarAccess.getRICH_TEXT_ENDRule() == ruleCall.getRule()) { offset += 1; length -= 4; } else if (grammarAccess.getRICH_TEXT_INBETWEENRule() == ruleCall.getRule()) { offset += 1; length -= 2; } else if (grammarAccess.getCOMMENT_RICH_TEXT_ENDRule() == ruleCall.getRule()) { offset += 2; length -= 5; } else if (grammarAccess.getCOMMENT_RICH_TEXT_INBETWEENRule() == ruleCall.getRule()) { offset += 2; length -= 3; } else { continue; } result = result.merge(toZeroBasedRegion(new TextRegionWithLineInformation(offset, length, region.getLineNumber(), region.getEndLineNumber()))); } return result; } return super.getSignificantTextRegion(element); }
Example 20
Source File: FormatScopingTest.java From dsl-devkit with Eclipse Public License 1.0 | 3 votes |
/** * Find first leaf node exactly matching given string. * * @param string * the string * @param node * the node * @return the leaf node or null */ private ILeafNode findLeafNode(final String string, final ICompositeNode node) { for (ILeafNode leafNode : node.getLeafNodes()) { if (string.equals(leafNode.getText())) { return leafNode; } } return null; }