Java Code Examples for com.intellij.openapi.roots.ModuleRootManager#getInstance()

The following examples show how to use com.intellij.openapi.roots.ModuleRootManager#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CS0227.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
@RequiredWriteAction
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
	Module moduleForPsiElement = ModuleUtilCore.findModuleForPsiElement(file);
	if(moduleForPsiElement == null)
	{
		return;
	}

	ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(moduleForPsiElement);

	final ModifiableRootModel modifiableModel = moduleRootManager.getModifiableModel();
	CSharpSimpleMutableModuleExtension cSharpModuleExtension = modifiableModel.getExtension(CSharpSimpleMutableModuleExtension.class);
	if(cSharpModuleExtension != null)
	{
		cSharpModuleExtension.setAllowUnsafeCode(true);
	}

	modifiableModel.commit();
}
 
Example 2
Source File: FieldReferenceProviderImpl.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiFile loadInMemoryDescriptorProto() {
    if (inm == null) {
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
            VirtualFile[] allSourceRoots = moduleRootManager.orderEntries().getAllSourceRoots();
            for (VirtualFile allSourceRoot : allSourceRoots) {
                PsiDirectory directory = PsiManager.getInstance(project).findDirectory(allSourceRoot);
                if (directory != null && directory.isValid()) {
                    String relPath = "google/protobuf/descriptor.proto";
                    VirtualFile file = directory.getVirtualFile().findFileByRelativePath(relPath);
                    if (file != null) {
                        PsiManager psiManager = PsiManager.getInstance(project);
                        PsiFile psiFile = psiManager.findFile(file);
                        if (psiFile instanceof ProtoPsiFileRoot) {
                            inm = psiFile;
                            return (ProtoPsiFileRoot) psiFile;
                        }
                    }
                }
            }
        }
    }
    return inm;
}
 
Example 3
Source File: ProtostuffPluginController.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Add global protobuf library to given module. Visible for testing.
 */
public void addLibrary(Module module) {
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    ModifiableRootModel modifiableRootModel = moduleRootManager.getModifiableModel();
    AtomicBoolean found = new AtomicBoolean(false);
    OrderEnumerator.orderEntries(module).forEachLibrary(library1 -> {
        if (LIB_NAME.equals(library1.getName())) {
            found.set(true);
            return false;
        }
        return true;
    });
    if (!found.get()) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            modifiableRootModel.addLibraryEntry(globalLibrary);
            modifiableRootModel.commit();
        });
    }
}
 
Example 4
Source File: HaxeModuleUtil.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to locate a file in the module directories that may not be a direct source
 * file (like the .hxml files).  If the given file is absolute, then module (.iml) directory,
 * source and classpaths will not be searched.
 *
 * @param module
 * @param haxeProjectPath
 * @return
 */

@Nullable
private VirtualFile locateModuleFile(Module module, String haxeProjectPath) {
  // Try to find the file.  If it's absolute, we either get it the first time or give up.
  VirtualFile projectFile = HaxeFileUtil.locateFile(haxeProjectPath);
  if ( ! HaxeFileUtil.isAbsolutePath(haxeProjectPath)) {
    if (null == projectFile) {
      ModuleRootManager rootMgr = ModuleRootManager.getInstance(module);
      projectFile = HaxeFileUtil.locateFile(haxeProjectPath, rootMgr.getSourceRootUrls());
    }
    if (null == projectFile) {
      projectFile = HaxeFileUtil.locateFile(haxeProjectPath, module.getModuleFilePath());
    }
  }
  return projectFile;
}
 
