Search in sources :

Example 1 with GoTypeSpec

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;
                }
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) GoTypeSpec(com.goide.psi.GoTypeSpec)

Example 2 with GoTypeSpec

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);
}
Also used : GoFunctionIndex(com.goide.stubs.index.GoFunctionIndex) GoTypeSpec(com.goide.psi.GoTypeSpec) GoTypesIndex(com.goide.stubs.index.GoTypesIndex) StringUtil(com.intellij.openapi.util.text.StringUtil) GoMethodDeclaration(com.goide.psi.GoMethodDeclaration) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) IdFilter(com.intellij.util.indexing.IdFilter) ContainerUtil(com.intellij.util.containers.ContainerUtil) PsiLocation(com.intellij.execution.PsiLocation) SMTestLocator(com.intellij.execution.testframework.sm.runner.SMTestLocator) GoIdFilter(com.goide.stubs.index.GoIdFilter) List(java.util.List) Project(com.intellij.openapi.project.Project) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) IdFilter(com.intellij.util.indexing.IdFilter) GoIdFilter(com.goide.stubs.index.GoIdFilter) GoMethodDeclaration(com.goide.psi.GoMethodDeclaration) GoTypeSpec(com.goide.psi.GoTypeSpec) PsiLocation(com.intellij.execution.PsiLocation) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GoTypeSpec

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);
}
Also used : GoTypeDeclaration(com.goide.psi.GoTypeDeclaration) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode) GoTypeSpec(com.goide.psi.GoTypeSpec) GoType(com.goide.psi.GoType) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) Template(com.intellij.codeInsight.template.Template)

Example 4 with GoTypeSpec

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);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) GoFile(com.goide.psi.GoFile) GoTypeSpec(com.goide.psi.GoTypeSpec) WorkspacePathResolverImpl(com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl) PsiDirectory(com.intellij.psi.PsiDirectory) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) WorkspaceLanguageSettings(com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) Caret(com.intellij.openapi.editor.Caret) PsiElement(com.intellij.psi.PsiElement) Test(org.junit.Test)

Aggregations

GoTypeSpec (com.goide.psi.GoTypeSpec)4 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 GoFile (com.goide.psi.GoFile)1 GoMethodDeclaration (com.goide.psi.GoMethodDeclaration)1 GoType (com.goide.psi.GoType)1 GoTypeDeclaration (com.goide.psi.GoTypeDeclaration)1 GoFunctionIndex (com.goide.stubs.index.GoFunctionIndex)1 GoIdFilter (com.goide.stubs.index.GoIdFilter)1 GoTypesIndex (com.goide.stubs.index.GoTypesIndex)1 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)1 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)1 FuncallExpression (com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)1 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)1 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)1 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)1 WorkspaceLanguageSettings (com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings)1 WorkspacePathResolverImpl (com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl)1 Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1