com.intellij.psi.tree.IElementType Java Examples

The following examples show how to use com.intellij.psi.tree.IElementType. 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: SharedParsingHelpers.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
protected static void reportErrorUntil(CSharpBuilderWrapper builder, String error, TokenSet originalSet, TokenSet softSet)
{
	while(!builder.eof())
	{
		if(originalSet.contains(builder.getTokenType()))
		{
			break;
		}

		if(builder.getTokenType() == CSharpTokens.IDENTIFIER)
		{
			builder.enableSoftKeywords(softSet);
			IElementType tokenType = builder.getTokenType();
			builder.disableSoftKeywords(softSet);
			if(softSet.contains(tokenType))
			{
				// remap
				builder.remapBackIfSoft();
				break;
			}
		}
		PsiBuilder.Marker mark = builder.mark();
		builder.advanceLexer();
		mark.error(error);
	}
}
 
Example #2
Source File: RedefinedTokenAnnotation.java    From glsl4idea with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void annotate(GLSLRedefinedToken identifier, AnnotationHolder holder) {
    String definition;

    final IElementType identifierType = identifier.getNode().getElementType();
    if(identifierType instanceof GLSLElementTypes.RedefinedTokenElementType){
        definition = ((GLSLElementTypes.RedefinedTokenElementType) identifierType).text;
    }else{
        GLSLMacroReference reference = identifier.getReference();
        GLSLDefineDirective referent = (reference != null) ? reference.resolve() : null;
        definition = (referent != null) ? referent.getBoundText() : null;
    }

    Annotation annotation = holder.createInfoAnnotation(identifier, definition);
    annotation.setTextAttributes(GLSLHighlighter.GLSL_REDEFINED_TOKEN[0]);
}
 
Example #3
Source File: ParagraphFillHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiElement getLastElement(@Nonnull final PsiElement element) {
  final IElementType elementType = element.getNode().getElementType();
  PsiElement nextSibling = element.getNextSibling();
  PsiElement result = element;
  while (nextSibling != null && (nextSibling.getNode().getElementType().equals(elementType) ||
                                 (atWhitespaceToken(nextSibling) &&
                                  StringUtil.countChars(nextSibling.getText(), '\n') <= 1))) {
    String text = nextSibling.getText();
    final String prefix = getPrefix(element);
    final String postfix = getPostfix(element);
    text = StringUtil.trimStart(text.trim(), prefix.trim());
    text = StringUtil.trimEnd(text, postfix);

    if (nextSibling.getNode().getElementType().equals(elementType) &&
        StringUtil.isEmptyOrSpaces(text)) {
      break;
    }
    if (nextSibling.getNode().getElementType().equals(elementType))
      result = nextSibling;
    nextSibling = nextSibling.getNextSibling();
  }
  return result;
}
 
Example #4
Source File: ExpressionParsing.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
private static PsiBuilder.Marker parseExpressionWithTypeInLParRPar(CSharpBuilderWrapper builder, int flags, PsiBuilder.Marker mark, IElementType to)
{
	PsiBuilder.Marker newMarker = mark == null ? builder.mark() : mark.precede();
	builder.advanceLexer();

	if(expect(builder, LPAR, "'(' expected"))
	{
		if(parseType(builder, flags, TokenSet.EMPTY) == null)
		{
			builder.error("Type expected");
		}
		expect(builder, RPAR, "')' expected");
	}
	newMarker.done(to);
	return newMarker;
}
 
Example #5
Source File: SoyLexer.java    From bamboo-soy with Apache License 2.0 6 votes vote down vote up
@Override
public MergeFunction getMergeFunction() {
  return ((final IElementType type, final Lexer originalLexer) -> {
    if (type == SoyTypes.OTHER || type == TokenType.WHITE_SPACE) {
      IElementType returnType =  type;
      while (originalLexer.getTokenType() == SoyTypes.OTHER
          || originalLexer.getTokenType() == TokenType.WHITE_SPACE) {
        if (originalLexer.getTokenType() == SoyTypes.OTHER) {
          returnType = SoyTypes.OTHER;
        }
        originalLexer.advance();
      }
      return returnType;
    }

    return type;
  });
}
 
