@Nullable private static Sdk moduleSdk(@NotNull Module module) { return sdk(ModuleRootManager.getInstance(module).getSdk()); }
private Sdk getGoSdk(@Nullable Module module) { if (module != null) { Sdk sdk = ModuleRootManager.getInstance(module).getSdk(); if (sdk != null && sdk.getSdkType() instanceof GoSdkType) { return sdk; } } Sdk sdk = ProjectRootManager.getInstance(myProject).getProjectSdk(); return sdk != null && sdk.getSdkType() instanceof GoSdkType ? sdk : null; } }
private Map<Module, Sdk> sdkByModule(@NotNull Project project) { final Map<Module, Sdk> sdkByModule = new LinkedHashMap<>(); final Module[] modules = ModuleManager.getInstance(project).getModules(); for (Module module : modules) { ModuleType moduleType = ModuleType.get(module); if (moduleType instanceof ElixirModuleType) { Sdk sdk = ModuleRootManager.getInstance(module).getSdk(); if (sdk != null && sdk.getSdkType() instanceof Type) { sdkByModule.put(module, sdk); } } } return sdkByModule; }
private Sdk getBallerinaSdk(@Nullable Module module) { if (module != null) { Sdk sdk = ModuleRootManager.getInstance(module).getSdk(); if (sdk != null && sdk.getSdkType() instanceof BallerinaSdkType) { return sdk; } } Sdk sdk = ProjectRootManager.getInstance(myProject).getProjectSdk(); return sdk != null && sdk.getSdkType() instanceof BallerinaSdkType ? sdk : null; }
/** * Finds a file in the module SDK. * * @param element a PsiElement * @param path relative file path in the SDK * @return {@code null} if the file cannot be found, otherwise returns the corresponding {@link VirtualFile}. */ @Nullable public static VirtualFile findFileInModuleSDK(@NotNull PsiElement element, @NotNull String path) { Module module = ModuleUtilCore.findModuleForPsiElement(element); if (module == null) { return null; } Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk(); if (moduleSdk == null) { return null; } VirtualFile[] roots = moduleSdk.getSdkModificator().getRoots(OrderRootType.SOURCES); VirtualFile file; for (VirtualFile root : roots) { file = VfsUtilCore.findRelativeFile(path, root); if (file != null) { return file; } } return null; }
@Override public void actionPerformed(@NotNull AnActionEvent e) { Project project = e.getProject(); assert project != null; FileDocumentManager.getInstance().saveAllDocuments(); for (Module module : GoSdkUtil.getGoModules(project)) { for (VirtualFile file : ModuleRootManager.getInstance(module).getContentRoots()) { fmt(project, module, "go fmt " + file.getPath(), file); } } }
private static void updateModules(@NotNull Project project, @NotNull Library lib, boolean remove) { Module[] modules = ModuleManager.getInstance(project).getModules(); for (Module module : modules) { ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); if (!remove) { if (model.findLibraryOrderEntry(lib) == null) { LibraryOrderEntry entry = model.addLibraryEntry(lib); entry.setScope(DependencyScope.PROVIDED); } } else { LibraryOrderEntry entry = model.findLibraryOrderEntry(lib); if (entry != null) { model.removeOrderEntry(entry); } } model.commit(); } }
/** * Searches for excluded roots in given {@link Project}. * * @param project current project * @return list of excluded roots */ public static List<VirtualFile> getExcludedRoots(@NotNull Project project) { List<VirtualFile> roots = ContainerUtil.newArrayList(); ModuleManager manager = ModuleManager.getInstance(project); for (Module module : manager.getModules()) { ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); if (model.isDisposed()) { continue; } Collections.addAll(roots, model.getExcludeRoots()); model.dispose(); } return roots; }
private void addContentRoot(@NotNull VirtualFile contentRoot) { ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel(); try { model.addContentEntry(contentRoot); contentRootsToClean.add(contentRoot); model.commit(); } finally { if (!model.isDisposed()) model.dispose(); } }
private static void updateModules(@NotNull Project project, @NotNull Library lib, boolean remove) { Module[] modules = ModuleManager.getInstance(project).getModules(); for (Module module : modules) { ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel(); if (!remove) { if (model.findLibraryOrderEntry(lib) == null) { LibraryOrderEntry entry = model.addLibraryEntry(lib); entry.setScope(DependencyScope.PROVIDED); } } else { LibraryOrderEntry entry = model.findLibraryOrderEntry(lib); if (entry != null) { model.removeOrderEntry(entry); } } model.commit(); } }
@NotNull public static Level level(@NotNull Project project) { Level level; final ModuleManager moduleManager = ModuleManager.getInstance(project); Level projectLevel = MAXIMUM; if (moduleManager != null) { Level maxLevel = null; for (Module module : moduleManager.getModules()) { final Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk(); if (moduleSdk != null) { final Level sdkLevel = level(moduleSdk); if (maxLevel == null || maxLevel.ordinal() < sdkLevel.ordinal()) { maxLevel = sdkLevel; } } } if (maxLevel != null) { projectLevel = maxLevel; } else { Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk(); projectLevel = level(projectSdk); } } level = projectLevel; return level; }
Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk(); if (moduleSdk != null) { VirtualFile homeDirectory = moduleSdk.getHomeDirectory();
private void assertLibrary(@NotNull Collection<String> libUrls, String... exclusionUrls) { UIUtil.dispatchAllInvocationEvents(); GoModuleLibrariesInitializer initializer = myModule.getComponent(GoModuleLibrariesInitializer.class); ModuleRootManager model = ModuleRootManager.getInstance(myModule); LibraryOrderEntry libraryOrderEntry = OrderEntryUtil.findLibraryOrderEntry(model, initializer.getLibraryName()); if (libUrls.isEmpty()) { assertNull(libraryOrderEntry); return; } LibraryEx library = (LibraryEx)libraryOrderEntry.getLibrary(); assertNotNull(library); assertSameElements(Arrays.asList(library.getUrls(OrderRootType.CLASSES)), libUrls); assertSameElements(library.getExcludedRootUrls(), exclusionUrls); }
public static String getSdkHome(Project project, Module module) { // Get the module SDK. Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk(); // If the SDK is Ballerina SDK, return the home path. if (moduleSdk != null && moduleSdk.getSdkType() == BallerinaSdkType.getInstance()) { return moduleSdk.getHomePath(); } // Ge the project SDK. Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk(); // If the SDK is Ballerina SDK, return the home path. if (projectSdk != null && projectSdk.getSdkType() == BallerinaSdkType.getInstance()) { return projectSdk.getHomePath(); } return ""; }
private void attachLibraries(@NotNull Collection<VirtualFile> libraryRoots, Set<VirtualFile> exclusions) { ApplicationManager.getApplication().assertIsDispatchThread(); if (!libraryRoots.isEmpty()) { ApplicationManager.getApplication().runWriteAction(() -> { ModuleRootManager model = ModuleRootManager.getInstance(myModule); LibraryOrderEntry goLibraryEntry = OrderEntryUtil.findLibraryOrderEntry(model, getLibraryName()); if (goLibraryEntry != null && goLibraryEntry.isValid()) { Library library = goLibraryEntry.getLibrary(); if (library != null && !((LibraryEx)library).isDisposed()) { fillLibrary(library, libraryRoots, exclusions); } } else { LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myModule.getProject()); Library library = libraryTable.createLibrary(getLibraryName()); fillLibrary(library, libraryRoots, exclusions); ModuleRootModificationUtil.addDependency(myModule, library); } }); showNotification(myModule.getProject()); } else { removeLibraryIfNeeded(); } }
tempRootsToClean.clear(); ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel(); try { for (ContentEntry entry : model.getContentEntries()) {
private void attachLibraries(@NotNull Collection<VirtualFile> libraryRoots, Set<VirtualFile> exclusions) { ApplicationManager.getApplication().assertIsDispatchThread(); if (!libraryRoots.isEmpty()) { ApplicationManager.getApplication().runWriteAction(() -> { ModuleRootManager model = ModuleRootManager.getInstance(myModule); LibraryOrderEntry ballerinaLibraryEntry = OrderEntryUtil.findLibraryOrderEntry(model, getLibraryName()); if (ballerinaLibraryEntry != null && ballerinaLibraryEntry.isValid()) { Library library = ballerinaLibraryEntry.getLibrary(); if (library != null && !((LibraryEx) library).isDisposed()) { fillLibrary(library, libraryRoots, exclusions); } } else { LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myModule .getProject()); Library library = libraryTable.createLibrary(getLibraryName()); fillLibrary(library, libraryRoots, exclusions); ModuleRootModificationUtil.addDependency(myModule, library); } }); showNotification(myModule.getProject()); } else { removeLibraryIfNeeded(); } }
private File relativePathToModuleContentRoots(final @NotNull PsiFile file, final @NotNull Module module, final @NotNull File baseTmpDir) { if (file.getParent() != null) { final String parentUrl = file.getParent().getVirtualFile().getUrl(); for (String moduleSourceRoot : ModuleRootManager.getInstance(module).getContentRootUrls()) { if (parentUrl.startsWith(moduleSourceRoot)) { return new File(baseTmpDir.getAbsolutePath() + parentUrl.substring(moduleSourceRoot.length())); } } } return null; }
private static boolean isTestClass(final PsiElement element) { final VirtualFile elementFile = element.getContainingFile().getVirtualFile(); if (elementFile == null) { return false; } final Module module = ModuleUtil.findModuleForPsiElement(element); if (module == null) { return false; } final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); return moduleRootManager != null && moduleRootManager.getFileIndex().isInTestSourceContent(elementFile); }
@Override public void run() { List<VirtualFile> filesToScan = null; if (module != null) { // all non-excluded files of a module final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module); filesToScan = flattenFiles(moduleRootManager.getContentRoots()); } else { // all non-excluded files of the project filesToScan = flattenFiles(new VirtualFile[]{project.getBaseDir()}); } project.getComponent(CheckStylePlugin.class).asyncScanFiles(filesToScan, selectedOverride); }