Example 5
Source File: HaxeTestsRunner.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
protected RunContentDescriptor doExecute(@NotNull final Project project,
                                         @NotNull RunProfileState state,
                                         final RunContentDescriptor descriptor,
                                         @NotNull final ExecutionEnvironment environment) throws ExecutionException {

  final HaxeTestsConfiguration profile = (HaxeTestsConfiguration)environment.getRunProfile();
  final Module module = profile.getConfigurationModule().getModule();

  ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
  VirtualFile[] roots = rootManager.getContentRoots();

  return super.doExecute(project, new CommandLineState(environment) {
    @NotNull
    @Override
    protected ProcessHandler startProcess() throws ExecutionException {
      //actually only neko target is supported for tests
      HaxeTarget currentTarget = HaxeTarget.NEKO;
      final GeneralCommandLine commandLine = new GeneralCommandLine();
      commandLine.setWorkDirectory(PathUtil.getParentPath(module.getModuleFilePath()));
      commandLine.setExePath(currentTarget.getFlag());
      final HaxeModuleSettings settings = HaxeModuleSettings.getInstance(module);
      String folder = settings.getOutputFolder() != null ? (settings.getOutputFolder() + "/release/") : "";
      commandLine.addParameter(getFileNameWithCurrentExtension(currentTarget, folder + settings.getOutputFileName()));

      final TextConsoleBuilder consoleBuilder = TextConsoleBuilderFactory.getInstance().createBuilder(module.getProject());
      consoleBuilder.addFilter(new ErrorFilter(module));
      setConsoleBuilder(consoleBuilder);

      return new OSProcessHandler(commandLine.createProcess(), commandLine.getCommandLineString());
    }

    private String getFileNameWithCurrentExtension(HaxeTarget haxeTarget, String fileName) {
      if (haxeTarget != null) {
        return haxeTarget.getTargetFileNameWithExtension(FileUtil.getNameWithoutExtension(fileName));
      }
      return fileName;
    }
  }, descriptor, environment);
}
 
Example 6
Source File: ModuleManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected ModuleEx createAndLoadModule(@Nonnull final ModuleLoadItem moduleLoadItem, @Nonnull ModuleModelImpl moduleModel, @Nullable final ProgressIndicator progressIndicator) {
  final ModuleEx module = createModule(moduleLoadItem.getName(), moduleLoadItem.getDirUrl(), progressIndicator);
  moduleModel.initModule(module);

  collapseOrExpandMacros(module, moduleLoadItem.getElement(), false);

  final ModuleRootManagerImpl moduleRootManager = (ModuleRootManagerImpl)ModuleRootManager.getInstance(module);
  AccessRule.read(() -> moduleRootManager.loadState(moduleLoadItem.getElement(), progressIndicator));

  return module;
}
 
Example 7
Source File: CreateDirectoryOrPackageAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredUIAccess
private static Trinity<ContentFolderTypeProvider, PsiDirectory, ChildType> getInfo(PsiDirectory d) {
  Project project = d.getProject();
  ProjectFileIndex projectFileIndex = ProjectFileIndex.getInstance(project);

  Module moduleForPsiElement = ModuleUtilCore.findModuleForPsiElement(d);
  if (moduleForPsiElement != null) {
    boolean isPackageSupported = false;
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(moduleForPsiElement);
    List<PsiPackageSupportProvider> extensions = PsiPackageSupportProvider.EP_NAME.getExtensionList();
    for (ModuleExtension moduleExtension : moduleRootManager.getExtensions()) {
      for (PsiPackageSupportProvider supportProvider : extensions) {
        if (supportProvider.isSupported(moduleExtension)) {
          isPackageSupported = true;
          break;
        }
      }
    }

    if (isPackageSupported) {
      ContentFolderTypeProvider contentFolderTypeForFile = projectFileIndex.getContentFolderTypeForFile(d.getVirtualFile());
      if (contentFolderTypeForFile != null) {
        Image childPackageIcon = contentFolderTypeForFile.getChildPackageIcon();
        return Trinity.create(contentFolderTypeForFile, d, childPackageIcon != null ? ChildType.Package : ChildType.Directory);
      }
    }
  }

  return Trinity.create(null, d, ChildType.Directory);
}
 
Example 8
Source File: TodoTreeHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void addPackagesToChildren0(Project project, ArrayList<AbstractTreeNode> children, Module module, TodoTreeBuilder builder) {
  final List<VirtualFile> roots = new ArrayList<VirtualFile>();
  final List<VirtualFile> sourceRoots = new ArrayList<VirtualFile>();
  final PsiManager psiManager = PsiManager.getInstance(project);
  if (module == null) {
    final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
    ContainerUtil.addAll(roots, projectRootManager.getContentRoots());
    ContainerUtil.addAll(sourceRoots, projectRootManager.getContentSourceRoots());
  }
  else {
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    ContainerUtil.addAll(roots, moduleRootManager.getContentRoots());
    ContainerUtil.addAll(sourceRoots, moduleRootManager.getContentFolderFiles(ContentFolderScopes.productionAndTest()));
  }
  roots.removeAll(sourceRoots);
  for (VirtualFile dir : roots) {
    final PsiDirectory directory = psiManager.findDirectory(dir);
    if (directory == null) {
      continue;
    }
    final Iterator<PsiFile> files = builder.getFiles(directory);
    if (!files.hasNext()) continue;
    TodoDirNode dirNode = new TodoDirNode(project, directory, builder);
    if (!children.contains(dirNode)) {
      children.add(dirNode);
    }
  }
}
 
