Presentation for packages and directories in refactoring preview

This commit is contained in:
Roman Shevchenko
2011-10-18 19:51:04 +02:00
parent 83aad1ae28
commit ff84d55b45
12 changed files with 156 additions and 25 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,14 +15,17 @@
*/
package com.intellij.psi.impl.file;
import com.intellij.navigation.ItemPresentation;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPackage;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
@@ -30,13 +33,13 @@ import org.jetbrains.annotations.NotNull;
public class PsiJavaDirectoryImpl extends PsiDirectoryImpl {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.file.PsiJavaDirectoryImpl");
public PsiJavaDirectoryImpl(PsiManagerImpl manager, VirtualFile file) {
public PsiJavaDirectoryImpl(final PsiManagerImpl manager, final VirtualFile file) {
super(manager, file);
}
@Override
public void checkCreateFile(@NotNull final String name) throws IncorrectOperationException {
FileTypeManager fileTypeManager = FileTypeManager.getInstance();
FileType type = fileTypeManager.getFileTypeByFileName(name);
final FileType type = FileTypeManager.getInstance().getFileTypeByFileName(name);
if (type == StdFileTypes.CLASS) {
throw new IncorrectOperationException("Cannot create class-file");
}
@@ -44,6 +47,7 @@ public class PsiJavaDirectoryImpl extends PsiDirectoryImpl {
super.checkCreateFile(name);
}
@Override
public PsiElement add(@NotNull final PsiElement element) throws IncorrectOperationException {
if (element instanceof PsiClass) {
final String name = ((PsiClass)element).getName();
@@ -61,6 +65,7 @@ public class PsiJavaDirectoryImpl extends PsiDirectoryImpl {
}
}
@Override
public void checkAdd(@NotNull final PsiElement element) throws IncorrectOperationException {
if (element instanceof PsiClass) {
if (((PsiClass)element).getContainingClass() == null) {
@@ -74,4 +79,10 @@ public class PsiJavaDirectoryImpl extends PsiDirectoryImpl {
super.checkAdd(element);
}
}
@Override
public ItemPresentation getPresentation() {
final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(this);
return aPackage != null && !StringUtil.isEmpty(aPackage.getName()) ? aPackage.getPresentation() : super.getPresentation();
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.psi.presentation.java;
import com.intellij.ide.projectView.PresentationData;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProvider;
import com.intellij.psi.PsiPackage;
import com.intellij.util.PlatformIcons;
public class PackagePresentationProvider implements ItemPresentationProvider<PsiPackage> {
@Override
public ItemPresentation getPresentation(final PsiPackage aPackage) {
return new PresentationData(aPackage.getName(), aPackage.getQualifiedName(), PlatformIcons.PACKAGE_OPEN_ICON, PlatformIcons.PACKAGE_ICON, null);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@ package com.intellij.psi.impl.file;
import com.intellij.codeInsight.completion.scope.JavaCompletionHints;
import com.intellij.lang.Language;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.openapi.ui.Queryable;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.vfs.VirtualFile;
@@ -41,12 +43,10 @@ import java.util.List;
import java.util.Set;
public class PsiPackageImpl extends PsiPackageBase implements PsiPackage, Queryable {
private volatile CachedValue<PsiModifierList> myAnnotationList;
private volatile CachedValue<Collection<PsiDirectory>> myDirectories;
private volatile Set<String> myPublicClassNamesCache;
private final Object myPublicClassNamesCacheLock = new String("package classnames cache lock");
private final Object myPublicClassNamesCacheLock = new String("package class names cache lock");
public PsiPackageImpl(PsiManager manager, String qualifiedName) {
super(manager, qualifiedName);
@@ -254,6 +254,11 @@ public class PsiPackageImpl extends PsiPackageBase implements PsiPackage, Querya
return isValid();
}
@Override
public ItemPresentation getPresentation() {
return ItemPresentationProviders.getItemPresentation(this);
}
public void navigate(final boolean requestFocus) {
PsiPackageImplementationHelper.getInstance().navigate(this, requestFocus);
}
@@ -297,5 +302,4 @@ public class PsiPackageImpl extends PsiPackageBase implements PsiPackage, Querya
public PsiQualifiedNamedElement getContainer() {
return getParentPackage();
}
}
@@ -17,6 +17,7 @@ package com.intellij.navigation;
import com.intellij.openapi.util.ClassExtension;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author yole
@@ -28,11 +29,15 @@ public class ItemPresentationProviders extends ClassExtension<ItemPresentationPr
super("com.intellij.itemPresentationProvider");
}
@Nullable
public static <T extends NavigationItem> ItemPresentationProvider<T> getItemPresentationProvider(@NotNull T element) {
return INSTANCE.forClass(element.getClass());
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) final ItemPresentationProvider<T> provider = INSTANCE.forClass(element.getClass());
return provider;
}
@Nullable
public static ItemPresentation getItemPresentation(NavigationItem element) {
return getItemPresentationProvider(element).getPresentation(element);
final ItemPresentationProvider<NavigationItem> provider = getItemPresentationProvider(element);
return provider != null ? provider.getPresentation(element) : null;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,10 @@ import org.jetbrains.annotations.Nullable;
public interface NavigationItem extends Navigatable {
NavigationItem[] EMPTY_NAVIGATION_ITEM_ARRAY = new NavigationItem[0];
@Nullable
String getName();
@Nullable
ItemPresentation getPresentation();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@ package com.intellij.psi.impl.file;
import com.intellij.ide.util.PsiNavigationSupport;
import com.intellij.lang.ASTNode;
import com.intellij.lang.Language;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
@@ -29,7 +31,10 @@ import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.openapi.vfs.NonPhysicalFileSystem;
import com.intellij.openapi.vfs.VfsBundle;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.CheckUtil;
import com.intellij.psi.impl.PsiElementBase;
@@ -546,6 +551,11 @@ public class PsiDirectoryImpl extends PsiElementBase implements PsiDirectory, Qu
return false;
}
@Override
public ItemPresentation getPresentation() {
return ItemPresentationProviders.getItemPresentation(this);
}
public void navigate(boolean requestFocus) {
PsiNavigationSupport.getInstance().navigateToDirectory(this, false);
}
@@ -558,4 +568,3 @@ public class PsiDirectoryImpl extends PsiElementBase implements PsiDirectory, Qu
info.put("fileName", getName());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,14 +19,13 @@ package com.intellij.find.findUsages;
import com.intellij.codeInsight.highlighting.HighlightUsagesHandler;
import com.intellij.find.FindManager;
import com.intellij.find.impl.FindManagerImpl;
import com.intellij.navigation.PsiElementNavigationItem;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.intellij.navigation.PsiElementNavigationItem;
import com.intellij.openapi.actionSystem.DataKey;
import com.intellij.openapi.actionSystem.DataSink;
import com.intellij.openapi.actionSystem.TypeSafeDataProvider;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
@@ -195,7 +194,7 @@ public class PsiElement2UsageTargetAdapter implements PsiElementUsageTarget, Typ
final ItemPresentation presentation = ((NavigationItem)element).getPresentation();
myIconOpen = presentation != null ? ComputableIcon.create(presentation, true) : null;
myIconClosed = presentation != null ? ComputableIcon.create(presentation, false) : null;
myPresentableText = UsageViewUtil.createNodeText(element);
myPresentableText = presentation != null ? presentation.getPresentableText() : UsageViewUtil.createNodeText(element);
if (myIconOpen == null || myIconClosed == null) {
if (element instanceof PsiMetaOwner) {
final PsiMetaOwner psiMetaOwner = (PsiMetaOwner)element;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,6 +46,10 @@ public class ProjectRootsUtil {
return projectFileIndex.isInSourceContent(directoryFile);
}
public static boolean isInTestSource(final PsiDirectory psiDirectory) {
return isInTestSource(psiDirectory.getVirtualFile(), psiDirectory.getProject());
}
public static boolean isInTestSource(final VirtualFile directoryFile, final Project project) {
final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
return projectFileIndex.isInTestSourceContent(directoryFile);
@@ -0,0 +1,60 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.navigation;
import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.projectView.impl.ProjectRootsUtil;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.util.PlatformIcons;
public class DirectoryPresentationProvider implements ItemPresentationProvider<PsiDirectory> {
@Override
public ItemPresentation getPresentation(final PsiDirectory directory) {
final VirtualFile vFile = directory.getVirtualFile();
final String locationString = vFile.getPath();
final Project project = directory.getProject();
if (vFile.equals(project.getBaseDir())) {
return new PresentationData(project.getName(), locationString,
PlatformIcons.PROJECT_ICON, PlatformIcons.PROJECT_ICON, null);
}
if (ProjectRootsUtil.isModuleContentRoot(directory)) {
final Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(vFile);
assert module != null : directory;
return new PresentationData(module.getName(), locationString,
PlatformIcons.CONTENT_ROOT_ICON_OPEN, PlatformIcons.CONTENT_ROOT_ICON_CLOSED, null);
}
if (ProjectRootsUtil.isSourceRoot(directory)) {
if (ProjectRootsUtil.isInTestSource(directory)) {
return new PresentationData(directory.getName(), locationString,
PlatformIcons.MODULES_TEST_SOURCE_FOLDER, PlatformIcons.MODULES_TEST_SOURCE_FOLDER, null);
}
else {
return new PresentationData(directory.getName(), locationString,
PlatformIcons.MODULES_SOURCE_FOLDERS_ICON, PlatformIcons.MODULES_SOURCE_FOLDERS_ICON, null);
}
}
return new PresentationData(directory.getName(), locationString,
PlatformIcons.DIRECTORY_OPEN_ICON, PlatformIcons.DIRECTORY_CLOSED_ICON, null);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package com.intellij.usageView;
import com.intellij.lang.Language;
import com.intellij.lang.findUsages.FindUsagesProvider;
import com.intellij.lang.findUsages.LanguageFindUsages;
import com.intellij.psi.ElementDescriptionLocation;
@@ -31,8 +32,7 @@ import org.jetbrains.annotations.NotNull;
* @author peter
*/
public class UsageViewNodeTextLocation extends ElementDescriptionLocation {
private UsageViewNodeTextLocation() {
}
private UsageViewNodeTextLocation() { }
public static final UsageViewNodeTextLocation INSTANCE = new UsageViewNodeTextLocation();
@@ -52,9 +52,12 @@ public class UsageViewNodeTextLocation extends ElementDescriptionLocation {
}
if (element instanceof PsiFile) {
return ((PsiFile) element).getName();
return ((PsiFile)element).getName();
}
FindUsagesProvider provider = LanguageFindUsages.INSTANCE.forLanguage(element.getLanguage());
Language language = element.getLanguage();
FindUsagesProvider provider = LanguageFindUsages.INSTANCE.forLanguage(language);
assert provider != null : "Element: " + element + ", language: " + language;
return provider.getNodeText(element, true);
}
};
@@ -702,4 +702,7 @@
<projectService serviceInterface="com.intellij.psi.util.PsiModificationTracker"
serviceImplementation="com.intellij.psi.impl.PsiModificationTrackerImpl"/>
<itemPresentationProvider forClass="com.intellij.psi.PsiDirectory"
implementationClass="com.intellij.navigation.DirectoryPresentationProvider"/>
</extensions>
+2
View File
@@ -1126,6 +1126,8 @@
<generation.topLevelFactory language="JAVA" implementationClass="com.intellij.psi.impl.JavaFactoryProvider"/>
<itemPresentationProvider forClass="com.intellij.psi.PsiPackage"
implementationClass="com.intellij.psi.presentation.java.PackagePresentationProvider"/>
<itemPresentationProvider forClass="com.intellij.psi.PsiClass"
implementationClass="com.intellij.psi.presentation.java.ClassPresentationProvider"/>
<itemPresentationProvider forClass="com.intellij.psi.PsiMethod"