@Nullable public static PsiFile findMainFileInDirectory(@NotNull VirtualFile packageDirectory, @NotNull Project project) { for (VirtualFile file : packageDirectory.getChildren()) { if (file == null) { continue; } PsiFile psiFile = PsiManager.getInstance(project).findFile(file); if (isMainGoFile(psiFile)) { return psiFile; } } return null; }
@Nullable public static PsiFile getFile(@NotNull Editor editor) { VirtualFile vf = EditorData.getVirtualFile(editor); if (vf != null) { Project proj = editor.getProject(); if (proj != null) { PsiManager mgr = PsiManager.getInstance(proj); return mgr.findFile(vf); } } return null; } }
@Nullable public static PsiFile findServiceFileInDirectory(@NotNull VirtualFile packageDirectory, @NotNull Project project) { for (VirtualFile file : packageDirectory.getChildren()) { if (file == null) { continue; } PsiFile psiFile = PsiManager.getInstance(project).findFile(file); if (hasServices(psiFile)) { return psiFile; } } return null; }
@Nullable public static PsiFile findMainFileInDirectory(@NotNull VirtualFile packageDirectory, @NotNull Project project) { for (VirtualFile file : packageDirectory.getChildren()) { if (file == null) { continue; } PsiFile psiFile = PsiManager.getInstance(project).findFile(file); if (isMainBallerinaFile(psiFile)) { return psiFile; } } return null; }
/** * Find the specified file in the project and returns the corresponding {@link PsiFile}. * * @param project a project * @param path file path * @return corresponding psi file */ @Nullable public static PsiFile findFileInProject(@NotNull Project project, @NotNull String path) { VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path); if (virtualFile == null) { return null; } return PsiManager.getInstance(project).findFile(virtualFile); }
public static void installBallerinaWithMainFileChooser(Project project, @NotNull TextFieldWithBrowseButton fileField) { installFileChooser(project, fileField, file -> isMainBallerinaFile(PsiManager.getInstance(project).findFile(file))); }
@NotNull private List<PsiFile> getPsiFiles() { Collection<VirtualFile> files = panel.getVirtualFiles(); List<PsiFile> psiFiles = ContainerUtil.newArrayList(); PsiManager manager = PsiManager.getInstance(panel.getProject()); for (VirtualFile file : files) { PsiFile psiFile = manager.findFile(file); if (psiFile instanceof GoFile) { psiFiles.add(psiFile); } } return psiFiles; } };
public static void installGoWithMainFileChooser(Project project, @NotNull TextFieldWithBrowseButton fileField) { installFileChooser(project, fileField, false, false, file -> { if (file.getFileType() != GoFileType.INSTANCE) { return false; } return isMainGoFile(PsiManager.getInstance(project).findFile(file)); }); }
/** * Finds {@link PsiFile} for the given {@link VirtualFile} instance. If file is outside current project, * it's required to create new {@link PsiFile} manually. * * @param project current project * @param virtualFile to handle * @return {@link PsiFile} instance */ @Nullable public static PsiFile getPsiFile(@NotNull Project project, @NotNull VirtualFile virtualFile) { PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile); if (psiFile == null) { FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(virtualFile); if (viewProvider != null) { IgnoreLanguage language = IgnoreBundle.obtainLanguage(virtualFile); if (language != null) { psiFile = language.createFile(viewProvider); } } } return psiFile; }
public static void installBallerinTestFileChooser(Project project, @NotNull TextFieldWithBrowseButton fileField) { installFileChooser(project, fileField, file -> isBallerinaTestFile(PsiManager.getInstance(project).findFile(file))); }
@Override public boolean contains(@NotNull VirtualFile file) { if (myParent.equals(file.getParent())) { if (myAllowedPackageInExcludedDirectory == null) { return false; } Project project = getProject(); PsiFile psiFile = project != null ? PsiManager.getInstance(project).findFile(file) : null; if (!(psiFile instanceof GoFile)) { return false; } if (!myAllowedPackageInExcludedDirectory.equals(((GoFile)psiFile).getPackageName())) { return false; } } return super.contains(file); } }
@Override public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull FileEditor fileEditor) { if (file.getFileType() != BallerinaFileType.INSTANCE) { return null; } PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file); if (psiFile == null) { return null; } if (psiFile.getLanguage() != BallerinaLanguage.INSTANCE) { return null; } Module module = ModuleUtilCore.findModuleForPsiElement(psiFile); if (module == null) { return null; } String sdkHomePath = BallerinaSdkService.getInstance(myProject).getSdkHomePath(module); if (StringUtil.isEmpty(sdkHomePath)) { return createMissingSdkPanel(myProject, module); } return null; }
@Override public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull FileEditor fileEditor) { if (file.getFileType() == GoFileType.INSTANCE) { PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file); if (InjectedLanguageUtil.findInjectionHost(psiFile) != null) { return null; } Module module = psiFile != null ? ModuleUtilCore.findModuleForPsiElement(psiFile) : null; if (GoUtil.fileToIgnore(file.getName())) { if (!PropertiesComponent.getInstance().getBoolean(DO_NOT_SHOW_NOTIFICATION_ABOUT_IGNORE_BY_BUILD_TOOL, false)) { return createIgnoredByBuildToolPanel(myProject, file); } } else if (module != null && !GoUtil.matchedForModuleBuildTarget(psiFile, module)) { return createMismatchedTargetPanel(module, file); } } return null; }
@Override public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull FileEditor fileEditor) { if (file.getFileType() != GoFileType.INSTANCE) return null; PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file); if (psiFile == null) return null; if (psiFile.getLanguage() != GoLanguage.INSTANCE) return null; Module module = ModuleUtilCore.findModuleForPsiElement(psiFile); if (module == null) return null; String sdkHomePath = GoSdkService.getInstance(myProject).getSdkHomePath(module); if (StringUtil.isEmpty(sdkHomePath)) { return createMissingSdkPanel(myProject, module); } if (!PropertiesComponent.getInstance().getBoolean(DO_NOT_SHOW_NOTIFICATION_ABOUT_EMPTY_GOPATH, false)) { String goPath = GoSdkUtil.retrieveGoPath(myProject, module); if (StringUtil.isEmpty(goPath.trim())) { return createEmptyGoPathPanel(myProject); } } return null; }
public static void addCompletions(@NotNull CompletionResultSet result, @NotNull Module module, @Nullable PsiElement context, @NotNull GlobalSearchScope scope, boolean allowMain) { Project project = module.getProject(); boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(module); String contextImportPath = GoCompletionUtil.getContextImportPath(context, vendoringEnabled); GoExcludedPathsSettings excludedSettings = GoExcludedPathsSettings.getInstance(project); PsiFile contextFile = context != null ? context.getContainingFile() : null; boolean testFileWithTestPackage = GoTestFinder.isTestFileWithTestPackage(contextFile); for (VirtualFile file : FileTypeIndex.getFiles(GoFileType.INSTANCE, scope)) { ProgressManager.checkCanceled(); PsiFile psiFile = PsiManager.getInstance(project).findFile(file); if (!(psiFile instanceof GoFile)) continue; PsiDirectory directory = psiFile.getContainingDirectory(); if (directory == null) continue; GoFile goFile = (GoFile)psiFile; if (!GoPsiImplUtil.canBeAutoImported(goFile, allowMain, module)) continue; String importPath = goFile.getImportPath(vendoringEnabled); if (StringUtil.isNotEmpty(importPath) && !excludedSettings.isExcluded(importPath) && (testFileWithTestPackage || !importPath.equals(contextImportPath))) { result.addElement(GoCompletionUtil.createPackageLookupElement(importPath, contextImportPath, directory, false)); } } } }
protected void checkFileConfiguration() throws RuntimeConfigurationError { VirtualFile file = findFile(getFilePath()); if (file == null) { throw new RuntimeConfigurationError("Main file is not specified"); } PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file); if (!(psiFile instanceof GoFile)) { throw new RuntimeConfigurationError("Main file is invalid"); } if (!GoRunUtil.isMainGoFile(psiFile)) { throw new RuntimeConfigurationError("Main file has non-main package or doesn't contain main function"); } }
@Override public void checkConfiguration() throws RuntimeConfigurationException { super.checkBaseConfiguration(); VirtualFile file = findFile(getFilePath()); if (file == null) { throw new RuntimeConfigurationError("Cannot find the specified main file."); } PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file); if (!(psiFile instanceof BallerinaFile)) { throw new RuntimeConfigurationError("Selected file is not a valid Ballerina file."); } if (!file.getName().endsWith(BallerinaConstants.BALLERINA_TEST_FILE_SUFFIX)) { throw new RuntimeConfigurationError("Selected file is not a Ballerina test file. File should end with " + "'" + BallerinaConstants.BALLERINA_TEST_FILE_SUFFIX + "' suffix."); } if (getPackage().isEmpty()) { throw new RuntimeConfigurationError("Test files must be in a package."); } }
protected void checkFileConfiguration() throws RuntimeConfigurationError { VirtualFile file = findFile(getFilePath()); if (file == null) { throw new RuntimeConfigurationError("Cannot find the specified main file."); } PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file); if (!(psiFile instanceof BallerinaFile)) { throw new RuntimeConfigurationError("Main file is not a valid Ballerina file."); } if (myRunKind == RunConfigurationKind.MAIN && !BallerinaRunUtil.hasMainFunction(psiFile)) { throw new RuntimeConfigurationError("Main run kind is selected, but the file does not contain a main " + "function."); } if (myRunKind == RunConfigurationKind.SERVICE && !BallerinaRunUtil.hasServices(psiFile)) { throw new RuntimeConfigurationError("Service run kind is selected, but the file does not contain any " + "services."); } }
case PACKAGE: VirtualFile packageDir = GoPackageUtil.findByImportPath(((GoTestRunConfiguration)configuration).getPackage(), project, module); PsiDirectory psiDirectory = packageDir != null ? PsiManager.getInstance(project).findDirectory(packageDir) : null; if (psiDirectory != null) { return GoPackageUtil.packageScope(psiDirectory, null);
PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file); if (psiFile != null && myTestFramework.isAvailableOnFile(psiFile)) { return; 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 + "`";