Search in sources :

Example 6 with ParserDefinition

use of com.intellij.lang.ParserDefinition in project intellij-community by JetBrains.

the class MultiHostRegistrarImpl method doneInjecting.

@Override
public void doneInjecting() {
    try {
        if (shreds.isEmpty()) {
            throw new IllegalStateException("Seems you haven't called addPlace()");
        }
        if (myReferenceInjector != null) {
            addToResults(new Place(shreds), null);
            return;
        }
        PsiDocumentManagerBase documentManager = (PsiDocumentManagerBase) PsiDocumentManager.getInstance(myProject);
        Place place = new Place(shreds);
        DocumentWindowImpl documentWindow = new DocumentWindowImpl(myHostDocument, isOneLineEditor, place);
        String fileName = PathUtil.makeFileName(myHostVirtualFile.getName(), fileExtension);
        VirtualFileWindowImpl virtualFile = new VirtualFileWindowImpl(fileName, myHostVirtualFile, documentWindow, myLanguage, outChars);
        Language forcedLanguage = myContextElement.getUserData(InjectedFileViewProvider.LANGUAGE_FOR_INJECTED_COPY_KEY);
        myLanguage = forcedLanguage == null ? LanguageSubstitutors.INSTANCE.substituteLanguage(myLanguage, virtualFile, myProject) : forcedLanguage;
        createDocument(virtualFile);
        InjectedFileViewProvider viewProvider = new InjectedFileViewProvider(myPsiManager, virtualFile, documentWindow, myLanguage);
        ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(myLanguage);
        assert parserDefinition != null : "Parser definition for language " + myLanguage + " is null";
        PsiFile psiFile = parserDefinition.createFile(viewProvider);
        SmartPsiElementPointer<PsiLanguageInjectionHost> pointer = ((ShredImpl) shreds.get(0)).getSmartPointer();
        synchronized (InjectedLanguageManagerImpl.ourInjectionPsiLock) {
            final ASTNode parsedNode = keepTreeFromChameleoningBack(psiFile);
            assert parsedNode instanceof FileElement : "Parsed to " + parsedNode + " instead of FileElement";
            String documentText = documentManager.getLastCommittedDocument(documentWindow).getText();
            assert ((FileElement) parsedNode).textMatches(outChars) : exceptionContext("Before patch: doc:\n'" + documentText + "'\n---PSI:\n'" + parsedNode.getText() + "'\n---chars:\n'" + outChars + "'");
            viewProvider.setPatchingLeaves(true);
            try {
                patchLeaves(parsedNode, escapers, place);
            } catch (ProcessCanceledException e) {
                throw e;
            } catch (RuntimeException e) {
                throw new RuntimeException(exceptionContext("Patch error"), e);
            } finally {
                viewProvider.setPatchingLeaves(false);
            }
            if (!((FileElement) parsedNode).textMatches(documentText)) {
                throw new AssertionError(exceptionContext("After patch: doc:\n'" + documentText + "'\n---PSI:\n'" + parsedNode.getText() + "'\n---chars:\n'" + outChars + "'"));
            }
            virtualFile.setContent(null, documentWindow.getText(), false);
            virtualFile.setWritable(virtualFile.getDelegate().isWritable());
            cacheEverything(place, documentWindow, viewProvider, psiFile, pointer);
            PsiFile cachedPsiFile = documentManager.getCachedPsiFile(documentWindow);
            assert cachedPsiFile == psiFile : "Cached psi :" + cachedPsiFile + " instead of " + psiFile;
            assert place.isValid();
            assert viewProvider.isValid();
            PsiFile newFile = registerDocument(documentWindow, psiFile, place, myHostPsiFile, documentManager);
            boolean mergeHappened = newFile != psiFile;
            if (mergeHappened) {
                InjectedLanguageUtil.clearCaches(psiFile, documentWindow);
                psiFile = newFile;
                viewProvider = (InjectedFileViewProvider) psiFile.getViewProvider();
                documentWindow = (DocumentWindowImpl) viewProvider.getDocument();
                virtualFile = (VirtualFileWindowImpl) viewProvider.getVirtualFile();
                boolean shredsRewritten = cacheEverything(place, documentWindow, viewProvider, psiFile, pointer);
                if (!shredsRewritten) {
                    place.dispose();
                    place = documentWindow.getShreds();
                }
            }
            assert psiFile.isValid();
            assert place.isValid();
            assert viewProvider.isValid();
            try {
                List<Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange>> tokens = obtainHighlightTokensFromLexer(myLanguage, outChars, escapers, place, virtualFile, myProject);
                psiFile.putUserData(InjectedLanguageUtil.HIGHLIGHT_TOKENS, tokens);
            } catch (ProcessCanceledException e) {
                throw e;
            } catch (RuntimeException e) {
                throw new RuntimeException(exceptionContext("Obtaining tokens error"), e);
            }
            addToResults(place, psiFile);
            assertEverythingIsAllright(documentManager, documentWindow, psiFile);
        }
    } finally {
        clear();
    }
}
Also used : VirtualFileWindowImpl(com.intellij.injected.editor.VirtualFileWindowImpl) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) ParserDefinition(com.intellij.lang.ParserDefinition) DocumentWindowImpl(com.intellij.injected.editor.DocumentWindowImpl) Language(com.intellij.lang.Language) ASTNode(com.intellij.lang.ASTNode) FileElement(com.intellij.psi.impl.source.tree.FileElement) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 7 with ParserDefinition

