use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class CreateHtmlDescriptionFix method createDescription.
private void createDescription(VirtualFile root) {
if (!root.isDirectory())
return;
final PsiManager psiManager = PsiManager.getInstance(myModule.getProject());
final PsiDirectory psiRoot = psiManager.findDirectory(root);
if (psiRoot == null)
return;
PsiDirectory descrRoot = StreamEx.of(psiRoot.getSubdirectories()).findFirst(dir -> getDescriptionFolderName().equals(dir.getName())).orElse(null);
try {
descrRoot = descrRoot == null ? psiRoot.createSubdirectory(getDescriptionFolderName()) : descrRoot;
if (isFixedDescriptionFilename()) {
PsiDirectory dir = descrRoot.findSubdirectory(myFilename);
if (dir == null) {
descrRoot = descrRoot.createSubdirectory(myFilename);
}
}
final FileTemplate descrTemplate = FileTemplateManager.getInstance(myModule.getProject()).getJ2eeTemplate(TEMPLATE_NAME);
final PsiElement template = FileTemplateUtil.createFromTemplate(descrTemplate, getNewFileName(), null, descrRoot);
if (template instanceof PsiFile) {
final VirtualFile file = ((PsiFile) template).getVirtualFile();
if (file != null) {
FileEditorManager.getInstance(myModule.getProject()).openFile(file, true);
}
}
} catch (Exception e) {
//
}
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class PackageTreeExpansionMonitor method install.
public static TreeExpansionMonitor<PackageDependenciesNode> install(final JTree tree, final Project project) {
return new TreeExpansionMonitor<PackageDependenciesNode>(tree) {
@Override
protected TreePath findPathByNode(final PackageDependenciesNode node) {
if (node.getPsiElement() == null) {
return new TreePath(node.getPath());
}
PsiManager manager = PsiManager.getInstance(project);
Enumeration enumeration = ((DefaultMutableTreeNode) tree.getModel().getRoot()).breadthFirstEnumeration();
while (enumeration.hasMoreElements()) {
final Object nextElement = enumeration.nextElement();
if (nextElement instanceof PackageDependenciesNode) {
//do not include root
PackageDependenciesNode child = (PackageDependenciesNode) nextElement;
if (manager.areElementsEquivalent(child.getPsiElement(), node.getPsiElement())) {
return new TreePath(child.getPath());
}
}
}
return null;
}
};
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class DirectoryChooser method fillList.
private void fillList(PsiDirectory[] directories, @Nullable PsiDirectory defaultSelection, Project project, String postfixToShow, Map<PsiDirectory, String> postfixes) {
if (myView.getItemsSize() > 0) {
myView.clearItems();
}
if (defaultSelection == null) {
defaultSelection = getDefaultSelection(directories, project);
if (defaultSelection == null && directories.length > 0) {
defaultSelection = directories[0];
}
}
int selectionIndex = -1;
for (int i = 0; i < directories.length; i++) {
PsiDirectory directory = directories[i];
if (directory.equals(defaultSelection)) {
selectionIndex = i;
break;
}
}
if (selectionIndex < 0 && directories.length == 1) {
selectionIndex = 0;
}
if (selectionIndex < 0) {
// find source root corresponding to defaultSelection
final PsiManager manager = PsiManager.getInstance(project);
VirtualFile[] sourceRoots = ProjectRootManager.getInstance(project).getContentSourceRoots();
for (VirtualFile sourceRoot : sourceRoots) {
if (sourceRoot.isDirectory()) {
PsiDirectory directory = manager.findDirectory(sourceRoot);
if (directory != null && isParent(defaultSelection, directory)) {
defaultSelection = directory;
break;
}
}
}
}
int existingIdx = 0;
for (int i = 0; i < directories.length; i++) {
PsiDirectory directory = directories[i];
final String postfixForDirectory;
if (postfixes == null) {
postfixForDirectory = postfixToShow;
} else {
postfixForDirectory = postfixes.get(directory);
}
final ItemWrapper itemWrapper = new ItemWrapper(directory, postfixForDirectory);
myItems.add(itemWrapper);
if (myFilterExisting) {
if (selectionIndex == i)
selectionIndex = -1;
if (postfixForDirectory != null && directory.getVirtualFile().findFileByRelativePath(StringUtil.trimStart(postfixForDirectory, File.separator)) == null) {
if (isParent(directory, defaultSelection)) {
myDefaultSelection = directory;
}
continue;
}
}
myView.addItem(itemWrapper);
if (selectionIndex < 0 && isParent(directory, defaultSelection)) {
selectionIndex = existingIdx;
}
existingIdx++;
}
buildFragments();
myView.listFilled();
if (myView.getItemsSize() > 0) {
if (selectionIndex != -1) {
myView.selectItemByIndex(selectionIndex);
} else {
myView.selectItemByIndex(0);
}
} else {
myView.clearSelection();
}
enableButtons();
myView.getComponent().repaint();
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class PlatformPackageUtil method findLongestExistingPackage.
@Nullable
private static String findLongestExistingPackage(Project project, String packageName, GlobalSearchScope scope) {
final PsiManager manager = PsiManager.getInstance(project);
DirectoryIndex index = DirectoryIndex.getInstance(project);
String nameToMatch = packageName;
while (true) {
Query<VirtualFile> vFiles = index.getDirectoriesByPackageName(nameToMatch, false);
PsiDirectory directory = getWritableModuleDirectory(vFiles, scope, manager);
if (directory != null)
return index.getPackageName(directory.getVirtualFile());
int lastDotIndex = nameToMatch.lastIndexOf('.');
if (lastDotIndex >= 0) {
nameToMatch = nameToMatch.substring(0, lastDotIndex);
} else {
return null;
}
}
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class ChangeUtil method copyElement.
public static TreeElement copyElement(TreeElement original, final PsiElement context, CharTable table) {
final TreeElement element = (TreeElement) original.clone();
final PsiManager manager = original.getManager();
DummyHolderFactory.createHolder(manager, element, context, table).getTreeElement();
encodeInformation(element, original);
TreeUtil.clearCaches(element);
saveIndentationToCopy(original, element);
return element;
}
Aggregations