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