Search in sources :

Example 21 with FileViewProvider

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

the class LanguageUtil method getRootLanguage.

@NotNull
public static Language getRootLanguage(@NotNull PsiElement element) {
    final FileViewProvider provider = element.getContainingFile().getViewProvider();
    final Set<Language> languages = provider.getLanguages();
    if (languages.size() > 1) {
        PsiElement current = element;
        while (current != null) {
            final Language language = current.getLanguage();
            if (languages.contains(language))
                return language;
            current = current.getParent();
        }
    }
    return provider.getBaseLanguage();
}
Also used : FileViewProvider(com.intellij.psi.FileViewProvider) TemplateLanguage(com.intellij.psi.templateLanguages.TemplateLanguage) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with FileViewProvider

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

the class PsiFileStubImpl method getStubRoots.

@NotNull
@Override
public PsiFileStub[] getStubRoots() {
    if (myStubRoots != null)
        return myStubRoots;
    final T psi = getPsi();
    if (psi == null) {
        return new PsiFileStub[] { this };
    }
    final FileViewProvider viewProvider = psi.getViewProvider();
    final PsiFile stubBindingRoot = viewProvider.getStubBindingRoot();
    StubTree baseTree = getOrCalcStubTree(stubBindingRoot);
    if (baseTree != null) {
        final List<PsiFileStub> roots = new SmartList<>(baseTree.getRoot());
        final List<Pair<IStubFileElementType, PsiFile>> stubbedRoots = StubTreeBuilder.getStubbedRoots(viewProvider);
        for (Pair<IStubFileElementType, PsiFile> stubbedRoot : stubbedRoots) {
            if (stubbedRoot.second == stubBindingRoot)
                continue;
            final StubTree secondaryStubTree = getOrCalcStubTree(stubbedRoot.second);
            if (secondaryStubTree != null) {
                final PsiFileStub root = secondaryStubTree.getRoot();
                roots.add(root);
            }
        }
        final PsiFileStub[] rootsArray = roots.toArray(new PsiFileStub[roots.size()]);
        for (PsiFileStub root : rootsArray) {
            if (root instanceof PsiFileStubImpl) {
                ((PsiFileStubImpl) root).setStubRoots(rootsArray);
            }
        }
        myStubRoots = rootsArray;
        return rootsArray;
    }
    return PsiFileStub.EMPTY_ARRAY;
}
Also used : IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) FileViewProvider(com.intellij.psi.FileViewProvider) PsiFile(com.intellij.psi.PsiFile) SmartList(com.intellij.util.SmartList) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with FileViewProvider

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

the class StubTreeBuilder method buildStubTree.

@Nullable
public static Stub buildStubTree(final FileContent inputData) {
    Stub data = inputData.getUserData(stubElementKey);
    if (data != null)
        return data;
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (inputData) {
        data = inputData.getUserData(stubElementKey);
        if (data != null)
            return data;
        final FileType fileType = inputData.getFileType();
        final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
        if (builder != null) {
            data = builder.buildStubTree(inputData);
            if (data instanceof PsiFileStubImpl && !((PsiFileStubImpl) data).rootsAreSet()) {
                ((PsiFileStubImpl) data).setStubRoots(new PsiFileStub[] { (PsiFileStubImpl) data });
            }
        } else {
            CharSequence contentAsText = inputData.getContentAsText();
            FileContentImpl fileContent = (FileContentImpl) inputData;
            PsiFile psi = fileContent.getPsiFileForPsiDependentIndex();
            final FileViewProvider viewProvider = psi.getViewProvider();
            psi = viewProvider.getStubBindingRoot();
            psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, contentAsText);
            // if we load AST, it should be easily gc-able. See PsiFileImpl.createTreeElementPointer()
            psi.getManager().startBatchFilesProcessingMode();
            try {
                IStubFileElementType stubFileElementType = ((PsiFileImpl) psi).getElementTypeForStubBuilder();
                if (stubFileElementType != null) {
                    final StubBuilder stubBuilder = stubFileElementType.getBuilder();
                    if (stubBuilder instanceof LightStubBuilder) {
                        LightStubBuilder.FORCED_AST.set(fileContent.getLighterASTForPsiDependentIndex());
                    }
                    data = stubBuilder.buildStubTree(psi);
                    final List<Pair<IStubFileElementType, PsiFile>> stubbedRoots = getStubbedRoots(viewProvider);
                    final List<PsiFileStub> stubs = new ArrayList<>(stubbedRoots.size());
                    stubs.add((PsiFileStub) data);
                    for (Pair<IStubFileElementType, PsiFile> stubbedRoot : stubbedRoots) {
                        final PsiFile secondaryPsi = stubbedRoot.second;
                        if (psi == secondaryPsi)
                            continue;
                        final StubBuilder stubbedRootBuilder = stubbedRoot.first.getBuilder();
                        if (stubbedRootBuilder instanceof LightStubBuilder) {
                            LightStubBuilder.FORCED_AST.set(new TreeBackedLighterAST(secondaryPsi.getNode()));
                        }
                        final StubElement element = stubbedRootBuilder.buildStubTree(secondaryPsi);
                        if (element instanceof PsiFileStub) {
                            stubs.add((PsiFileStub) element);
                        }
                    }
                    final PsiFileStub[] stubsArray = stubs.toArray(new PsiFileStub[stubs.size()]);
                    for (PsiFileStub stub : stubsArray) {
                        if (stub instanceof PsiFileStubImpl) {
                            ((PsiFileStubImpl) stub).setStubRoots(stubsArray);
                        }
                    }
                }
            } finally {
                psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, null);
                psi.getManager().finishBatchFilesProcessingMode();
            }
        }
        inputData.putUserData(stubElementKey, data);
        return data;
    }
}
Also used : IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) ArrayList(java.util.ArrayList) StubBuilder(com.intellij.psi.StubBuilder) FileViewProvider(com.intellij.psi.FileViewProvider) FileType(com.intellij.openapi.fileTypes.FileType) TreeBackedLighterAST(com.intellij.lang.TreeBackedLighterAST) PsiFile(com.intellij.psi.PsiFile) FileContentImpl(com.intellij.util.indexing.FileContentImpl) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project intellij-plugins by JetBrains.

