Java Code Examples for org.eclipse.xtext.nodemodel.INode#getAsTreeIterable()

The following examples show how to use org.eclipse.xtext.nodemodel.INode#getAsTreeIterable() . 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: DotArrowTypeSemanticHighlightingCalculator.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@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();
			String ruleName = r.getName();
			switch (ruleName) {
			case "DeprecatedShape": //$NON-NLS-1$
				acceptor.addPosition(node.getOffset(), node.getLength(),
						DotHighlightingConfiguration.DEPRECATED_ATTRIBUTE_VALUE);
				break;
			}
		}
	}
}
 
Example 2
Source File: JSONSemanticHighlightingCalculator.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor,
		CancelIndicator cancelIndicator) {
	if (resource == null || resource.getParseResult() == null) {
		// skip invalid resources
		return;
	}

	// obtain root node
	INode root = resource.getParseResult().getRootNode();

	for (INode node : root.getAsTreeIterable()) {
		EObject grammarElement = node.getGrammarElement();

		// special handling of the names of name-value-pairs in order to differentiate
		// keys and values
		if (grammarElement instanceof RuleCall && grammarElement.eContainer() instanceof Assignment
				&& ((RuleCall) grammarElement).getRule() == grammarAccess.getSTRINGRule()) {
			final Assignment assignment = ((Assignment) grammarElement.eContainer());

			// if the STRING value is assigned to the feature 'name' of NameValuePair
			if (assignment.getFeature().equals(JSONPackage.Literals.NAME_VALUE_PAIR__NAME.getName())) {
				// enable PROPERTY_NAME highlighting
				acceptor.addPosition(node.getOffset(), node.getLength(),
						JSONHighlightingConfiguration.PROPERTY_NAME_ID);
			} else {
				// otherwise enable string literal highlighting
				acceptor.addPosition(node.getOffset(), node.getLength(), JSONHighlightingConfiguration.STRING_ID);
			}
		}
	}
}
 
Example 3
Source File: XbaseQualifiedNameValueConverter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String toValue(String string, INode node) throws ValueConverterException {
	StringBuilder buffer = new StringBuilder();
	boolean isFirst = true;
	if (node != null) {
		for(INode child: node.getAsTreeIterable()) {
			EObject grammarElement = child.getGrammarElement();
			if (isDelegateRuleCall(grammarElement) || isWildcardLiteral(grammarElement)) {
				if (!isFirst)
					buffer.append(getValueNamespaceDelimiter());
				isFirst = false;
				if (isDelegateRuleCall(grammarElement))
					for(ILeafNode leafNode :child.getLeafNodes()){
						if(!leafNode.isHidden())
							buffer.append(delegateToValue(leafNode));
					}
				else 
					buffer.append(getWildcardLiteral());
			}
		}
	} else {
		for (String segment : Strings.split(string, getStringNamespaceDelimiter())) {
			if (!isFirst)
				buffer.append(getValueNamespaceDelimiter());
			isFirst = false;
			if(getWildcardLiteral().equals(segment)) {
				buffer.append(getWildcardLiteral());
			} else {
				buffer.append((String) valueConverterService.toValue(segment, getDelegateRuleName(), null));
			}
		}
	}
	return buffer.toString();
}
 
Example 4
Source File: ReorderingHiddenTokenSequencer.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the leading comments of the given node.
 * First leaf node is considered to be an end point of the search.
 *
 * @param node
 *          element of the node model to find comments for, can be {@code null}
 * @return list of hidden tokens, can be empty if nothing found, never {@code null}
 */
private List<INode> getLeadingCommentsIncludingWhitespace(final INode node) {
  if (node instanceof ICompositeNode) {
    for (INode child : node.getAsTreeIterable()) {
      if (child instanceof ILeafNode && !((ILeafNode) child).isHidden()) {
        return getLeadingCommentsIncludingWhitespace((ILeafNode) child);
      }
    }
  } else if (node instanceof ILeafNode) {
    return getLeadingCommentsIncludingWhitespace((ILeafNode) node);
  }
  return Collections.emptyList();
}
 
Example 5
Source File: DotHtmlLabelSemanticHighlightingCalculator.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@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 6
Source File: DotSemanticHighlightingCalculator.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@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);
			}
		}
	}
}