Example #6
Source File: BlockFactoryTest.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void allParserRulesAreRegistered() throws Exception {
    Map<IElementType, BlockFactory.Factory> registry = BlockFactory.REGISTRY;

    Set<String> allRules = ImmutableSet.copyOf(ProtoParser.ruleNames);

    Set<String> registeredRules = new HashSet<>();
    for (IElementType type : registry.keySet()) {
        registeredRules.add(type.toString());
    }

    Sets.SetView<String> diff = Sets.difference(allRules, registeredRules);
    if (!diff.isEmpty()) {
        Assert.fail("Following rules are not registered: " + diff);
    }

}
 
Example #7
Source File: ANTLRv4GrammarParser.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected ParseTree parse(Parser parser, IElementType root) {
	int startRule;
	if (root instanceof IFileElementType) {
		startRule = ANTLRv4Parser.RULE_grammarSpec;
	}
	else if (root == ANTLRv4TokenTypes.TOKEN_ELEMENT_TYPES.get(ANTLRv4Lexer.TOKEN_REF)
		|| root == ANTLRv4TokenTypes.TOKEN_ELEMENT_TYPES.get(ANTLRv4Lexer.RULE_REF)) {
		startRule = ANTLRv4Parser.RULE_atom;
	}
	else {
		startRule = Token.INVALID_TYPE;
	}

	switch (startRule) {
	case ANTLRv4Parser.RULE_grammarSpec:
		return ((ANTLRv4Parser) parser).grammarSpec();

	case ANTLRv4Parser.RULE_atom:
		return ((ANTLRv4Parser) parser).atom();

	default:
		String ruleName = ANTLRv4Parser.ruleNames[startRule];
		throw new UnsupportedOperationException(String.format("cannot start parsing using root element %s", root));
	}
}
 
Example #8
Source File: ForLoopParsingFunction.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
/**
 * parses an optional arithmetic expression with an expected end token
 *
 * @param builder  The builder which provides the tokens.
 * @param endToken The token which is at the end of the arithmetic expression.
 * @return True if the parsing has been successfull.
 */
private boolean parseArithmeticExpression(BashPsiBuilder builder, IElementType endToken) {
    if (builder.getTokenType() == endToken) {//the expression can be empty
        builder.advanceLexer();
        return true;
    }

    while ((builder.getTokenType() != endToken) && !builder.eof()) {
        if (!ArithmeticFactory.entryPoint().parse(builder)) {
            return false;
        }
    }

    final IElementType foundEndToken = ParserUtil.getTokenAndAdvance(builder);
    return (!builder.eof() && foundEndToken == endToken);
}
 
Example #9
Source File: LightLexerTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public String transform(String testName, String[] data) throws Exception {
  final StringBuilder output = new StringBuilder();
  Lexer lexer = getLexer();
  final String text = data[0].replaceAll("$(\\n+)", "");
  lexer.start(text);
  while (lexer.getTokenType() != null) {
    final int s = lexer.getTokenStart();
    final int e = lexer.getTokenEnd();
    final IElementType tokenType = lexer.getTokenType();
    final String str = tokenType + ": [" + s + ", " + e + "], {" + text.substring(s, e) + "}\n";
    output.append(str);

    lexer.advance();
  }
  return output.toString();
}
 
Example #10
Source File: PlainTextSyntaxHighlighterFactory.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public SyntaxHighlighter getSyntaxHighlighter(final Project project, final VirtualFile virtualFile) {
  return new SyntaxHighlighterBase() {
    @Nonnull
    @Override
    public Lexer getHighlightingLexer() {
      return createPlainTextLexer();
    }

    @Nonnull
    @Override
    public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
      return EMPTY;
    }
  };
}
 
Example #11
Source File: CypherUtil.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
public static <T extends CypherNamedElement> List<T> findAllAndFilter(Project project, IElementType elementType, Filter filter) {
    Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance()
            .getContainingFiles(FileTypeIndex.NAME, CypherFileType.INSTANCE, GlobalSearchScope.allScope(project));

    List<T> result = new ArrayList<>();
    for (VirtualFile virtualFile : virtualFiles) {
        CypherFile cypherFile = (CypherFile) PsiManager.getInstance(project).findFile(virtualFile);
        if (cypherFile != null) {
            List<T> found = findAllAndFilter(cypherFile, elementType, filter);
            result.addAll(found);
        }
    }

    return result;
}
 
