@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); }
@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; } };
/** * Searches for directory or file using {@link PsiManager}. * * @param manager {@link PsiManager} instance * @param file working file * @return Psi item */ @Nullable private PsiFileSystemItem getPsiFileSystemItem(@NotNull PsiManager manager, @NotNull VirtualFile file) { if (!file.isValid()) { return null; } return file.isDirectory() ? manager.findDirectory(file) : manager.findFile(file); } }
public static void installBallerinaWithMainFileChooser(Project project, @NotNull TextFieldWithBrowseButton fileField) { installFileChooser(project, fileField, file -> isMainBallerinaFile(PsiManager.getInstance(project).findFile(file))); }
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)); }); }
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) 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; }
@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; }
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."); } }
private void doDirectoryTest(@NotNull VirtualFile file) { VfsUtilCore.processFilesRecursively(file, new FilteringProcessor<>( virtualFile -> !virtualFile.isDirectory() && virtualFile.getName().endsWith(".go"), virtualFile -> { PsiFile goFile = myFixture.getPsiManager().findFile(virtualFile); assert goFile instanceof GoFile; processPsiFile((GoFile)goFile); return true; } )); doResolveTest(); } }
@Nullable public static GoFile findBuiltinFile(@NotNull PsiElement context) { Project project = context.getProject(); // it's important to ask module on file, otherwise module won't be found for elements in libraries files [zolotov] Module moduleFromContext = ModuleUtilCore.findModuleForPsiElement(context.getContainingFile()); if (moduleFromContext == null) { for (Module module : ModuleManager.getInstance(project).getModules()) { if (GoSdkService.getInstance(project).isGoModule(module)) { moduleFromContext = module; break; } } } Module module = moduleFromContext; UserDataHolder holder = ObjectUtils.notNull(module, project); VirtualFile file = CachedValuesManager.getManager(context.getProject()).getCachedValue(holder, () -> { VirtualFile sdkSrcDir = getSdkSrcDir(project, module); VirtualFile result = sdkSrcDir != null ? sdkSrcDir.findFileByRelativePath(GoConstants.BUILTIN_FILE_PATH) : null; return CachedValueProvider.Result.create(result, getSdkAndLibrariesCacheDependencies(project, module, result)); }); if (file == null) return null; PsiFile psiBuiltin = context.getManager().findFile(file); return psiBuiltin instanceof GoFile ? (GoFile)psiBuiltin : null; }
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."); } }