use of com.intellij.openapi.roots.OrderEntry in project ballerina by ballerina-lang.
the class BallerinaUtil method suggestPackageNameForDirectory.
/**
* Suggest the package name for a directory.
*
* @param directory directory which needs to be processed
* @return suggested package name or empty string if package name cannot be determined.
*/
@NotNull
public static String suggestPackageNameForDirectory(@Nullable PsiDirectory directory) {
// If the directory is not null, get the package name
if (directory != null) {
VirtualFile currentDirectory = directory.getVirtualFile();
Module module = ModuleUtilCore.findModuleForPsiElement(directory);
// Check directories in module content roots.
if (module != null) {
VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots();
for (VirtualFile contentRoot : contentRoots) {
if (!directory.getVirtualFile().getPath().startsWith(contentRoot.getPath())) {
continue;
}
return getImportPath(currentDirectory, contentRoot);
}
}
Project project = directory.getProject();
// Check directories in project content roots.
VirtualFile[] contentRoots = ProjectRootManager.getInstance(project).getContentRoots();
for (VirtualFile contentRoot : contentRoots) {
if (!directory.getVirtualFile().getPath().startsWith(contentRoot.getPath())) {
continue;
}
return getImportPath(currentDirectory, contentRoot);
}
// Then we check the sources of module sdk.
if (module != null) {
Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk();
String root = getImportPath(directory, currentDirectory, moduleSdk);
if (root != null) {
return root;
}
// Suggest package name for packages in BALLERINA_REPOSITORY.
// Todo - This can be used to get package names for packages in SDK, BALLERINA_REPO, etc.
OrderEntry[] entries = ModuleRootManager.getInstance(module).getOrderEntries();
for (OrderEntry entry : entries) {
for (VirtualFile file : entry.getFiles(OrderRootType.SOURCES)) {
if (currentDirectory.getPath().startsWith(file.getPath())) {
return getImportPath(currentDirectory, file);
}
}
}
}
// Then we check the sources of project sdk.
Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
String root = getImportPath(directory, currentDirectory, projectSdk);
if (root != null) {
return root;
}
String sdkHomePath = BallerinaSdkService.getInstance(project).getSdkHomePath(null);
if (sdkHomePath == null) {
return "";
}
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(sdkHomePath + "/src");
if (virtualFile == null) {
return "";
}
return getImportPath(currentDirectory, virtualFile);
}
// If the directory is null, return empty string
return "";
}
use of com.intellij.openapi.roots.OrderEntry in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getMatchingPackagesFromSDK.
@NotNull
private static List<PsiDirectory> getMatchingPackagesFromSDK(@NotNull Project project, @NotNull Module module, @NotNull List<PsiElement> packages) {
List<PsiDirectory> results = new ArrayList<>();
// First we check the sources of module SDK.
Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk();
if (moduleSdk != null) {
VirtualFile[] roots = moduleSdk.getSdkModificator().getRoots(OrderRootType.SOURCES);
for (VirtualFile root : roots) {
VirtualFile match = getMatchingDirectory(root, packages);
if (match != null) {
results.add(PsiManager.getInstance(project).findDirectory(match));
}
}
}
// return that result.
if (!results.isEmpty()) {
return results;
}
// Then we check the sources of project SDK.
Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
if (projectSdk != null) {
VirtualFile[] roots = projectSdk.getSdkModificator().getRoots(OrderRootType.SOURCES);
for (VirtualFile root : roots) {
VirtualFile match = getMatchingDirectory(root, packages);
if (match != null) {
results.add(PsiManager.getInstance(project).findDirectory(match));
}
}
}
String sdkHomePath = BallerinaSdkService.getInstance(project).getSdkHomePath(null);
if (sdkHomePath == null) {
return results;
}
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(sdkHomePath + "/src");
if (virtualFile == null) {
return results;
}
VirtualFile match = getMatchingDirectory(virtualFile, packages);
if (match != null) {
results.add(PsiManager.getInstance(project).findDirectory(match));
}
// Find matching packages in BALLERINA_REPOSITORY.
// Todo - This can be used to find all matching packages in SDK, BALLERINA_REPOSITORY, etc.
OrderEntry[] entries = ModuleRootManager.getInstance(module).getOrderEntries();
for (OrderEntry entry : entries) {
for (VirtualFile file : entry.getFiles(OrderRootType.SOURCES)) {
virtualFile = LocalFileSystem.getInstance().findFileByPath(file.getPath());
if (virtualFile != null) {
match = getMatchingDirectory(virtualFile, packages);
if (match != null) {
results.add(PsiManager.getInstance(project).findDirectory(match));
}
}
}
}
return results;
}
use of com.intellij.openapi.roots.OrderEntry in project azure-tools-for-java by Microsoft.
the class LibrariesConfigurationDialog method addLibrary.
private void addLibrary() {
AddLibraryWizardModel model = new AddLibraryWizardModel(module);
AddLibraryWizardDialog wizard = new AddLibraryWizardDialog(model);
wizard.setTitle(message("addLibraryTitle"));
wizard.show();
if (wizard.isOK()) {
AzureLibrary azureLibrary = model.getSelectedLibrary();
final LibrariesContainer.LibraryLevel level = LibrariesContainer.LibraryLevel.MODULE;
AccessToken token = WriteAction.start();
try {
final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
for (OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
if (orderEntry instanceof ModuleLibraryOrderEntryImpl && azureLibrary.getName().equals(((ModuleLibraryOrderEntryImpl) orderEntry).getLibraryName())) {
AzureTaskManager.getInstance().runLater(() -> PluginUtil.displayErrorDialog(message("error"), message("libraryExistsError")));
return;
}
}
Library newLibrary = LibrariesContainerFactory.createContainer(modifiableModel).createLibrary(azureLibrary.getName(), level, new ArrayList<OrderRoot>());
if (model.isExported()) {
for (OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
if (orderEntry instanceof ModuleLibraryOrderEntryImpl && azureLibrary.getName().equals(((ModuleLibraryOrderEntryImpl) orderEntry).getLibraryName())) {
((ModuleLibraryOrderEntryImpl) orderEntry).setExported(true);
break;
}
}
}
Library.ModifiableModel newLibraryModel = newLibrary.getModifiableModel();
// if there is separate resources folder
if (azureLibrary.getLocation() != null) {
File file = new File(String.format("%s%s%s", AzurePlugin.pluginFolder, File.separator, azureLibrary.getLocation()));
AddLibraryUtility.addLibraryRoot(file, newLibraryModel);
}
// if some files already contained in plugin dependencies, take them from there - true for azure sdk library
if (azureLibrary.getFiles().length > 0) {
AddLibraryUtility.addLibraryFiles(new File(PluginHelper.getAzureLibLocation()), newLibraryModel, azureLibrary.getFiles());
}
newLibraryModel.commit();
modifiableModel.commit();
((DefaultListModel) librariesList.getModel()).addElement(azureLibrary);
tempList.add(azureLibrary);
} catch (Exception ex) {
PluginUtil.displayErrorDialogAndLog(message("error"), message("addLibraryError"), ex);
} finally {
token.finish();
}
LocalFileSystem.getInstance().findFileByPath(PluginUtil.getModulePath(module)).refresh(true, true);
}
}
use of com.intellij.openapi.roots.OrderEntry in project sonarlint-intellij by SonarSource.
the class JavaAnalysisConfigurator method getProjectClasspath.
private static VirtualFile[] getProjectClasspath(@Nullable final Module module) {
if (module == null) {
return new VirtualFile[0];
}
final List<VirtualFile> found = new LinkedList<>();
final ModuleRootManager mrm = ModuleRootManager.getInstance(module);
final OrderEntry[] orderEntries = mrm.getOrderEntries();
for (final OrderEntry entry : orderEntries) {
if (entry instanceof ModuleOrderEntry) {
// Add dependent module output dir as library
Module dependentModule = ((ModuleOrderEntry) entry).getModule();
found.addAll(getModuleEntries(dependentModule));
} else if (entry instanceof LibraryOrderEntry) {
Library lib = ((LibraryOrderEntry) entry).getLibrary();
found.addAll(getLibraryEntries(lib));
}
}
return found.toArray(new VirtualFile[found.size()]);
}
Aggregations