the class CfmlCommenter method getCfmlElementAtOffset.

private static PsiElement getCfmlElementAtOffset(int offset, PsiFile file) {
    final FileViewProvider fileViewProvider = file.getViewProvider();
    file = fileViewProvider.getPsi(fileViewProvider.getBaseLanguage());
    final Language cfmlLanguage = file.getLanguage();
    return fileViewProvider.findElementAt(offset, cfmlLanguage);
}
Also used : FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language)

Example 25 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project intellij-plugins by JetBrains.

the class AngularJSIndexingHandler method createLiteralImplicitElementProvider.

@Override
public JSLiteralImplicitElementProvider createLiteralImplicitElementProvider(@NotNull final String command) {
    return new JSLiteralImplicitElementProvider() {

        @Override
        public void fillIndexingData(@NotNull JSLiteralExpression argument, @NotNull JSCallExpression callExpression, @NotNull JSElementIndexingData outIndexingData) {
            JSExpression[] arguments = callExpression.getArguments();
            if (arguments.length == 0 || arguments[0] != argument)
                return;
            final JSExpression methodExpression = callExpression.getMethodExpression();
            if (!(methodExpression instanceof JSReferenceExpression))
                return;
            JSExpression qualifier = ((JSReferenceExpression) methodExpression).getQualifier();
            if (qualifier == null)
                return;
            final StubIndexKey<String, JSImplicitElementProvider> index = INDEXERS.get(command);
            if (index != null) {
                if (argument.isQuotedLiteral()) {
                    final Processor<JSArgumentList> argumentListProcessor = ARGUMENT_LIST_CHECKERS.get(command);
                    if (argumentListProcessor != null && !argumentListProcessor.process(callExpression.getArgumentList()))
                        return;
                    final Function<PsiElement, String> calculator = DATA_CALCULATORS.get(command);
                    final String data = calculator != null ? calculator.fun(argument) : null;
                    final String argumentText = unquote(argument);
                    addImplicitElements(argument, command, index, argumentText, data, outIndexingData);
                }
            } else if (INJECTABLE_METHODS.contains(command)) {
                // INTERESTING_METHODS are contained in INJECTABLE_METHODS
                if (argument.isQuotedLiteral()) {
                    generateNamespace(argument, outIndexingData);
                }
            }
            if (START_SYMBOL.equals(command) || END_SYMBOL.equals(command)) {
                while (qualifier != null) {
                    if (qualifier instanceof JSReferenceExpression) {
                        if ("$interpolateProvider".equals(((JSReferenceExpression) qualifier).getReferenceName())) {
                            if (argument.isQuotedLiteral()) {
                                String interpolation = unquote(argument);
                                // '//' interpolations are usually dragged from examples folder and not supposed to be used by real users
                                if ("//".equals(interpolation))
                                    return;
                                FileViewProvider provider = qualifier.getContainingFile().getOriginalFile().getViewProvider();
                                VirtualFile virtualFile = provider.getVirtualFile();
                                virtualFile = virtualFile instanceof LightVirtualFile ? ((LightVirtualFile) virtualFile).getOriginalFile() : virtualFile;
                                if (JSLibraryUtil.isProbableLibraryFile(virtualFile))
                                    return;
                                addImplicitElements(argument, null, AngularInjectionDelimiterIndex.KEY, command, interpolation, outIndexingData);
                            }
                        }
                        qualifier = ((JSReferenceExpression) qualifier).getQualifier();
                    } else {
                        qualifier = qualifier instanceof JSCallExpression ? ((JSCallExpression) qualifier).getMethodExpression() : null;
                    }
                }
            }
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) JSLiteralImplicitElementProvider(com.intellij.lang.javascript.psi.literal.JSLiteralImplicitElementProvider) NotNull(org.jetbrains.annotations.NotNull) JSElementIndexingData(com.intellij.lang.javascript.psi.stubs.JSElementIndexingData) FileViewProvider(com.intellij.psi.FileViewProvider) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiElement(com.intellij.psi.PsiElement)

Aggregations

FileViewProvider (com.intellij.psi.FileViewProvider)41 PsiFile (com.intellij.psi.PsiFile)22 Language (com.intellij.lang.Language)21 PsiElement (com.intellij.psi.PsiElement)15 NotNull (org.jetbrains.annotations.NotNull)12 TextRange (com.intellij.openapi.util.TextRange)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 TemplateLanguageFileViewProvider (com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider)5 Project (com.intellij.openapi.project.Project)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 Pair (com.intellij.openapi.util.Pair)3 Nullable (org.jetbrains.annotations.Nullable)3 HighlightingLevelManager (com.intellij.codeInsight.daemon.impl.analysis.HighlightingLevelManager)2 FileASTNode (com.intellij.lang.FileASTNode)2 ExternalAnnotator (com.intellij.lang.annotation.ExternalAnnotator)2 XMLLanguage (com.intellij.lang.xml.XMLLanguage)2 FileType (com.intellij.openapi.fileTypes.FileType)2 SingleRootFileViewProvider (com.intellij.psi.SingleRootFileViewProvider)2 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)2