use of com.intellij.lang.ParserDefinition in project intellij-community by JetBrains.

the class PlatformIdTableBuilding method getTodoIndexer.

@Nullable
public static DataIndexer<TodoIndexEntry, Integer, FileContent> getTodoIndexer(FileType fileType, final VirtualFile virtualFile) {
    final DataIndexer<TodoIndexEntry, Integer, FileContent> extIndexer;
    if (fileType instanceof SubstitutedFileType && !((SubstitutedFileType) fileType).isSameFileType()) {
        SubstitutedFileType sft = (SubstitutedFileType) fileType;
        extIndexer = new CompositeTodoIndexer(getTodoIndexer(sft.getOriginalFileType(), virtualFile), getTodoIndexer(sft.getFileType(), virtualFile));
    } else {
        extIndexer = TodoIndexers.INSTANCE.forFileType(fileType);
    }
    if (extIndexer != null) {
        return extIndexer;
    }
    if (fileType instanceof LanguageFileType) {
        final Language lang = ((LanguageFileType) fileType).getLanguage();
        final ParserDefinition parserDef = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
        final TokenSet commentTokens = parserDef != null ? parserDef.getCommentTokens() : null;
        if (commentTokens != null) {
            return new TokenSetTodoIndexer(commentTokens, virtualFile);
        }
    }
    if (fileType instanceof CustomSyntaxTableFileType) {
        return new TokenSetTodoIndexer(ABSTRACT_FILE_COMMENT_TOKENS, virtualFile);
    }
    return null;
}
Also used : FileContent(com.intellij.util.indexing.FileContent) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) ParserDefinition(com.intellij.lang.ParserDefinition) SubstitutedFileType(com.intellij.util.indexing.SubstitutedFileType) Language(com.intellij.lang.Language) TokenSet(com.intellij.psi.tree.TokenSet) TodoIndexEntry(com.intellij.psi.impl.cache.impl.todo.TodoIndexEntry) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with ParserDefinition

use of com.intellij.lang.ParserDefinition in project intellij-community by JetBrains.

the class JspIndexPatternBuilder method getCommentTokenSet.

@Override
public TokenSet getCommentTokenSet(@NotNull final PsiFile file) {
    final JspFile jspFile = JspPsiUtil.getJspFile(file);
    TokenSet commentTokens = TokenSet.orSet(JavaIndexPatternBuilder.XML_COMMENT_BIT_SET, StdTokenSets.COMMENT_BIT_SET);
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(jspFile.getViewProvider().getTemplateDataLanguage());
    if (parserDefinition != null) {
        commentTokens = TokenSet.orSet(commentTokens, parserDefinition.getCommentTokens());
    }
    return commentTokens;
}
Also used : ParserDefinition(com.intellij.lang.ParserDefinition) JspFile(com.intellij.psi.jsp.JspFile) TokenSet(com.intellij.psi.tree.TokenSet)

Example 9 with ParserDefinition

use of com.intellij.lang.ParserDefinition in project intellij-community by JetBrains.

the class ShiftIndentInsideHelper method isComment.

private static boolean isComment(final ASTNode node) {
    final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(node);
    if (psiElement instanceof PsiComment)
        return true;
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(psiElement.getLanguage());
    if (parserDefinition == null)
        return false;
    final TokenSet commentTokens = parserDefinition.getCommentTokens();
    return commentTokens.contains(node.getElementType());
}
Also used : ParserDefinition(com.intellij.lang.ParserDefinition) TokenSet(com.intellij.psi.tree.TokenSet)

Example 10 with ParserDefinition

use of com.intellij.lang.ParserDefinition in project intellij-community by JetBrains.

the class CacheUtil method isInComments.

public static boolean isInComments(final IElementType tokenType) {
    final Language language = tokenType.getLanguage();
    for (CommentTokenSetProvider provider : CommentTokenSetProvider.EXTENSION.allForLanguage(language)) {
        if (provider.isInComments(tokenType)) {
            return true;
        }
    }
    boolean inComments = false;
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
    if (parserDefinition != null) {
        final TokenSet commentTokens = parserDefinition.getCommentTokens();
        if (commentTokens.contains(tokenType)) {
            inComments = true;
        }
    }
    return inComments;
}
Also used : ParserDefinition(com.intellij.lang.ParserDefinition) Language(com.intellij.lang.Language) TokenSet(com.intellij.psi.tree.TokenSet)

Aggregations

ParserDefinition (com.intellij.lang.ParserDefinition)23 Language (com.intellij.lang.Language)8 ASTNode (com.intellij.lang.ASTNode)7 TokenSet (com.intellij.psi.tree.TokenSet)6 PsiElement (com.intellij.psi.PsiElement)5 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)4 FileType (com.intellij.openapi.fileTypes.FileType)3 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)3 IElementType (com.intellij.psi.tree.IElementType)3 IFileElementType (com.intellij.psi.tree.IFileElementType)3 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)3 Annotation (com.intellij.lang.annotation.Annotation)2 CustomSyntaxTableFileType (com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 DocumentWindowImpl (com.intellij.injected.editor.DocumentWindowImpl)1 VirtualFileWindowImpl (com.intellij.injected.editor.VirtualFileWindowImpl)1 PsiBuilder (com.intellij.lang.PsiBuilder)1