Java Code Examples for org.eclipse.xtext.nodemodel.ILeafNode#getText()

The following examples show how to use org.eclipse.xtext.nodemodel.ILeafNode#getText() . 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: SingleLineCommentDocumentationProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public List<INode> getDocumentationNodes(final EObject object) {
  ICompositeNode node = NodeModelUtils.getNode(object);
  if (node == null) {
    return Collections.emptyList();
  }

  // get all single line comments before a non hidden leaf node
  List<INode> result = Lists.newArrayList();
  for (ILeafNode leaf : node.getLeafNodes()) {
    if (!leaf.isHidden()) {
      break;
    }
    EObject grammarElement = leaf.getGrammarElement();
    if (grammarElement instanceof AbstractRule && ruleName.equals(((AbstractRule) grammarElement).getName())) {
      String comment = leaf.getText();
      if (getCommentPattern().matcher(comment).matches() && !comment.matches(ignore)) {
        result.add(leaf);
      }
    }
  }

  return result;
}
 
Example 2
Source File: MultiLineCommentDocumentationProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the nearest multi line comment node that precedes the given object. 
 * @since 2.3
 * @return a list with exactly one node or an empty list if the object is undocumented.
 */
/* @NonNull */
@Override
public List<INode> getDocumentationNodes(/* @NonNull */ EObject object) {
	ICompositeNode node = NodeModelUtils.getNode(object);
	List<INode> result = Collections.emptyList();
	if (node != null) {
		// get the last multi line comment before a non hidden leaf node
		for (ILeafNode leafNode : node.getLeafNodes()) {
			if (!leafNode.isHidden())
				break;
			if (leafNode.getGrammarElement() instanceof TerminalRule
					&& ruleName.equalsIgnoreCase(((TerminalRule) leafNode.getGrammarElement()).getName())) {
				String comment = leafNode.getText();
				if (commentStartTagRegex.matcher(comment).matches()) {
					result = Collections.<INode>singletonList(leafNode);
				}
			}
		}
	}
	return result;
}
 
