use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoImportReferenceSet method computeDefaultContexts.
@NotNull
@Override
public Collection<PsiFileSystemItem> computeDefaultContexts() {
PsiFile file = getContainingFile();
if (file == null || !file.isValid() || isAbsolutePathReference()) {
return Collections.emptyList();
}
PsiManager psiManager = file.getManager();
Module module = ModuleUtilCore.findModuleForPsiElement(file);
Project project = file.getProject();
LinkedHashSet<VirtualFile> sourceRoots = GoVendoringUtil.isVendoringEnabled(module) ? GoSdkUtil.getVendoringAwareSourcesPathsToLookup(project, module, file.getVirtualFile()) : GoSdkUtil.getSourcesPathsToLookup(project, module);
return ContainerUtil.mapNotNull(sourceRoots, psiManager::findDirectory);
}
use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoTestFinder method findTestsForClass.
@NotNull
@Override
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
PsiFile file = InjectedLanguageUtil.getTopLevelFile(element);
if (file instanceof GoFile) {
PsiDirectory directory = file.getContainingDirectory();
PsiFile testFile = directory.findFile(FileUtil.getNameWithoutExtension(file.getName()) + GoConstants.TEST_SUFFIX_WITH_EXTENSION);
if (testFile != null) {
return ContainerUtil.newSmartList(testFile);
}
}
return Collections.emptyList();
}
use of com.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoTestRunConfiguration method checkConfiguration.
@Override
public void checkConfiguration() throws RuntimeConfigurationException {
super.checkConfiguration();
GoModuleBasedConfiguration configurationModule = getConfigurationModule();
if (!myTestFramework.isAvailable(configurationModule.getModule())) {
throw new RuntimeConfigurationError("Framework `" + myTestFramework.getName() + "` is not available in selected module");
}
switch(myKind) {
case DIRECTORY:
String directoryPath = FileUtil.isAbsolutePlatformIndependent(myDirectoryPath) ? myDirectoryPath : FileUtil.join(getWorkingDirectory(), myDirectoryPath);
if (!FileUtil.isAncestor(getWorkingDirectory(), directoryPath, false)) {
throw new RuntimeConfigurationError("Working directory should be ancestor of testing directory");
}
VirtualFile testingDirectory = LocalFileSystem.getInstance().findFileByPath(directoryPath);
if (testingDirectory == null) {
throw new RuntimeConfigurationError("Testing directory doesn't exist");
}
break;
case PACKAGE:
Module module = configurationModule.getModule();
assert module != null;
VirtualFile packageDirectory = GoPackageUtil.findByImportPath(myPackage, module.getProject(), module);
if (packageDirectory == null || !packageDirectory.isDirectory()) {
throw new RuntimeConfigurationError("Cannot find package '" + myPackage + "'");
}
for (VirtualFile file : packageDirectory.getChildren()) {
PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file);
if (psiFile != null && myTestFramework.isAvailableOnFile(psiFile)) {
return;
}
}
String message = "Cannot find Go test files in '" + myPackage + "' compatible with `" + myTestFramework.getName() + "` framework";
throw new RuntimeConfigurationError(message);
case FILE:
VirtualFile virtualFile = findFile(getFilePath());
if (virtualFile == null) {
throw new RuntimeConfigurationError("Test file doesn't exist");
}
PsiFile file = PsiManager.getInstance(getProject()).findFile(virtualFile);
if (file == null || !myTestFramework.isAvailableOnFile(file)) {
message = "Framework `" + myTestFramework.getName() + "` is not available on file `" + myFilePath + "`";
throw new RuntimeConfigurationError(message);
}
break;
}
}
use of com.intellij.psi.PsiFile 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.intellij.psi.PsiFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoGenerateTestMethodActionGroup method getChildren.
@NotNull
@Override
public AnAction[] getChildren(@Nullable AnActionEvent e) {
if (e == null) {
return AnAction.EMPTY_ARRAY;
}
Project project = e.getProject();
Editor editor = e.getData(CommonDataKeys.EDITOR);
if (project == null || editor == null)
return AnAction.EMPTY_ARRAY;
PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
ArrayList<AnAction> children = ContainerUtil.newArrayList();
for (GoTestFramework framework : GoTestFramework.all()) {
if (framework.isAvailableOnFile(file)) {
children.addAll(framework.getGenerateMethodActions());
}
}
return !children.isEmpty() ? children.toArray(new AnAction[children.size()]) : AnAction.EMPTY_ARRAY;
}
Aggregations