Java Code Examples for com.intellij.psi.tree.TokenSet

The following examples show how to use com.intellij.psi.tree.TokenSet. These examples are extracted from open source projects. 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 Project: consulo   Source File: CompositeElement.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public <T extends PsiElement> T[] getChildrenAsPsiElements(@Nullable TokenSet filter, @Nonnull ArrayFactory<? extends T> constructor) {
  assertReadAccessAllowed();
  int count = countChildren(filter);
  T[] result = constructor.create(count);
  if (count == 0) {
    return result;
  }
  int idx = 0;
  for (ASTNode child = getFirstChildNode(); child != null && idx < count; child = child.getTreeNext()) {
    if (filter == null || filter.contains(child.getElementType())) {
      @SuppressWarnings("unchecked") T element = (T)child.getPsi();
      LOG.assertTrue(element != null, child);
      result[idx++] = element;
    }
  }
  return result;
}
 
Example 2
Source Project: BashSupport   Source File: WordParsing.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Looks ahead if the current token is the start of a static double-quoted string value.
 *
 * @param builder         the current bulder
 * @param allowWhitespace if whitespace content is allowed
 * @return true if this is a quoted string which only consists of quotes and static string content
 */
public boolean isSimpleComposedString(BashPsiBuilder builder, boolean allowWhitespace) {
    if (builder.getTokenType() != STRING_BEGIN) {
        return false;
    }

    TokenSet accepted = TokenSet.create(STRING_CONTENT);
    if (allowWhitespace) {
        accepted = TokenSet.orSet(accepted, TokenSet.create(WHITESPACE));
    }

    int lookahead = 1;
    while (accepted.contains(builder.rawLookup(lookahead))) {
        lookahead++;
    }

    if (builder.rawLookup(lookahead) != STRING_END) {
        return false;
    }

    IElementType end = builder.rawLookup(lookahead + 1);
    return end == null || end == WHITESPACE || end == LINE_FEED;
}
 
Example 3
private boolean isComment(int offset) {
  final HighlighterIterator it = myEditor.getHighlighter().createIterator(offset);
  IElementType tokenType = it.getTokenType();
  Language language = tokenType.getLanguage();
  TokenSet comments = myComments.get(language);
  if (comments == null) {
    ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
    if (definition != null) {
      comments = definition.getCommentTokens();
    }
    if (comments == null) {
      return false;
    }
    else {
      myComments.put(language, comments);
    }
  }
  return comments.contains(tokenType);
}
 
Example 4
Source Project: consulo   Source File: ASTDelegatePsiElement.java    License: Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Nonnull
protected <T extends PsiElement> List<T> findChildrenByType(TokenSet elementType) {
  List<T> result = EMPTY;
  ASTNode child = getNode().getFirstChildNode();
  while (child != null) {
    final IElementType tt = child.getElementType();
    if (elementType.contains(tt)) {
      if (result == EMPTY) {
        result = new ArrayList<T>();
      }
      result.add((T)child.getPsi());
    }
    child = child.getTreeNext();
  }
  return result;
}
 
Example 5
Source Project: Intellij-Plugin   Source File: ConceptFoldingBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode astNode, @NotNull Document document) {
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    for (ASTNode node : astNode.getChildren(TokenSet.create(ConceptTokenTypes.CONCEPT)))
        addNode(descriptors, node, node.findChildByType(ConceptTokenTypes.CONCEPT_HEADING));
    return descriptors.toArray(new FoldingDescriptor[0]);
}
 
Example 6
Source Project: consulo   Source File: SyntaxHighlighterBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tries to update the map by associating given keys with a given value.
 * Throws error if the map already contains different mapping for one of given keys.
 */
protected static void safeMap(
  @Nonnull final Map<IElementType, TextAttributesKey> map,
  @Nonnull final TokenSet keys,
  @Nonnull final TextAttributesKey value)
{
  for (final IElementType type : keys.getTypes()) {
    safeMap(map, type, value);
  }
}
 
Example 7
Source Project: consulo   Source File: GeneratedParserUtilBase.java    License: Apache License 2.0 5 votes vote down vote up
public static void initState(ErrorState state, PsiBuilder builder, IElementType root, TokenSet[] extendsSets) {
  state.extendsSets = extendsSets;
  PsiFile file = builder.getUserData(FileContextUtil.CONTAINING_FILE_KEY);
  state.completionState = file == null? null: file.getUserData(COMPLETION_STATE_KEY);
  Language language = file == null? root.getLanguage() : file.getLanguage();
  state.caseSensitive = language.isCaseSensitive();
  PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(language);
  state.braces = matcher == null ? null : matcher.getPairs();
  if (state.braces != null && state.braces.length == 0) state.braces = null;
}
 
Example 8
@Nonnull
@Override
public TokenSet getWhitespaceTokens(@Nonnull LanguageVersion languageVersion) {
  if(languageVersion instanceof LanguageVersionWithParsing) {
    return ((LanguageVersionWithParsing)languageVersion).getWhitespaceTokens();
  }
  throw new IllegalArgumentException("'getWhitespaceTokens' need override for language version '" + languageVersion + "'");
}
 
