Search in sources :

Example 1 with LeafNode

use of org.eclipse.xtext.nodemodel.impl.LeafNode in project xtext-core by eclipse.

the class TransformationDiagnosticTest method testDiagnostic.

@Test
public void testDiagnostic() throws Exception {
    INode node = new LeafNode();
    TransformationDiagnostic diagnostic = new TransformationDiagnostic(node, "message", TransformationErrorCode.FeatureWithDifferentConfigurationAlreadyExists);
    String expected = "org.eclipse.xtext.xtext.ecoreInference.TransformationErrorCode.FeatureWithDifferentConfigurationAlreadyExists";
    assertEquals(expected, diagnostic.getCode());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) Test(org.junit.Test)

Example 2 with LeafNode

use of org.eclipse.xtext.nodemodel.impl.LeafNode in project xtext-core by eclipse.

the class LazyLinkingResourceTest method testEObjectReference.

@Test
public void testEObjectReference() throws Exception {
    final EAnnotation source = EcoreFactory.eINSTANCE.createEAnnotation();
    final EObject referencedObject = XtextFactory.eINSTANCE.createGrammar();
    LazyLinkingResource res = new LazyLinkingResource();
    res.setLinkingHelper(new LinkingHelper() {

        @Override
        public String getCrossRefNodeAsString(INode node, boolean convert) {
            return node.getText();
        }
    });
    res.setEncoder(new LazyURIEncoder() {

        @Override
        public boolean isCrossLinkFragment(Resource res, String s) {
            return "foo".equals(s);
        }

        @Override
        public Triple<EObject, EReference, INode> decode(Resource res, String uriFragment) {
            return Tuples.create((EObject) source, EcorePackage.Literals.EANNOTATION__REFERENCES, (INode) new LeafNode());
        }
    });
    res.setLinkingService(new ILinkingService() {

        @Override
        public List<EObject> getLinkedObjects(EObject context, EReference reference, INode node) throws IllegalNodeException {
            return Lists.newArrayList(referencedObject);
        }
    });
    res.setDiagnosticMessageProvider(new LinkingDiagnosticMessageProvider());
    assertEquals(referencedObject, res.getEObject("foo"));
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Resource(org.eclipse.emf.ecore.resource.Resource) Triple(org.eclipse.xtext.util.Triple) EAnnotation(org.eclipse.emf.ecore.EAnnotation) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) IllegalNodeException(org.eclipse.xtext.linking.impl.IllegalNodeException) ILinkingService(org.eclipse.xtext.linking.ILinkingService) LinkingDiagnosticMessageProvider(org.eclipse.xtext.linking.impl.LinkingDiagnosticMessageProvider) List(java.util.List) InternalEList(org.eclipse.emf.ecore.util.InternalEList) LinkingHelper(org.eclipse.xtext.linking.impl.LinkingHelper) EReference(org.eclipse.emf.ecore.EReference) Test(org.junit.Test)

Example 3 with LeafNode

use of org.eclipse.xtext.nodemodel.impl.LeafNode in project n4js by eclipse.

the class N4JSDocumentationProvider method searchLeafNodeDocumentation.

/**
 * This is a fix for shadowed jsdoc-style documentation. Example:
 *
 * <pre>
 * "use strict" /&#42;&#42;
 * jsdoc-style comment
 * &#42;/
 * class A {
 * }
 * </pre>
 *
 * Parsing the snippet above will trigger automatic semicolon insertion (ASI) in the first line. Currently ASI
 * replaces the HiddenLeaf MultiLine comment starting in the same line as the missing ';' by a
 * {@link LeafNodeWithSyntaxError} which is not hidden. It also transfers the "ownership" from the class-node to the
 * directive-statement in the parse-tree. The text of the LeafNodeWithSyntaxError is still the original
 * comment-text.
 *
 * Giving the concrete node for "class A", this method will return the LeafNodeWithSyntaxError representing an ASI
 * and having the textual content of the jsdoc.
 *
 * <p>
 * Looking for lost documentation due to ASI. Goes backward in the AST unless an ASI-modified
 * {@link LeafNodeWithSyntaxError} is encountered.
 *
 * @return A hidden leaf node with jsdoc-documentation or a LeafNodeWithSyntaxError containing the documentation.
 *         {@code null} if no jsdoc-style documentation could be found.
 */
