Search in sources :

Example 1 with GoPackageClause

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

the class GoCreateFileAction method postProcess.

@Override
protected void postProcess(PsiFile createdElement, String templateName, Map<String, String> customProperties) {
    if (createdElement instanceof GoFile) {
        GoPackageClause packageClause = ((GoFile) createdElement).getPackage();
        if (packageClause == null) {
            return;
        }
        Project project = createdElement.getProject();
        Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
        if (editor == null) {
            return;
        }
        VirtualFile virtualFile = createdElement.getContainingFile().getVirtualFile();
        if (virtualFile == null) {
            return;
        }
        if (FileDocumentManager.getInstance().getDocument(virtualFile) == editor.getDocument()) {
            editor.getCaretModel().moveToOffset(packageClause.getTextRange().getEndOffset());
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GoFile(com.goide.psi.GoFile) Project(com.intellij.openapi.project.Project) GoPackageClause(com.goide.psi.GoPackageClause) Editor(com.intellij.openapi.editor.Editor)

Example 2 with GoPackageClause

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

Example 3 with GoPackageClause

use of com.goide.psi.GoPackageClause 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 4 with GoPackageClause

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

the class GoMultiplePackagesInspection method checkFile.

@Override
protected void checkFile(@NotNull GoFile file, @NotNull ProblemsHolder problemsHolder) {
    if (((ScratchFileType) ScratchFileType.INSTANCE).isMyFileType(file.getVirtualFile()))
        return;
    GoPackageClause packageClause = file.getPackage();
    if (packageClause != null) {
        String packageName = file.getPackageName();
        if (packageName == null || packageName.equals(GoConstants.DOCUMENTATION))
            return;
        PsiDirectory dir = file.getContainingDirectory();
        Collection<String> packages = GoPackageUtil.getAllPackagesInDirectory(dir, null, true);
        packages.remove(GoConstants.DOCUMENTATION);
        if (packages.size() > 1) {
            Collection<LocalQuickFix> fixes = ContainerUtil.newArrayList();
            if (problemsHolder.isOnTheFly()) {
                fixes.add(new GoMultiplePackagesQuickFix(packageClause, packageName, packages, true));
            } else {
                for (String name : packages) {
                    fixes.add(new GoMultiplePackagesQuickFix(packageClause, name, packages, false));
                }
            }
            problemsHolder.registerProblem(packageClause, "Multiple packages in directory", fixes.toArray(new LocalQuickFix[fixes.size()]));
        }
    }
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) GoPackageClause(com.goide.psi.GoPackageClause) ScratchFileType(com.intellij.ide.scratch.ScratchFileType) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) GoMultiplePackagesQuickFix(com.goide.quickfix.GoMultiplePackagesQuickFix)

Aggregations

GoPackageClause (com.goide.psi.GoPackageClause)4 GoFile (com.goide.psi.GoFile)3 GoMultiplePackagesQuickFix (com.goide.quickfix.GoMultiplePackagesQuickFix)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ScratchFileType (com.intellij.ide.scratch.ScratchFileType)1 Editor (com.intellij.openapi.editor.Editor)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiFile (com.intellij.psi.PsiFile)1 AssertionErrorCase (com.intellij.testFramework.exceptionCases.AssertionErrorCase)1