Search in sources :

Example 46 with PsiManager

use of com.intellij.psi.PsiManager in project checkstyle-idea by jshiell.

the class ScanFiles method findAllFilesFor.

private List<PsiFile> findAllFilesFor(@NotNull final List<VirtualFile> virtualFiles) {
    final List<PsiFile> childFiles = new ArrayList<>();
    final PsiManager psiManager = PsiManager.getInstance(this.plugin.getProject());
    for (final VirtualFile virtualFile : virtualFiles) {
        childFiles.addAll(buildFilesList(psiManager, virtualFile));
    }
    return childFiles;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile)

Example 47 with PsiManager

use of com.intellij.psi.PsiManager in project yii2support by nvlad.

the class CompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
    final PsiElement psiElement = completionParameters.getPosition();
    final MethodReference method = PsiTreeUtil.getParentOfType(psiElement, MethodReference.class);
    if (method == null || method.getParameters().length == 0) {
        return;
    }
    if (!ViewUtil.isValidRenderMethod(method)) {
        return;
    }
    PsiElement viewParameter = psiElement;
    while (viewParameter != null && !(viewParameter.getParent() instanceof ParameterList)) {
        viewParameter = viewParameter.getParent();
    }
    if (viewParameter == null || !viewParameter.equals(method.getParameters()[0])) {
        return;
    }
    final ViewResolve resolve = ViewUtil.resolveView(viewParameter);
    if (resolve == null) {
        return;
    }
    final Project project = psiElement.getProject();
    final GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
    final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
    int prefixLength = resolve.key.length();
    int lastSlashPosition = resolve.key.lastIndexOf('/');
    if (lastSlashPosition != -1 && !resolve.key.endsWith("/")) {
        prefixLength = lastSlashPosition + 1;
    }
    if (!completionParameters.isAutoPopup()) {
        if (completionResultSet.getPrefixMatcher().getPrefix().startsWith("@") && lastSlashPosition == -1) {
            final String prefix = completionResultSet.getPrefixMatcher().getPrefix();
            completionResultSet = completionResultSet.withPrefixMatcher(prefix.substring(1));
            prefixLength = 1;
        } else {
            completionResultSet = completionResultSet.withPrefixMatcher(resolve.key.substring(prefixLength));
        }
    }
    if (completionResultSet.getPrefixMatcher().getPrefix().equals("@")) {
        completionResultSet = completionResultSet.withPrefixMatcher("");
    }
    final String prefixFilter = resolve.key.substring(0, prefixLength);
    final Set<String> keys = new HashSet<>();
    fileBasedIndex.processAllKeys(ViewFileIndex.identity, key -> {
        if (key.startsWith(prefixFilter)) {
            keys.add(key);
        }
        return true;
    }, scope, null);
    final PsiManager psiManager = PsiManager.getInstance(project);
    boolean localViewSearch = false;
    if (resolve.from == ViewResolveFrom.View) {
        final String value = PhpUtil.getValue(viewParameter);
        localViewSearch = !value.startsWith("@") && !value.startsWith("//");
    }
    final String defaultViewExtension = '.' + Yii2SupportSettings.getInstance(psiElement.getProject()).defaultViewExtension;
    for (String key : keys) {
        Collection<ViewInfo> views = fileBasedIndex.getValues(ViewFileIndex.identity, key, scope);
        for (ViewInfo view : views) {
            if (!resolve.application.equals(view.application)) {
                continue;
            }
            if (localViewSearch && !resolve.theme.equals(view.theme)) {
                continue;
            }
            PsiFile psiFile = psiManager.findFile(view.getVirtualFile());
            if (psiFile != null) {
                String insertText = key.substring(prefixLength);
                if (insertText.endsWith(defaultViewExtension)) {
                    insertText = insertText.substring(0, insertText.length() - defaultViewExtension.length());
                }
                completionResultSet.addElement(new ViewLookupElement(psiFile, insertText));
                break;
            } else {
                System.out.println(view.fileUrl + " => not exists");
            }
        }
    }
}
Also used : ViewResolve(com.nvlad.yii2support.views.entities.ViewResolve) PsiManager(com.intellij.psi.PsiManager) ViewInfo(com.nvlad.yii2support.views.entities.ViewInfo) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ParameterList(com.jetbrains.php.lang.psi.elements.ParameterList) MethodReference(com.jetbrains.php.lang.psi.elements.MethodReference) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) HashSet(java.util.HashSet)