Example 9
Source File: PantsUtil.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
public static boolean isSourceModule(@NotNull Module module) {
  // A source module must either contain content root(s),
  // or depending on other module(s). Otherwise it can be a gen module
  // or 3rdparty module placeholder.
  ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
  return moduleRootManager.getDependencies().length > 0 ||
         moduleRootManager.getContentRoots().length > 0 ||
         moduleRootManager.getContentEntries().length > 0;
}
 
Example 10
Source File: ProjectViewModuleNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@RequiredReadAction
public Collection<AbstractTreeNode> getChildren() {
  Module module = getValue();
  if (module == null || module.isDisposed()) {
    return Collections.emptyList();
  }
  ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
  ModuleFileIndex moduleFileIndex = rootManager.getFileIndex();

  final VirtualFile[] contentRoots = rootManager.getContentRoots();
  final List<AbstractTreeNode> children = new ArrayList<>(contentRoots.length + 1);
  final PsiManager psiManager = PsiManager.getInstance(module.getProject());
  for (final VirtualFile contentRoot : contentRoots) {
    if (!moduleFileIndex.isInContent(contentRoot)) continue;

    if (contentRoot.isDirectory()) {
      PsiDirectory directory = psiManager.findDirectory(contentRoot);
      if(directory != null) {
        children.add(new PsiDirectoryNode(getProject(), directory, getSettings()));
      }
    }
    else {
      PsiFile file = psiManager.findFile(contentRoot);
      if(file != null) {
        children.add(new PsiFileNode(getProject(), file, getSettings()));
      }
    }
  }
  return children;
}
 
Example 11
Source File: CompilerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void computeIntersectingPaths(final Project project,
                                            final Collection<VirtualFile> outputPaths,
                                            final Collection<VirtualFile> result) {
  for (Module module : ModuleManager.getInstance(project).getModules()) {
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    final VirtualFile[] sourceRoots = rootManager.getContentFolderFiles(ContentFolderScopes.productionAndTest());
    for (final VirtualFile outputPath : outputPaths) {
      for (VirtualFile sourceRoot : sourceRoots) {
        if (VfsUtilCore.isAncestor(outputPath, sourceRoot, true) || VfsUtilCore.isAncestor(sourceRoot, outputPath, false)) {
          result.add(outputPath);
        }
      }
    }
  }
}
 
Example 12
Source File: FormatChangedTextUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Allows to answer if any file that belongs to the given module has changes in comparison with VCS.
 *
 * @param module target module to check
 * @return <code>true</code> if any file that belongs to the given module has changes in comparison with VCS
 * <code>false</code> otherwise
 */
public static boolean hasChanges(@Nonnull Module module) {
  final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
  for (VirtualFile root : rootManager.getSourceRoots()) {
    if (hasChanges(root, module.getProject())) {
      return true;
    }
  }
  return false;
}
 
Example 13
Source File: AllSourceRootsProvider.java    From protobuf-jetbrains-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public VirtualFile[] getSourceRoots(Module module, ProtoPsiFileRoot psiFileRoot) {
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    return moduleRootManager.orderEntries().getAllSourceRoots();
}
 
Example 14
Source File: LibrariesAndSdkClassesRootsProvider.java    From protobuf-jetbrains-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public VirtualFile[] getSourceRoots(Module module, ProtoPsiFileRoot psiFileRoot) {
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
    return moduleRootManager.orderEntries().getAllLibrariesAndSdkClassesRoots();
}
 
Example 15
Source File: ProjectHelper.java    From android-codegenerator-plugin-intellij with Apache License 2.0 4 votes vote down vote up
private ModuleRootManager getModuleRootManager(AnActionEvent event) {
    return ModuleRootManager.getInstance(event.getData(LangDataKeys.MODULE));
}
 