Example #12
Source File: BashSpacingProcessorBasic.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether a node is contained in a parameter expansion.
 *
 * @param node
 * @return True if the on of the nodes is embedded in a string
 */
private static boolean hasAncestorNodeType(@Nullable ASTNode node, int levelsUp, @NotNull IElementType parentNodeType) {
    if (node == null) {
        return false;
    }

    if (levelsUp <= 0) {
        return node.getElementType() == parentNodeType;
    }

    return hasAncestorNodeType(node.getTreeParent(), levelsUp - 1, parentNodeType);
}
 
Example #13
Source File: BuckPsiUtils.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Test the specific type of the given identifier element.
 * For it could be a BUCK_RULE_NAME or PROPERTY_LVALUE.
 */
public static boolean testIdentifierType(PsiElement element, IElementType type) {
  if (element.getNode().getElementType() != BuckTypes.IDENTIFIER) {
    return false;
  }
  return element.getParent() != null && element.getParent().getNode().getElementType() == type;
}
 
Example #14
Source File: BashPsiBuilder.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Nullable
public IElementType getRemappingTokenType() {
    try {
        parsingStateData.enterSimpleCommand();

        return getTokenType();
    } finally {
        parsingStateData.leaveSimpleCommand();
    }
}
 
Example #15
Source File: RTJSParser.java    From react-templates-plugin with MIT License 5 votes vote down vote up
public RTJSParser(PsiBuilder builder) {
    super(JavaScriptSupportLoader.JAVASCRIPT_1_5, builder);
    myExpressionParser = new RTExpressionParser();
    myStatementParser = new StatementParser<RTJSParser>(this) {
        @Override
        protected void doParseStatement(boolean canHaveClasses) {
            final IElementType firstToken = builder.getTokenType();
            if (firstToken == JSTokenTypes.LBRACE) {
                parseExpressionStatement();
                checkForSemicolon();
                return;
            }
            if (isIdentifierToken(firstToken)) {
                final IElementType nextToken = builder.lookAhead(1);
                if (nextToken == JSTokenTypes.IN_KEYWORD) {
                    parseInStatement();
                    return;
                }
            }
            if (builder.getTokenType() == JSTokenTypes.LPAR) {
                if (parseInStatement()) {
                    return;
                }
            }
            super.doParseStatement(canHaveClasses);
        }

        private boolean parseInStatement() {
            PsiBuilder.Marker statement = builder.mark();
            if (!getExpressionParser().parseInExpression()) {
                statement.drop();
                return false;
            }
            statement.done(JSElementTypes.EXPRESSION_STATEMENT);
            return true;
        }
    };
}
 
Example #16
Source File: Factory.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static LeafElement createSingleLeafElement(@Nonnull IElementType type, CharSequence buffer, int startOffset, int endOffset, CharTable table, PsiManager manager, PsiFile originalFile) {
  DummyHolder dummyHolder = DummyHolderFactory.createHolder(manager, table, type.getLanguage());
  dummyHolder.setOriginalFile(originalFile);

  FileElement holderElement = dummyHolder.getTreeElement();

  LeafElement newElement = ASTFactory.leaf(type, holderElement.getCharTable().intern(
          buffer, startOffset, endOffset));
  holderElement.rawAddChildren(newElement);
  CodeEditUtil.setNodeGenerated(newElement, true);
  return newElement;
}
 
Example #17
Source File: PairedBraceMatcherAdapter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public BracePair findPair(boolean left, HighlighterIterator iterator, CharSequence fileText, FileType fileType) {
  final IElementType tokenType = iterator.getTokenType();
  final BracePair[] pairs = myMatcher.getPairs();
  for (BracePair pair : pairs) {
    if (tokenType == (left ? pair.getLeftBraceType() : pair.getRightBraceType())) return pair;
  }
  return null;
}
 
Example #18
Source File: BraceMatchingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isPairedBracesAllowedBeforeTypeInFileType(@Nonnull IElementType lbraceType,
                                                                final IElementType tokenType,
                                                                @Nonnull FileType fileType) {
  try {
    return getBraceMatcher(fileType, lbraceType).isPairedBracesAllowedBeforeType(lbraceType, tokenType);
  }
  catch (AbstractMethodError incompatiblePluginThatWeDoNotCare) {
    // Do nothing
  }
  return true;
}
 
