com.intellij.psi.tree.TokenSet Java Examples

The following examples show how to use com.intellij.psi.tree.TokenSet. 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: ASTDelegatePsiElement.java    From consulo with 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 #2
Source File: FilteredIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #3
Source File: CompositeElement.java    From consulo with 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 #4
Source File: WordParsing.java    From BashSupport with 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 #5
Source File: LanguageVersionableParserDefinition.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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 #6
Source File: CSharpBuilderWrapper.java    From consulo-csharp with 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 #7
Source File: GLSLTokenTypes.java    From glsl4idea with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static TokenSet merge(TokenSet... sets) {
    TokenSet tokenSet = TokenSet.create();
    for (TokenSet set : sets) {
        tokenSet = TokenSet.orSet(tokenSet, set);
    }
    return tokenSet;
}
 
Example #8
Source File: LightTreeUtil.java    From consulo with 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 #9
Source File: PsiAwareLineWrapPositionStrategy.java    From consulo with 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 #10
Source File: TreeUtil.java    From consulo with 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 #11
Source File: TokenSetBidiRegionsSeparator.java    From consulo with 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 #12
Source File: RmlFindUsagesProvider.java    From reasonml-idea-plugin with 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 #13
Source File: BaseIndentEnterHandler.java    From consulo with 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 #14
Source File: PrattParsingUtil.java    From consulo with 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 #15
Source File: GeneratedParserUtilBase.java    From consulo with 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 #16
Source File: LatteFindUsagesProvider.java    From intellij-latte with 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 #17
Source File: CSharpCfsLanguageVersion.java    From consulo-csharp with 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 #18
Source File: SyntaxHighlighterBase.java    From consulo with 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 #19
Source File: ConceptFoldingBuilder.java    From Intellij-Plugin with 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 #20
Source File: CompositeElement.java    From consulo with 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 #21
Source File: BuildFindUsagesProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
private static TokenSet tokenSet(TokenKind token) {
  return TokenSet.create(BuildToken.fromKind(token));
}
 
Example #22
Source File: LatteMacroLexerAdapter.java    From intellij-latte with MIT License 4 votes vote down vote up
public LatteMacroLexerAdapter() {
	super(
		new FlexAdapter(new LatteMacroLexer((java.io.Reader) null)),
		TokenSet.create(LatteTypes.T_MACRO_CONTENT)
	);
}
 
Example #23
Source File: SpecParserDefinition.java    From Intellij-Plugin with Apache License 2.0 4 votes vote down vote up
@NotNull
public TokenSet getWhitespaceTokens() {
    return WHITE_SPACES;
}
 
Example #24
Source File: WeaveParserDefinition.java    From mule-intellij-plugins with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public TokenSet getWhitespaceTokens() {
    return WHITE_SPACES;
}
 
Example #25
Source File: FusionParserDefinition.java    From intellij-neos with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
@Override
public TokenSet getWhitespaceTokens() {
    return WHITE_SPACES;
}
 
Example #26
Source File: ShaderLabParserDefinition.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public TokenSet getCommentTokens(@Nonnull LanguageVersion languageVersion)
{
	return ShaderLabTokenSets.COMMENTS;
}
 
Example #27
Source File: GraphQLParserDefinition.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
@NotNull
public TokenSet getWhitespaceTokens() {
  return WHITE_SPACES;
}
 
Example #28
Source File: SpacingBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
public RuleBuilder betweenInside(TokenSet leftSet, TokenSet rightSet, IElementType parentType) {
  return new RuleBuilder(new RuleCondition(TokenSet.create(parentType), leftSet, rightSet));
}
 
Example #29
Source File: RTLexer.java    From react-templates-plugin with MIT License 4 votes vote down vote up
public RTLexer() {
    super(new FlexAdapter(new _RTLexer((Reader) null)), TokenSet.create(JSTokenTypes.STRING_LITERAL));
}
 
Example #30
Source File: LombokConfigParserDefinition.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
public TokenSet getCommentTokens() {
  return COMMENTS;
}