Java Code Examples for org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor#addPosition()

The following examples show how to use org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor#addPosition() . 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: CheckHighlightingCalculator.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void highlightSpecialIdentifiers(final IHighlightedPositionAcceptor acceptor, final ICompositeNode root) {
  TerminalRule idRule = grammarAccess.getIDRule();

  for (ILeafNode leaf : root.getLeafNodes()) {
    if (commentProvider.isJavaDocComment(leaf)) {
      // not really a special identifier, but we don't want to iterate over the leaf nodes twice, do we?
      acceptor.addPosition(leaf.getOffset(), leaf.getLength(), CheckHighlightingConfiguration.JAVADOC_ID);
    } else if (!leaf.isHidden()) {
      if (leaf.getGrammarElement() instanceof Keyword) {
        // Check if it is a keyword used as an identifier.
        ParserRule rule = GrammarUtil.containingParserRule(leaf.getGrammarElement());
        if (FEATURE_CALL_ID_RULE_NAME.equals(rule.getName())) {
          acceptor.addPosition(leaf.getOffset(), leaf.getLength(), DefaultHighlightingConfiguration.DEFAULT_ID);
        }
      } else {
        highlightSpecialIdentifiers(leaf, acceptor, idRule);
      }
    }
  }
}
 
Example 2
Source File: SoliditySemanticHighlighter.java    From solidity-ide with Eclipse Public License 1.0 6 votes vote down vote up
private void provideHighLightForNamedElement(NamedElement namedElement, INode nextNode, String textStyle,
		IHighlightedPositionAcceptor acceptor) {
	acceptor.addPosition(nextNode.getOffset(), nextNode.getLength(), textStyle);
	List<ElementReferenceExpression> references = EcoreUtil2.getAllContentsOfType(namedElement.eContainer(),
			ElementReferenceExpression.class);
	for (ElementReferenceExpression elementReferenceExpression : references) {
		EObject reference = elementReferenceExpression.getReference();
		if (reference.equals(namedElement)) {
			ICompositeNode referencingNode = NodeModelUtils.findActualNodeFor(elementReferenceExpression);
			BidiIterator<INode> bidiIterator = referencingNode.getChildren().iterator();
			while (bidiIterator.hasNext()) {
				INode currentNode = bidiIterator.next();
				if (currentNode.getText().trim().equals(namedElement.getName())) {
					acceptor.addPosition(currentNode.getOffset(), currentNode.getLength(), textStyle);
				}
			}
		}
	}
}
 
Example 3
Source File: SoliditySemanticHighlighter.java    From solidity-ide with Eclipse Public License 1.0 6 votes vote down vote up
private void provideHighligtingFor(ElementReferenceExpression expression, IHighlightedPositionAcceptor acceptor) {
	EObject reference = expression.getReference();
	if (reference instanceof Declaration) {
		Declaration decl = (Declaration) expression.getReference();
		switch (decl.getName()) {
		case "msg":
		case "block":
		case "tx":
		case "now":
		case "this":
		case "super":
			ICompositeNode node = NodeModelUtils.findActualNodeFor(expression);
			acceptor.addPosition(node.getTotalOffset(), node.getLength() + 1,
					DefaultHighlightingConfiguration.KEYWORD_ID);
		}
	}
}
 
Example 4
Source File: DotSemanticHighlightingCalculator.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
private void provideHighlightingForHtmlString(INode node,
		IHighlightedPositionAcceptor acceptor) {
	// highlight the leading '<' symbol
	int openingSymbolOffset = node.getOffset();
	acceptor.addPosition(openingSymbolOffset, 1,
			DotHighlightingConfiguration.HTML_TAG);

	// highlight the trailing '>' symbol
	int closingSymbolOffset = node.getOffset() + node.getText().length()
			- 1;
	acceptor.addPosition(closingSymbolOffset, 1,
			DotHighlightingConfiguration.HTML_TAG);

	// trim the leading '<' and trailing '>' symbols
	String htmlString = node.getText().substring(1,
			node.getText().length() - 1);

	// delegate the highlighting of the the html-label substring to the
	// corresponding sub-grammar highlighter
	DotSubgrammarHighlighter htmlLabelHighlighter = new DotSubgrammarHighlighter(
			DotActivator.ORG_ECLIPSE_GEF_DOT_INTERNAL_LANGUAGE_DOTHTMLLABEL);
	htmlLabelHighlighter.provideHightlightingFor(htmlString,
			node.getOffset() + 1, acceptor);
}
 
Example 5
Source File: SemanticHighlighter.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Highlights the non-hidden parts of {@code node} with the style that is associated with {@code id}.
 */
protected void highlightNode(INode node, String id, IHighlightedPositionAcceptor acceptor) {
	if (node == null)
		return;
	if (node instanceof ILeafNode) {
		ITextRegion textRegion = node.getTextRegion();
		acceptor.addPosition(textRegion.getOffset(), textRegion.getLength(), id);
	} else {
		for (ILeafNode leaf : node.getLeafNodes()) {
			if (!leaf.isHidden()) {
				ITextRegion leafRegion = leaf.getTextRegion();
				acceptor.addPosition(leafRegion.getOffset(), leafRegion.getLength(), id);
			}
		}
	}
}
 
Example 6
Source File: SemanticHighlightingCalculator.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Highlights a given parse tree node.
 *
 * @param node
 *          the node from the parse tree
 * @param id
 *          the id of the configuration
 * @param acceptor
 *          the acceptor
 */