Example #19
Source File: AbstractBashSyntaxHighlighterTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
protected void doLexerHighlightingTest(String fileContent, IElementType targetElementType) {
    BashSyntaxHighlighter syntaxHighlighter = new BashSyntaxHighlighter();
    TextAttributesKey[] keys = syntaxHighlighter.getTokenHighlights(targetElementType);
    Assert.assertEquals("Expected one key", 1, keys.length);

    TextAttributesKey attributesKey = keys[0];
    Assert.assertNotNull(attributesKey);

    EditorColorsManager manager = EditorColorsManager.getInstance();
    EditorColorsScheme scheme = (EditorColorsScheme) manager.getGlobalScheme().clone();
    manager.addColorsScheme(scheme);
    EditorColorsManager.getInstance().setGlobalScheme(scheme);

    TextAttributes targetAttributes = new TextAttributes(JBColor.RED, JBColor.BLUE, JBColor.GRAY, EffectType.BOXED, Font.BOLD);
    scheme.setAttributes(attributesKey, targetAttributes);

    myFixture.configureByText(BashFileType.BASH_FILE_TYPE, fileContent);

    TextAttributes actualAttributes = null;
    HighlighterIterator iterator = ((EditorImpl) myFixture.getEditor()).getHighlighter().createIterator(0);
    while (!iterator.atEnd()) {
        if (iterator.getTokenType() == targetElementType) {
            actualAttributes = iterator.getTextAttributes();
            break;
        }

        iterator.advance();
    }

    Assert.assertEquals("Expected text attributes for " + attributesKey, targetAttributes, actualAttributes);
}
 
Example #20
Source File: TemplateDataHighlighterWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public TextAttributesKey[] getTokenHighlights(final IElementType tokenType) {
  if (tokenType == BAD_CHARACTER) {
    return new TextAttributesKey[0];
  }

  return myHighlighter.getTokenHighlights(tokenType);
}
 
Example #21
Source File: DotEnvParser.java    From idea-php-dotenv-plugin with MIT License 5 votes vote down vote up
public void parseLight(IElementType t, PsiBuilder b) {
  boolean r;
  b = adapt_builder_(t, b, this, null);
  Marker m = enter_section_(b, 0, _COLLAPSE_, null);
  r = parse_root_(t, b);
  exit_section_(b, 0, m, t, r, true, TRUE_CONDITION);
}
 
Example #22
Source File: MakefileFindUsagesProvider.java    From CppTools with Apache License 2.0 5 votes vote down vote up
@NotNull
public String getType(@NotNull PsiElement psiElement) {
  IElementType iElementType = MakefileIdentifierReference.type(psiElement);
  if(iElementType == MakefileTokenTypes.VAR_DEFINITION) return "definition";
  if(iElementType == MakefileTokenTypes.TARGET_IDENTIFIER) return "target";
  return "should not happen type";
}
 
Example #23
Source File: CSharpQuoteHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
public static boolean isAppropriateElementTypeForLiteralStatic(final IElementType tokenType)
{
	return CSharpTokenSets.COMMENTS.contains(tokenType) ||
			tokenType == CSharpTokens.WHITE_SPACE ||
			tokenType == CSharpTokens.SEMICOLON ||
			tokenType == CSharpTokens.COMMA ||
			tokenType == CSharpTokens.RPAR ||
			tokenType == CSharpTokens.RBRACKET ||
			tokenType == CSharpTokens.RBRACE ||
			tokenType == CSharpTokens.STRING_LITERAL ||
			tokenType == CSharpTokens.CHARACTER_LITERAL;
}
 
Example #24
Source File: BashFoldingBuilder.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
private static boolean isFoldable(ASTNode node) {
    IElementType type = node.getElementType();

    if (type == HEREDOC_CONTENT_ELEMENT) {
        // only the first heredoc element of a single heredoc is foldable,
        // the range expansion expands it until the last content element
        ASTNode prev = node.getTreePrev();
        if (prev != null && prev.getElementType() == BashTokenTypes.LINE_FEED) {
            //first heredoc content element, better PSI would be an improvement here
            return true;
        }
    }

    return foldableTokens.contains(type);
}
 
Example #25
Source File: IdeaUtils.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public boolean isWhiteSpace(PsiElement element) {
    IElementType type = element.getNode().getElementType();
    if (type == TokenType.WHITE_SPACE) {
        return true;
    }
    return false;
}
 
