Java Code Examples for com.intellij.openapi.module.ModuleUtilCore#findModuleForPsiElement()

The following examples show how to use com.intellij.openapi.module.ModuleUtilCore#findModuleForPsiElement() . 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: IncompatibleDartPluginNotificationProvider.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull FileEditor fileEditor) {
  if (!FlutterUtils.isFlutteryFile(file)) return null;

  final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
  if (psiFile == null) return null;

  if (psiFile.getLanguage() != DartLanguage.INSTANCE) return null;

  final Module module = ModuleUtilCore.findModuleForPsiElement(psiFile);
  if (module == null) return null;

  if (!FlutterModuleUtils.isFlutterModule(module)) return null;

  final Version minimumVersion = DartPlugin.getInstance().getMinimumVersion();
  final Version dartVersion = DartPlugin.getInstance().getVersion();
  if (dartVersion.minor == 0 && dartVersion.bugfix == 0) {
    return null; // Running from sources.
  }
  return dartVersion.compareTo(minimumVersion) < 0 ? createUpdateDartPanel(myProject, module, dartVersion.toCompactString(),
                                                                           getPrintableRequiredDartVersion()) : null;
}
 
Example 2
Source File: BaseCSharpModuleExtension.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
@RequiredReadAction
public PsiElement[] getEntryPointElements()
{
	final List<DotNetTypeDeclaration> typeDeclarations = new ArrayList<DotNetTypeDeclaration>();
	Collection<DotNetLikeMethodDeclaration> methods = MethodIndex.getInstance().get("Main", getProject(), GlobalSearchScope.moduleScope(getModule()));
	for(DotNetLikeMethodDeclaration method : methods)
	{
		if(method instanceof CSharpMethodDeclaration && DotNetRunUtil.isEntryPoint((DotNetMethodDeclaration) method))
		{
			Module moduleForPsiElement = ModuleUtilCore.findModuleForPsiElement(method);
			// scope is broken?
			if(!getModule().equals(moduleForPsiElement))
			{
				continue;
			}
			ContainerUtil.addIfNotNull(typeDeclarations, ObjectUtil.tryCast(method.getParent(), DotNetTypeDeclaration.class));
		}
	}
	return ContainerUtil.toArray(typeDeclarations, DotNetTypeDeclaration.ARRAY_FACTORY);
}
 
Example 3
Source File: IncompatibleDartPluginNotificationProvider.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull FileEditor fileEditor) {
  if (!FlutterUtils.isFlutteryFile(file)) return null;

  final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
  if (psiFile == null) return null;

  if (psiFile.getLanguage() != DartLanguage.INSTANCE) return null;

  final Module module = ModuleUtilCore.findModuleForPsiElement(psiFile);
  if (module == null) return null;

  if (!FlutterModuleUtils.isFlutterModule(module)) return null;

  final Version minimumVersion = DartPlugin.getInstance().getMinimumVersion();
  final Version dartVersion = DartPlugin.getInstance().getVersion();
  if (dartVersion.minor == 0 && dartVersion.bugfix == 0) {
    return null; // Running from sources.
  }
  return dartVersion.compareTo(minimumVersion) < 0 ? createUpdateDartPanel(myProject, module, dartVersion.toCompactString(),
                                                                           getPrintableRequiredDartVersion()) : null;
}
 
Example 4
Source File: SdkConfigurationNotificationProvider.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public EditorNotificationPanel createNotificationPanel(@NotNull final VirtualFile file, @NotNull final FileEditor fileEditor) {

  // If this is a Bazel configured Flutter project, exit immediately, neither of the notifications should be shown for this project type.
  if (FlutterModuleUtils.isFlutterBazelProject(project)) return null;

  if (file.getFileType() != DartFileType.INSTANCE) return null;

  final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
  if (psiFile == null || psiFile.getLanguage() != DartLanguage.INSTANCE) return null;

  final Module module = ModuleUtilCore.findModuleForPsiElement(psiFile);
  if (!FlutterModuleUtils.isFlutterModule(module)) return null;

  final FlutterSdk flutterSdk = FlutterSdk.getFlutterSdk(project);
  if (flutterSdk == null) {
    return createNoFlutterSdkPanel(project);
  }
  else if (!flutterSdk.getVersion().isMinRecommendedSupported()) {
    return createOutOfDateFlutterSdkPanel(flutterSdk);
  }

  return null;
}
 