Example 3
Source File: InsertionOffsets.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected int inEmpty(final XtendTypeDeclaration ownerType) {
  int _xblockexpression = (int) 0;
  {
    final ICompositeNode classNode = NodeModelUtils.findActualNodeFor(ownerType);
    final Function1<ILeafNode, Boolean> _function = (ILeafNode it) -> {
      String _text = it.getText();
      return Boolean.valueOf(Objects.equal(_text, "{"));
    };
    final ILeafNode openingBraceNode = IterableExtensions.<ILeafNode>findFirst(classNode.getLeafNodes(), _function);
    int _xifexpression = (int) 0;
    if ((openingBraceNode != null)) {
      int _offset = openingBraceNode.getOffset();
      _xifexpression = (_offset + 1);
    } else {
      _xifexpression = classNode.getEndOffset();
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example 4
Source File: ScopeProviderAccess.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private QualifiedName getErrorName(INode errorNode) {
	List<String> segments = Lists.newArrayListWithCapacity(4);
	for(ILeafNode leaf: errorNode.getLeafNodes()) {
		if (!leaf.isHidden()) {
			String text = leaf.getText();
			// XParenthesizedExpression
			if (text.equals("(") || text.equals(")")) {
				continue;
			}
			if (!text.equals(".") && !text.equals("::")) {
				if (text.charAt(0) == '^')
					segments.add(text.substring(1));
				else
					segments.add(text);
			}
		}
	}
	return QualifiedName.create(segments);
}
 
Example 5
Source File: DotAutoEditStrategy.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
private HtmlTag findOpenTag(XtextResource resource,
		DocumentCommand command) {
	if (!resource.getContents().isEmpty()) {
		EObject dotAst = resource.getContents().get(0);
		INode rootNode = NodeModelUtils.getNode(dotAst);
		int cursorPosition = command.offset;
		ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(rootNode,
				cursorPosition);

		String leafNodeText = leafNode.getText();

		String htmlLabelText = extractHtmlLabelContent(leafNodeText);

		if (htmlLabelText == null) {
			return null;
		}

		int htmlLabelStartOffset = leafNode.getOffset() + 1
				+ leafNodeText.substring(1).indexOf('<');
		int htmlLabelCursorPosition = cursorPosition - htmlLabelStartOffset;

		return findOpenTag(htmlLabelText, htmlLabelCursorPosition);

	}
	return null;
}
 
Example 6
Source File: XtextLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private String getLabel(RuleCall ruleCall) {
	if (ruleCall.getRule() != null) {
		return ruleCall.getRule().getName();
	}
	ICompositeNode node = NodeModelUtils.getNode(ruleCall);
	String ruleName = UNKNOWN;
	if (node != null) {
		for (ILeafNode leaf : node.getLeafNodes()) {
			if (!leaf.isHidden()) {
				ruleName = leaf.getText();
				break;
			}
		}
	}
	return ruleName;
}
 
Example 7
Source File: XtextLabelProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private String getLiteralName(EnumLiteralDeclaration declaration) {
	if (declaration.getEnumLiteral() != null) {
		return declaration.getEnumLiteral().getName();
	}
	ICompositeNode node = NodeModelUtils.getNode(declaration);
	String literalName = UNKNOWN;
	if (node != null) {
		for (ILeafNode leaf : node.getLeafNodes()) {
			if (!leaf.isHidden()) {
				literalName = leaf.getText();
				break;
			}
		}
	}
	return literalName;
}
 
Example 8
Source File: ContentAssistTokenTypeMapper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Converts a leaf node an Antlr token type (int).
 */
public int getInternalTokenType(ILeafNode leafNode) {
	EObject grammarElement = leafNode.getGrammarElement();
	if (grammarElement != null) {
		return getInternalTokenType(grammarElement);
	}
	String text = leafNode.getText();
	Integer type = tokenTypes.get("'" + text + "'");
	if (type != null) {
		return type;
	}
	throw new IllegalArgumentException(text);
}
 
Example 9
Source File: RenameService2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String getConvertedValue(EObject grammarElement, ILeafNode leaf) {
	try {
		if (grammarElement instanceof RuleCall) {
			return valueConverterService
					.toValue(leaf.getText(), ((RuleCall) grammarElement).getRule().getName(), leaf).toString();
		} else if (grammarElement instanceof CrossReference) {
			return linkingHelper.getCrossRefNodeAsString(leaf, true);
		}
	} catch (Exception e) {
		// The current name text cannot be converted to a value.
		// Rename could be used to fix this.
	}
	return leaf.getText();
}
 
Example 10
Source File: NodeModelBasedRegionAccessBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isComment(ILeafNode leaf) {
	String text = leaf.getText();
	for (int i = 0; i < text.length(); i++)
		if (!Character.isWhitespace(text.charAt(i)))
			return true;
	return false;
}
 
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: FeatureCallAsTypeLiteralHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected List<String> getTypeNameSegmentsFromConcreteSyntax(List<INode> nodes, boolean staticNotation) {
	List<String> result = null;
	for(INode node: nodes) {
		for(ILeafNode leaf: node.getLeafNodes()) {
			if (!leaf.isHidden()) {
				String text = leaf.getText();
				// XParenthesizedExpression
				if (text.equals("(") || text.equals(")")) {
					return null;
				}
				if (text.equals(".")) {
					if (staticNotation) {
						return null;
					}
				}
				if (text.equals("::")) {
					if (!staticNotation) {
						return null;
					}
				}
				if (!text.equals(".") && !text.equals("::")) {
					if (result == null) {
						result = Lists.newArrayListWithCapacity(4);
					}
					if (text.charAt(0) == '^') {
						result.add(text.substring(1));
					} else {
						result.add(text);
					}
				}
			}
		}
	}
	return result;
}
 
Example 13
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Produce an Antlr token for the prefix of the given leaf that overlaps the requested region
 *
 * @see #endOffset
 */
private Token toPrefixToken(ILeafNode leaf) {
	Lexer lexer = new InternalN4JSLexer();
	String text = leaf.getText();
	String prefix = text.substring(0, endOffset - leaf.getTotalOffset());
	ANTLRStringStream stream = new ANTLRStringStream(prefix);
	lexer.setCharStream(stream);
	Token nextToken = lexer.nextToken();
	// copy to get rid of the reference to the stream again
	return new CommonToken(nextToken.getType(), nextToken.getText());
}
 
Example 14
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Skips the given leaf as it's hidden. If it was the last token to be returned, a hidden token may be syntesized if
 * would affect the semicolon insertion.
 */
private Token processHiddenToken(ILeafNode leaf) {
	Token result = nextToken();
	if (result == Token.EOF_TOKEN && Strings.countLineBreaks(leaf.getText()) > 0) {
		next = result;
		CommonToken hidden = new CommonToken(tokenTypeMapper.getInternalTokenType(leaf), leaf.getText());
		hidden.setChannel(Token.HIDDEN_CHANNEL);
		return hidden;
	}
	return result;
}
 
Example 15
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Implementation of the {@link TokenSource} interface. Return new tokens as long as there are some, afterwards
 * return {@link Token#EOF_TOKEN}.
 */
@Override
public Token nextToken() {
	if (next != null) {
		Token result = next;
		next = null;
		return result;
	}
	if (!leafNodes.hasNext()) {
		return Token.EOF_TOKEN;
	}
	ILeafNode leaf = leafNodes.next();
	if (leaf.getTotalOffset() >= endOffset) {
		leafNodes = Collections.emptyIterator();
		return Token.EOF_TOKEN;
	}
	if (leaf.getTotalEndOffset() <= startOffset) {
		return nextToken();
	}
	if (leaf.getTotalEndOffset() > endOffset) {
		return toPrefixToken(leaf);
	}
	SyntaxErrorMessage syntaxErrorMessage = leaf.getSyntaxErrorMessage();
	if (syntaxErrorMessage != null && SEMICOLON_INSERTED.equals(syntaxErrorMessage.getIssueCode())) {
		return toASIToken(leaf);
	}
	if (leaf.isHidden()) {
		return processHiddenToken(leaf);
	}
	int tokenType = tokenTypeMapper.getInternalTokenType(leaf);
	return new CommonToken(tokenType, leaf.getText());
}
 
Example 16
Source File: ASTGraphProvider.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private String getDocumentation(/* @NonNull */EObject object) {
	if (object.eContainer() == null) {
		// if a comment is at the beginning of the file it will be returned for
		// the root element (e.g. Script in N4JS) as well -> avoid this!
		return null;
	}

	ICompositeNode node = NodeModelUtils.getNode(object);
	if (node != null) {
		// get the last multi line comment before a non hidden leaf node
		for (ILeafNode leafNode : node.getLeafNodes()) {
			if (!leafNode.isHidden())
				break;

			EObject grammarElem = leafNode.getGrammarElement();
			if (grammarElem instanceof TerminalRule
					&& "ML_COMMENT".equalsIgnoreCase(((TerminalRule) grammarElem).getName())) {

				String comment = leafNode.getText();
				if (commentStartTagRegex.matcher(comment).matches()) {
					return leafNode.getText();
				}
			}
		}
	}
	return null;
}
 
Example 17
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Creates a new semicolon token with the text of the given leaf. It is put on the default channel.
 */
private Token newSemicolonToken(ILeafNode leaf) {
	int tokenType = tokenTypeMapper.getInternalTokenType(semicolon);
	return new CommonToken(tokenType, leaf.getText());
}
 
Example 18
Source File: MultiLineJavaDocTypeReferenceProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Computes regions between a given string to search and different ends searched by their precedence
 *
 * @param regions - List to put new regions in
 * @param leafNodes - nodes to search in
 * @param toSearch - String to search
 * @param ends - ends in decreasing precedence
 * @since 2.6
 */
protected void computeRegions(List<ReplaceRegion> regions, Iterable<ILeafNode> leafNodes, String toSearch, String... ends) {
	for (ILeafNode leafNode : leafNodes) {
		String text = leafNode.getText();
		int offset = leafNode.getOffset();
		int position = text.indexOf(toSearch);
		int textLength = text.length();
		while (position != -1) {
			int beginIndex = position + toSearch.length();
			// Skip leading whitespaces
			if(Character.isWhitespace(text.charAt(beginIndex))){
				while(beginIndex < textLength && Character.isWhitespace(text.charAt(beginIndex))){
					beginIndex ++;
				}
			}
			int endIndex = -1;
			for (int i = ends.length -1; i >= 0; i--) {
				String end = ends[i];
				int endCandidate = text.indexOf(end, beginIndex);
				if (endCandidate != -1)  {
					if (endIndex == -1) {
						endIndex = endCandidate;
					} else {
						if (endIndex > endCandidate) {
							endIndex = endCandidate;
						}
					}
				}
			}
			if (endIndex == -1) { 
				break;
			} else {
				String simpleName = text.substring(beginIndex, endIndex).replaceAll(" ", "");
				if(simpleName.length() > 0 && simpleName.matches("[0-9a-zA-Z\\.\\$_]*")){
					ReplaceRegion region = new ReplaceRegion(offset + beginIndex, simpleName.length(), simpleName);
					regions.add(region);
				}
			} 
			position = text.indexOf(toSearch, endIndex);
		}
	}
}
 
Example 19
Source File: SingleLineCommentDocumentationProvider.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Add a comment for a given node (if applicable).
 *
 * @param builder
 *          add comments to this
 * @param node
 *          the node read comments from
 * @param contextEndLine
 *          the end line (number) of the root object, used for line number comparison
 */
private void addComment(final StringBuilder builder, final ILeafNode node, final int contextEndLine) {
  if (node.getGrammarElement() instanceof TerminalRule && ruleName.equals(((TerminalRule) node.getGrammarElement()).getName())
      && node.getStartLine() == contextEndLine) {
    final String comment = node.getText();
    if (getCommentPattern().matcher(comment).matches()) {
      builder.append(comment);
    }
  }
}