Search in sources :

Example 51 with PsiFileImpl

use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.

the class StubTreeBuilder method getStubbedRoots.

/** Order is deterministic. First element matches {@link FileViewProvider#getStubBindingRoot()} */
@NotNull
public static List<Pair<IStubFileElementType, PsiFile>> getStubbedRoots(@NotNull FileViewProvider viewProvider) {
    final List<Trinity<Language, IStubFileElementType, PsiFile>> roots = new SmartList<>();
    final PsiFile stubBindingRoot = viewProvider.getStubBindingRoot();
    for (Language language : viewProvider.getLanguages()) {
        final PsiFile file = viewProvider.getPsi(language);
        if (file instanceof PsiFileImpl) {
            final IElementType type = ((PsiFileImpl) file).getElementTypeForStubBuilder();
            if (type != null) {
                roots.add(Trinity.create(language, (IStubFileElementType) type, file));
            }
        }
    }
    ContainerUtil.sort(roots, (o1, o2) -> {
        if (o1.third == stubBindingRoot)
            return o2.third == stubBindingRoot ? 0 : -1;
        else if (o2.third == stubBindingRoot)
            return 1;
        else
            return StringUtil.compare(o1.first.getID(), o2.first.getID(), false);
    });
    return ContainerUtil.map(roots, trinity -> Pair.create(trinity.second, trinity.third));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) Trinity(com.intellij.openapi.util.Trinity) Language(com.intellij.lang.Language) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with PsiFileImpl

use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.

the class PsiFileGistImpl method getPsiFile.

private static PsiFile getPsiFile(@NotNull Project project, @NotNull VirtualFile file) {
    PsiFile psi = PsiManager.getInstance(project).findFile(file);
    if (psi == null || !(psi instanceof PsiFileImpl) || ((PsiFileImpl) psi).isContentsLoaded()) {
        return psi;
    }
    FileType fileType = file.getFileType();
    if (!(fileType instanceof LanguageFileType))
        return null;
    return FileContentImpl.createFileFromText(project, psi.getViewProvider().getContents(), (LanguageFileType) fileType, file, file.getName());
}
Also used : LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile)

Example 53 with PsiFileImpl

use of com.intellij.psi.impl.source.PsiFileImpl in project intellij-community by JetBrains.

the class PatternCompiler method compilePattern.

public static CompiledPattern compilePattern(final Project project, final MatchOptions options) throws MalformedPatternException, UnsupportedOperationException {
    FileType fileType = options.getFileType();
    assert fileType instanceof LanguageFileType;
    Language language = ((LanguageFileType) fileType).getLanguage();
    StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language);
    assert profile != null;
    CompiledPattern result = profile.createCompiledPattern();
    final String[] prefixes = result.getTypedVarPrefixes();
    assert prefixes.length > 0;
    final CompileContext context = new CompileContext();
    if (ApplicationManager.getApplication().isUnitTestMode())
        lastTestingContext = context;
    try {
        context.init(result, options, project, options.getScope() instanceof GlobalSearchScope);
        List<PsiElement> elements = compileByAllPrefixes(project, options, result, context, prefixes);
        final CompiledPattern pattern = context.getPattern();
        checkForUnknownVariables(pattern, elements);
        pattern.setNodes(elements);
        if (context.getSearchHelper().doOptimizing() && context.getSearchHelper().isScannedSomething()) {
            final Set<PsiFile> set = context.getSearchHelper().getFilesSetToScan();
            final List<PsiFile> filesToScan = new ArrayList<>(set.size());
            final GlobalSearchScope scope = (GlobalSearchScope) options.getScope();
            for (final PsiFile file : set) {
                if (!scope.contains(file.getVirtualFile())) {
                    continue;
                }
                if (file instanceof PsiFileImpl) {
                    ((PsiFileImpl) file).clearCaches();
                }
                filesToScan.add(file);
            }
            if (filesToScan.size() == 0) {
                throw new MalformedPatternException(SSRBundle.message("ssr.will.not.find.anything"));
            }
            result.setScope(new LocalSearchScope(PsiUtilCore.toPsiElementArray(filesToScan)));
        }
    } finally {
        context.clear();
    }
    return result;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) TIntArrayList(gnu.trove.TIntArrayList) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) CompiledPattern(com.intellij.structuralsearch.impl.matcher.CompiledPattern)

Aggregations

PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)53 Document (com.intellij.openapi.editor.Document)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 NotNull (org.jetbrains.annotations.NotNull)8 PsiFile (com.intellij.psi.PsiFile)7 Language (com.intellij.lang.Language)6 Nullable (org.jetbrains.annotations.Nullable)6 FileType (com.intellij.openapi.fileTypes.FileType)5 StubTree (com.intellij.psi.stubs.StubTree)5 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)5 DocumentWindow (com.intellij.injected.editor.DocumentWindow)3 ASTNode (com.intellij.lang.ASTNode)3 ParserDefinition (com.intellij.lang.ParserDefinition)3 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)3 Project (com.intellij.openapi.project.Project)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 StubElement (com.intellij.psi.stubs.StubElement)3 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 FileASTNode (com.intellij.lang.FileASTNode)2