public static PsiClass getContainingClass( PsiElement element ) { final PsiClass psiClass = PsiTreeUtil.getParentOfType( element, PsiClass.class, false ); if( psiClass == null ) { final PsiFile containingFile = element.getContainingFile(); if( containingFile instanceof PsiClassOwner ) { final PsiClass[] classes = ((PsiClassOwner)containingFile).getClasses(); if( classes.length == 1 ) { return classes[0]; } } } return psiClass; }
@Override public boolean contains(@NotNull VirtualFile file) { for (VirtualFile scopeDir : myDirs) { boolean inDir = myIncludeSubpackages ? VfsUtilCore.isAncestor(scopeDir, file, false) : Comparing.equal(file.getParent(), scopeDir); if (inDir) return true; } if (myPartOfPackagePrefix && myIncludeSubpackages) { final PsiFile psiFile = myPackage.getManager().findFile(file); if (psiFile instanceof PsiClassOwner) { final String packageName = ((PsiClassOwner)psiFile).getPackageName(); if (myPackageQualifiedName.equals(packageName) || packageName.startsWith(myPackageQNamePrefix)) return true; } } return false; }
@Nullable @Override public XBreakpoint<JavaExceptionBreakpointProperties> addBreakpoint(final Project project, JComponent parentComponent) { final PsiClass throwableClass = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_LANG_THROWABLE, GlobalSearchScope.allScope(project)); TreeClassChooser chooser = TreeClassChooserFactory.getInstance(project) .createInheritanceClassChooser(DebuggerBundle.message("add.exception.breakpoint.classchooser.title"), GlobalSearchScope.allScope(project), throwableClass, true, true, null); chooser.showDialog(); final PsiClass selectedClass = chooser.getSelected(); final String qName = selectedClass == null ? null : JVMNameUtil.getNonAnonymousClassName(selectedClass); if (qName != null && qName.length() > 0) { return ApplicationManager.getApplication().runWriteAction(new Computable<XBreakpoint<JavaExceptionBreakpointProperties>>() { @Override public XBreakpoint<JavaExceptionBreakpointProperties> compute() { return XDebuggerManager.getInstance(project).getBreakpointManager().addBreakpoint( JavaExceptionBreakpointType.this, new JavaExceptionBreakpointProperties(qName, ((PsiClassOwner)selectedClass.getContainingFile()).getPackageName())); } }); } return null; }
@Override public void collectVariables(@NotNull Map<String, Object> context, Project project, Module module, @NotNull final PsiFile file) { if (file instanceof PsiClassOwner) { final FileInfo info = new FileInfo(file) { @Override public String getClassName() { if (file instanceof PsiJavaFile) { return ((PsiJavaFile)file).getClasses()[0].getName(); } else { return super.getClassName(); } } @Override public String getQualifiedClassName() { if (file instanceof PsiJavaFile) { return ((PsiJavaFile)file).getClasses()[0].getQualifiedName(); } else { return super.getQualifiedClassName(); } } }; context.put("file", info); } }
@Test public void testSuiteLocationResolves() { PsiFile javaFile = workspace.createPsiFile( new WorkspacePath("java/com/google/lib/JavaClass.java"), "package com.google.lib;", "import org.junit.Test;", "import org.junit.runner.RunWith;", "import org.junit.runners.JUnit4;", "@RunWith(JUnit4.class)", "public class JavaClass {", " @Test", " public void testMethod() {}", "}"); PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0]; assertThat(javaClass).isNotNull(); String url = handler.suiteLocationUrl(null, "JavaClass"); Location<?> location = getLocation(url); assertThat(location.getPsiElement()).isEqualTo(javaClass); }
@Test public void testMethodLocationResolves() { PsiFile javaFile = workspace.createPsiFile( new WorkspacePath("java/com/google/lib/JavaClass.java"), "package com.google.lib;", "import org.junit.Test;", "import org.junit.runner.RunWith;", "import org.junit.runners.JUnit4;", "@RunWith(JUnit4.class)", "public class JavaClass {", " @Test", " public void testMethod() {}", "}"); PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0]; PsiMethod method = javaClass.findMethodsByName("testMethod", false)[0]; assertThat(method).isNotNull(); String url = handler.testLocationUrl( null, null, "JavaClass-testMethod", "com.google.test.AndroidTestBase"); Location<?> location = getLocation(url); assertThat(location.getPsiElement()).isEqualTo(method); }
@Override public boolean matchesSource( Project project, TargetInfo target, @Nullable PsiFile sourcePsiFile, File sourceFile, @Nullable TestSize testSize) { if (!(sourcePsiFile instanceof PsiClassOwner)) { return false; } String targetName = target.label.targetName().toString(); if (!targetName.contains(".")) { return false; } for (PsiClass psiClass : ((PsiClassOwner) sourcePsiFile).getClasses()) { String fqcn = psiClass.getQualifiedName(); if (fqcn != null && fqcn.endsWith(targetName)) { return true; } } return false; }
@Test public void testReferencesJavaClass() { PsiFile javaFile = workspace.createPsiFile( new WorkspacePath("java/com/google/bin/Main.java"), "package com.google.bin;", "public class Main {", " public void main() {}", "}"); PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0]; BuildFile file = createBuildFile( new WorkspacePath("java/com/google/BUILD"), "java_binary(", " name = 'binary',", " main_class = 'com.google.bin.Main',", ")"); ArgumentList args = file.firstChildOfClass(FuncallExpression.class).getArgList(); assertThat(args.getKeywordArgument("main_class").getValue().getReferencedElement()) .isEqualTo(javaClass); }
@Test public void testIgnoreTestClassWitNoTestSubclasses() { PsiFile javaFile = createAndIndexFile( new WorkspacePath("java/com/google/test/TestClass.java"), "package com.google.test;", "@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)", "public class TestClass {", " @org.junit.Test", " public void testMethod1() {}", " @org.junit.Test", " public void testMethod2() {}", "}"); PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0]; assertThat(javaClass).isNotNull(); ConfigurationContext context = createContextFromPsi(javaClass); ConfigurationFromContext fromContext = new BlazeJavaAbstractTestCaseConfigurationProducer() .createConfigurationFromContext(context); assertThat(fromContext).isNull(); }
@Test public void testMethodLocationResolves() { PsiFile javaFile = workspace.createPsiFile( new WorkspacePath("java/com/google/lib/JavaClass.java"), "package com.google.lib;", "public class JavaClass {", " public void testMethod() {}", "}"); PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0]; PsiMethod method = javaClass.findMethodsByName("testMethod", false)[0]; assertThat(method).isNotNull(); String url = handler.testLocationUrl(null, null, "testMethod", "com.google.lib.JavaClass"); Location<?> location = getLocation(url); assertThat(location.getPsiElement()).isEqualTo(method); }
@Test public void testParameterizedMethodLocationResolves() { PsiFile javaFile = workspace.createPsiFile( new WorkspacePath("java/com/google/lib/JavaClass.java"), "package com.google.lib;", "public class JavaClass {", " public void testMethod() {}", "}"); PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0]; PsiMethod method = javaClass.findMethodsByName("testMethod", false)[0]; assertThat(method).isNotNull(); String url = handler.testLocationUrl( null, "testMethod", "[0] true (testMethod)", "com.google.lib.JavaClass"); Location<?> location = getLocation(url); assertThat(location.getPsiElement()).isEqualTo(method); }
@Override public boolean contains(VirtualFile file) { for (VirtualFile scopeDir : myDirs) { boolean inDir = myIncludeSubpackages ? VfsUtilCore.isAncestor(scopeDir, file, false) : Comparing.equal(file.getParent(), scopeDir); if (inDir) return true; } if (myPartOfPackagePrefix && myIncludeSubpackages) { final PsiFile psiFile = myPackage.getManager().findFile(file); if (psiFile instanceof PsiClassOwner) { final String packageName = ((PsiClassOwner)psiFile).getPackageName(); if (myPackageQualifiedName.equals(packageName) || packageName.startsWith(myPackageQNamePrefix)) return true; } } return false; }
@Override public Breakpoint addBreakpoint(Project project) { ExceptionBreakpoint breakpoint = null; final PsiClass throwableClass = JavaPsiFacade.getInstance(project).findClass("java.lang.Throwable", GlobalSearchScope.allScope(project)); TreeClassChooser chooser = TreeClassChooserFactory.getInstance(project) .createInheritanceClassChooser(DebuggerBundle.message("add.exception.breakpoint.classchooser.title"), GlobalSearchScope.allScope(project), throwableClass, true, true, null); chooser.showDialog(); PsiClass selectedClass = chooser.getSelected(); String qName = selectedClass == null ? null : JVMNameUtil.getNonAnonymousClassName(selectedClass); if (qName != null && qName.length() > 0) { breakpoint = DebuggerManagerEx.getInstanceEx(project).getBreakpointManager() .addExceptionBreakpoint(qName, ((PsiClassOwner)selectedClass.getContainingFile()).getPackageName()); } return breakpoint; }
@Override protected boolean isOutOfCodeBlock(@NotNull PsiElement element) { for(PsiElement e : SyntaxTraverser.psiApi().parents(element)) { if(e instanceof PsiModifiableCodeBlock) { // trigger OOCBM for final variables initialized in constructors & class initializers if(!((PsiModifiableCodeBlock) e).shouldChangeModificationCount(element)) { return false; } } if(e instanceof PsiClass) { break; } if(e instanceof PsiClassOwner) { break; } } return true; }
@Override public void update(AnActionEvent e) { e.getPresentation().setEnabled(false); e.getPresentation().setIcon(AllIcons.Toolwindows.Documentation); final Project project = e.getData(CommonDataKeys.PROJECT); if(project != null) { final PsiElement psiElement = getPsiElement(e.getDataContext(), project, e.getData(CommonDataKeys.EDITOR)); if(psiElement != null) { if(psiElement.getContainingFile() instanceof PsiClassOwner && ByteCodeViewerManager.getContainingClass(psiElement) != null) { e.getPresentation().setEnabled(true); } } } }
@Nullable @Override public XBreakpoint<JavaExceptionBreakpointProperties> addBreakpoint(final Project project, JComponent parentComponent) { final PsiClass throwableClass = JavaPsiFacade.getInstance(project).findClass("java.lang.Throwable", GlobalSearchScope.allScope(project)); TreeClassChooser chooser = TreeClassChooserFactory.getInstance(project).createInheritanceClassChooser(DebuggerBundle.message("add.exception" + ".breakpoint.classchooser.title"), GlobalSearchScope.allScope(project), throwableClass, true, true, null); chooser.showDialog(); final PsiClass selectedClass = chooser.getSelected(); final String qName = selectedClass == null ? null : JVMNameUtil.getNonAnonymousClassName(selectedClass); if(qName != null && qName.length() > 0) { return ApplicationManager.getApplication().runWriteAction(new Computable<XBreakpoint<JavaExceptionBreakpointProperties>>() { @Override public XBreakpoint<JavaExceptionBreakpointProperties> compute() { return XDebuggerManager.getInstance(project).getBreakpointManager().addBreakpoint(JavaExceptionBreakpointType.this, new JavaExceptionBreakpointProperties(qName, ((PsiClassOwner) selectedClass.getContainingFile()).getPackageName())); } }); } return null; }
public static Location stepIntoSingleClass(final Location location) { PsiElement element = location.getPsiElement(); if(!(element instanceof PsiClassOwner)) { if(PsiTreeUtil.getParentOfType(element, PsiClass.class) != null) { return location; } element = PsiTreeUtil.getParentOfType(element, PsiClassOwner.class); if(element == null) { return location; } } final PsiClassOwner psiFile = (PsiClassOwner) element; final PsiClass[] classes = psiFile.getClasses(); if(classes.length != 1) { return location; } return PsiLocation.fromPsiElement(classes[0]); }
@Override public ItemPresentation getPresentation(@NotNull final PsiClass psiClass) { return new ColoredItemPresentation() { @Override public String getPresentableText() { return ClassPresentationUtil.getNameForClass(psiClass, false); } @Override public String getLocationString() { PsiFile file = psiClass.getContainingFile(); if (file instanceof PsiClassOwner) { PsiClassOwner classOwner = (PsiClassOwner)file; String packageName = classOwner.getPackageName(); if (packageName.isEmpty()) return null; return "(" + packageName + ")"; } return null; } @Override public TextAttributesKey getTextAttributesKey() { try { if (psiClass.isDeprecated()) { return CodeInsightColors.DEPRECATED_ATTRIBUTES; } } catch (IndexNotReadyException ignore) { } return null; } @Override public Icon getIcon(boolean open) { return psiClass.getIcon(Iconable.ICON_FLAG_VISIBILITY | Iconable.ICON_FLAG_READ_STATUS); } }; }
@Override @Nullable public StructureViewBuilder getStructureViewBuilder(@NotNull FileType fileType, @NotNull VirtualFile file, @NotNull Project project) { final PsiFile psiFile = PsiManager.getInstance(project).findFile(file); if (fileType == JavaClassFileType.INSTANCE && psiFile != null) { Language language = psiFile.getLanguage(); if (language != JavaLanguage.INSTANCE) { PsiStructureViewFactory factory = LanguageStructureViewBuilder.INSTANCE.forLanguage(language); if (factory != null) { return factory.getStructureViewBuilder(psiFile); } } } if (psiFile instanceof PsiClassOwner) { return new TreeBasedStructureViewBuilder() { @Override @NotNull public StructureViewModel createStructureViewModel(@Nullable Editor editor) { return new JavaFileTreeModel((PsiClassOwner)psiFile, editor); } }; } return null; }
@Nullable public static String getPackageName(final PsiClass psiClass) { final PsiFile file = psiClass.getContainingFile(); if (file instanceof PsiClassOwner){ return ((PsiClassOwner)file).getPackageName(); } else{ return null; } }
public void update(AnActionEvent e) { PsiFile file = e.getData(CommonDataKeys.PSI_FILE); Presentation presentation = e.getPresentation(); String place = e.getPlace(); boolean enabled = file != null && (file instanceof PsiClassOwner || ActionPlaces.EDITOR_TAB_POPUP.equals(place)) && place != ActionPlaces.EDITOR_POPUP && e.getData(CommonDataKeys.PROJECT) != null; presentation.setEnabled(enabled); presentation.setVisible(enabled); if (enabled) { presentation.setText(RENAME_FILE); presentation.setDescription("Rename selected file"); } }
@Nullable private Result<PsiFile> getSourceFileResult(ClsFileImpl clsFile, VirtualFile root) { // This code is adapted from JavaPsiImplementationHelperImpl#getClsFileNavigationElement PsiClass[] classes = clsFile.getClasses(); if (classes.length == 0) { return null; } String sourceFileName = ((ClsClassImpl) classes[0]).getSourceFileName(); String packageName = clsFile.getPackageName(); String relativePath = packageName.isEmpty() ? sourceFileName : packageName.replace('.', '/') + '/' + sourceFileName; VirtualFile source = root.findFileByRelativePath(relativePath); if (source != null && source.isValid()) { // Since we have an actual source jar tracked down, use that source jar as the modification // tracker. This means the result will continue to be cached unless that source jar changes. // If we didn't find a source jar, we use a modification tracker that invalidates on every // Blaze sync, which is less efficient. PsiFile psiSource = clsFile.getManager().findFile(source); if (psiSource instanceof PsiClassOwner) { return Result.create(psiSource, source); } return Result.create(null, source); } return null; }
private void indexQualifiedClassNames(PsiFile psiFile) { if (!(psiFile instanceof PsiClassOwner)) { return; } ReadAction.run( () -> { for (PsiClass psiClass : ((PsiClassOwner) psiFile).getClasses()) { qualifiedNameToPsiFile.put(psiClass.getQualifiedName(), psiFile); } }); }
private static boolean collectContextElements( DataContext dataContext, boolean checkAbstract, boolean checkIsTest, PsiElementProcessor.CollectElements<PsiElement> processor) { PsiElement[] elements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext); if (elements != null) { collectTestMembers(elements, checkAbstract, checkIsTest, processor); return true; } else { final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext); if (files != null) { Project project = CommonDataKeys.PROJECT.getData(dataContext); if (project != null) { final PsiManager psiManager = PsiManager.getInstance(project); for (VirtualFile file : files) { final PsiFile psiFile = psiManager.findFile(file); if (psiFile instanceof PsiClassOwner) { collectTestMembers( ((PsiClassOwner) psiFile).getClasses(), checkAbstract, checkIsTest, processor); } } return true; } } } return false; }
@Test public void testParameterizedMethods() { PsiFile javaFile = workspace.createPsiFile( new WorkspacePath("java/com/google/lib/JavaClass.java"), "package com.google.lib;", "import org.junit.Test;", "import org.junit.runner.RunWith;", "import org.junit.runners.JUnit4;", "@RunWith(JUnit4.class)", "public class JavaClass {", " @Test", " public void testMethod1() {}", " @Test", " public void testMethod2() {}", "}"); PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0]; PsiMethod method1 = javaClass.findMethodsByName("testMethod1", false)[0]; Location<?> location1 = new PsiMemberParameterizedLocation(getProject(), method1, javaClass, "[param]"); PsiMethod method2 = javaClass.findMethodsByName("testMethod2", false)[0]; Location<?> location2 = new PsiMemberParameterizedLocation(getProject(), method2, javaClass, "[3]"); assertThat( BlazeJUnitTestFilterFlags.testFilterForClassesAndMethods( ImmutableMap.of(javaClass, ImmutableList.of(location1, location2)))) .isEqualTo("com.google.lib.JavaClass#(testMethod1\\[param\\]|testMethod2\\[3\\])$"); }
@Test public void testSuiteLocationResolves() { PsiFile javaFile = workspace.createPsiFile( new WorkspacePath("java/com/google/lib/JavaClass.java"), "package com.google.lib;", "public class JavaClass {}"); PsiClass javaClass = ((PsiClassOwner) javaFile).getClasses()[0]; assertThat(javaClass).isNotNull(); String url = handler.suiteLocationUrl(null, "com.google.lib.JavaClass"); Location<?> location = getLocation(url); assertThat(location.getPsiElement()).isEqualTo(javaClass); }
@Override public ItemPresentation getPresentation(final PsiClass psiClass) { return new ColoredItemPresentation() { @Override public String getPresentableText() { return ClassPresentationUtil.getNameForClass(psiClass, false); } @Override public String getLocationString() { PsiFile file = psiClass.getContainingFile(); if (file instanceof PsiClassOwner) { PsiClassOwner classOwner = (PsiClassOwner)file; String packageName = classOwner.getPackageName(); if (packageName.length() == 0) return null; return "(" + packageName + ")"; } return null; } @Override public TextAttributesKey getTextAttributesKey() { if (psiClass.isDeprecated()) { return CodeInsightColors.DEPRECATED_ATTRIBUTES; } return null; } @Override public Icon getIcon(boolean open) { return psiClass.getIcon(Iconable.ICON_FLAG_VISIBILITY | Iconable.ICON_FLAG_READ_STATUS); } }; }
public void update(AnActionEvent e) { PsiFile file = e.getData(LangDataKeys.PSI_FILE); Presentation presentation = e.getPresentation(); boolean enabled = file instanceof PsiClassOwner && e.getPlace() != ActionPlaces.EDITOR_POPUP && e.getData(PlatformDataKeys.PROJECT) != null; presentation.setEnabled(enabled); presentation.setVisible(enabled); if (enabled) { presentation.setText(RENAME_FILE); presentation.setDescription("Rename selected file"); } }
@Override public ItemPresentation getPresentation(final PsiClass psiClass) { return new ColoredItemPresentation() { @Override public String getPresentableText() { return ClassPresentationUtil.getNameForClass(psiClass, false); } @Override public String getLocationString() { PsiFile file = psiClass.getContainingFile(); if (file instanceof PsiClassOwner) { PsiClassOwner classOwner = (PsiClassOwner)file; String packageName = classOwner.getPackageName(); if (packageName.length() == 0) return null; return "(" + packageName + ")"; } return null; } @Override public TextAttributesKey getTextAttributesKey() { if (psiClass.isDeprecated()) { return CodeInsightColors.DEPRECATED_ATTRIBUTES; } return null; } @Override public Icon getIcon(boolean open) { return IconDescriptorUpdaters.getIcon(psiClass, Iconable.ICON_FLAG_VISIBILITY | Iconable.ICON_FLAG_READ_STATUS); } }; }
@Nullable public static String getContainerTextStatic(final PsiElement element) { PsiFile file = element.getContainingFile(); if(file instanceof PsiClassOwner) { String packageName = ((PsiClassOwner) file).getPackageName(); if(packageName.isEmpty()) { return null; } return "(" + packageName + ")"; } return null; }
@Override public PsiElement getElementToSelect(Object object) { PsiElement psiElement = super.getElementToSelect(object); if (psiElement != null) { final PsiFile containingFile = psiElement.getContainingFile(); if (containingFile instanceof PsiClassOwner) { final PsiClass[] classes = ((PsiClassOwner)containingFile).getClasses(); if (classes.length == 1) return classes[0]; for (PsiClass aClass : classes) { if (PsiTreeUtil.isAncestor(aClass, psiElement, false)) return aClass; } } } return psiElement; }
@Override public boolean canSelectInCoverageView(Object object) { final PsiFile psiFile = object instanceof VirtualFile ? PsiManager.getInstance(getProject()).findFile((VirtualFile)object) : null; if (psiFile instanceof PsiClassOwner) { final String packageName = ((PsiClassOwner)psiFile).getPackageName(); return isInCoverageScope(JavaPsiFacade.getInstance(getProject()).findPackage(packageName), getSuitesBundle()); } if (object instanceof PsiPackage) { return isInCoverageScope((PsiElement)object, getSuitesBundle()); } return false; }
@Nullable private static PsiClass getContainingClass(PsiElement element) { final PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class, false); if (psiClass == null) { final PsiFile containingFile = element.getContainingFile(); if (containingFile instanceof PsiClassOwner) { final PsiClass[] classes = ((PsiClassOwner)containingFile).getClasses(); if (classes.length == 1) { return classes[0]; } } } return psiClass; }
@Override protected void preparePackage() throws OperationFailedException { final String targetPackageName = getTargetPackageName(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(myProject); final PsiFile containingFile = mySourceClass.getContainingFile(); final boolean fromDefaultPackage = containingFile instanceof PsiClassOwner && ((PsiClassOwner)containingFile).getPackageName().isEmpty(); if (!(fromDefaultPackage && StringUtil.isEmpty(targetPackageName)) && !PsiNameHelper.getInstance(myProject).isQualifiedName(targetPackageName)) { throw new OperationFailedException("Invalid package name: " + targetPackageName); } final PsiJavaPackage aPackage = psiFacade.findPackage(targetPackageName); if (aPackage != null) { final PsiDirectory[] directories = aPackage.getDirectories(mySourceClass.getResolveScope()); if (directories.length >= 1) { myTargetDirectory = getDirUnderSameSourceRoot(directories); } } final MoveDestination moveDestination = myDestinationFolderComboBox.selectDirectory(new PackageWrapper(PsiManager.getInstance(myProject), targetPackageName), false); if (moveDestination == null) return; myTargetDirectory = myTargetDirectory != null ? ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() { @Override public PsiDirectory compute() { return moveDestination.getTargetDirectory(getTargetDirectory()); } }) : null; if (myTargetDirectory == null) { throw new OperationFailedException(""); // message already reported by PackageUtil } String error = RefactoringMessageUtil.checkCanCreateClass(myTargetDirectory, getExtractedSuperName()); if (error != null) { throw new OperationFailedException(error); } }
public void update(AnActionEvent e) { PsiFile file = e.getData(LangDataKeys.PSI_FILE); Presentation presentation = e.getPresentation(); boolean enabled = file instanceof PsiClassOwner && e.getPlace() != ActionPlaces.EDITOR_POPUP && e.getData(CommonDataKeys.PROJECT) != null; presentation.setEnabled(enabled); presentation.setVisible(enabled); if (enabled) { presentation.setText(RENAME_FILE); presentation.setDescription("Rename selected file"); } }
@Nullable private static PsiFile obtainContainingFile(PsiElement element, PsiElement[] elements) { final PsiClass[] classes = ((PsiClassOwner)element.getParent()).getClasses(); final Set<PsiClass> nonMovedClasses = new HashSet<PsiClass>(); for (PsiClass aClass : classes) { if (ArrayUtil.find(elements, aClass) < 0) { nonMovedClasses.add(aClass); } } final PsiFile containingFile = element.getContainingFile(); if (nonMovedClasses.isEmpty()) { return containingFile; } else { final PsiDirectory containingDirectory = containingFile.getContainingDirectory(); if (containingDirectory != null) { try { JavaDirectoryServiceImpl.checkCreateClassOrInterface(containingDirectory, ((PsiClass)element).getName()); final PsiElement createdClass = containingDirectory.add(element); element.delete(); return createdClass.getContainingFile(); } catch (IncorrectOperationException e) { final Iterator<PsiClass> iterator = nonMovedClasses.iterator(); final PsiClass nonMovedClass = iterator.next(); final PsiElement createdFile = containingDirectory.add(nonMovedClass).getContainingFile(); nonMovedClass.delete(); while (iterator.hasNext()) { final PsiClass currentClass = iterator.next(); createdFile.add(currentClass); currentClass.delete(); } return containingFile; } } } return null; }
public JavaFileTreeElement(PsiClassOwner file) { super(file); }