Example 5
Source File: AssemblyModule.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nonnull
@RequiredReadAction
static AssemblyModule resolve(@Nonnull PsiElement element)
{
	Module module = ModuleUtilCore.findModuleForPsiElement(element);
	if(module != null)
	{
		return new ConsuloModuleAsAssemblyModule(module);
	}

	VirtualFile virtualFile = PsiUtilCore.getVirtualFile(element);
	if(virtualFile != null)
	{
		VirtualFile rootFile = ArchiveVfsUtil.getVirtualFileForArchive(virtualFile);
		if(rootFile != null && rootFile.getFileType() == DotNetModuleFileType.INSTANCE)
		{
			return new DotNetModuleAsAssemblyModule(element.getProject(), rootFile);
		}
	}
	return UnknownAssemblyModule.INSTANCE;
}
 
Example 6
Source File: TodoTreeBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @return read-only iterator of all valid PSI files that can have T.O.D.O items
 * and which are located under specified {@code psiDirectory}.
 * @see FileTree#getFiles(VirtualFile)
 */
public Iterator<PsiFile> getFilesUnderDirectory(PsiDirectory psiDirectory) {
  List<VirtualFile> files = myFileTree.getFilesUnderDirectory(psiDirectory.getVirtualFile());
  List<PsiFile> psiFileList = new ArrayList<>(files.size());
  PsiManager psiManager = PsiManager.getInstance(myProject);
  for (VirtualFile file : files) {
    final Module module = ModuleUtilCore.findModuleForPsiElement(psiDirectory);
    if (module != null) {
      final boolean isInContent = ModuleRootManager.getInstance(module).getFileIndex().isInContent(file);
      if (!isInContent) continue;
    }
    if (file.isValid()) {
      PsiFile psiFile = psiManager.findFile(file);
      if (psiFile != null) {
        psiFileList.add(psiFile);
      }
    }
  }
  return psiFileList.iterator();
}
 
