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);
}
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);
}
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()) };
}
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;
}
}
});
}
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));
}
}
}
Aggregations