Search in sources :

Example 1 with GoFile

use of com.goide.psi.GoFile in project intellij by bazelbuild.

the class BlazeGoGotoDeclarationHandler method getGotoDeclarationTargets.

@Nullable
@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
    if (sourceElement == null) {
        return null;
    }
    if (!Blaze.isBlazeProject(sourceElement.getProject()) || !BlazeGoSupport.blazeGoSupportEnabled.getValue()) {
        return null;
    }
    PsiFile sourcefile = sourceElement.getContainingFile();
    if (!(sourcefile instanceof GoFile)) {
        return null;
    }
    PsiElement targetElement = TargetElementUtil.getInstance().findTargetElement(editor, TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED, offset);
    if (targetElement instanceof PsiDirectory) {
        return resolveDirectory(sourceElement, (PsiDirectory) targetElement);
    } else {
        return resolveElement(targetElement);
    }
}
Also used : GoFile(com.goide.psi.GoFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

Example 2 with GoFile

use of com.goide.psi.GoFile in project intellij by bazelbuild.

the class BlazeGoBinaryConfigurationProducer method getMainFile.

@Nullable
private static PsiFile getMainFile(ConfigurationContext context) {
    PsiElement element = GoRunUtil.getContextElement(context);
    if (element == null) {
        return null;
    }
    PsiFile file = element.getContainingFile();
    if (file instanceof GoFile && GoRunUtil.isMainGoFile(file)) {
        return file;
    }
    return null;
}
Also used : GoFile(com.goide.psi.GoFile) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

Example 3 with GoFile

use of com.goide.psi.GoFile in project intellij by bazelbuild.

the class BlazeGoTestConfigurationProducer method testLocation.

@Nullable
private static TestLocation testLocation(ConfigurationContext context) {
    // Handled by SM runner.
    if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
        return null;
    }
    PsiElement element = GoRunUtil.getContextElement(context);
    if (element == null) {
        return null;
    }
    PsiFile file = element.getContainingFile();
    if (!(file instanceof GoFile) || !GoTestFinder.isTestFile(file)) {
        return null;
    }
    TargetInfo testTarget = TestTargetHeuristic.testTargetForPsiElement(element);
    return testTarget != null ? new TestLocation(testTarget, (GoFile) file, GoTestFinder.findTestFunctionInContext(element)) : null;
}
Also used : GoFile(com.goide.psi.GoFile) TargetInfo(com.google.idea.blaze.base.dependencies.TargetInfo) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

Example 4 with GoFile

use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoPathUseScope method contains.

@Override
public boolean contains(@NotNull VirtualFile referenceFile) {
    VirtualFile referenceDirectory = referenceFile.isDirectory() ? referenceFile : referenceFile.getParent();
    if (referenceDirectory == null) {
        return false;
    }
    VirtualFile declarationDirectory = myDeclarationFile.getParent();
    if (referenceDirectory.equals(declarationDirectory)) {
        return true;
    }
    Project project = ObjectUtils.assertNotNull(getProject());
    PsiManager psiManager = PsiManager.getInstance(project);
    PsiFile referencePsiFile = psiManager.findFile(referenceFile);
    Module module = referencePsiFile != null ? ModuleUtilCore.findModuleForPsiElement(referencePsiFile) : null;
    GoPathScopeHelper scopeHelper = GoPathScopeHelper.fromReferenceFile(project, module, referenceFile);
    if (!scopeHelper.couldBeReferenced(myDeclarationFile, referenceFile)) {
        return false;
    }
    if (!myFilterByImportList) {
        return true;
    }
    if (!(referencePsiFile instanceof GoFile)) {
        // it's some injection or cross-reference, so we cannot check its imports
        return true;
    }
    PsiFile declarationPsiFile = psiManager.findFile(myDeclarationFile);
    if (declarationPsiFile instanceof GoFile) {
        String importPath = ((GoFile) declarationPsiFile).getImportPath(scopeHelper.isVendoringEnabled());
        Map<String, GoImportSpec> importedPackagesMap = ((GoFile) referencePsiFile).getImportedPackagesMap();
        if (importedPackagesMap.containsKey(importPath)) {
            return true;
        }
        if (hasRelativeImportOfTargetPackage(importedPackagesMap.keySet(), referenceDirectory, declarationDirectory)) {
            return true;
        }
        for (GoFile packageFile : GoPackageUtil.getAllPackageFiles(referencePsiFile.getContainingDirectory(), null)) {
            if (packageFile != referencePsiFile && referencePsiFile.getOriginalFile() != packageFile) {
                Map<String, GoImportSpec> packagesMap = packageFile.getImportedPackagesMap();
                if (packagesMap.containsKey(importPath)) {
                    return true;
                }
                if (hasRelativeImportOfTargetPackage(packagesMap.keySet(), referenceDirectory, declarationDirectory)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GoFile(com.goide.psi.GoFile) GoImportSpec(com.goide.psi.GoImportSpec) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 5 with GoFile

use of com.goide.psi.GoFile 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)

Aggregations

GoFile (com.goide.psi.GoFile)31 PsiFile (com.intellij.psi.PsiFile)20 PsiElement (com.intellij.psi.PsiElement)12 Project (com.intellij.openapi.project.Project)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 PsiDirectory (com.intellij.psi.PsiDirectory)8 Module (com.intellij.openapi.module.Module)6 NotNull (org.jetbrains.annotations.NotNull)6 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)4 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)4 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)4 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)4 WorkspaceLanguageSettings (com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings)4 WorkspacePathResolverImpl (com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl)4 Nullable (javax.annotation.Nullable)4 Test (org.junit.Test)4 GoPackageClause (com.goide.psi.GoPackageClause)3 Nullable (org.jetbrains.annotations.Nullable)3 GoFunctionDeclaration (com.goide.psi.GoFunctionDeclaration)2 GoFunctionOrMethodDeclaration (com.goide.psi.GoFunctionOrMethodDeclaration)2