Example #26
Source File: ConditionalCommandParsingFunction.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * From http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs :
 * <br>
 * <br>
 * <code>( expression )</code>
 * Returns the value of expression. This may be used to override the normal precedence of operators.
 * <br>
 * <code>! expression</code>
 * True if expression is false.
 * <br>
 * <code>expression1 && expression2</code>
 * True if both expression1 and expression2 are true.
 * <br>
 * <code>expression1 || expression2</code>
 * True if either expression1 or expression2 is true.
 * <br>
 * The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.
 * <br>
 * An expression is a normal test expression as used in the conditional expression parsing function.
 *
 * @param builder The provider of the tokens.
 * @return True if the parsing was successful
 */
@Override
public boolean parse(BashPsiBuilder builder) {
    IElementType token = builder.getTokenType();
    log.assertTrue(token == BRACKET_KEYWORD);

    PsiBuilder.Marker startMarker = builder.mark();
    builder.advanceLexer();

    boolean ok;
    if (builder.getTokenType() == _BRACKET_KEYWORD) {
        builder.error("Empty expression is not allowed");
        ok = false;
    } else {
        ok = parseExpression(builder);
    }

    ok &= (builder.getTokenType() == _BRACKET_KEYWORD);

    if (ok) {
        builder.advanceLexer();
        startMarker.done(BashElementTypes.EXTENDED_CONDITIONAL_COMMAND);
        return true;
    }

    startMarker.drop();
    return false;
}
 
Example #27
Source File: FusionFoldingBuilder.java    From intellij-neos with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param node Block node
 * @return The placeholder text for the given node, if it is foldable.
 */
public String getPlaceholderText(@NotNull ASTNode node) {
    IElementType elementType = node.getElementType();
    if (!isFoldable(elementType)) {
        return null;
    }
    String placeholderText = "";
    if (elementType == FusionTypes.BLOCK) {
        placeholderText = "{...}";
    } else if (elementType == FusionTypes.DOC_COMMENT) {
        placeholderText = "/*...*/";
    }
    return placeholderText;
}
 
Example #28
Source File: MockPsiBuilder.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public void doneBefore(@NotNull IElementType elementType, @NotNull Marker marker, String message) {
    if (closed) {
        throw new IllegalStateException("This marker is already closed (either dropped or done)");
    }

    if (!(marker instanceof MockMarker)) {
        throw new IllegalStateException("The stopMarker must be a MockMarker");
    }

    closed = true;

    int index = MockPsiBuilder.this.markers.indexOf(this);
    int indexStopMarker = MockPsiBuilder.this.markers.indexOf(marker);

    if (indexStopMarker == -1) {
        throw new IllegalStateException("stopMarker wasn't found");
    }
    if (index == -1) {
        throw new IllegalStateException("marker wasn't found");
    }
    if (index > indexStopMarker) {
        throw new IllegalStateException(String.format("The stopMarker must have been created after the current marker: index %d, stop marker index %s", index, indexStopMarker));
    }

    MockPsiBuilder.this.markers.remove(index);

    doneMarkers.add(Pair.create(this, elementType));
}
 
Example #29
Source File: OclParser.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
private void transitionToLetNamed(@NotNull PsiBuilder builder, @NotNull ParserState state) {
    state.updateCurrentResolution(letNamed).complete().
            wrapWith(m_types.C_LOWER_SYMBOL);
    IElementType tokenType = builder.getTokenType();
    if (tokenType != m_types.EQ && tokenType != m_types.COLON) {
        state.add(mark(builder, letBinding, letNamedBinding, m_types.C_LET_BINDING).complete()).add(mark(builder, function, m_types.C_FUN_EXPR).complete())
                .add(mark(builder, function, functionParameters, m_types.C_FUN_PARAMS).complete());
    }
}
 
Example #30
Source File: RTParserDefinition.java    From react-templates-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public PsiElement createElement(ASTNode node) {
    final IElementType type = node.getElementType();
    if (type.equals(RTElementTypes.REPEAT_EXPRESSION)) {
        return new RTRepeatExpression(node);
    }
    if (type.equals(RTElementTypes.FILTER_EXPRESSION)) {
        return new RTFilterExpression(node);
    }
    if (type.equals(RTElementTypes.AS_EXPRESSION)) {
        return new RTAsExpression(node);
    }
    return super.createElement(node);
}