Skip to content

Commit

Permalink
Add useful code snippets for developers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanFoster committed Aug 17, 2013
1 parent 2768bc9 commit bb3f7f5
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cheatSheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Useful References
==================

Find Module for PsiElement

```java
Module moduleForDomElement = ModuleUtil.findModuleForPsiElement(domElement.getXmlElement());
Module moduleForPsiFile = ModuleUtil.findModuleForPsiElement(psiFile);
```

Find virtual files by type

```java
Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(
FileTypeIndex.NAME, StdFileTypes.JAVA, GlobalSearchScope.moduleScope(moduleForPsiElement));
```

Get JavaPsiFile with Module scope from VirtualFile

```java
Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(
FileTypeIndex.NAME, StdFileTypes.JAVA, GlobalSearchScope.moduleScope(module));

VirtualFile virtualFile = virtualFiles.iterator().next();

PsiFile file = PsiManager.getInstance(project)
.findFile(virtualFile);

PsiJavaFile javaFile = (PsiJavaFile) file;
```

Find Java class by name, with specified scope

```java
final PsiClass psiClass = JavaPsiFacade.getInstance(module.getProject())
.findClass("fully.qualified.name.Foo", module.getModuleWithDependenciesAndLibrariesScope(true));
```

0 comments on commit bb3f7f5

Please sign in to comment.