private void highlightNode(final INode node, final String id, final IHighlightedPositionAcceptor acceptor) {
  if (node == null) {
    return;
  }
  if (node instanceof ILeafNode) {
    acceptor.addPosition(node.getOffset(), node.getLength(), id);
  } else {
    for (ILeafNode leaf : node.getLeafNodes()) {
      if (!leaf.isHidden()) {
        acceptor.addPosition(leaf.getOffset(), leaf.getLength(), id);
      }
    }
  }
}
 
Example 7
Source File: SemanticHighlightingCalculatorImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public boolean doHighlightNode(final IHighlightedPositionAcceptor acceptor, final EObject object, final EStructuralFeature feature, final String style) {
  final Consumer<INode> _function = (INode it) -> {
    acceptor.addPosition(it.getOffset(), it.getLength(), style);
  };
  NodeModelUtils.findNodesForFeature(object, feature).forEach(_function);
  return false;
}
 
Example 8
Source File: XtendHighlightingCalculator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void highlightSpecialIdentifiers(ILeafNode leafNode, IHighlightedPositionAcceptor acceptor,
		TerminalRule idRule) {
	super.highlightSpecialIdentifiers(leafNode, acceptor, idRule);
	if (contextualKeywords != null && contextualKeywords.contains(leafNode.getGrammarElement())) {
		ITextRegion leafRegion = leafNode.getTextRegion();
		acceptor.addPosition(leafRegion.getOffset(), leafRegion.getLength(),
				HighlightingStyles.DEFAULT_ID);
	}
}
 
Example 9
Source File: FlexerBasedTemplateBodyHighlighter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doProvideHighlightingFor(final String body, final IHighlightedPositionAcceptor acceptor) {
  StringReader _stringReader = new StringReader(body);
  final FlexTokenSource tokenSource = this._flexerFactory.createTokenSource(_stringReader);
  Token token = tokenSource.nextToken();
  while ((!Objects.equal(token, Token.EOF_TOKEN))) {
    {
      final String id = this._abstractAntlrTokenToAttributeIdMapper.getId(token.getType());
      final int offset = TokenTool.getOffset(token);
      final int length = TokenTool.getLength(token);
      acceptor.addPosition(offset, length, id);
      token = tokenSource.nextToken();
    }
  }
}
 
Example 10
Source File: StatemachineSemanticHighlightingCalculator.java    From xtext-web with Eclipse Public License 2.0 5 votes vote down vote up
protected void highlightSignal(EObject owner, Signal signal, EStructuralFeature feature,
		IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
	operationCanceledManager.checkCanceled(cancelIndicator);
	for (INode n : NodeModelUtils.findNodesForFeature(owner, feature)) {
		acceptor.addPosition(n.getOffset(), n.getLength(), signal.eClass().getName());
	}
}
 
Example 11
Source File: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void highlightSpecialIdentifiers(ILeafNode leafNode, IHighlightedPositionAcceptor acceptor,
		TerminalRule idRule) {
	ITextRegion leafRegion = leafNode.getTextRegion();
	if (idLengthsToHighlight.get(leafRegion.getLength())) {
		EObject element = leafNode.getGrammarElement();
		if (element == idRule || (element instanceof RuleCall && ((RuleCall) element).getRule() == idRule)) {
			String text = leafNode.getText();
			String highlightingID = highlightedIdentifiers.get(text);
			if (highlightingID != null) {
				acceptor.addPosition(leafRegion.getOffset(), leafRegion.getLength(), highlightingID);
			}
		}
	}
}
 
Example 12
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 13
Source File: SleighHighlightingCalculator.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void processHiddenNode(IHighlightedPositionAcceptor acceptor,
		HiddenLeafNode node) {
	if (node.getGrammarElement() instanceof TerminalRuleImpl) {
		TerminalRuleImpl ge = (TerminalRuleImpl) node.getGrammarElement();
		String name = ge.getName();
		if (name.equalsIgnoreCase("PDL_COMMENT") || name.equalsIgnoreCase("ML_COMMENT") || name.equalsIgnoreCase("SL_COMMENT")) {
			acceptor.addPosition(node.getOffset(), node.getLength(), COMMENT_ID);
		}
	}

}
 
Example 14
Source File: SleighHighlightingCalculator.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void setStyles(IHighlightedPositionAcceptor acceptor,
		BidiIterator<INode> it, String... styles) {
	for (String s : styles) {
		if (!it.hasNext())
			return;
		INode n = skipWhiteSpace(acceptor, it);
		if (n != null && s != null)
			acceptor.addPosition(n.getOffset(), n.getLength(), s);
	}
}
 
Example 15
Source File: DeprecatedMergingHighlightedPositionAcceptorTest2.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
	acceptor.addPosition(2, 4, "1", "2");
}
 
Example 16
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 17
Source File: GamlSemanticHighlightingCalculator.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
protected void highlightTasks(final XtextResource resource, final IHighlightedPositionAcceptor acceptor) {
	final List<Task> tasks = taskFinder.findTasks(resource);
	for (final Task task : tasks) {
		acceptor.addPosition(task.getOffset(), task.getTagLength(), TASK_ID);
	}
}
 
Example 18
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);
			}
		}
	}
}
 
Example 19
Source File: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void highlightNumberLiterals(XNumberLiteral literal, IHighlightedPositionAcceptor acceptor) {
	ICompositeNode node = NodeModelUtils.findActualNodeFor(literal);
	ITextRegion textRegion = node.getTextRegion();
	acceptor.addPosition(textRegion.getOffset(), textRegion.getLength(), NUMBER_ID);
}
 
Example 20
Source File: SleighHighlightingCalculator.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void setNodeStyle(IHighlightedPositionAcceptor acceptor, INode n, String styleName) {
	acceptor.addPosition(n.getOffset(), n.getLength(), styleName);
}