Search in sources :

Example 16 with GoFile

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

the class BlazeGoTestEventsHandlerTest method testFunctionLocationResolves.

@Test
public void testFunctionLocationResolves() {
    TargetMap targetMap = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setLabel("//foo/bar:foo_test").setKind("go_test").setBuildFile(src("foo/bar/BUILD")).addSource(src("foo/bar/foo_test.go")).setGoInfo(GoIdeInfo.builder().addSources(ImmutableList.of(src("foo/bar/foo_test.go"))).setImportPath("google3/foo/bar/foo"))).build();
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(new BlazeProjectData(0L, targetMap, null, null, new WorkspacePathResolverImpl(workspaceRoot), location -> workspaceRoot.fileForPath(new WorkspacePath(location.getRelativePath())), new WorkspaceLanguageSettings(WorkspaceType.GO, ImmutableSet.of(LanguageClass.GO)), null, null)));
    GoFile goFile = (GoFile) workspace.createPsiFile(new WorkspacePath("foo/bar/foo_test.go"), "package foo", "import \"testing\"", "func TestFoo(t *testing.T) {}");
    workspace.createFile(new WorkspacePath("foo/bar/BUILD"), "go_test(", "    name = 'foo_test',", "    srcs = ['foo_test.go'],", ")");
    GoFunctionDeclaration function = PsiUtils.findFirstChildOfClassRecursive(goFile, GoFunctionDeclaration.class);
    assertThat(function).isNotNull();
    String url = handler.testLocationUrl(null, "foo/bar/foo_test", "TestFoo", null);
    Location<?> location = getLocation(url);
    assertThat(location).isNotNull();
    assertThat(location.getPsiElement()).isEqualTo(function);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) GoFile(com.goide.psi.GoFile) GoFunctionDeclaration(com.goide.psi.GoFunctionDeclaration) WorkspacePathResolverImpl(com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) WorkspaceLanguageSettings(com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) Test(org.junit.Test)

Example 17 with GoFile

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

the class BlazeGoTestEventsHandlerTest method testSuiteLocationResolvesToSingleSourceFile.

@Test
public void testSuiteLocationResolvesToSingleSourceFile() {
    TargetMap targetMap = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setLabel("//foo/bar:foo_test").setKind("go_test").setBuildFile(src("foo/bar/BUILD")).addSource(src("foo/bar/foo_test.go")).setGoInfo(GoIdeInfo.builder().addSources(ImmutableList.of(src("foo/bar/foo_test.go"))).setImportPath("google3/foo/bar/foo"))).build();
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(new BlazeProjectData(0L, targetMap, null, null, new WorkspacePathResolverImpl(workspaceRoot), location -> workspaceRoot.fileForPath(new WorkspacePath(location.getRelativePath())), new WorkspaceLanguageSettings(WorkspaceType.GO, ImmutableSet.of(LanguageClass.GO)), null, null)));
    GoFile goFile = (GoFile) workspace.createPsiFile(new WorkspacePath("foo/bar/foo_test.go"), "package foo", "import \"testing\"", "func TestFoo(t *testing.T) {}");
    workspace.createFile(new WorkspacePath("foo/bar/BUILD"), "go_test(", "    name = 'foo_test',", "    srcs = ['foo_test.go'],", ")");
    String url = handler.suiteLocationUrl(null, "foo/bar/foo_test");
    Location<?> location = getLocation(url);
    assertThat(location).isNotNull();
    assertThat(location.getPsiElement()).isEqualTo(goFile);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) GoFile(com.goide.psi.GoFile) WorkspacePathResolverImpl(com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) WorkspaceLanguageSettings(com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) Test(org.junit.Test)

Example 18 with GoFile

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

the class BlazeGoGotoDeclarationHandler method resolveElement.

