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());
}
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"));
}
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" /**
* jsdoc-style comment
* */
* 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;
}
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);
}
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());
}
Aggregations