use of com.goide.psi.GoTypeSpec in project go-lang-idea-plugin by go-lang-plugin-org.
the class DlvXValue method computeTypeSourcePosition.
@Override
public void computeTypeSourcePosition(@NotNull XNavigatable navigatable) {
readActionInPooledThread(() -> {
boolean isStructure = myVariable.isStructure();
boolean isPtr = myVariable.isPtr();
if (!isStructure && !isPtr)
return;
Project project = getProject();
if (project == null)
return;
String dlvType = myVariable.type;
String fqn = dlvType.replaceFirst(isPtr ? "\\*struct " : "struct ", "");
List<String> split = StringUtil.split(fqn, ".");
boolean noFqn = split.size() == 1;
if (split.size() == 2 || noFqn) {
String name = ContainerUtil.getLastItem(split);
assert name != null;
Collection<GoTypeSpec> types = GoTypesIndex.find(name, project, GlobalSearchScope.allScope(project), null);
for (GoTypeSpec type : types) {
if (noFqn || Comparing.equal(fqn, type.getQualifiedName())) {
navigatable.setSourcePosition(XDebuggerUtil.getInstance().createPositionByOffset(type.getContainingFile().getVirtualFile(), type.getTextOffset()));
return;
}
}
}
});
}
use of com.goide.psi.GoTypeSpec in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoTestLocator method getLocation.
@NotNull
@Override
public List<Location> getLocation(@NotNull String protocolId, @NotNull String path, @NotNull Project project, @NotNull GlobalSearchScope scope) {
if (PROTOCOL.equals(protocolId)) {
IdFilter idFilter = GoIdFilter.getTestsFilter(project);
List<String> locationDataItems = StringUtil.split(path, ".");
// Location is a function name, e.g. `TestCheckItOut`
if (locationDataItems.size() == 1) {
return ContainerUtil.mapNotNull(GoFunctionIndex.find(path, project, scope, idFilter), function -> PsiLocation.fromPsiElement(project, function));
}
// Location is a method name, e.g. `FooSuite.TestCheckItOut`
if (locationDataItems.size() == 2) {
List<Location> locations = ContainerUtil.newArrayList();
for (GoTypeSpec typeSpec : GoTypesIndex.find(locationDataItems.get(0), project, scope, idFilter)) {
for (GoMethodDeclaration method : typeSpec.getMethods()) {
if (locationDataItems.get(1).equals(method.getName())) {
ContainerUtil.addIfNotNull(locations, PsiLocation.fromPsiElement(method));
}
}
}
return locations;
}
} else if (SUITE_PROTOCOL.equals(protocolId)) {
IdFilter idFilter = GoIdFilter.getTestsFilter(project);
return ContainerUtil.mapNotNull(GoTypesIndex.find(path, project, scope, idFilter), spec -> PsiLocation.fromPsiElement(project, spec));
} else {
return Collections.emptyList();
}
throw new RuntimeException("Unsupported location: " + path);
}
use of com.goide.psi.GoTypeSpec in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoCreateWrapperTypeQuickFix method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
if (editor == null) {
LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile()));
return;
}
if (!(startElement instanceof GoType))
return;
GoType type = (GoType) startElement;
PsiElement anchor = PsiTreeUtil.findPrevParent(file, type);
String name = "TypeName";
GoTypeDeclaration decl = (GoTypeDeclaration) file.addBefore(GoElementFactory.createTypeDeclaration(project, name, type), anchor);
if (decl == null)
return;
decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
if (decl == null)
return;
GoTypeSpec spec = ContainerUtil.getFirstItem(decl.getTypeSpecList());
if (spec == null)
return;
TemplateBuilderImpl builder = new TemplateBuilderImpl(file);
builder.replaceElement(type, OTHER_NAME, INPUT_NAME, false);
builder.replaceElement(spec.getIdentifier(), INPUT_NAME, new ConstantNode(name), true);
editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset());
Template template = builder.buildInlineTemplate();
editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset());
TemplateManager.getInstance(project).startTemplate(editor, template);
}
use of com.goide.psi.GoTypeSpec in project intellij by bazelbuild.
the class BlazeGoGotoDeclarationHandlerTest method testResolveGoDirectories.
@Test
public void testResolveGoDirectories() {
TargetMap targetMap = TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setBuildFile(src("foo/bar/BUILD")).setLabel("//foo/bar:binary").setKind("go_binary").addSource(src("foo/bar/binary.go")).addDependency("//one/two:library").setGoInfo(GoIdeInfo.builder().addSources(ImmutableList.of(src("foo/bar/binary.go"))).setImportPath("prefix/foo/bar/binary"))).addTarget(TargetIdeInfo.builder().setBuildFile(src("one/two/BUILD")).setLabel("//one/two:library").setKind("go_library").addSource(src("one/two/library.go")).setGoInfo(GoIdeInfo.builder().addSources(ImmutableList.of(src("one/two/library.go"))).setImportPath("prefix/one/two/library"))).build();
BlazeProjectData projectData = 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);
registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(projectData));
GoFile fooBarBinary = (GoFile) workspace.createPsiFile(new WorkspacePath("foo/bar/binary.go"), "package main", "import \"p<caret>refix/one<caret>/<caret>two/lib<caret>rary\"", "func foo(a library.One<caret>Two) {}", "func main() {}");
GoFile oneTwoLibrary = (GoFile) workspace.createPsiFile(new WorkspacePath("one/two/library.go"), "package library", "type OneTwo struct {}");
GoTypeSpec oneTwoStruct = PsiTreeUtil.findChildOfType(oneTwoLibrary, GoTypeSpec.class);
PsiDirectory oneTwoDirectory = oneTwoLibrary.getParent();
assertThat(oneTwoDirectory).isNotNull();
PsiDirectory oneDirectory = oneTwoDirectory.getParent();
assertThat(oneDirectory).isNotNull();
BuildFile oneTwoBUILD = (BuildFile) workspace.createPsiFile(new WorkspacePath("one/two/BUILD"), "go_library(", " name = 'library',", " srcs = ['library.go'],", ")");
FuncallExpression oneTwoLibraryRule = PsiUtils.findFirstChildOfClassRecursive(oneTwoBUILD, FuncallExpression.class);
BlazeGoRootsProvider.createGoPathSourceRoot(getProject(), projectData);
testFixture.configureFromExistingVirtualFile(fooBarBinary.getVirtualFile());
List<Caret> carets = testFixture.getEditor().getCaretModel().getAllCarets();
assertThat(carets).hasSize(5);
PsiElement gotoPrefix = GotoDeclarationAction.findTargetElement(getProject(), testFixture.getEditor(), carets.get(0).getOffset());
assertThat(gotoPrefix).isEqualTo(oneTwoLibraryRule);
PsiElement gotoOne = GotoDeclarationAction.findTargetElement(getProject(), testFixture.getEditor(), carets.get(1).getOffset());
assertThat(gotoOne).isEqualTo(oneDirectory);
PsiElement gotoTwo = GotoDeclarationAction.findTargetElement(getProject(), testFixture.getEditor(), carets.get(2).getOffset());
assertThat(gotoTwo).isEqualTo(oneTwoDirectory);
PsiElement gotoLibrary = GotoDeclarationAction.findTargetElement(getProject(), testFixture.getEditor(), carets.get(3).getOffset());
assertThat(gotoLibrary).isEqualTo(oneTwoLibraryRule);
PsiElement gotoOneTwoType = GotoDeclarationAction.findTargetElement(getProject(), testFixture.getEditor(), carets.get(4).getOffset());
assertThat(gotoOneTwoType).isEqualTo(oneTwoStruct);
}
Aggregations