Example 9
Source Project: consulo   Source File: BaseIndentEnterHandler.java    License: Apache License 2.0 5 votes vote down vote up
public BaseIndentEnterHandler(
        final Language language,
        final TokenSet indentTokens,
        final IElementType lineCommentType,
        final String lineCommentPrefix,
        final TokenSet whitespaceTokens,
        final boolean worksWithFormatter)
{
  myLanguage = language;
  myIndentTokens = indentTokens;
  myLineCommentType = lineCommentType;
  myLineCommentPrefix = lineCommentPrefix;
  myWhitespaceTokens = whitespaceTokens;
  myWorksWithFormatter = worksWithFormatter;
}
 
Example 10
Source Project: consulo   Source File: TokenSetBidiRegionsSeparator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean createBorderBetweenTokens(@Nonnull IElementType previousTokenType, @Nonnull IElementType tokenType) {
  for (TokenSet set : myTokenSets) {
    if (set.contains(previousTokenType) && set.contains(tokenType)) {
      return false;
    }
  }
  return true;
}
 
Example 11
Source Project: reasonml-idea-plugin   Source File: RmlFindUsagesProvider.java    License: MIT License 5 votes vote down vote up
@Nullable
@Override
public WordsScanner getWordsScanner() {
    RmlTypes types = RmlTypes.INSTANCE;
    return new DefaultWordsScanner(new RmlLexer(), TokenSet.create(types.C_UPPER_SYMBOL, types.C_LOWER_SYMBOL, types.C_VARIANT), TokenSet.EMPTY,
                                   TokenSet.EMPTY);
}
 
Example 12
Source Project: consulo   Source File: PsiAwareLineWrapPositionStrategy.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates new <code>PsiAwareLineWrapPositionStrategy</code> object.
 * 
 * @param nonVirtualOnly  defines if current PSI-aware logic should be exploited only for 'real wrap' position requests
 * @param enabledTypes    target element/token types where line wrapping is allowed
 */
public PsiAwareLineWrapPositionStrategy(boolean nonVirtualOnly, @Nonnull IElementType ... enabledTypes) {
  myEnabledTypes = TokenSet.create(enabledTypes);
  myNonVirtualOnly = nonVirtualOnly;
  if (enabledTypes.length <= 0) {
    LOG.warn(String.format("%s instance is created with empty token/element types. That will lead to inability to perform line wrap",
                           getClass().getName()));
  }
}
 
Example 13
Source Project: consulo   Source File: TreeUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static ASTNode findParent(ASTNode element, TokenSet types) {
  for (ASTNode parent = element.getTreeParent(); parent != null; parent = parent.getTreeParent()) {
    if (types.contains(parent.getElementType())) return parent;
  }
  return null;
}
 
Example 14
Source Project: consulo-csharp   Source File: CSharpCfsLanguageVersion.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public Lexer createInnerLexer()
{
	if(myExpressionElementType == null)
	{
		myExpressionElementType = createExpressionElementType();
	}
	return new MergingLexerAdapter(new CSharpInterpolationStringLexer(myExpressionElementType), TokenSet.create(myExpressionElementType, CfsTokens.TEXT, CfsTokens.FORMAT));
}
 
Example 15
Source Project: intellij-latte   Source File: LatteFindUsagesProvider.java    License: MIT License 5 votes vote down vote up
@Nullable
@Override
public WordsScanner getWordsScanner() {
    return new DefaultWordsScanner(
            new LatteMacroLexerAdapter(),
            TokenSet.create(LatteTypes.PHP_VARIABLE),
            TokenSet.create(LatteTypes.MACRO_COMMENT),
            TokenSet.EMPTY
    );
}
 
Example 16
public static TokenSet merge(TokenSet... sets) {
    TokenSet tokenSet = TokenSet.create();
    for (TokenSet set : sets) {
        tokenSet = TokenSet.orSet(tokenSet, set);
    }
    return tokenSet;
}
 
Example 17
Source Project: consulo   Source File: CompositeElement.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public ASTNode findChildByType(@Nonnull TokenSet types) {
  if (DebugUtil.CHECK_INSIDE_ATOMIC_ACTION_ENABLED) {
    assertReadAccessAllowed();
  }
  for (ASTNode element = getFirstChildNode(); element != null; element = element.getTreeNext()) {
    if (types.contains(element.getElementType())) return element;
  }
  return null;
}
 
Example 18
Source Project: consulo-csharp   Source File: CSharpBuilderWrapper.java    License: Apache License 2.0 5 votes vote down vote up
public boolean enableSoftKeyword(@Nonnull IElementType elementType)
{
	if(mySoftSet.contains(elementType))
	{
		return false;
	}
	mySoftSet = TokenSet.orSet(mySoftSet, TokenSet.create(elementType));
	return true;
}
 
