Search in sources :

Example 41 with PsiFile

use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoImportReferenceSet method computeDefaultContexts.

@NotNull
@Override
public Collection<PsiFileSystemItem> computeDefaultContexts() {
    PsiFile file = getContainingFile();
    if (file == null || !file.isValid() || isAbsolutePathReference()) {
        return Collections.emptyList();
    }
    PsiManager psiManager = file.getManager();
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    Project project = file.getProject();
    LinkedHashSet<VirtualFile> sourceRoots = GoVendoringUtil.isVendoringEnabled(module) ? GoSdkUtil.getVendoringAwareSourcesPathsToLookup(project, module, file.getVirtualFile()) : GoSdkUtil.getSourcesPathsToLookup(project, module);
    return ContainerUtil.mapNotNull(sourceRoots, psiManager::findDirectory);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with PsiFile

use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoTestFinder method findTestsForClass.

@NotNull
@Override
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
    PsiFile file = InjectedLanguageUtil.getTopLevelFile(element);
    if (file instanceof GoFile) {
        PsiDirectory directory = file.getContainingDirectory();
        PsiFile testFile = directory.findFile(FileUtil.getNameWithoutExtension(file.getName()) + GoConstants.TEST_SUFFIX_WITH_EXTENSION);
        if (testFile != null) {
            return ContainerUtil.newSmartList(testFile);
        }
    }
    return Collections.emptyList();
}
Also used : GoFile(com.goide.psi.GoFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 43 with PsiFile

use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoTestRunConfiguration method checkConfiguration.

@Override
public void checkConfiguration() throws RuntimeConfigurationException {
    super.checkConfiguration();
    GoModuleBasedConfiguration configurationModule = getConfigurationModule();
    if (!myTestFramework.isAvailable(configurationModule.getModule())) {
        throw new RuntimeConfigurationError("Framework `" + myTestFramework.getName() + "` is not available in selected module");
    }
    switch(myKind) {
        case DIRECTORY:
            String directoryPath = FileUtil.isAbsolutePlatformIndependent(myDirectoryPath) ? myDirectoryPath : FileUtil.join(getWorkingDirectory(), myDirectoryPath);
            if (!FileUtil.isAncestor(getWorkingDirectory(), directoryPath, false)) {
                throw new RuntimeConfigurationError("Working directory should be ancestor of testing directory");
            }
            VirtualFile testingDirectory = LocalFileSystem.getInstance().findFileByPath(directoryPath);
            if (testingDirectory == null) {
                throw new RuntimeConfigurationError("Testing directory doesn't exist");
            }
            break;
        case PACKAGE:
            Module module = configurationModule.getModule();
            assert module != null;
            VirtualFile packageDirectory = GoPackageUtil.findByImportPath(myPackage, module.getProject(), module);
            if (packageDirectory == null || !packageDirectory.isDirectory()) {
                throw new RuntimeConfigurationError("Cannot find package '" + myPackage + "'");
            }
            for (VirtualFile file : packageDirectory.getChildren()) {
                PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file);
                if (psiFile != null && myTestFramework.isAvailableOnFile(psiFile)) {
                    return;
                }
            }
            String message = "Cannot find Go test files in '" + myPackage + "' compatible with `" + myTestFramework.getName() + "` framework";
            throw new RuntimeConfigurationError(message);
        case FILE:
            VirtualFile virtualFile = findFile(getFilePath());
            if (virtualFile == null) {
                throw new RuntimeConfigurationError("Test file doesn't exist");
            }
            PsiFile file = PsiManager.getInstance(getProject()).findFile(virtualFile);
            if (file == null || !myTestFramework.isAvailableOnFile(file)) {
                message = "Framework `" + myTestFramework.getName() + "` is not available on file `" + myFilePath + "`";
                throw new RuntimeConfigurationError(message);
            }
            break;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) GoModuleBasedConfiguration(com.goide.runconfig.GoModuleBasedConfiguration) Module(com.intellij.openapi.module.Module)

Example 44 with PsiFile

use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoMultiplePackagesQuickFix method renamePackagesInDirectory.

private static void renamePackagesInDirectory(@NotNull Project project, @NotNull PsiDirectory dir, @NotNull String newName) {
    WriteCommandAction.runWriteCommandAction(project, () -> {
        Module module = ModuleUtilCore.findModuleForPsiElement(dir);
        for (PsiFile file : dir.getFiles()) {
            if (file instanceof GoFile && GoPsiImplUtil.allowed(file, null, module)) {
                GoPackageClause packageClause = ((GoFile) file).getPackage();
                String oldName = ((GoFile) file).getPackageName();
                if (packageClause != null && oldName != null) {
                    String fullName = GoTestFinder.isTestFile(file) && StringUtil.endsWith(oldName, GoConstants.TEST_SUFFIX) ? newName + GoConstants.TEST_SUFFIX : newName;
                    packageClause.replace(GoElementFactory.createPackageClause(project, fullName));
                }
            }
        }
    });
}
Also used : GoFile(com.goide.psi.GoFile) GoPackageClause(com.goide.psi.GoPackageClause) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 45 with PsiFile

use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoGenerateTestMethodActionGroup method getChildren.

@NotNull
@Override
public AnAction[] getChildren(@Nullable AnActionEvent e) {
    if (e == null) {
        return AnAction.EMPTY_ARRAY;
    }
    Project project = e.getProject();
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    if (project == null || editor == null)
        return AnAction.EMPTY_ARRAY;
    PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
    ArrayList<AnAction> children = ContainerUtil.newArrayList();
    for (GoTestFramework framework : GoTestFramework.all()) {
        if (framework.isAvailableOnFile(file)) {
            children.addAll(framework.getGenerateMethodActions());
        }
    }
    return !children.isEmpty() ? children.toArray(new AnAction[children.size()]) : AnAction.EMPTY_ARRAY;
}
Also used : Project(com.intellij.openapi.project.Project) GoTestFramework(com.goide.runconfig.testing.GoTestFramework) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) AnAction(com.intellij.openapi.actionSystem.AnAction) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiFile (com.intellij.psi.PsiFile)1785 VirtualFile (com.intellij.openapi.vfs.VirtualFile)496 PsiElement (com.intellij.psi.PsiElement)468 Project (com.intellij.openapi.project.Project)267 Nullable (org.jetbrains.annotations.Nullable)267 NotNull (org.jetbrains.annotations.NotNull)248 Document (com.intellij.openapi.editor.Document)181 Editor (com.intellij.openapi.editor.Editor)168 XmlFile (com.intellij.psi.xml.XmlFile)126 PsiDirectory (com.intellij.psi.PsiDirectory)114 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)109 Module (com.intellij.openapi.module.Module)108 TextRange (com.intellij.openapi.util.TextRange)88 ArrayList (java.util.ArrayList)84 XmlTag (com.intellij.psi.xml.XmlTag)68 File (java.io.File)58 PsiManager (com.intellij.psi.PsiManager)56 PsiClass (com.intellij.psi.PsiClass)51 List (java.util.List)46 Language (com.intellij.lang.Language)45