use of com.goide.psi.GoFile in project intellij by bazelbuild.
the class BlazeGoGotoDeclarationHandler method getGotoDeclarationTargets.
@Nullable
@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
if (sourceElement == null) {
return null;
}
if (!Blaze.isBlazeProject(sourceElement.getProject()) || !BlazeGoSupport.blazeGoSupportEnabled.getValue()) {
return null;
}
PsiFile sourcefile = sourceElement.getContainingFile();
if (!(sourcefile instanceof GoFile)) {
return null;
}
PsiElement targetElement = TargetElementUtil.getInstance().findTargetElement(editor, TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED, offset);
if (targetElement instanceof PsiDirectory) {
return resolveDirectory(sourceElement, (PsiDirectory) targetElement);
} else {
return resolveElement(targetElement);
}
}
use of com.goide.psi.GoFile in project intellij by bazelbuild.
the class BlazeGoBinaryConfigurationProducer method getMainFile.
@Nullable
private static PsiFile getMainFile(ConfigurationContext context) {
PsiElement element = GoRunUtil.getContextElement(context);
if (element == null) {
return null;
}
PsiFile file = element.getContainingFile();
if (file instanceof GoFile && GoRunUtil.isMainGoFile(file)) {
return file;
}
return null;
}
use of com.goide.psi.GoFile in project intellij by bazelbuild.
the class BlazeGoTestConfigurationProducer method testLocation.
@Nullable
private static TestLocation testLocation(ConfigurationContext context) {
// Handled by SM runner.
if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
return null;
}
PsiElement element = GoRunUtil.getContextElement(context);
if (element == null) {
return null;
}
PsiFile file = element.getContainingFile();
if (!(file instanceof GoFile) || !GoTestFinder.isTestFile(file)) {
return null;
}
TargetInfo testTarget = TestTargetHeuristic.testTargetForPsiElement(element);
return testTarget != null ? new TestLocation(testTarget, (GoFile) file, GoTestFinder.findTestFunctionInContext(element)) : null;
}
use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoPathUseScope method contains.
@Override
public boolean contains(@NotNull VirtualFile referenceFile) {
VirtualFile referenceDirectory = referenceFile.isDirectory() ? referenceFile : referenceFile.getParent();
if (referenceDirectory == null) {
return false;
}
VirtualFile declarationDirectory = myDeclarationFile.getParent();
if (referenceDirectory.equals(declarationDirectory)) {
return true;
}
Project project = ObjectUtils.assertNotNull(getProject());
PsiManager psiManager = PsiManager.getInstance(project);
PsiFile referencePsiFile = psiManager.findFile(referenceFile);
Module module = referencePsiFile != null ? ModuleUtilCore.findModuleForPsiElement(referencePsiFile) : null;
GoPathScopeHelper scopeHelper = GoPathScopeHelper.fromReferenceFile(project, module, referenceFile);
if (!scopeHelper.couldBeReferenced(myDeclarationFile, referenceFile)) {
return false;
}
if (!myFilterByImportList) {
return true;
}
if (!(referencePsiFile instanceof GoFile)) {
// it's some injection or cross-reference, so we cannot check its imports
return true;
}
PsiFile declarationPsiFile = psiManager.findFile(myDeclarationFile);
if (declarationPsiFile instanceof GoFile) {
String importPath = ((GoFile) declarationPsiFile).getImportPath(scopeHelper.isVendoringEnabled());
Map<String, GoImportSpec> importedPackagesMap = ((GoFile) referencePsiFile).getImportedPackagesMap();
if (importedPackagesMap.containsKey(importPath)) {
return true;
}
if (hasRelativeImportOfTargetPackage(importedPackagesMap.keySet(), referenceDirectory, declarationDirectory)) {
return true;
}
for (GoFile packageFile : GoPackageUtil.getAllPackageFiles(referencePsiFile.getContainingDirectory(), null)) {
if (packageFile != referencePsiFile && referencePsiFile.getOriginalFile() != packageFile) {
Map<String, GoImportSpec> packagesMap = packageFile.getImportedPackagesMap();
if (packagesMap.containsKey(importPath)) {
return true;
}
if (hasRelativeImportOfTargetPackage(packagesMap.keySet(), referenceDirectory, declarationDirectory)) {
return true;
}
}
}
}
return false;
}
use of com.goide.psi.GoFile 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));
}
}
}
});
}
Aggregations