@Nullable
private static PsiElement[] resolveElement(@Nullable PsiElement element) {
    if (element == null) {
        return null;
    }
    PsiFile targetPsiFile = element.getContainingFile();
    if (!(targetPsiFile instanceof GoFile)) {
        return null;
    }
    LocalFileSystem lfs = VirtualFileSystemProvider.getInstance().getSystem();
    FileOperationProvider provider = FileOperationProvider.getInstance();
    VirtualFile targetVirtualFile = targetPsiFile.getVirtualFile();
    File targetFile = VfsUtil.virtualToIoFile(targetVirtualFile);
    if (!provider.isSymbolicLink(targetFile)) {
        return null;
    }
    VirtualFile resolved;
    try {
        // Resolve only one layer of symlink.
        File resolvedFile = provider.readSymbolicLink(targetFile);
        resolved = lfs.findFileByIoFile(resolvedFile);
    } catch (IOException e) {
        logger.error(e);
        return null;
    }
    if (resolved == null) {
        return null;
    }
    PsiFile resolvedFile = PsiManager.getInstance(element.getProject()).findFile(resolved);
    if (!(resolvedFile instanceof GoFile)) {
        return null;
    }
    PsiElement foundElement = resolvedFile.findElementAt(element.getTextOffset());
    return new PsiElement[] { PsiTreeUtil.getParentOfType(foundElement, element.getClass()) };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GoFile(com.goide.psi.GoFile) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) GoFile(com.goide.psi.GoFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement) Nullable(javax.annotation.Nullable)

Example 19 with GoFile

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

the class GoPackageClauseStubTest method testParsingPsi.

public void testParsingPsi() throws Throwable {
    GoFile file = (GoFile) myFixture.addFileToProject("bar/bar.go", "package bar; import `foo`; func _() { println(CONST_NAME) }");
    failOnFileLoading();
    GoPackageClause packageClause = file.getPackage();
    assertNotNull(packageClause);
    assertException(new AssertionErrorCase() {

        @Override
        public void tryClosure() {
            try {
                packageClause.getIdentifier();
            } catch (AssertionError e) {
                String message = e.getMessage();
                assertTrue(message.contains("Access to tree elements not allowed in tests"));
                assertTrue(message.contains("bar.go"));
                throw e;
            }
        }
    });
}
Also used : GoFile(com.goide.psi.GoFile) AssertionErrorCase(com.intellij.testFramework.exceptionCases.AssertionErrorCase) GoPackageClause(com.goide.psi.GoPackageClause)

Example 20 with GoFile

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

the class GoImportPathsCompletionProvider method addCompletions.

public static void addCompletions(@NotNull CompletionResultSet result, @NotNull Module module, @Nullable PsiElement context, @NotNull GlobalSearchScope scope, boolean allowMain) {
    Project project = module.getProject();
    boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(module);
    String contextImportPath = GoCompletionUtil.getContextImportPath(context, vendoringEnabled);
    GoExcludedPathsSettings excludedSettings = GoExcludedPathsSettings.getInstance(project);
    PsiFile contextFile = context != null ? context.getContainingFile() : null;
    boolean testFileWithTestPackage = GoTestFinder.isTestFileWithTestPackage(contextFile);
    for (VirtualFile file : FileTypeIndex.getFiles(GoFileType.INSTANCE, scope)) {
        ProgressManager.checkCanceled();
        PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
        if (!(psiFile instanceof GoFile))
            continue;
        PsiDirectory directory = psiFile.getContainingDirectory();
        if (directory == null)
            continue;
        GoFile goFile = (GoFile) psiFile;
        if (!GoPsiImplUtil.canBeAutoImported(goFile, allowMain, module))
            continue;
        String importPath = goFile.getImportPath(vendoringEnabled);
        if (StringUtil.isNotEmpty(importPath) && !excludedSettings.isExcluded(importPath) && (testFileWithTestPackage || !importPath.equals(contextImportPath))) {
            result.addElement(GoCompletionUtil.createPackageLookupElement(importPath, contextImportPath, directory, false));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GoFile(com.goide.psi.GoFile) PsiDirectory(com.intellij.psi.PsiDirectory) GoExcludedPathsSettings(com.goide.project.GoExcludedPathsSettings) PsiFile(com.intellij.psi.PsiFile) GoImportString(com.goide.psi.GoImportString)

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