Java Code Examples for org.eclipse.xtext.nodemodel.INode#getSemanticElement()
The following examples show how to use
org.eclipse.xtext.nodemodel.INode#getSemanticElement() .
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: SolidityHyperlinkHelper.java From solidity-ide with Eclipse Public License 1.0 | 6 votes |
protected void createImportedNamespacesHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) { INode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset); if (node != null) { List<INode> importNodes = NodeModelUtils.findNodesForFeature(node.getSemanticElement(), SolidityPackage.Literals.IMPORT_DIRECTIVE__IMPORTED_NAMESPACE); if (importNodes != null && !importNodes.isEmpty()) { for (INode importNode : importNodes) { ImportDirective importElement = (ImportDirective) importNode.getSemanticElement(); URI targetURI = getFileURIOfImport(importElement); XtextHyperlink result = getHyperlinkProvider().get(); result.setURI(targetURI); Region region = new Region(importNode.getOffset(), importNode.getLength()); result.setHyperlinkRegion(region); result.setHyperlinkText(targetURI.toString()); acceptor.accept(result); } } } }
Example 2
Source File: XbaseStratumBreakpointSupport.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
protected boolean isValidLineForBreakpoint(ICompositeNode node, int line) { for (INode n : node.getChildren()) { ITextRegionWithLineInformation textRegion = n.getTextRegionWithLineInformation(); if (textRegion.getLineNumber()<= line && textRegion.getEndLineNumber() >= line) { EObject eObject = n.getSemanticElement(); if (eObject instanceof XExpression && !(eObject.eClass() == XbasePackage.Literals.XBLOCK_EXPRESSION)) { return true; } if (n instanceof ICompositeNode && isValidLineForBreakpoint((ICompositeNode) n, line)) { return true; } } if (textRegion.getLineNumber() > line) { return false; } } return false; }
Example 3
Source File: PackageImportHyperlinkHelper.java From statecharts with Eclipse Public License 1.0 | 6 votes |
protected void createPackageImportHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) { INode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset); if (node != null && node.getGrammarElement() instanceof RuleCall && node.getSemanticElement() instanceof ImportScope) { NodeModelUtils.findNodesForFeature(node.getSemanticElement(), TypesPackage.Literals.PACKAGE__IMPORT); ImportScope importScope = (ImportScope) node.getSemanticElement(); EList<String> imports = importScope.getImports(); for (String pkgImport : imports) { Optional<PackageImport> mappedImport = mapper.findPackageImport(resource, pkgImport); if (mappedImport.isPresent()) { acceptor.accept(createHyperlink(node, mappedImport.get())); } } } }
Example 4
Source File: ParserBasedContentAssistContextFactory.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Override protected void handleLastCompleteNodeIsAtEndOfDatatypeNode() throws BadLocationException { String prefix = getPrefix(lastCompleteNode); INode previousNode = getLastCompleteNodeByOffset(rootNode, lastCompleteNode.getOffset()); EObject previousModel = previousNode.getSemanticElement(); INode currentDatatypeNode = getContainingDatatypeRuleNode(currentNode); Collection<FollowElement> followElements = parseFollowElements(lastCompleteNode.getOffset(), false); int prevSize = contextBuilders.size(); doCreateContexts(previousNode, currentDatatypeNode, prefix, previousModel, followElements); if (lastCompleteNode instanceof ILeafNode && lastCompleteNode.getGrammarElement() == null && contextBuilders.size() != prevSize) { handleLastCompleteNodeHasNoGrammarElement(contextBuilders.subList(prevSize, contextBuilders.size()), previousModel); } }
Example 5
Source File: PartialContentAssistContextFactory.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Override protected void handleLastCompleteNodeIsAtEndOfDatatypeNode() { String prefix = getPrefix(lastCompleteNode); INode previousNode = getLastCompleteNodeByOffset(rootNode, lastCompleteNode.getOffset()); EObject previousModel = previousNode.getSemanticElement(); INode currentDatatypeNode = getContainingDatatypeRuleNode(currentNode); Collection<FollowElement> followElements = parseFollowElements(lastCompleteNode.getOffset(), false); int prevSize = contextBuilders.size(); doCreateContexts(previousNode, currentDatatypeNode, prefix, previousModel, followElements); if (lastCompleteNode instanceof ILeafNode && lastCompleteNode.getGrammarElement() == null && contextBuilders.size() != prevSize) { handleLastCompleteNodeHasNoGrammarElement(contextBuilders.subList(prevSize, contextBuilders.size()), previousModel); } }
Example 6
Source File: StyledTemplateProposal.java From n4js with Eclipse Public License 1.0 | 5 votes |
static private boolean hasTrailingComma(ContentAssistContext context) { boolean trailingComma = false; // add trailing comma, if the name-value pair is inserted in the middle of a // list of existing pairs. final INode currentNode = context.getCurrentNode(); if (currentNode.hasNextSibling()) { final INode nextSibling = currentNode.getNextSibling(); if (nextSibling.getSemanticElement() instanceof NameValuePair) { trailingComma = true; } } return trailingComma; }
Example 7
Source File: ContentAssistContextFactory.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected void handleLastCompleteNodeIsAtEndOfDatatypeNode() { String prefix = getPrefix(lastCompleteNode); String completeInput = getInputToParse(lastCompleteNode); INode previousNode = getLastCompleteNodeByOffset(rootNode, lastCompleteNode.getOffset()); EObject previousModel = previousNode.getSemanticElement(); INode currentDatatypeNode = getContainingDatatypeRuleNode(currentNode); Collection<FollowElement> followElements = parser.getFollowElements(completeInput, false); int prevSize = contextBuilders.size(); doCreateContexts(previousNode, currentDatatypeNode, prefix, previousModel, followElements); if (lastCompleteNode instanceof ILeafNode && lastCompleteNode.getGrammarElement() == null && contextBuilders.size() != prevSize) { handleLastCompleteNodeHasNoGrammarElement(contextBuilders.subList(prevSize, contextBuilders.size()), previousModel); } }
Example 8
Source File: GH1462Test.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void test12a() throws Exception { Root root = (Root) getModelAndExpect("#12 123 s", 1); Rule12 rule12 = (Rule12) root.getElement(); assertEquals(123, rule12.getLeft()); // TODO (sza, cdi): This should be an invalid grammar instead assertTrue(rule12.isRight()); INode featureNode = NodeModelUtils.findNodesForFeature(rule12, Gh1462TestPackage.Literals.RULE12__RIGHT).get(0); assertEquals("", featureNode.getText()); assertTrue(featureNode.hasDirectSemanticElement()); EObject element = featureNode.getSemanticElement(); assertNull(element.eResource()); }
Example 9
Source File: AbstractDiagnostic.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
public URI getUriToProblem() { INode node = getNode(); if (node == null) return null; EObject eObject = node.getSemanticElement(); if (eObject==null || eObject.eResource()==null) return null; return EcoreUtil.getURI(eObject); }
Example 10
Source File: SemanticNodeIterator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected EObject getEObjectNodeEObject(INode node) { if (node.hasDirectSemanticElement()) return node.getSemanticElement(); Iterator<INode> i = node.getAsTreeIterable().iterator(); while (i.hasNext()) { INode n = i.next(); if (n.hasDirectSemanticElement()) return n.getSemanticElement(); } return null; }
Example 11
Source File: DefaultCommentAssociater.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected EObject getEObjectForRemainingComments(ICompositeNode rootNode) { TreeIterator<INode> i = rootNode.getAsTreeIterable().iterator(); while (i.hasNext()) { INode o = i.next(); if (o.hasDirectSemanticElement()) return o.getSemanticElement(); } return null; }
Example 12
Source File: ParserBasedContentAssistContextFactory.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected void handleLastCompleteNodeIsAtEndOfDatatypeNode() throws BadLocationException { String prefix = getPrefix(lastCompleteNode); String completeInput = getInputToParse(lastCompleteNode); INode previousNode = getLastCompleteNodeByOffset(rootNode, lastCompleteNode.getOffset()); EObject previousModel = previousNode.getSemanticElement(); INode currentDatatypeNode = getContainingDatatypeRuleNode(currentNode); Collection<FollowElement> followElements = parser.getFollowElements(completeInput, false); int prevSize = contextBuilders.size(); doCreateContexts(previousNode, currentDatatypeNode, prefix, previousModel, followElements); if (lastCompleteNode instanceof ILeafNode && lastCompleteNode.getGrammarElement() == null && contextBuilders.size() != prevSize) { handleLastCompleteNodeHasNoGrammarElement(contextBuilders.subList(prevSize, contextBuilders.size()), previousModel); } }
Example 13
Source File: ReferenceResolutionFinder.java From n4js with Eclipse Public License 1.0 | 5 votes |
private EObject getParentImportElement(INode parseTreeNode) { while (parseTreeNode != null) { EObject semanticElement = parseTreeNode.getSemanticElement(); if (semanticElement instanceof ImportCallExpression || semanticElement instanceof ImportDeclaration) { return semanticElement; } parseTreeNode = parseTreeNode.getParent(); } return null; }
Example 14
Source File: PartialParsingHelper.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
public void unloadNode(INode node) { if (node != null) { EObject semantic = node.getSemanticElement(); unloadSemanticObject(semantic); } }
Example 15
Source File: DotSemanticHighlightingCalculator.java From gef with Eclipse Public License 2.0 | 4 votes |
@Override public void doProvideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) { // It gets a node model. INode root = resource.getParseResult().getRootNode(); for (INode node : root.getAsTreeIterable()) { EObject grammarElement = node.getGrammarElement(); if (grammarElement instanceof RuleCall) { RuleCall rc = (RuleCall) grammarElement; AbstractRule r = rc.getRule(); EObject c = grammarElement.eContainer(); // handle ID elements specifically if (r.getName().equals("ID")) { //$NON-NLS-1$ EObject semanticElement = node.getSemanticElement(); switch (((Assignment) c).getFeature()) { case "name": //$NON-NLS-1$ if (semanticElement instanceof DotGraph) { acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.GRAPH_NAME_ID); } else if (semanticElement instanceof NodeStmt || semanticElement instanceof NodeId) { acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.NODE_NAME_ID); } else if (semanticElement instanceof Attribute) { acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.ATTRIBUTE_NAME_ID); } else if (semanticElement instanceof Port) { acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.PORT_NAME_ID); } break; case "value": //$NON-NLS-1$ if (semanticElement instanceof Attribute) { switch (((Attribute) semanticElement).getName() .toValue()) { case DotAttributes.ARROWHEAD__E: case DotAttributes.ARROWTAIL__E: provideHighlightingForArrowTypeString(node, acceptor); break; } } break; } } if (r.getName().equals("EdgeOp")) { //$NON-NLS-1$ acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.EDGE_OP_ID); } if (r.getName().equals("HTML_STRING")) { //$NON-NLS-1$ provideHighlightingForHtmlString(node, acceptor); } } } }
Example 16
Source File: DotHtmlLabelSemanticHighlightingCalculator.java From gef with Eclipse Public License 2.0 | 4 votes |
@Override public void doProvideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) { // It gets a node model. INode root = resource.getParseResult().getRootNode(); for (INode node : root.getAsTreeIterable()) { EObject grammarElement = node.getGrammarElement(); if (grammarElement instanceof TerminalRule) { if ("HTML_COMMENT" //$NON-NLS-1$ .equals(((TerminalRule) grammarElement).getName())) { acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.HTML_COMMENT); } } if (grammarElement instanceof RuleCall) { RuleCall rc = (RuleCall) grammarElement; AbstractRule r = rc.getRule(); String ruleName = r.getName(); switch (ruleName) { case "ATTR_VALUE": //$NON-NLS-1$ acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.HTML_ATTRIBUTE_VALUE); break; case "TAG_START_CLOSE": //$NON-NLS-1$ case "TAG_START": //$NON-NLS-1$ case "TAG_END": //$NON-NLS-1$ case "TAG_END_CLOSE": //$NON-NLS-1$ acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.HTML_TAG); break; case "ASSIGN": //$NON-NLS-1$ acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.HTML_ATTRIBUTE_EQUAL_SIGN); default: EObject c = grammarElement.eContainer(); if (c instanceof Assignment) { EObject semanticElement = node.getSemanticElement(); String feature = ((Assignment) c).getFeature(); if (r.getName().equals("ID")) { //$NON-NLS-1$ if (semanticElement instanceof HtmlAttr && "name".equals(feature)) { //$NON-NLS-1$ acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.HTML_ATTRIBUTE_NAME); } else if ("name".equals(feature) //$NON-NLS-1$ || "closeName".equals(feature)) { //$NON-NLS-1$ acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.HTML_TAG); } } else if (semanticElement instanceof HtmlContent && "text".equals(feature)) { //$NON-NLS-1$ acceptor.addPosition(node.getOffset(), node.getLength(), DotHighlightingConfiguration.HTML_CONTENT); } } } } } }
Example 17
Source File: HiddenTokenSequencer.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
protected boolean belongsToDeletedElement(INode node) { return node instanceof ICompositeNode && node.getSemanticElement() != null && node.getSemanticElement().eResource() == null; }
Example 18
Source File: FixedPartialParsingHelper.java From dsl-devkit with Eclipse Public License 1.0 | 4 votes |
public void unloadNode(final INode node) { if (node != null) { EObject semantic = node.getSemanticElement(); unloadSemanticObject(semantic); } }
Example 19
Source File: ResourceStorageTest.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
@Test public void testWriteAndLoad() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("{"); _builder.newLine(); _builder.append("\t"); _builder.append("var x = \"Hello\""); _builder.newLine(); _builder.append("\t"); _builder.append("var y = \"\""); _builder.newLine(); _builder.append("\t"); _builder.append("val it = x"); _builder.newLine(); _builder.append("\t"); _builder.append("y = length.toString"); _builder.newLine(); _builder.append("\t"); _builder.append("println(x)"); _builder.newLine(); _builder.append("\t"); _builder.append("y = length.toString"); _builder.newLine(); _builder.append("\t"); _builder.append("println(x)"); _builder.newLine(); _builder.append("\t"); _builder.append("y = length.toString"); _builder.newLine(); _builder.append("\t"); _builder.append("println(x)"); _builder.newLine(); _builder.append("}"); _builder.newLine(); final String contents = _builder.toString(); final XExpression file = this.expression(contents); final ByteArrayOutputStream bout = new ByteArrayOutputStream(); ((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true); Resource _eResource = file.eResource(); this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource)); byte[] _byteArray = bout.toByteArray(); ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray); final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream); Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/Test.___xbase")); final StorageAwareResource resource = ((StorageAwareResource) _createResource); final ResourceStorageTest.InMemoryURIConverter converter = new ResourceStorageTest.InMemoryURIConverter(); converter.addModel(resource.getURI().toString(), contents); ResourceSet _resourceSet = resource.getResourceSet(); _resourceSet.setURIConverter(converter); EList<Resource> _resources = file.eResource().getResourceSet().getResources(); _resources.add(resource); resource.loadFromStorage(in); EObject _get = resource.getContents().get(0); final XExpression root = ((XExpression) _get); Assert.assertTrue((root instanceof XBlockExpression)); final BidiTreeIterator<INode> restoredNodes = NodeModelUtils.findActualNodeFor(IterableExtensions.<EObject>head(resource.getContents())).getAsTreeIterable().iterator(); final BidiTreeIterator<INode> originalNodes = NodeModelUtils.findActualNodeFor(file).getAsTreeIterable().iterator(); while (restoredNodes.hasNext()) { { final INode rest = restoredNodes.next(); final INode orig = originalNodes.next(); Assert.assertEquals(orig.getStartLine(), rest.getStartLine()); Assert.assertEquals(orig.getEndLine(), rest.getEndLine()); Assert.assertEquals(orig.getOffset(), rest.getOffset()); Assert.assertEquals(orig.getEndOffset(), rest.getEndOffset()); Assert.assertEquals(orig.getLength(), rest.getLength()); Assert.assertEquals(orig.getTotalStartLine(), rest.getTotalStartLine()); Assert.assertEquals(orig.getTotalEndLine(), rest.getTotalEndLine()); Assert.assertEquals(orig.getTotalOffset(), rest.getTotalOffset()); Assert.assertEquals(orig.getTotalEndOffset(), rest.getTotalEndOffset()); Assert.assertEquals(orig.getTotalLength(), rest.getTotalLength()); Assert.assertSame(orig.getGrammarElement(), rest.getGrammarElement()); Assert.assertTrue((((orig.getSemanticElement() != null) && (rest.getSemanticElement() != null)) || ((orig.getSemanticElement() == null) && (rest.getSemanticElement() == null)))); EObject _semanticElement = orig.getSemanticElement(); boolean _tripleNotEquals = (_semanticElement != null); if (_tripleNotEquals) { Assert.assertEquals(file.eResource().getURIFragment(orig.getSemanticElement()), resource.getURIFragment(rest.getSemanticElement())); } Assert.assertEquals(orig.getText(), rest.getText()); } } Assert.assertFalse(originalNodes.hasNext()); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
Example 20
Source File: N4JSSyntaxValidator.java From n4js with Eclipse Public License 1.0 | 4 votes |
/** * Returns the first keyword with the given value, or null if no such keyword is found. */ private ILeafNode doFindLeafWithKeyword(EObject semanticElement, String stopAtKeyword, ICompositeNode node, String keyWord, boolean commaAlternative, int hitNumber) { EObject grammarElement; int foundHits = 0; for (BidiTreeIterator<INode> iter = node.getAsTreeIterable().iterator(); iter.hasNext();) { INode child = iter.next(); EObject childSemElement = child.getSemanticElement(); if (child != node && childSemElement != null && childSemElement != semanticElement) { iter.prune(); } else if (child instanceof ILeafNode) { ILeafNode leaf = (ILeafNode) child; grammarElement = leaf.getGrammarElement(); if (grammarElement instanceof Keyword) { String value = ((Keyword) grammarElement).getValue(); if (stopAtKeyword.equals(value)) { return null; } if (keyWord.equals(value)) { if (grammarElement.eContainer() instanceof Alternatives) { AbstractElement first = ((Alternatives) (grammarElement.eContainer())).getElements().get(0); boolean inCommaAlternative = (first instanceof Keyword && ",".equals(((Keyword) first) .getValue())); if (inCommaAlternative == commaAlternative) { foundHits++; if (foundHits >= hitNumber) { return leaf; } } } else { if (!commaAlternative) { foundHits++; if (foundHits >= hitNumber) { return leaf; } } } } } } } return null; }