Example 16
Source File: ModuleRootCompileScope.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean belongs(final String url) {
  if (myScopeModules.isEmpty()) {
    return false; // optimization
  }
  Module candidateModule = null;
  int maxUrlLength = 0;
  final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
  for (final Module module : myModules) {
    final String[] contentRootUrls = getModuleContentUrls(module);
    for (final String contentRootUrl : contentRootUrls) {
      if (contentRootUrl.length() < maxUrlLength) {
        continue;
      }
      if (!isUrlUnderRoot(url, contentRootUrl)) {
        continue;
      }
      if (contentRootUrl.length() == maxUrlLength) {
        if (candidateModule == null) {
          candidateModule = module;
        }
        else {
          // the same content root exists in several modules
          if (!candidateModule.equals(module)) {
            candidateModule = ApplicationManager.getApplication().runReadAction(new Computable<Module>() {
              @Override
              public Module compute() {
                final VirtualFile contentRootFile = VirtualFileManager.getInstance().findFileByUrl(contentRootUrl);
                if (contentRootFile != null) {
                  return projectFileIndex.getModuleForFile(contentRootFile);
                }
                return null;
              }
            });
          }
        }
      }
      else {
        maxUrlLength = contentRootUrl.length();
        candidateModule = module;
      }
    }
  }

  if (candidateModule != null && myScopeModules.contains(candidateModule)) {
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(candidateModule);
    String[] excludeRootUrls = moduleRootManager.getContentFolderUrls(ContentFolderScopes.excluded());
    for (String excludeRootUrl : excludeRootUrls) {
      if (isUrlUnderRoot(url, excludeRootUrl)) {
        return false;
      }
    }
    for (String sourceRootUrl : getModuleContentUrls(candidateModule)) {
      if (isUrlUnderRoot(url, sourceRootUrl)) {
        return true;
      }
    }
  }

  return false;
}
 
Example 17
Source File: DefaultModulesProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public ModuleRootModel getRootModel(@Nonnull Module module) {
  return ModuleRootManager.getInstance(module);
}
 
Example 18
Source File: HaxeModuleBuilder.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
private void createHelloWorldIfEligible(Module module) {
  ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
  VirtualFile[] srcDirs = rootManager.getSourceRoots();
  VirtualFile[] rootDirs = rootManager.getContentRoots();
  if(rootDirs.length != 1 || srcDirs.length != 1) {
    return;
  }

  VirtualFile root = rootDirs[0];
  VirtualFile src = srcDirs[0];

  if(src.getChildren().length != 0) {
    return;
  }
  for(VirtualFile item:root.getChildren()) {
    if(item.getExtension() == "hxml") {
      return;
    }
  }


  ApplicationManager.getApplication().runWriteAction(new Runnable() {
    @Override
    public void run() {
      try {
        VirtualFile mainHx = src.createChildData(this, "Main.hx");
        String mainHxSource = "class Main {\n"
          + "    static public function main() {\n"
          + "        trace(\"Hello, world!\");\n"
          + "    }\n"
          + "}\n";
        mainHx.setBinaryContent(mainHxSource.getBytes(StandardCharsets.UTF_8));

        VirtualFile buildHxml = root.createChildData(this, "build.hxml");
        String buildHxmlSource = "-cp src\n"
          + "-D analyzer-optimize\n"
          + "-main Main\n"
          + "--interp";
        buildHxml.setBinaryContent(buildHxmlSource.getBytes(StandardCharsets.UTF_8));

        createDefaultRunConfiguration(module, buildHxml.getPath());

        FileEditorManager editorManager = FileEditorManager.getInstance(module.getProject());
        editorManager.openFile(mainHx, true);

      } catch (IOException e) {
      }
    }
  });
}
 
Example 19
Source File: VFSUtils.java    From SmartIM4IntelliJ with Apache License 2.0 4 votes vote down vote up
private static void findFileInModule(final Set<VirtualFile> found, Module module, String path) {
    ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    findInRoots(found, rootManager.getContentRoots(), path);
}
 
Example 20
Source File: MavenImportingTestCase.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
public ModuleRootManager getRootManager(String module) {
  return ModuleRootManager.getInstance(getModule(module));
}