use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class PackageViewProjectNode method getChildren.
@Override
@NotNull
public Collection<AbstractTreeNode> getChildren() {
if (getSettings().isShowModules()) {
final List<Module> allModules = new ArrayList<>(Arrays.asList(ModuleManager.getInstance(getProject()).getModules()));
for (Iterator<Module> it = allModules.iterator(); it.hasNext(); ) {
final Module module = it.next();
final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
if (sourceRoots.length == 0) {
// do not show modules with no source roots configured
it.remove();
}
}
return modulesAndGroups(allModules.toArray(new Module[allModules.size()]));
} else {
final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
final PsiManager psiManager = PsiManager.getInstance(myProject);
final List<AbstractTreeNode> children = new ArrayList<>();
final Set<PsiPackage> topLevelPackages = new HashSet<>();
for (final VirtualFile root : projectRootManager.getContentSourceRoots()) {
final PsiDirectory directory = psiManager.findDirectory(root);
if (directory == null) {
continue;
}
final PsiPackage directoryPackage = JavaDirectoryService.getInstance().getPackage(directory);
if (directoryPackage == null || PackageUtil.isPackageDefault(directoryPackage)) {
// add subpackages
final PsiDirectory[] subdirectories = directory.getSubdirectories();
for (PsiDirectory subdirectory : subdirectories) {
final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(subdirectory);
if (aPackage != null && !PackageUtil.isPackageDefault(aPackage)) {
topLevelPackages.add(aPackage);
}
}
// add non-dir items
children.addAll(ProjectViewDirectoryHelper.getInstance(myProject).getDirectoryChildren(directory, getSettings(), false));
} else {
// this is the case when a source root has pakage prefix assigned
topLevelPackages.add(directoryPackage);
}
}
for (final PsiPackage psiPackage : topLevelPackages) {
PackageUtil.addPackageAsChild(children, psiPackage, null, getSettings(), false);
}
if (getSettings().isShowLibraryContents()) {
children.add(new PackageViewLibrariesNode(getProject(), null, getSettings()));
}
return children;
}
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class RelaxSymbolIndex method getSymbolsByName.
public static NavigationItem[] getSymbolsByName(final String name, Project project, boolean includeNonProjectItems) {
final GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
final Collection<NavigationItem> result = new ArrayList<>();
PsiManager psiManager = PsiManager.getInstance(project);
for (VirtualFile file : FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope)) {
final PsiFile psiFile = psiManager.findFile(file);
if (psiFile instanceof XmlFile) {
final Grammar grammar = GrammarFactory.getGrammar((XmlFile) psiFile);
if (grammar != null) {
grammar.acceptChildren(new CommonElement.Visitor() {
@Override
public void visitDefine(Define define) {
if (name.equals(define.getName())) {
final PsiElement psi = define.getPsiElement();
if (psi != null) {
MyNavigationItem.add((NavigationItem) define.getPsiElement(), result);
}
}
}
});
}
}
}
return result.toArray(new NavigationItem[result.size()]);
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class RelaxIncludeIndex method processRelatedFiles.
private static boolean processRelatedFiles(PsiFile file, VirtualFile[] files, PsiElementProcessor<XmlFile> processor) {
Project project = file.getProject();
final PsiManager psiManager = PsiManager.getInstance(project);
final PsiFile[] psiFiles = ContainerUtil.map2Array(files, PsiFile.class, (NullableFunction<VirtualFile, PsiFile>) file1 -> psiManager.findFile(file1));
for (final PsiFile psiFile : psiFiles) {
if (!processFile(psiFile, processor)) {
return false;
}
}
return true;
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class RncFileReferenceManipulator method handleContentChange.
@Override
public RncFileReference handleContentChange(@NotNull RncFileReference element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
final ASTNode node = element.getNode();
assert node != null;
final ASTNode literal = node.findChildByType(RncTokenTypes.LITERAL);
if (literal != null) {
assert range.equals(element.getReferenceRange());
final PsiManager manager = element.getManager();
final ASTNode newChild = RenameUtil.createLiteralNode(manager, newContent);
literal.getTreeParent().replaceChild(literal, newChild);
}
return element;
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class PyTokenSeparatorGenerator method generateWhitespaceBetweenTokens.
public ASTNode generateWhitespaceBetweenTokens(ASTNode left, ASTNode right) {
PsiManager manager = right.getTreeParent().getPsi().getManager();
if (left.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
return createLineBreak(manager);
}
if (left.getPsi().isValid() && right.getPsi().isValid()) {
final PsiElement commonParent = PsiTreeUtil.findCommonParent(left.getPsi(), right.getPsi());
if (commonParent == null)
return null;
final PsiElement leftPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, left.getPsi());
final PsiElement rightPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, right.getPsi());
if (isStatementOrFunction(leftPrevAncestor) && isStatementOrFunction(rightPrevAncestor)) {
int leftIndent = PyPsiUtils.getElementIndentation(leftPrevAncestor);
int rightIndent = PyPsiUtils.getElementIndentation(rightPrevAncestor);
int maxIndent = Math.max(leftIndent, rightIndent);
return createWhitespace(manager, "\n" + StringUtil.repeatSymbol(' ', maxIndent));
}
}
if (right.getElementType() == PyTokenTypes.DEF_KEYWORD || right.getElementType() == PyTokenTypes.CLASS_KEYWORD) {
return createLineBreak(manager);
}
if (left.getElementType() == TokenType.WHITE_SPACE || right.getElementType() == TokenType.WHITE_SPACE) {
return null;
}
final PyStatement leftStatement = PsiTreeUtil.getParentOfType(left.getPsi(), PyStatement.class);
if (leftStatement != null && !PsiTreeUtil.isAncestor(leftStatement, right.getPsi(), false)) {
return createLineBreak(manager);
}
final Lexer lexer = new PythonIndentingLexer();
if (LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer) == ParserDefinition.SpaceRequirements.MUST) {
return createSpace(manager);
}
return null;
}
Aggregations