org.eclipse.xtext.nodemodel.ICompositeNode Java Examples
The following examples show how to use
org.eclipse.xtext.nodemodel.ICompositeNode.
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: BaseContentAssistParser.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected AbstractElement getEntryGrammarElement(ICompositeNode entryPoint) { EObject grammarElement = entryPoint.getGrammarElement(); if (grammarElement instanceof RuleCall) { AbstractRule rule = ((RuleCall) grammarElement).getRule(); if (rule instanceof ParserRule) { if (!GrammarUtil.isMultipleCardinality(rule.getAlternatives())) { grammarElement = rule.getAlternatives(); } } } else if (grammarElement instanceof ParserRule) { grammarElement = ((ParserRule) grammarElement).getAlternatives(); } else if (grammarElement instanceof CrossReference) { grammarElement = GrammarUtil.containingAssignment(grammarElement); } AbstractElement result = (AbstractElement) grammarElement; if (result instanceof Action) { return getEntryGrammarElement((ICompositeNode) entryPoint.getFirstChild()); } return result; }
Example #2
Source File: JSDoc2SpecAcceptor.java From n4js with Eclipse Public License 1.0 | 6 votes |
private String toPos(EObject eobj) { if (eobj == null) return ""; StringBuilder strb = new StringBuilder(); String res = null; if (eobj.eResource() != null) { res = eobj.eResource().getURI().toString(); if (res.startsWith("platform:/resource/")) { res = res.substring("platform:/resource/".length()); } } if (res != null) strb.append(res); EObject astNode = eobj instanceof SyntaxRelatedTElement ? ((SyntaxRelatedTElement) eobj).getAstElement() : eobj; ICompositeNode node = NodeModelUtils.findActualNodeFor(astNode); if (node != null) { strb.append(":").append(node.getStartLine()); } return strb.toString(); }
Example #3
Source File: PartialParserTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testPartialParseConcreteRuleFirstInnerToken_02() throws Exception { with(PartialParserTestLanguageStandaloneSetup.class); String model = "container c1 {\n" + " children {\n" + " -> C ( ch1 )\n" + " }" + "}"; XtextResource resource = getResourceFromString(model); assertTrue(resource.getErrors().isEmpty()); ICompositeNode root = resource.getParseResult().getRootNode(); ILeafNode childrenLeaf = findLeafNodeByText(root, model, "children"); ILeafNode arrowLeaf = findLeafNodeByText(root, model, "->"); // change the model and undo the change resource.update(model.indexOf("->"), 2, "-> "); resource.update(model.indexOf("->"), 3, "->"); assertSame(root, resource.getParseResult().getRootNode()); assertSame(childrenLeaf, findLeafNodeByText(root, model, "children")); assertNotSame(arrowLeaf, findLeafNodeByText(root, model, "->")); }
Example #4
Source File: AbstractLabelProvider.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** * Creates a default styled string for the given {@link EObject} model element. * * @param eObject * the {@link EObject} model element * @return the styled string for the given {@link EObject} model element */ public Object getStyledLabel(final EObject eObject) { // first check if object is foreign to this grammar if (isForeignXtextObject(eObject)) { return getForeignObjectLabel(eObject); } // first check if object has a name String name = getNameOfObject(eObject); if (name != null) { return qualifiedStyledString(name, getTypeOfObject(eObject)); } // secondly check first parsed element (keyword / feature) ICompositeNode node = NodeModelUtils.getNode(eObject); if (node != null) { Iterator<ILeafNode> leafNodesIterator = node.getLeafNodes().iterator(); while (leafNodesIterator.hasNext()) { ILeafNode genericLeafNode = leafNodesIterator.next(); if (!(genericLeafNode instanceof HiddenLeafNode)) { LeafNode leafNode = (LeafNode) genericLeafNode; return getLabelName(leafNode.getText(), eObject.eClass().getName(), true); } } } // fallback return super.doGetText(eObject); }
Example #5
Source File: AbstractPartialParserTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected void assertSameStructure(ICompositeNode first, ICompositeNode second) { BidiTreeIterator<INode> firstIter = first.getAsTreeIterable().iterator(); BidiTreeIterator<INode> secondIter = second.getAsTreeIterable().iterator(); while(firstIter.hasNext()) { assertTrue(secondIter.hasNext()); INode firstNext = firstIter.next(); INode secondNext = secondIter.next(); assertEquals(firstNext.getGrammarElement(), secondNext.getGrammarElement()); assertEquals(firstNext.getClass(), secondNext.getClass()); assertEquals(firstNext.getTotalOffset(), secondNext.getTotalOffset()); assertEquals(firstNext.getTotalLength(), secondNext.getTotalLength()); assertEquals(firstNext.getText(), secondNext.getText()); } assertEquals(firstIter.hasNext(), secondIter.hasNext()); }
Example #6
Source File: PartialParserTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testPartialParseConcreteRuleFirstInnerToken_01() throws Exception { with(PartialParserTestLanguageStandaloneSetup.class); String model = "container c1 {\n" + " children {\n" + " -> C ( ch1 )\n" + " }" + "}"; XtextResource resource = getResourceFromString(model); assertTrue(resource.getErrors().isEmpty()); ICompositeNode root = resource.getParseResult().getRootNode(); ILeafNode childrenLeaf = findLeafNodeByText(root, model, "children"); ILeafNode arrowLeaf = findLeafNodeByText(root, model, "->"); resource.update(model.indexOf("->"), 2, "->"); resource.update(model.indexOf("->"), 2, "->"); assertSame(root, resource.getParseResult().getRootNode()); assertSame(childrenLeaf, findLeafNodeByText(root, model, "children")); assertSame(arrowLeaf, findLeafNodeByText(root, model, "->")); }
Example #7
Source File: ExtendedFormattingConfigBasedStream.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** * Returns predecessor of the given node in the parse tree. * Copied method from: org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator * * @param node * for which predecessor should be found * @return predecessor */ private INode findPrevious(final INode node) { ICompositeNode parent = node.getParent(); if (parent == null) { return null; } INode predecessor = node.getPreviousSibling(); if (predecessor != null) { while (predecessor instanceof ICompositeNode) { INode lastChild = ((ICompositeNode) predecessor).getLastChild(); if (lastChild == null) { return predecessor; } predecessor = lastChild; } return predecessor; } return parent; }
Example #8
Source File: XtextDiagnosticConverter.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Override protected IssueLocation getLocationData(EObject obj, EStructuralFeature structuralFeature, int index) { if (NodeModelUtils.getNode(obj) == null) { ITextRegion location = locationInFileProvider.getSignificantTextRegion(obj); if (location != null) { ICompositeNode rootNode = NodeModelUtils.getNode(EcoreUtil.getRootContainer(obj)); if (rootNode != null) { ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, location.getOffset()); return getLocationForNode(leafNode); } } else { return super.getLocationData(obj.eContainer(), null, index); } } return super.getLocationData(obj, structuralFeature, index); }
Example #9
Source File: SARLQuickfixProvider.java From sarl with Apache License 2.0 | 6 votes |
private void addAbstractKeyword(XtendTypeDeclaration typeDeclaration, IXtextDocument document, String declarationKeyword) throws BadLocationException { final ICompositeNode clazzNode = NodeModelUtils.findActualNodeFor(typeDeclaration); if (clazzNode == null) { throw new IllegalStateException("Cannot determine node for the type declaration" //$NON-NLS-1$ + typeDeclaration.getName()); } int offset = -1; final Iterator<ILeafNode> nodes = clazzNode.getLeafNodes().iterator(); while (offset == -1 && nodes.hasNext()) { final ILeafNode leafNode = nodes.next(); if (leafNode.getText().equals(declarationKeyword)) { offset = leafNode.getOffset(); } } final ReplacingAppendable appendable = this.appendableFactory.create(document, (XtextResource) typeDeclaration.eResource(), offset, 0); appendable.append(getGrammarAccess() .getAbstractKeyword()) .append(" "); //$NON-NLS-1$ appendable.commitChanges(); }
Example #10
Source File: TokenSequencePreservingPartialParsingHelper.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
protected PartialParsingPointers calculatePartialParsingPointers(ICompositeNode oldRoot, ILeafNode left, ILeafNode right) { ICompositeNode result = right.getParent(); while(result.getTotalOffset() > left.getTotalOffset()) { result = result.getParent(); } List<ICompositeNode> nodesEnclosingRegion = getAllParents(result); Range range = new Range(left.getTotalOffset(), right.getTotalEndOffset()); List<ICompositeNode> validReplaceRootNodes = internalFindValidReplaceRootNodeForChangeRegion(nodesEnclosingRegion); filterInvalidRootNodes(validReplaceRootNodes); if (validReplaceRootNodes.isEmpty()) { validReplaceRootNodes = Collections.singletonList(oldRoot); } return new PartialParsingPointers(oldRoot, range.getOffset(), range.getLength(), validReplaceRootNodes, nodesEnclosingRegion); }
Example #11
Source File: NodeModelUtils.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
public static ParserRule getEntryParserRule(INode node) { ICompositeNode root = node.getRootNode(); EObject ge1 = root.getGrammarElement(); if (ge1 instanceof ParserRule) { return (ParserRule) ge1; } else if (ge1 instanceof Action) { INode firstChild = root.getFirstChild(); while (firstChild.getGrammarElement() instanceof Action && firstChild instanceof CompositeNode) { firstChild = ((CompositeNode)firstChild).getFirstChild(); } EObject ge2 = firstChild.getGrammarElement(); if (ge2 instanceof ParserRule) { return (ParserRule) ge2; } } throw new IllegalStateException("No Root Parser Rule found; The Node Model is broken."); }
Example #12
Source File: NodeIterator.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
private INode findPrevious(INode node) { ICompositeNode parent = node.getParent(); if (parent == null) { return null; } INode predecessor = node.getPreviousSibling(); if (predecessor != null) { while (predecessor instanceof ICompositeNode && !prunedComposites.contains(predecessor)) { INode lastChild = ((ICompositeNode) predecessor).getLastChild(); if (lastChild == null) { return predecessor; } predecessor = lastChild; } return predecessor; } return parent; }
Example #13
Source File: Bug419429Test.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected void compareWithFullParse(String model, int offset, int length, String newText) throws Exception { XtextResource resource = getResourceFromStringAndExpect(model, UNKNOWN_EXPECTATION); resource.update(offset, length, newText); String text = resource.getParseResult().getRootNode().getText(); XtextResource newResource = getResourceFromStringAndExpect(text, UNKNOWN_EXPECTATION); assertEquals(text, resource.getContents().size(), newResource.getContents().size()); EcoreUtil.resolveAll(resource); EcoreUtil.resolveAll(newResource); for(int i = 0; i < resource.getContents().size(); i++) { assertEquals(text, EmfFormatter.objToStr(newResource.getContents().get(i)), EmfFormatter.objToStr(resource.getContents().get(i))); } ICompositeNode rootNode = resource.getParseResult().getRootNode(); ICompositeNode newRootNode = newResource.getParseResult().getRootNode(); Iterator<INode> iterator = rootNode.getAsTreeIterable().iterator(); Iterator<INode> newIterator = newRootNode.getAsTreeIterable().iterator(); while(iterator.hasNext()) { assertTrue(newIterator.hasNext()); assertEqualNodes(text, iterator.next(), newIterator.next()); } assertFalse(iterator.hasNext()); assertFalse(newIterator.hasNext()); }
Example #14
Source File: LazyURIEncoder.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
/** * ONLY public to be testable */ public INode getNode(final INode node, String path) { final List<String> split = Strings.split(path, '/'); INode result = node; for (String string : split) { String trimmed = string.trim(); if (trimmed.length() > 0) { if ("..".equals(trimmed)) { if (result.getParent() == null) throw new IllegalStateException("node has no parent"); result = result.getParent(); } else { int index = Integer.parseInt(string); if (index >= 0) { INode child = ((ICompositeNode) result).getFirstChild(); while(index > 0) { child = child.getNextSibling(); index--; } result = child; } } } } return result; }
Example #15
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 #16
Source File: NodeTreeIterator.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Override public boolean hasPrevious() { if (previousComputed) return previous != null; if (lastPreviousReturned == null) { previous = lastNextReturned != null ? lastNextReturned : root; } else if (!pruned && lastPreviousReturned instanceof ICompositeNode && ((ICompositeNode) lastPreviousReturned).hasChildren()) { previous = ((ICompositeNode) lastPreviousReturned).getLastChild(); } else if (root.equals(lastPreviousReturned)) { previous = null; } else if ((previous = lastPreviousReturned.getPreviousSibling()) != null) { // previous found } else { for (INode parent = lastPreviousReturned.getParent(); previous == null && !root.equals(parent); parent = parent.getParent()) { previous = parent.getPreviousSibling(); } } previousComputed = true; return previous != null; }
Example #17
Source File: AbstractFragmentsTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testFragmentRecursive_01() throws Exception { ParserRuleFragments fragments = parseAndValidate("#10 myName myPrev"); Assert.assertNotNull(fragments); Assert.assertEquals("myName", fragments.getElement().getName()); PRFNamed prev = ((PRFNamedWithAction) fragments.getElement()).getPrev(); Assert.assertEquals("myPrev", prev.getName()); ICompositeNode node = NodeModelUtils.findActualNodeFor(prev); Assert.assertEquals(" myPrev", node.getText()); EObject lookup = NodeModelUtils.findActualSemanticObjectFor(node); Assert.assertSame(prev, lookup); }
Example #18
Source File: NodeModelUtils.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
/** * Returns the list of nodes that were used to assign values to the given feature for the given object. * * @return the list of nodes that were used to assign values to the given feature for the given object. */ /* @NonNull */ public static List<INode> findNodesForFeature(EObject semanticObject, EStructuralFeature structuralFeature) { ICompositeNode node = findActualNodeFor(semanticObject); if (node != null && structuralFeature != null) { return findNodesForFeature(semanticObject, node, structuralFeature); } return Collections.emptyList(); }
Example #19
Source File: XbaseQuickfixProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected <T extends EObject> void remove(EObject element, Class<T> type, IModificationContext context) throws BadLocationException { T container = EcoreUtil2.getContainerOfType(element, type); if (container == null) { return; } ICompositeNode node = NodeModelUtils.findActualNodeFor(container); if (node == null) { return; } remove(context.getXtextDocument(), node); }
Example #20
Source File: EmbeddedEditorModelAccess.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
public void updateModel(final String model, final String editablePartUriFragment) { updateModel(model, new IUnitOfWork<ITextRegion, XtextResource>() { @Override public ITextRegion exec(XtextResource state) throws Exception { EObject editablePart = state.getEObject(editablePartUriFragment); ICompositeNode node = NodeModelUtils.findActualNodeFor(editablePart); if (node != null) return new TextRegion(node.getOffset(), node.getLength()); return null; } }); }
Example #21
Source File: Bug250313Test.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testNestedDatatypeConversion_01() throws Exception { Model model = (Model) getModel("1 zonk + foo - bar"); assertEquals("str", model.getValue()); assertEquals("org.eclipse.xtext.valueconverter.Bug250313.NestedDatatype", lexerRule); assertTrue(node instanceof ICompositeNode); assertEquals(5, Iterables.size(((ICompositeNode)node).getChildren())); assertEquals("zonk + foo - bar", string); assertEquals(1, convertCallCount); }
Example #22
Source File: Bug250313Test.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testDatatypeConversion_01() throws Exception { Model model = (Model) getModel("1 foo - bar"); assertEquals("str", model.getValue()); assertEquals("org.eclipse.xtext.valueconverter.Bug250313.Datatype", lexerRule); assertTrue(node instanceof ICompositeNode); assertEquals(6, Iterables.size(((ICompositeNode)node).getChildren())); assertEquals("foo - bar", string); assertEquals(1, convertCallCount); }
Example #23
Source File: XbaseHighlightingCalculator.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected void highlightAnnotation(XAnnotation annotation, IHighlightedPositionAcceptor acceptor, String highlightingConfiguration) { JvmType annotationType = annotation.getAnnotationType(); if (annotationType != null && !annotationType.eIsProxy() && annotationType instanceof JvmAnnotationType) { ICompositeNode xannotationNode = NodeModelUtils.findActualNodeFor(annotation); if (xannotationNode != null) { ILeafNode firstLeafNode = NodeModelUtils.findLeafNodeAtOffset(xannotationNode, xannotationNode.getOffset() ); if(firstLeafNode != null) highlightNode(acceptor, firstLeafNode, highlightingConfiguration); } highlightReferenceJvmType(acceptor, annotation, XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, annotationType, highlightingConfiguration); } }
Example #24
Source File: FixedPartialParsingHelper.java From dsl-devkit with Eclipse Public License 1.0 | 5 votes |
private INode getLastChild(final ICompositeNode parent) { BidiTreeIterator<? extends INode> iterator = parent.getAsTreeIterable().iterator(); while (iterator.hasPrevious()) { INode previous = iterator.previous(); if (previous instanceof ILeafNode) { return previous; } else if (previous instanceof ICompositeNode) { if (!((ICompositeNode) previous).hasChildren()) { return previous; } } } return parent; }
Example #25
Source File: XtendProposalProvider.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
@Override protected void completeWithinBlock(EObject model, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { if (model instanceof AnonymousClass) { ICompositeNode node = NodeModelUtils.getNode(model); if (node.getOffset() >= context.getOffset()) { super.completeWithinBlock(model, context, acceptor); } else { return; } } if (model instanceof XtendMember && model.eContainer() instanceof AnonymousClass) { return; } super.completeWithinBlock(model, context, acceptor); }
Example #26
Source File: LeafNodeFinder.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
public ILeafNode searchIn(INode node) { if (node instanceof ICompositeNode) { return caseCompositeNode((ICompositeNode) node); } else { return caseLeafNode((ILeafNode) node); } }
Example #27
Source File: DocumentExtensions.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
public Position newPosition(Resource resource, int offset) { if (resource instanceof XtextResource) { ICompositeNode rootNode = ((XtextResource) resource).getParseResult().getRootNode(); LineAndColumn lineAndColumn = getLineAndColumn(rootNode, offset); return new Position(lineAndColumn.getLine() - 1, lineAndColumn.getColumn() - 1); } return null; }
Example #28
Source File: EntryPointFinder.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected ICompositeNode normalizeToParent(ICompositeNode result) { if (result == null) { return result; } ICompositeNode parent = result.getParent(); while (parent != null && parent.getFirstChild().equals(result) && parent.getLookAhead() == result.getLookAhead()) { result = parent; parent = result.getParent(); } return result; }
Example #29
Source File: Bug480686Test.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
private void assertEqualNodes(INode node, INode other) { Assert.assertEquals(node.getClass(), other.getClass()); if (node instanceof ILeafNode) { ILeafNode leafNode = (ILeafNode) node; Assert.assertEquals(leafNode.getTotalOffset(), other.getTotalOffset()); Assert.assertEquals(leafNode.getTotalLength(), other.getTotalLength()); } Assert.assertEquals(node.getGrammarElement(), other.getGrammarElement()); Assert.assertEquals(Boolean.valueOf(node.hasDirectSemanticElement()), other.hasDirectSemanticElement()); Assert.assertEquals(node.getSyntaxErrorMessage(), other.getSyntaxErrorMessage()); if (node instanceof ICompositeNode) { ICompositeNode compositeNode = (ICompositeNode) node; Assert.assertEquals(compositeNode.getText(), compositeNode.getLookAhead(), ((ICompositeNode) other).getLookAhead()); } }
Example #30
Source File: TokenSequencePreservingPartialParsingHelper.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected ICompositeNode getReplacedNode(PartialParsingPointers parsingPointers) { List<ICompositeNode> validReplaceRootNodes = parsingPointers.getValidReplaceRootNodes(); ICompositeNode replaceMe = null; for (int i = validReplaceRootNodes.size() - 1; i >= 0; --i) { replaceMe = validReplaceRootNodes.get(i); if (!(replaceMe instanceof SyntheticCompositeNode)) { break; } } return replaceMe; }