private LeafNode searchLeafNodeDocumentation(ICompositeNode ptNodeOfASTNode) {
    BidiTreeIterator<INode> rootIterator = ptNodeOfASTNode.getRootNode().getAsTreeIterable().iterator();
    // First linearly search the current node, then go back until a documentation or a non-hidden node is
    // encountered.
    // First search:
    int counter = 0;
    // flag for finding right position in iterator.
    boolean found = false;
    while (rootIterator.hasNext()) {
        INode next = rootIterator.next();
        counter++;
        dump(counter, next);
        if (next == ptNodeOfASTNode) {
            found = true;
            break;
        }
    }
    if (!found) {
        throw new IllegalStateException("Node model broken. Unable to find the current node in the node model by traversing from root.");
    }
    // Second search:
    // go back until we find a LeafNodeWithSyntaxErrors or a different non-hidden leaf.
    // .getParent();
    ICompositeNode directParent = ptNodeOfASTNode;
    boolean beforeDirectParentNode = false;
    // direct parent. This is marked in beforDirectParentNode
    while (rootIterator.hasPrevious()) {
        INode prev = rootIterator.previous();
        counter--;
        dump(counter, prev);
        if (!beforeDirectParentNode) {
            if (!hasAsParent(prev, directParent)) {
                beforeDirectParentNode = true;
            } else {
                continue;
            }
        }
        if (prev instanceof LeafNodeWithSyntaxError) {
            if (((LeafNodeWithSyntaxError) prev).getSyntaxErrorMessage().getIssueCode() != InternalSemicolonInjectingParser.SEMICOLON_INSERTED) {
                // Some other error, not ASI, stop looking.
                return null;
            }
            // Two cases can happen:
            // 1) it is a ML-documentation
            // 2) it is a line-break
            // single-line documentations don't match the
            String text = prev.getText();
            if (isDocumentationStyle(text)) {
                // found a documentation
                return (LeafNodeWithSyntaxError) prev;
            } else {
            // not a documentation, then continue
            }
        } else if (prev instanceof LeafNode) {
            LeafNode ln = (LeafNode) prev;
            if (!ln.isHidden()) {
                // found a non-hidden node, stop looking!
                return null;
            } else {
                // hidden leaf-node, check for comment.
                String text = prev.getText();
                if (isDocumentationStyle(text)) {
                    // found a documentation
                    return (LeafNode) prev;
                }
            }
        }
    // other wise keep looking.
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) LeafNodeWithSyntaxError(org.eclipse.xtext.nodemodel.impl.LeafNodeWithSyntaxError)

Example 4 with LeafNode

use of org.eclipse.xtext.nodemodel.impl.LeafNode in project dsl-devkit by dsldevkit.

the class AbstractLabelProvider method getStyledLabel.

/**
 * 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);
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) HiddenLeafNode(org.eclipse.xtext.nodemodel.impl.HiddenLeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) StyledString(org.eclipse.jface.viewers.StyledString) HiddenLeafNode(org.eclipse.xtext.nodemodel.impl.HiddenLeafNode)

Example 5 with LeafNode

use of org.eclipse.xtext.nodemodel.impl.LeafNode in project xtext-core by eclipse.

the class LazyLinkingResourceTest method testWarningInsteadOfError.

@Test
public void testWarningInsteadOfError() throws Exception {
    final EAnnotation source = EcoreFactory.eINSTANCE.createEAnnotation();
    LazyLinkingResource res = new LazyLinkingResource();
    res.setEncoder(new LazyURIEncoder() {

        @Override
        public boolean isCrossLinkFragment(Resource res, String s) {
            return "foo".equals(s);
        }

        @Override
        public Triple<EObject, EReference, INode> decode(Resource res, String uriFragment) {
            return Tuples.create((EObject) source, EcorePackage.Literals.EANNOTATION__REFERENCES, (INode) new LeafNode());
        }
    });
    res.setLinkingService(new ILinkingService() {

        @Override
        public List<EObject> getLinkedObjects(EObject context, EReference reference, INode node) throws IllegalNodeException {
            return Collections.emptyList();
        }
    });
    res.setDiagnosticMessageProvider(new LinkingDiagnosticMessageProvider() {

        @Override
        public DiagnosticMessage getUnresolvedProxyMessage(ILinkingDiagnosticContext context) {
            return new DiagnosticMessage("myMessage", Severity.WARNING, null);
        }
    });
    assertTrue(res.getWarnings().isEmpty());
    assertNull(res.getEObject("foo"));
    assertEquals(1, res.getWarnings().size());
    assertTrue(res.getErrors().isEmpty());
    assertEquals("myMessage", res.getWarnings().get(0).getMessage());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Resource(org.eclipse.emf.ecore.resource.Resource) Triple(org.eclipse.xtext.util.Triple) EAnnotation(org.eclipse.emf.ecore.EAnnotation) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) IllegalNodeException(org.eclipse.xtext.linking.impl.IllegalNodeException) ILinkingService(org.eclipse.xtext.linking.ILinkingService) LinkingDiagnosticMessageProvider(org.eclipse.xtext.linking.impl.LinkingDiagnosticMessageProvider) List(java.util.List) InternalEList(org.eclipse.emf.ecore.util.InternalEList) EReference(org.eclipse.emf.ecore.EReference) Test(org.junit.Test)

Aggregations

LeafNode (org.eclipse.xtext.nodemodel.impl.LeafNode)12 INode (org.eclipse.xtext.nodemodel.INode)8 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)6 Test (org.junit.Test)6 EObject (org.eclipse.emf.ecore.EObject)4 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)4 List (java.util.List)3 CompositeNode (org.eclipse.xtext.nodemodel.impl.CompositeNode)3 NodeModelBuilder (org.eclipse.xtext.nodemodel.impl.NodeModelBuilder)3 EAnnotation (org.eclipse.emf.ecore.EAnnotation)2 EClass (org.eclipse.emf.ecore.EClass)2 EReference (org.eclipse.emf.ecore.EReference)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 InternalEList (org.eclipse.emf.ecore.util.InternalEList)2 ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)2 ILinkingService (org.eclipse.xtext.linking.ILinkingService)2 IllegalNodeException (org.eclipse.xtext.linking.impl.IllegalNodeException)2 LinkingDiagnosticMessageProvider (org.eclipse.xtext.linking.impl.LinkingDiagnosticMessageProvider)2 HiddenLeafNode (org.eclipse.xtext.nodemodel.impl.HiddenLeafNode)2