Search in sources :

Example 1 with ILazyParseableElementType

use of com.intellij.psi.tree.ILazyParseableElementType in project intellij-community by JetBrains.

the class XmlParsing method parseTagContent.

public void parseTagContent() {
    PsiBuilder.Marker xmlText = null;
    while (token() != XML_END_TAG_START && !eof()) {
        final IElementType tt = token();
        if (tt == XML_START_TAG_START) {
            xmlText = terminateText(xmlText);
            parseTag(false);
        } else if (tt == XML_PI_START) {
            xmlText = terminateText(xmlText);
            parseProcessingInstruction();
        } else if (tt == XML_ENTITY_REF_TOKEN) {
            xmlText = terminateText(xmlText);
            parseReference();
        } else if (tt == XML_CHAR_ENTITY_REF) {
            xmlText = startText(xmlText);
            parseReference();
        } else if (tt == XML_CDATA_START) {
            xmlText = startText(xmlText);
            parseCData();
        } else if (isCommentToken(tt)) {
            xmlText = terminateText(xmlText);
            parseComment();
        } else if (tt == XML_BAD_CHARACTER) {
            xmlText = startText(xmlText);
            final PsiBuilder.Marker error = mark();
            advance();
            error.error(XmlErrorMessages.message("unescaped.ampersand.or.nonterminated.character.entity.reference"));
        } else if (tt instanceof ICustomParsingType || tt instanceof ILazyParseableElementType) {
            xmlText = terminateText(xmlText);
            advance();
        } else {
            xmlText = startText(xmlText);
            advance();
        }
    }
    terminateText(xmlText);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ILazyParseableElementType(com.intellij.psi.tree.ILazyParseableElementType) PsiBuilder(com.intellij.lang.PsiBuilder) ICustomParsingType(com.intellij.psi.tree.ICustomParsingType)

Example 2 with ILazyParseableElementType

use of com.intellij.psi.tree.ILazyParseableElementType in project intellij-community by JetBrains.

the class TraverserBasedASTNode method childrenImpl.

private List<ASTNode> childrenImpl() {
    List<ASTNode> children = myTraverser.children(myNode).transform(CHILD_TRANSFORM(myTraverser, 0)).toList();
    if (!children.isEmpty() || getTreeParent() == null)
        return ContainerUtil.immutableList(children);
    // expand (parse) non-file lazy-parseable nodes
    IElementType type = myTraverser.api.typeOf(myNode);
    if (!(type instanceof ILazyParseableElementType))
        return ContainerUtil.immutableList(children);
    PsiBuilder builder = ((ILazyParseableElementType) type).parseLight(this);
    SyntaxTraverser<LighterASTNode> s = SyntaxTraverser.lightTraverser(builder);
    // avoid ShiftedNode double-shifting
    int shift = myTraverser.api.rangeOf(myNode).getStartOffset();
    List<ASTNode> childrenLazy = s.api.children(s.getRoot()).transform(CHILD_TRANSFORM(s, shift)).toList();
    return ContainerUtil.immutableList(childrenLazy);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ILazyParseableElementType(com.intellij.psi.tree.ILazyParseableElementType)

Example 3 with ILazyParseableElementType

use of com.intellij.psi.tree.ILazyParseableElementType in project intellij-community by JetBrains.

the class DeclarationParser method parse.

@Nullable
public PsiBuilder.Marker parse(final PsiBuilder builder, final Context context) {
    final IElementType tokenType = builder.getTokenType();
    if (tokenType == null)
        return null;
    if (tokenType == JavaTokenType.LBRACE) {
        if (context == Context.FILE || context == Context.CODE_BLOCK)
            return null;
    } else if (tokenType == JavaTokenType.IDENTIFIER || ElementType.PRIMITIVE_TYPE_BIT_SET.contains(tokenType)) {
        if (context == Context.FILE)
            return null;
    } else if (tokenType instanceof ILazyParseableElementType) {
        builder.advanceLexer();
        return null;
    } else if (!ElementType.MODIFIER_BIT_SET.contains(tokenType) && !ElementType.CLASS_KEYWORD_BIT_SET.contains(tokenType) && tokenType != JavaTokenType.AT && (context == Context.CODE_BLOCK || tokenType != JavaTokenType.LT)) {
        return null;
    }
    final PsiBuilder.Marker declaration = builder.mark();
    final int declarationStart = builder.getCurrentOffset();
    final Pair<PsiBuilder.Marker, Boolean> modListInfo = parseModifierList(builder);
    final PsiBuilder.Marker modList = modListInfo.first;
    if (expect(builder, JavaTokenType.AT)) {
        if (builder.getTokenType() == JavaTokenType.INTERFACE_KEYWORD) {
            final PsiBuilder.Marker result = parseClassFromKeyword(builder, declaration, true, context);
            return result != null ? result : modList;
        } else {
            declaration.rollbackTo();
            return null;
        }
    } else if (ElementType.CLASS_KEYWORD_BIT_SET.contains(builder.getTokenType())) {
        final PsiBuilder.Marker result = parseClassFromKeyword(builder, declaration, false, context);
        return result != null ? result : modList;
    }
    PsiBuilder.Marker typeParams = null;
    if (builder.getTokenType() == JavaTokenType.LT) {
        typeParams = myParser.getReferenceParser().parseTypeParameters(builder);
    }
    if (context == Context.FILE) {
        error(builder, JavaErrorMessages.message("expected.class.or.interface"), typeParams);
        declaration.drop();
        return modList;
    }
    if (builder.getTokenType() == JavaTokenType.LBRACE) {
        if (context == Context.CODE_BLOCK) {
            error(builder, JavaErrorMessages.message("expected.identifier.or.type"), typeParams);
            declaration.drop();
            return modList;
        }
        PsiBuilder.Marker codeBlock = myParser.getStatementParser().parseCodeBlock(builder);
        assert codeBlock != null : builder.getOriginalText();
        if (typeParams != null) {
            PsiBuilder.Marker error = typeParams.precede();
            error.errorBefore(JavaErrorMessages.message("unexpected.token"), codeBlock);
        }
        done(declaration, JavaElementType.CLASS_INITIALIZER);
        return declaration;
    }
    PsiBuilder.Marker type = null;
    if (TYPE_START.contains(builder.getTokenType())) {
        PsiBuilder.Marker pos = builder.mark();
        type = myParser.getReferenceParser().parseType(builder, ReferenceParser.EAT_LAST_DOT | ReferenceParser.WILDCARD);
        if (type == null) {
            pos.rollbackTo();
        } else if (builder.getTokenType() == JavaTokenType.LPARENTH) {
            // constructor
            if (context == Context.CODE_BLOCK) {
                declaration.rollbackTo();
                return null;
            }
            pos.rollbackTo();
            if (typeParams == null) {
                emptyElement(builder, JavaElementType.TYPE_PARAMETER_LIST);
            }
            parseAnnotations(builder);
            if (!expect(builder, JavaTokenType.IDENTIFIER)) {
                PsiBuilder.Marker primitive = builder.mark();
                builder.advanceLexer();
                primitive.error(JavaErrorMessages.message("expected.identifier"));
            }
            if (builder.getTokenType() == JavaTokenType.LPARENTH) {
                return parseMethodFromLeftParenth(builder, declaration, false, true);
            } else {
                declaration.rollbackTo();
                return null;
            }
        } else {
            pos.drop();
        }
    }
    if (type == null) {
        PsiBuilder.Marker error = typeParams != null ? typeParams.precede() : builder.mark();
        error.error(JavaErrorMessages.message("expected.identifier.or.type"));
        declaration.drop();
        return modList;
    }
    if (!expect(builder, JavaTokenType.IDENTIFIER)) {
        if ((context == Context.CODE_BLOCK) && Boolean.TRUE.equals(modListInfo.second)) {
            declaration.rollbackTo();
            return null;
        } else {
            if (typeParams != null) {
                typeParams.precede().errorBefore(JavaErrorMessages.message("unexpected.token"), type);
            }
            builder.error(JavaErrorMessages.message("expected.identifier"));
            declaration.drop();
            return modList;
        }
    }
    if (builder.getTokenType() == JavaTokenType.LPARENTH) {
        if (context == Context.CLASS || context == Context.ANNOTATION_INTERFACE) {
            // method
            if (typeParams == null) {
                emptyElement(type, JavaElementType.TYPE_PARAMETER_LIST);
            }
            return parseMethodFromLeftParenth(builder, declaration, (context == Context.ANNOTATION_INTERFACE), false);
        }
    }
    if (typeParams != null) {
        typeParams.precede().errorBefore(JavaErrorMessages.message("unexpected.token"), type);
    }
    return parseFieldOrLocalVariable(builder, declaration, declarationStart, context);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ILazyParseableElementType(com.intellij.psi.tree.ILazyParseableElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ILazyParseableElementType

use of com.intellij.psi.tree.ILazyParseableElementType in project intellij-community by JetBrains.

the class BraceHighlightingHandler method getLazyParsableHighlighterIfAny.

@NotNull
static EditorHighlighter getLazyParsableHighlighterIfAny(Project project, Editor editor, PsiFile psiFile) {
    if (!PsiDocumentManager.getInstance(project).isCommitted(editor.getDocument())) {
        return ((EditorEx) editor).getHighlighter();
    }
    PsiElement elementAt = psiFile.findElementAt(editor.getCaretModel().getOffset());
    for (PsiElement e : SyntaxTraverser.psiApi().parents(elementAt).takeWhile(Conditions.notEqualTo(psiFile))) {
        if (!(PsiUtilCore.getElementType(e) instanceof ILazyParseableElementType))
            continue;
        Language language = ILazyParseableElementType.LANGUAGE_KEY.get(e.getNode());
        if (language == null)
            continue;
        TextRange range = e.getTextRange();
        final int offset = range.getStartOffset();
        SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, psiFile.getVirtualFile());
        LexerEditorHighlighter highlighter = new LexerEditorHighlighter(syntaxHighlighter, editor.getColorsScheme()) {

            @NotNull
            @Override
            public HighlighterIterator createIterator(int startOffset) {
                return new HighlighterIteratorWrapper(super.createIterator(Math.max(startOffset - offset, 0))) {

                    @Override
                    public int getStart() {
                        return super.getStart() + offset;
                    }

                    @Override
                    public int getEnd() {
                        return super.getEnd() + offset;
                    }
                };
            }
        };
        highlighter.setText(editor.getDocument().getText(range));
        return highlighter;
    }
    return ((EditorEx) editor).getHighlighter();
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIteratorWrapper(com.intellij.openapi.editor.ex.util.HighlighterIteratorWrapper) Language(com.intellij.lang.Language) ILazyParseableElementType(com.intellij.psi.tree.ILazyParseableElementType) TextRange(com.intellij.openapi.util.TextRange) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LightweightHint(com.intellij.ui.LightweightHint) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ILazyParseableElementType

use of com.intellij.psi.tree.ILazyParseableElementType in project intellij-community by JetBrains.

the class ChameleonSyntaxHighlightingPass method collectInformationWithProgress.

@Override
public void collectInformationWithProgress(@NotNull ProgressIndicator progress) {
    SyntaxTraverser<PsiElement> s = psiTraverser(myFile).filter(o -> {
        IElementType type = PsiUtilCore.getElementType(o);
        return type instanceof ILazyParseableElementType && !(type instanceof IFileElementType);
    });
    List<PsiElement> lazyOutside = ContainerUtil.newArrayListWithCapacity(100);
    List<PsiElement> lazyInside = ContainerUtil.newArrayListWithCapacity(100);
    List<HighlightInfo> outside = ContainerUtil.newArrayListWithCapacity(100);
    List<HighlightInfo> inside = ContainerUtil.newArrayListWithCapacity(100);
    for (PsiElement e : s) {
        (e.getTextRange().intersects(myPriorityRange) ? lazyInside : lazyOutside).add(e);
    }
    for (PsiElement e : lazyInside) {
        collectHighlights(e, inside, outside, myPriorityRange);
    }
    myHighlightInfoProcessor.highlightsInsideVisiblePartAreProduced(myHighlightingSession, inside, myPriorityRange, myRestrictRange, getId());
    for (PsiElement e : lazyOutside) {
        collectHighlights(e, inside, outside, myPriorityRange);
    }
    myHighlightInfoProcessor.highlightsOutsideVisiblePartAreProduced(myHighlightingSession, outside, myPriorityRange, myRestrictRange, getId());
    myHighlights.addAll(inside);
    myHighlights.addAll(outside);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IFileElementType(com.intellij.psi.tree.IFileElementType) ILazyParseableElementType(com.intellij.psi.tree.ILazyParseableElementType) PsiElement(com.intellij.psi.PsiElement)

Aggregations

ILazyParseableElementType (com.intellij.psi.tree.ILazyParseableElementType)7 IElementType (com.intellij.psi.tree.IElementType)6 PsiBuilder (com.intellij.lang.PsiBuilder)4 ICustomParsingType (com.intellij.psi.tree.ICustomParsingType)2 Nullable (org.jetbrains.annotations.Nullable)2 Language (com.intellij.lang.Language)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 HighlighterIteratorWrapper (com.intellij.openapi.editor.ex.util.HighlighterIteratorWrapper)1 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)1 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 IFileElementType (com.intellij.psi.tree.IFileElementType)1 LightweightHint (com.intellij.ui.LightweightHint)1 NotNull (org.jetbrains.annotations.NotNull)1