Example 19
Source Project: consulo   Source File: LightTreeUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static LighterASTNode getParentOfType(@Nonnull LighterAST tree, @Nullable LighterASTNode node,
                                             @Nonnull TokenSet types, @Nonnull TokenSet stopAt) {
  if (node == null) return null;
  node = tree.getParent(node);
  while (node != null) {
    final IElementType type = node.getTokenType();
    if (types.contains(type)) return node;
    if (stopAt.contains(type)) return null;
    node = tree.getParent(node);
  }
  return null;
}
 
Example 20
Source Project: consulo   Source File: PrattParsingUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean searchFor(final PrattBuilder builder, final boolean consume, final PrattTokenType... types) {
  final TokenSet set = TokenSet.create(types);
  if (!set.contains(builder.getTokenType())) {
    builder.assertToken(types[0]);
    while (!set.contains(builder.getTokenType()) && !builder.isEof()) {
      builder.advance();
    }
  }
  if (consume) {
    builder.advance();
  }
  return !builder.isEof();
}
 
Example 21
Source Project: consulo-csharp   Source File: SharedParsingHelpers.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
protected static <T> Pair<PsiBuilder.Marker, T> parseWithSoftElements(Function<CSharpBuilderWrapper, Pair<PsiBuilder.Marker, T>> func, CSharpBuilderWrapper builderWrapper, IElementType... softs)
{
	return parseWithSoftElements(func, builderWrapper, TokenSet.create(softs));
}
 
Example 22
Source Project: consulo   Source File: IdTableBuilding.java    License: Apache License 2.0 4 votes vote down vote up
private static WordsScanner createWordScanner(final CustomSyntaxTableFileType customSyntaxTableFileType) {
  return new DefaultWordsScanner(new CustomFileTypeLexer(customSyntaxTableFileType.getSyntaxTable(), true), TokenSet.create(CustomHighlighterTokenType.IDENTIFIER),
                                 TokenSet.create(CustomHighlighterTokenType.LINE_COMMENT, CustomHighlighterTokenType.MULTI_LINE_COMMENT),
                                 TokenSet.create(CustomHighlighterTokenType.STRING, CustomHighlighterTokenType.SINGLE_QUOTED_STRING));

}
 
Example 23
@Nonnull
@Override
public TokenSet getWhitespaceTokens(@Nonnull LanguageVersion languageVersion)
{
	return TokenSet.create(CSharpTokens.WHITE_SPACE);
}
 
Example 24
@Nullable
private static PsiBuilder.Marker parseBinary(final PsiBuilder builder, final ExprType type, final TokenSet ops)
{
	PsiBuilder.Marker result = parseExpression(builder, type);
	if(result == null)
	{
		return null;
	}
	int operandCount = 1;

	IElementType tokenType = builder.getTokenType();
	IElementType currentExprTokenType = tokenType;
	while(true)
	{
		if(tokenType == null || !ops.contains(tokenType))
		{
			break;
		}

		builder.advanceLexer();

		final PsiBuilder.Marker right = parseExpression(builder, type);
		operandCount++;
		tokenType = builder.getTokenType();
		if(tokenType == null || !ops.contains(tokenType) || tokenType != currentExprTokenType || right == null)
		{
			// save
			result = result.precede();
			if(right == null)
			{
				builder.error("Expression expected");
			}
			result.done(operandCount > 2 ? POLYADIC_EXPRESSION : BINARY_EXPRESSION);
			if(right == null)
			{
				break;
			}
			currentExprTokenType = tokenType;
			operandCount = 1;
		}
	}

	return result;
}
 
Example 25
Source Project: consulo   Source File: LazyParseablePsiElement.java    License: Apache License 2.0 4 votes vote down vote up
@Override
@Nonnull
public PsiElement[] getChildren() {
  return getChildrenAsPsiElements((TokenSet)null, PsiElement.ARRAY_FACTORY);
}
 
Example 26
@NotNull
public TokenSet getCommentTokens() {
	return COMMENTS;
}
 
Example 27
Source Project: reasonml-idea-plugin   Source File: OclMlgParserDefinition.java    License: MIT License 4 votes vote down vote up
@NotNull
public TokenSet getStringLiteralElements() {
    return STRINGS;
}
 
Example 28
Source Project: reasonml-idea-plugin   Source File: OclP4ParserDefinition.java    License: MIT License 4 votes vote down vote up
@NotNull
public TokenSet getCommentTokens() {
    return COMMENTS;
}
 
Example 29
Source Project: intellij-latte   Source File: LatteParserDefinition.java    License: MIT License 4 votes vote down vote up
@NotNull
@Override
public TokenSet getCommentTokens() {
	return COMMENTS;
}
 
Example 30
Source Project: consulo-csharp   Source File: SharedParsingHelpers.java    License: Apache License 2.0 4 votes vote down vote up
@Nullable
public static TypeInfo parseType(@Nonnull CSharpBuilderWrapper builder)
{
	return parseType(builder, NONE, TokenSet.EMPTY);
}