Example 48 with PsiManager

use of com.intellij.psi.PsiManager in project Perl5-IDEA by Camelcade.

the class PodFileUtil method processPodFilesByDescriptor.

public static void processPodFilesByDescriptor(Project project, PodLinkDescriptor descriptor, Processor<PsiFile> processor) {
    if (descriptor.getFileId() != null) {
        // seek file
        String fileId = descriptor.getFileId();
        if (// can be Foo/Bar.pod or Foo/Bar.pm
        fileId.contains(PerlPackageUtil.PACKAGE_SEPARATOR) || !StringUtil.startsWith(fileId, "perl")) {
            final PsiManager psiManager = PsiManager.getInstance(project);
            String podRelativePath = PodFileUtil.getFilenameFromPackage(fileId);
            String packageRelativePath = PerlPackageUtil.getPackagePathByName(fileId);
            for (VirtualFile classRoot : PerlProjectManager.getInstance(project).getAllLibraryRoots()) {
                VirtualFile targetVirtualFile = classRoot.findFileByRelativePath(podRelativePath);
                if (targetVirtualFile != null) {
                    if (!processor.process(psiManager.findFile(targetVirtualFile))) {
                        return;
                    }
                }
                targetVirtualFile = classRoot.findFileByRelativePath(packageRelativePath);
                if (targetVirtualFile != null) {
                    if (!processor.process(psiManager.findFile(targetVirtualFile))) {
                        return;
                    }
                }
            }
        } else // top level file perl.*
        {
            fileId += "." + PodFileType.EXTENSION;
            for (PsiFile podFile : FilenameIndex.getFilesByName(project, fileId, GlobalSearchScope.allScope(project))) {
                if (!processor.process(podFile)) {
                    return;
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile)

Example 49 with PsiManager

use of com.intellij.psi.PsiManager in project Perl5-IDEA by Camelcade.

the class TemplateToolkitFileReferenceHelper method getRoots.

@NotNull
@Override
public Collection<PsiFileSystemItem> getRoots(@NotNull Module module) {
    Collection<PsiFileSystemItem> result = new ArrayList<>();
    PsiManager psiManager = PsiManager.getInstance(module.getProject());
    for (VirtualFile root : ManagingFS.getInstance().getLocalRoots()) {
        PsiDirectory directory = psiManager.findDirectory(root);
        if (directory != null) {
            result.add(directory);
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) PsiManager(com.intellij.psi.PsiManager) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with PsiManager

use of com.intellij.psi.PsiManager in project sonarlint-intellij by SonarSource.

the class SonarClearIssuesAction method findFiles.

public Collection<PsiFile> findFiles(Project project, VirtualFile[] files) {
    PsiManager psiManager = PsiManager.getInstance(project);
    List<PsiFile> psiFiles = new ArrayList<>(files.length);
    for (VirtualFile vFile : files) {
        if (!vFile.isValid()) {
            continue;
        }
        PsiFile psiFile = psiManager.findFile(vFile);
        if (psiFile != null) {
            psiFiles.add(psiFile);
        } else {
            LOGGER.warn("Couldn't find PSI for file: " + vFile.getPath());
        }
    }
    return psiFiles;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile)

Aggregations

PsiManager (com.intellij.psi.PsiManager)120 VirtualFile (com.intellij.openapi.vfs.VirtualFile)84 PsiFile (com.intellij.psi.PsiFile)61 PsiDirectory (com.intellij.psi.PsiDirectory)31 NotNull (org.jetbrains.annotations.NotNull)26 Project (com.intellij.openapi.project.Project)20 File (java.io.File)19 PsiElement (com.intellij.psi.PsiElement)18 ArrayList (java.util.ArrayList)18 Module (com.intellij.openapi.module.Module)17 Nullable (org.jetbrains.annotations.Nullable)16 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 XmlFile (com.intellij.psi.xml.XmlFile)11 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)9 PsiClass (com.intellij.psi.PsiClass)7 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)6 HashSet (java.util.HashSet)6 Nullable (javax.annotation.Nullable)5 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)4