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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: LazyParseableElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
public LazyParseableElement(@Nonnull IElementType type, @Nullable CharSequence text) {
  super(type);
  synchronized (lock) {
    if (text == null) {
      myParsed = true;
      myText = NO_TEXT;
    }
    else {
      myText = new StaticGetter<>(ImmutableCharSequence.asImmutable(text));
      setCachedLength(text.length());
    }
  }
}
 
Example #14
Source File: FormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean hasPrecedingSiblingOfType(@Nullable ASTNode node, IElementType expectedSiblingType, IElementType... skipTypes) {
  for (ASTNode prevNode = node == null ? null : node.getTreePrev(); prevNode != null; prevNode = prevNode.getTreePrev()) {
    if (isWhitespaceOrEmpty(prevNode) || isOneOf(prevNode, skipTypes)) continue;
    if (prevNode.getElementType() == expectedSiblingType) return true;
  }
  return false;
}
 
Example #15
Source File: IncludeVariableCollector.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
private void collectContextVars(IElementType iElementType, @NotNull PsiElement element, @NotNull PsiElement includeTag) {
    String templateName = includeTag.getText();

    if(StringUtils.isNotBlank(templateName)) {
        for(PsiFile templateFile: TwigUtil.getTemplatePsiElements(element.getProject(), templateName)) {
            if(templateFile.equals(psiFile)) {
                collectIncludeContextVars(iElementType, element, includeTag, variables, parameter.getVisitedFiles());
            }
        }
    }
}
 
Example #16
Source File: BuildParserDefinition.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static TokenSet convert(TokenKind... blazeTokens) {
  return TokenSet.create(
      Lists.newArrayList(blazeTokens)
          .stream()
          .map(BuildToken::fromKind)
          .toArray(IElementType[]::new));
}
 
Example #17
Source File: ProjectViewParserDefinition.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement createElement(ASTNode node) {
  IElementType type = node.getElementType();
  if (type instanceof ProjectViewElementType) {
    return ((ProjectViewElementType) type).createElement(node);
  }
  return new ASTWrapperPsiElement(node);
}
 
Example #18
Source File: KeywordCompletionContributor.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
KeywordCompletionContributor(ORTypes types) {
    extend(CompletionType.BASIC, psiElement(), new CompletionProvider<CompletionParameters>() {
        @Override
        protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
            PsiElement position = parameters.getPosition();
            PsiElement originalPosition = parameters.getOriginalPosition();
            PsiElement element = originalPosition == null ? position : originalPosition;
            IElementType prevNodeType = CompletionUtils.getPrevNodeType(element);
            PsiElement parent = element.getParent();
            PsiElement grandParent = parent == null ? null : parent.getParent();

            if (LOG.isTraceEnabled()) {
                LOG.trace("»» Completion: position: " + position + ", " + position.getText());
                LOG.trace("               original: " + originalPosition + ", " + (originalPosition == null ? null : originalPosition.getText()));
                LOG.trace("                element: " + element);
                LOG.trace("                 parent: " + parent);
                LOG.trace("           grand-parent: " + grandParent);
                LOG.trace("                   file: " + parameters.getOriginalFile());
            }

            if (originalPosition == null && parent instanceof FileBase) {
                if (prevNodeType != types.DOT && prevNodeType != types.SHARPSHARP && prevNodeType != types.C_LOWER_SYMBOL) {
                    addFileKeywords(result);
                }
            }
        }
    });
}
 
Example #19
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 #20
Source File: WhitespacesBinders.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static WhitespacesAndCommentsBinder trailingCommentsBinder(@Nonnull final TokenSet commentTypes) {
  return new WhitespacesAndCommentsBinder() {
    @Override
    public int getEdgePosition(List<IElementType> tokens, boolean atStreamEdge, TokenTextGetter getter) {
      int i = tokens.size() - 1;
      while (i >= 0 && !commentTypes.contains(tokens.get(i))) {
        i--;
      }
      return i + 1;
    }
  };
}
 
Example #21
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);
}
 
Example #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
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 #29
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 #30
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;
}