Example 7
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 8
Source File: GlobalConfigsToolWindowPanel.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
private boolean isDevKitConfig(XmlTag tag, XmlFile baseFile) {
    Module module = ModuleUtilCore.findModuleForPsiElement(baseFile);

    String namespace = tag.getNamespace();
    List<XmlSchemaProvider> providers = XmlSchemaProvider.getAvailableProviders(baseFile);

    for (XmlSchemaProvider provider : providers) {
        Set<String> locations = provider.getLocations(namespace, baseFile);
        for (String location : locations) {
            XmlFile schema = provider.getSchema(location, module, baseFile);
            if (schema != null) {
                String schemaFile = schema.getText();
                if (schemaFile.contains("http://www.mulesoft.org/schema/mule/devkit")) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 9
Source File: TodoTreeBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @return read-only iterator of all valid PSI files that can have T.O.D.O items
 * and which are located under specified {@code psiDirectory}.
 * @see FileTree#getFiles(VirtualFile)
 */
public Iterator<PsiFile> getFiles(PsiDirectory psiDirectory, final boolean skip) {
  List<VirtualFile> files = myFileTree.getFiles(psiDirectory.getVirtualFile());
  List<PsiFile> psiFileList = new ArrayList<>(files.size());
  PsiManager psiManager = PsiManager.getInstance(myProject);
  for (VirtualFile file : files) {
    final Module module = ModuleUtilCore.findModuleForPsiElement(psiDirectory);
    if (module != null) {
      final boolean isInContent = ModuleRootManager.getInstance(module).getFileIndex().isInContent(file);
      if (!isInContent) continue;
    }
    if (file.isValid()) {
      PsiFile psiFile = psiManager.findFile(file);
      if (psiFile != null) {
        final PsiDirectory directory = psiFile.getContainingDirectory();
        if (directory == null || !skip || !TodoTreeHelper.skipDirectory(directory)) {
          psiFileList.add(psiFile);
        }
      }
    }
  }
  return psiFileList.iterator();
}
 
Example 10
Source File: CSharpLanguageVersionResolver.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Nonnull
@Override
public LanguageVersion getLanguageVersion(@Nonnull Language language, @Nullable PsiElement element)
{
	if(element == null)
	{
		return CSharpLanguageVersionHelper.getInstance().getHighestVersion();
	}
	Module moduleForPsiElement = ModuleUtilCore.findModuleForPsiElement(element);
	if(moduleForPsiElement == null)
	{
		return CSharpLanguageVersionHelper.getInstance().getHighestVersion();
	}
	CSharpSimpleModuleExtension extension = ModuleUtilCore.getExtension(moduleForPsiElement, CSharpSimpleModuleExtension.class);
	if(extension == null)
	{
		return CSharpLanguageVersionHelper.getInstance().getHighestVersion();
	}
	return CSharpLanguageVersionHelper.getInstance().getWrapper(extension.getLanguageVersion());
}
 
Example 11
Source File: BeanReference.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public Optional<ReferenceableBeanId> findReferenceableBeanId() {
    final Module module = ModuleUtilCore.findModuleForPsiElement(getElement());
    if (module != null) {
        return BeanUtils.getService().findReferenceableBeanId(module, id);
    } else {
        return Optional.empty();
    }
}
 
Example 12
Source File: UsingNamespaceFix.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredReadAction
private static Set<NamespaceReference> collectAllAvailableNamespaces(CSharpReferenceExpression ref, CSharpReferenceExpression.ResolveToKind kind)
{
	if(PsiTreeUtil.getParentOfType(ref, CSharpUsingListChild.class) != null || !ref.isValid())
	{
		return Collections.emptySet();
	}
	String referenceName = ref.getReferenceName();
	if(StringUtil.isEmpty(referenceName))
	{
		return Collections.emptySet();
	}
	Set<NamespaceReference> resultSet = new LinkedHashSet<>();
	if(kind == CSharpReferenceExpression.ResolveToKind.TYPE_LIKE || kind == CSharpReferenceExpression.ResolveToKind.CONSTRUCTOR || ref.getQualifier() == null)
	{
		collectAvailableNamespaces(ref, resultSet, referenceName);
	}
	if(kind == CSharpReferenceExpression.ResolveToKind.METHOD)
	{
		collectAvailableNamespacesForMethodExtensions(ref, resultSet, referenceName);
	}

	Module moduleForPsiElement = ModuleUtilCore.findModuleForPsiElement(ref);
	if(moduleForPsiElement != null)
	{
		resultSet.addAll(DotNetLibraryAnalyzerComponent.getInstance(moduleForPsiElement.getProject()).get(moduleForPsiElement, referenceName));
	}
	return resultSet;
}
 
Example 13
Source File: ConfigurationContext.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
public ConfigurationContext(PsiElement element) {
  myModule = ModuleUtilCore.findModuleForPsiElement(element);
  myLocation = new PsiLocation<>(element.getProject(), myModule, element);
  myRuntimeConfiguration = null;
  myContextComponent = null;
}
 
Example 14
Source File: CSharpCreateFromTemplateHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
@RequiredReadAction
public void prepareProperties(Map<String, Object> props)
{
	PsiDirectory directory = (PsiDirectory) props.get("psiDirectory");
	if(directory == null)
	{
		return;
	}

	Module module = ModuleUtilCore.findModuleForPsiElement(directory);
	if(module != null)
	{
		props.put("MODULE", module.getName());
	}
	props.put("GUID", UUID.randomUUID().toString());

	DotNetSimpleModuleExtension<?> extension = ModuleUtilCore.getExtension(directory, DotNetSimpleModuleExtension.class);
	if(extension == null)
	{
		Module moduleByPsiDirectory = findModuleByPsiDirectory(directory);
		if(moduleByPsiDirectory != null)
		{
			extension = ModuleUtilCore.getExtension(moduleByPsiDirectory, DotNetSimpleModuleExtension.class);
		}
	}

	String namespace = null;
	if(extension != null)
	{
		namespace = formatNamespace(extension.getNamespaceGeneratePolicy().calculateNamespace(directory));
	}
	props.put("NAMESPACE_NAME", namespace);
}
 
Example 15
Source File: PsiProximityComparator.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static WeighingComparable<PsiElement, ProximityLocation> getProximity(final Computable<? extends PsiElement> elementComputable, final PsiElement context, ProcessingContext processingContext) {
  PsiElement element = elementComputable.compute();
  if (element == null || context == null) return null;
  Module contextModule = processingContext.get(MODULE_BY_LOCATION);
  if (contextModule == null) {
    contextModule = ModuleUtilCore.findModuleForPsiElement(context);
    processingContext.put(MODULE_BY_LOCATION, contextModule);
  }

  return new WeighingComparable<>(elementComputable, new ProximityLocation(context, contextModule, processingContext), getProximityWeighers());
}
 
Example 16
Source File: BeanReferenceCompletionExtension.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
private List<ReferenceableBeanId> findTargets(PsiElement element, String query) {
    Module module = ModuleUtilCore.findModuleForPsiElement(element);
    if (module == null) {
        return Collections.emptyList();
    }

    PsiType expectedType = getExpectedType(element);

    Predicate<String> beanIdPredicate = b -> b.startsWith(query);
    if (expectedType != null) {
        return BeanUtils.getService().findReferenceableBeanIdsByType(module, beanIdPredicate, expectedType);
    } else {
        return BeanUtils.getService().findReferenceableBeanIds(module, beanIdPredicate);
    }
}
 
Example 17
Source File: CSharpHighlightContext.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
@RequiredReadAction
protected Module compute()
{
	return ModuleUtilCore.findModuleForPsiElement(myFile);
}
 
Example 18
Source File: HaxeResolveUtil.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@NotNull
public static GlobalSearchScope getScopeForElement(@NotNull PsiElement context) {
  final Project project = context.getProject();
  if (ApplicationManager.getApplication().isUnitTestMode()) {
    return GlobalSearchScope.allScope(project);
  }
  final Module module = ModuleUtilCore.findModuleForPsiElement(context);
  return module != null ? GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module) : GlobalSearchScope.allScope(project);
}
 
Example 19
Source File: FileReferenceSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static Collection<PsiFileSystemItem> getAbsoluteTopLevelDirLocations(@Nonnull final PsiFile file) {
  final VirtualFile virtualFile = file.getVirtualFile();
  if (virtualFile == null) return Collections.emptyList();

  final PsiDirectory parent = file.getParent();
  final Module module = ModuleUtilCore.findModuleForPsiElement(parent == null ? file : parent);
  if (module == null) return Collections.emptyList();

  final List<PsiFileSystemItem> list = new ArrayList<>();
  final Project project = file.getProject();
  for (FileReferenceHelper helper : FileReferenceHelperRegistrar.getHelpers()) {
    if (helper.isMine(project, virtualFile)) {
      if (helper.isFallback() && !list.isEmpty()) {
        continue;
      }
      final Collection<PsiFileSystemItem> roots = helper.getRoots(module);
      for (PsiFileSystemItem root : roots) {
        if (root == null) {
          LOG.error("Helper " + helper + " produced a null root for " + file);
        }
      }
      list.addAll(roots);
    }
  }
  return list;
}
 
Example 20
Source File: BaseCSharpModuleExtension.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
@RequiredReadAction
public String getAssemblyTitle()
{
	GlobalSearchScope moduleScope = GlobalSearchScope.moduleScope(getModule());
	Collection<CSharpAttributeList> attributeLists = AttributeListIndex.getInstance().get(DotNetAttributeTargetType.ASSEMBLY, getProject(), moduleScope);

	loop:
	for(CSharpAttributeList attributeList : attributeLists)
	{
		for(CSharpAttribute attribute : attributeList.getAttributes())
		{
			DotNetTypeDeclaration typeDeclaration = attribute.resolveToType();
			if(typeDeclaration == null)
			{
				continue;
			}

			if(DotNetTypes.System.Reflection.AssemblyTitleAttribute.equals(typeDeclaration.getVmQName()))
			{
				Module attributeModule = ModuleUtilCore.findModuleForPsiElement(attribute);
				if(attributeModule == null || !attributeModule.equals(getModule()))
				{
					continue;
				}
				DotNetExpression[] parameterExpressions = attribute.getParameterExpressions();
				if(parameterExpressions.length == 0)
				{
					break loop;
				}
				String valueAs = new ConstantExpressionEvaluator(parameterExpressions[0]).getValueAs(String.class);
				if(valueAs != null)
				{
					return valueAs;
				}
			}
		}
	}
	return null;
}