From 99954a6c04d9694710980cabf63a684c9e802091 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 13 Oct 2016 11:14:50 +0200 Subject: [PATCH] Cleanup (duplicates; warnings; typos; formatting) --- .../lang/java/JavaFindUsagesProvider.java | 102 +++++++----------- .../rename/PsiPackageRenameValidator.java | 32 +++--- .../refactoring/rename/RenameDialog.java | 85 +++++++-------- resources/src/META-INF/IdeaPlugin.xml | 3 +- 4 files changed, 98 insertions(+), 124 deletions(-) diff --git a/java/java-impl/src/com/intellij/lang/java/JavaFindUsagesProvider.java b/java/java-impl/src/com/intellij/lang/java/JavaFindUsagesProvider.java index 14e35dba6bb5..84951dce2bcd 100644 --- a/java/java-impl/src/com/intellij/lang/java/JavaFindUsagesProvider.java +++ b/java/java-impl/src/com/intellij/lang/java/JavaFindUsagesProvider.java @@ -160,9 +160,9 @@ public class JavaFindUsagesProvider implements FindUsagesProvider { return "anonymous " + StringUtil.notNullize(name, "class"); } else { - final PsiClass aClass = (PsiClass)element; - String qName = aClass.getQualifiedName(); - return qName == null ? aClass.getName() : qName; + PsiClass aClass = (PsiClass)element; + String qName = aClass.getQualifiedName(); + return qName != null ? qName : aClass.getName() != null ? aClass.getName() : ""; } } if (element instanceof PsiMethod) { @@ -226,15 +226,19 @@ public class JavaFindUsagesProvider implements FindUsagesProvider { if (element instanceof PsiDirectory) { return getPackageName((PsiDirectory)element, false); } + if (element instanceof PsiPackage) { return getPackageName((PsiPackage)element); } + if (element instanceof PsiFile) { return useFullName ? ((PsiFile)element).getVirtualFile().getPresentableUrl() : ((PsiFile)element).getName(); } + if (element instanceof PsiLabeledStatement) { return ((PsiLabeledStatement)element).getLabelIdentifier().getText(); } + if (ThrowSearchUtil.isSearchable(element)) { return ThrowSearchUtil.getSearchableTypeName(element); } @@ -246,87 +250,55 @@ public class JavaFindUsagesProvider implements FindUsagesProvider { } if (name != null) return name; } + if (element instanceof PsiMethod) { PsiMethod psiMethod = (PsiMethod)element; if (useFullName) { - String s = PsiFormatUtil.formatMethod((PsiMethod)element, - PsiSubstitutor.EMPTY, PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | - PsiFormatUtilBase.SHOW_NAME | - PsiFormatUtilBase.SHOW_PARAMETERS, - PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME); - final PsiClass psiClass = psiMethod.getContainingClass(); - if (psiClass != null) { - final String qName = psiClass.getQualifiedName(); - if (qName != null) { - if (psiClass.isInterface()) { - s = LangBundle.message("java.terms.of.interface", s, qName); - } - else { - s = LangBundle.message("java.terms.of.class", s, qName); - } - } - } - return s; + int options = PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS; + String s = PsiFormatUtil.formatMethod((PsiMethod)element, PsiSubstitutor.EMPTY, options, PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME); + return appendClassName(s, psiMethod.getContainingClass()); } else { - return PsiFormatUtil.formatMethod(psiMethod, - PsiSubstitutor.EMPTY, - PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, - PsiFormatUtilBase.SHOW_TYPE); + int options = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS; + return PsiFormatUtil.formatMethod(psiMethod, PsiSubstitutor.EMPTY, options, PsiFormatUtilBase.SHOW_TYPE); } } + if (element instanceof PsiParameter && ((PsiParameter)element).getDeclarationScope() instanceof PsiMethod) { PsiMethod method = (PsiMethod)((PsiParameter)element).getDeclarationScope(); + int varOptions = PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME; + int methodOptions = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS; String s = LangBundle.message("java.terms.variable.of.method", - PsiFormatUtil.formatVariable((PsiVariable)element, - PsiFormatUtilBase.TYPE_AFTER | - PsiFormatUtilBase.SHOW_TYPE | - PsiFormatUtilBase.SHOW_NAME, - PsiSubstitutor.EMPTY), - PsiFormatUtil.formatMethod(method, - PsiSubstitutor.EMPTY, - PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, - PsiFormatUtilBase.SHOW_TYPE)); - - final PsiClass psiClass = method.getContainingClass(); - if (psiClass != null && psiClass.getQualifiedName() != null) { - if (psiClass.isInterface()) { - s = LangBundle.message("java.terms.of.interface", s, psiClass.getQualifiedName()); - } - else { - s = LangBundle.message("java.terms.of.class", s, psiClass.getQualifiedName()); - } - } - return s; + PsiFormatUtil.formatVariable((PsiVariable)element, varOptions, PsiSubstitutor.EMPTY), + PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, methodOptions, PsiFormatUtilBase.SHOW_TYPE)); + return appendClassName(s, method.getContainingClass()); } + if (element instanceof PsiField) { PsiField psiField = (PsiField)element; - String s = PsiFormatUtil.formatVariable(psiField, - PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME, - PsiSubstitutor.EMPTY); - PsiClass psiClass = psiField.getContainingClass(); - if (psiClass != null) { - String qName = psiClass.getQualifiedName(); - if (qName != null) { - if (psiClass.isInterface()) { - s = LangBundle.message("java.terms.of.interface", s, qName); - } - else { - s = LangBundle.message("java.terms.of.class", s, qName); - } - } - } - return s; + int options = PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME; + String s = PsiFormatUtil.formatVariable(psiField, options, PsiSubstitutor.EMPTY); + return appendClassName(s, psiField.getContainingClass()); } + if (element instanceof PsiVariable) { - return PsiFormatUtil.formatVariable((PsiVariable)element, - PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME, - PsiSubstitutor.EMPTY); + int options = PsiFormatUtilBase.TYPE_AFTER | PsiFormatUtilBase.SHOW_TYPE | PsiFormatUtilBase.SHOW_NAME; + return PsiFormatUtil.formatVariable((PsiVariable)element, options, PsiSubstitutor.EMPTY); } return ""; } + private static String appendClassName(String s, PsiClass psiClass) { + if (psiClass != null) { + String qName = psiClass.getQualifiedName(); + if (qName != null) { + s = LangBundle.message(psiClass.isInterface() ? "java.terms.of.interface" : "java.terms.of.class", s, qName); + } + } + return s; + } + public static String getPackageName(PsiDirectory directory, boolean includeRootDir) { PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(directory); if (aPackage == null) { @@ -374,4 +346,4 @@ public class JavaFindUsagesProvider implements FindUsagesProvider { public WordsScanner getWordsScanner() { return null; } -} +} \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/refactoring/rename/PsiPackageRenameValidator.java b/java/java-impl/src/com/intellij/refactoring/rename/PsiPackageRenameValidator.java index fda2e7312092..3ddc3eeeca65 100644 --- a/java/java-impl/src/com/intellij/refactoring/rename/PsiPackageRenameValidator.java +++ b/java/java-impl/src/com/intellij/refactoring/rename/PsiPackageRenameValidator.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2016 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,40 +19,44 @@ import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.project.Project; import com.intellij.patterns.ElementPattern; import com.intellij.patterns.PlatformPatterns; -import com.intellij.psi.JavaPsiFacade; import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiNameHelper; import com.intellij.psi.PsiPackage; import com.intellij.psi.impl.file.PsiDirectoryFactory; import com.intellij.util.ProcessingContext; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** - * User: anna - * Date: 3/14/11 + * @author anna + * @since 14.03.2011 */ public class PsiPackageRenameValidator implements RenameInputValidatorEx { + private final ElementPattern myPattern = PlatformPatterns.psiElement(PsiPackage.class); + + @NotNull + @Override + public ElementPattern getPattern() { + return myPattern; + } + @Nullable @Override - public String getErrorMessage(String newName, Project project) { + public String getErrorMessage(@NotNull String newName, @NotNull Project project) { if (FileTypeManager.getInstance().isFileIgnored(newName)) { return "Trying to create a package with ignored name, result will not be visible"; } + if (newName.length() > 0) { if (!PsiDirectoryFactory.getInstance(project).isValidPackageName(newName)) { return "Not a valid package name"; } } + return null; } @Override - public ElementPattern getPattern() { - return PlatformPatterns.psiElement(PsiPackage.class); + public boolean isInputValid(@NotNull String newName, @NotNull PsiElement element, @NotNull ProcessingContext context) { + return !newName.isEmpty(); } - - @Override - public boolean isInputValid(String newName, PsiElement element, ProcessingContext context) { - return newName != null && newName.length() > 0; - } -} +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameDialog.java b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameDialog.java index 5adfc5107abd..9c031a89bf81 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameDialog.java +++ b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameDialog.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.refactoring.rename; import com.intellij.lang.findUsages.DescriptiveNameUtil; @@ -27,6 +26,7 @@ import com.intellij.openapi.help.HelpManager; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.SuggestedNameInfo; @@ -40,6 +40,7 @@ import com.intellij.ui.NonFocusableCheckBox; import com.intellij.usageView.UsageViewUtil; import com.intellij.util.ArrayUtil; import com.intellij.util.Function; +import com.intellij.util.ui.JBUI; import com.intellij.xml.util.XmlStringUtil; import com.intellij.xml.util.XmlTagUtilBase; import org.jetbrains.annotations.NotNull; @@ -53,24 +54,23 @@ import java.util.Map; public class RenameDialog extends RefactoringDialog { private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.rename.RenameDialog"); - private SuggestedNameInfo mySuggestedNameInfo; + private static final String REFACTORING_NAME = RefactoringBundle.message("rename.title"); + private SuggestedNameInfo mySuggestedNameInfo; private JLabel myNameLabel; private NameSuggestionsField myNameSuggestionsField; private JCheckBox myCbSearchInComments; - private JCheckBox myCbSearchTextOccurences; + private JCheckBox myCbSearchTextOccurrences; private final JLabel myNewNamePrefix = new JLabel(""); private final String myHelpID; - @NotNull private final PsiElement myPsiElement; + private final PsiElement myPsiElement; private final PsiElement myNameSuggestionContext; private final Editor myEditor; - private static final String REFACTORING_NAME = RefactoringBundle.message("rename.title"); private NameSuggestionsField.DataChanged myNameChangedListener; - private final Map myAutomaticRenamers = new HashMap<>(); + private final Map myAutoRenamerFactories = new HashMap<>(); private String myOldName; - public RenameDialog(@NotNull Project project, @NotNull PsiElement psiElement, @Nullable PsiElement nameSuggestionContext, - Editor editor) { + public RenameDialog(@NotNull Project project, @NotNull PsiElement psiElement, @Nullable PsiElement nameSuggestionContext, Editor editor) { super(project, true); assert psiElement.isValid(); @@ -87,9 +87,9 @@ public class RenameDialog extends RefactoringDialog { boolean toSearchInComments = isToSearchInCommentsForRename(); myCbSearchInComments.setSelected(toSearchInComments); - if (myCbSearchTextOccurences.isEnabled()) { - boolean toSearchForTextOccurences = isToSearchForTextOccurencesForRename(); - myCbSearchTextOccurences.setSelected(toSearchForTextOccurences); + if (myCbSearchTextOccurrences.isEnabled()) { + boolean toSearchForTextOccurrences = isToSearchForTextOccurrencesForRename(); + myCbSearchTextOccurrences.setSelected(toSearchForTextOccurrences); } if (!ApplicationManager.getApplication().isUnitTestMode()) validateButtons(); @@ -99,7 +99,6 @@ public class RenameDialog extends RefactoringDialog { public static void showRenameDialog(DataContext dataContext, RenameDialog dialog) { if (ApplicationManager.getApplication().isUnitTestMode()) { final String name = PsiElementRenameHandler.DEFAULT_NAME.getData(dataContext); - //noinspection TestOnlyProblems dialog.performRename(name); dialog.close(OK_EXIT_CODE); } @@ -129,6 +128,13 @@ public class RenameDialog extends RefactoringDialog { super.dispose(); } + @SuppressWarnings("deprecation") + protected boolean isToSearchForTextOccurrencesForRename() { + return isToSearchForTextOccurencesForRename(); + } + + /** @deprecated use/override {@link #isToSearchForTextOccurrencesForRename()} instead (to be deleted in IDEA 2018) */ + @SuppressWarnings({"SpellCheckingInspection", "DeprecatedIsStillUsed"}) protected boolean isToSearchForTextOccurencesForRename() { return RenamePsiElementProcessor.forElement(myPsiElement).isToSearchForTextOccurrences(myPsiElement); } @@ -154,14 +160,8 @@ public class RenameDialog extends RefactoringDialog { if (myPsiElement instanceof PsiFile && myEditor == null) { myNameSuggestionsField.selectNameWithoutExtension(); } - myNameChangedListener = new NameSuggestionsField.DataChanged() { - @Override - public void dataChanged() { - processNewNameChanged(); - } - }; + myNameChangedListener = () -> processNewNameChanged(); myNameSuggestionsField.addDataChangedListener(myNameChangedListener); - } protected void preselectExtension(int start, int end) { @@ -190,7 +190,6 @@ public class RenameDialog extends RefactoringDialog { return ArrayUtil.toStringArray(result); } - public String getNewName() { return myNameSuggestionsField.getEnteredName().trim(); } @@ -200,7 +199,7 @@ public class RenameDialog extends RefactoringDialog { } public boolean isSearchInNonJavaFiles() { - return myCbSearchTextOccurences.isSelected(); + return myCbSearchTextOccurrences.isSelected(); } @Override @@ -218,7 +217,7 @@ public class RenameDialog extends RefactoringDialog { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbConstraints = new GridBagConstraints(); - gbConstraints.insets = new Insets(0, 0, 4, 0); + gbConstraints.insets = JBUI.insetsBottom(4); gbConstraints.weighty = 0; gbConstraints.weightx = 1; gbConstraints.gridwidth = GridBagConstraints.REMAINDER; @@ -226,7 +225,7 @@ public class RenameDialog extends RefactoringDialog { myNameLabel = new JLabel(); panel.add(myNameLabel, gbConstraints); - gbConstraints.insets = new Insets(0, 0, 4, "".equals(myNewNamePrefix.getText()) ? 0 : 1); + gbConstraints.insets = JBUI.insets(0, 0, 4, StringUtil.isEmpty(myNewNamePrefix.getText()) ? 0 : 1); gbConstraints.gridwidth = 1; gbConstraints.fill = GridBagConstraints.NONE; gbConstraints.weightx = 0; @@ -234,7 +233,7 @@ public class RenameDialog extends RefactoringDialog { gbConstraints.anchor = GridBagConstraints.WEST; panel.add(myNewNamePrefix, gbConstraints); - gbConstraints.insets = new Insets(0, 0, 8, 0); + gbConstraints.insets = JBUI.insetsBottom(8); gbConstraints.gridwidth = 2; gbConstraints.fill = GridBagConstraints.BOTH; gbConstraints.weightx = 1; @@ -248,7 +247,7 @@ public class RenameDialog extends RefactoringDialog { } protected void createCheckboxes(JPanel panel, GridBagConstraints gbConstraints) { - gbConstraints.insets = new Insets(0, 0, 4, 0); + gbConstraints.insets = JBUI.insetsBottom(4); gbConstraints.gridwidth = 1; gbConstraints.gridx = 0; gbConstraints.weighty = 0; @@ -259,26 +258,26 @@ public class RenameDialog extends RefactoringDialog { myCbSearchInComments.setSelected(true); panel.add(myCbSearchInComments, gbConstraints); - gbConstraints.insets = new Insets(0, 0, 4, 0); + gbConstraints.insets = JBUI.insetsBottom(4); gbConstraints.gridwidth = GridBagConstraints.REMAINDER; gbConstraints.gridx = 1; gbConstraints.weightx = 1; gbConstraints.fill = GridBagConstraints.BOTH; - myCbSearchTextOccurences = new NonFocusableCheckBox(); - myCbSearchTextOccurences.setText(RefactoringBundle.getSearchForTextOccurrencesText()); - myCbSearchTextOccurences.setSelected(true); - panel.add(myCbSearchTextOccurences, gbConstraints); + myCbSearchTextOccurrences = new NonFocusableCheckBox(); + myCbSearchTextOccurrences.setText(RefactoringBundle.getSearchForTextOccurrencesText()); + myCbSearchTextOccurrences.setSelected(true); + panel.add(myCbSearchTextOccurrences, gbConstraints); if (!TextOccurrencesUtil.isSearchTextOccurencesEnabled(myPsiElement)) { - myCbSearchTextOccurences.setEnabled(false); - myCbSearchTextOccurences.setSelected(false); - myCbSearchTextOccurences.setVisible(false); + myCbSearchTextOccurrences.setEnabled(false); + myCbSearchTextOccurrences.setSelected(false); + myCbSearchTextOccurrences.setVisible(false); } for(AutomaticRenamerFactory factory: Extensions.getExtensions(AutomaticRenamerFactory.EP_NAME)) { if (factory.isApplicable(myPsiElement) && factory.getOptionName() != null) { - gbConstraints.insets = new Insets(0, 0, 4, 0); - gbConstraints.gridwidth = myAutomaticRenamers.size() % 2 == 0 ? 1 : GridBagConstraints.REMAINDER; - gbConstraints.gridx = myAutomaticRenamers.size() % 2; + gbConstraints.insets = JBUI.insetsBottom(4); + gbConstraints.gridwidth = myAutoRenamerFactories.size() % 2 == 0 ? 1 : GridBagConstraints.REMAINDER; + gbConstraints.gridx = myAutoRenamerFactories.size() % 2; gbConstraints.weightx = 1; gbConstraints.fill = GridBagConstraints.BOTH; @@ -286,7 +285,7 @@ public class RenameDialog extends RefactoringDialog { checkBox.setText(factory.getOptionName()); checkBox.setSelected(factory.isEnabled()); panel.add(checkBox, gbConstraints); - myAutomaticRenamers.put(factory, checkBox); + myAutoRenamerFactories.put(factory, checkBox); } } } @@ -299,7 +298,6 @@ public class RenameDialog extends RefactoringDialog { @Override protected void doAction() { LOG.assertTrue(myPsiElement.isValid()); - final String newName = getNewName(); performRename(newName); } @@ -307,7 +305,7 @@ public class RenameDialog extends RefactoringDialog { public void performRename(final String newName) { final RenamePsiElementProcessor elementProcessor = RenamePsiElementProcessor.forElement(myPsiElement); elementProcessor.setToSearchInComments(myPsiElement, isSearchInComments()); - if (myCbSearchTextOccurences.isEnabled()) { + if (myCbSearchTextOccurrences.isEnabled()) { elementProcessor.setToSearchForTextOccurrences(myPsiElement, isSearchInNonJavaFiles()); } if (mySuggestedNameInfo != null) { @@ -316,7 +314,7 @@ public class RenameDialog extends RefactoringDialog { final RenameProcessor processor = createRenameProcessor(newName); - for(Map.Entry e: myAutomaticRenamers.entrySet()) { + for(Map.Entry e: myAutoRenamerFactories.entrySet()) { e.getKey().setEnabled(e.getValue().isSelected()); if (e.getValue().isSelected()) { processor.addRenamerFactory(e.getKey()); @@ -327,8 +325,7 @@ public class RenameDialog extends RefactoringDialog { } protected RenameProcessor createRenameProcessor(String newName) { - return new RenameProcessor(getProject(), myPsiElement, newName, isSearchInComments(), - isSearchInNonJavaFiles()); + return new RenameProcessor(getProject(), myPsiElement, newName, isSearchInComments(), isSearchInNonJavaFiles()); } @Override @@ -356,4 +353,4 @@ public class RenameDialog extends RefactoringDialog { public JCheckBox getCbSearchInComments() { return myCbSearchInComments; } -} +} \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 2484030200b8..c0a703d6db83 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1541,9 +1541,10 @@ - + +