diff --git a/java-execution-api/src/main/java/consulo/java/execution/configurations/OwnJavaParameters.java b/java-execution-api/src/main/java/consulo/java/execution/configurations/OwnJavaParameters.java index c47117ab7..fe6850607 100644 --- a/java-execution-api/src/main/java/consulo/java/execution/configurations/OwnJavaParameters.java +++ b/java-execution-api/src/main/java/consulo/java/execution/configurations/OwnJavaParameters.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.java.execution.configurations; import com.intellij.java.language.projectRoots.roots.NativeLibraryOrderRootType; @@ -52,12 +51,12 @@ public class OwnJavaParameters extends OwnSimpleJavaParameters { public static final Key JAVA_PARAMETERS = Key.create("javaParameters"); public String getJdkPath() throws CantRunException { - final Sdk jdk = getJdk(); + Sdk jdk = getJdk(); if (jdk == null) { throw new CantRunException(ExecutionLocalize.noJdkSpecifiedErrorMessage().get()); } - final VirtualFile jdkHome = jdk.getHomeDirectory(); + VirtualFile jdkHome = jdk.getHomeDirectory(); if (jdkHome == null) { throw new CantRunException(ExecutionLocalize.homeDirectoryNotSpecifiedForJdkErrorMessage().get()); } @@ -74,7 +73,7 @@ public String getJdkPath() throws CantRunException { public static final int JDK_AND_CLASSES_AND_PROVIDED = JDK_ONLY | CLASSES_ONLY | INCLUDE_PROVIDED; public void configureByModule( - final Module module, + Module module, @MagicConstant(valuesFromClass = OwnJavaParameters.class) int classPathType, @Nullable Sdk jdk ) throws CantRunException { @@ -110,12 +109,12 @@ private void configureJavaLibraryPath(OrderEnumerator enumerator) { } } - public void setDefaultCharset(final Project project) { + public void setDefaultCharset(Project project) { Charset encoding = EncodingProjectManager.getInstance(project).getDefaultCharset(); setCharset(encoding); } - public void configureByModule(final Module module, @MagicConstant(valuesFromClass = OwnJavaParameters.class) final int classPathType) + public void configureByModule(Module module, @MagicConstant(valuesFromClass = OwnJavaParameters.class) int classPathType) throws CantRunException { configureByModule(module, classPathType, getValidJdkToRunModule(module, (classPathType & TESTS_ONLY) == 0)); } @@ -123,16 +122,16 @@ public void configureByModule(final Module module, @MagicConstant(valuesFromClas /** * @deprecated use {@link #getValidJdkToRunModule(Module, boolean)} instead */ - public static Sdk getModuleJdk(final Module module) throws CantRunException { + public static Sdk getModuleJdk(Module module) throws CantRunException { return getValidJdkToRunModule(module, false); } - public static Sdk getValidJdkToRunModule(final Module module, boolean productionOnly) throws CantRunException { + public static Sdk getValidJdkToRunModule(Module module, boolean productionOnly) throws CantRunException { Sdk jdk = getJdkToRunModule(module, productionOnly); if (jdk == null) { throw CantRunException.noJdkForModule(module); } - final VirtualFile homeDirectory = jdk.getHomeDirectory(); + VirtualFile homeDirectory = jdk.getHomeDirectory(); if (homeDirectory == null || !homeDirectory.isValid()) { throw CantRunException.jdkMisconfigured(jdk, module); } @@ -141,18 +140,17 @@ public static Sdk getValidJdkToRunModule(final Module module, boolean production @Nullable public static Sdk getJdkToRunModule(Module module, boolean productionOnly) { - final Sdk moduleSdk = ModuleUtilCore.getSdk(module, JavaModuleExtension.class); + Sdk moduleSdk = ModuleUtilCore.getSdk(module, JavaModuleExtension.class); if (moduleSdk == null) { return null; } - final Set sdksFromDependencies = new LinkedHashSet<>(); + Set sdksFromDependencies = new LinkedHashSet<>(); OrderEnumerator enumerator = OrderEnumerator.orderEntries(module).runtimeOnly().recursively(); if (productionOnly) { enumerator = enumerator.productionOnly(); } - enumerator.forEachModule(module1 -> - { + enumerator.forEachModule(module1 -> { Sdk sdk = ModuleUtilCore.getSdk(module1, JavaModuleExtension.class); if (sdk != null && sdk.getSdkType().equals(moduleSdk.getSdkType())) { sdksFromDependencies.add(sdk); diff --git a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/RenameWrongRefFix.java b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/RenameWrongRefFix.java index f829498cc..6270e1022 100644 --- a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/RenameWrongRefFix.java +++ b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/RenameWrongRefFix.java @@ -13,22 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * Created by IntelliJ IDEA. - * User: mike - * Date: Aug 20, 2002 - * Time: 8:49:24 PM - * To change template for new class use - * Code Style | Class Templates options (Tools | IDE Options). - */ package com.intellij.java.impl.codeInsight.daemon.impl.quickfix; import com.intellij.java.language.psi.*; +import consulo.annotation.access.RequiredReadAction; import consulo.codeEditor.Editor; +import consulo.codeEditor.util.AWTEditorUtil; import consulo.document.util.TextRange; -import consulo.ide.impl.idea.openapi.editor.ex.util.EditorUtil; -import consulo.java.analysis.impl.JavaQuickFixBundle; import consulo.java.analysis.impl.localize.JavaQuickFixLocalize; import consulo.language.editor.FileModificationService; import consulo.language.editor.completion.lookup.LookupElement; @@ -44,183 +35,206 @@ import consulo.language.psi.util.PsiTreeUtil; import consulo.localize.LocalizeValue; import consulo.project.Project; +import consulo.ui.annotation.RequiredUIAccess; import java.util.*; +/** + * @author mike + * @since 2002-08-20 + */ public class RenameWrongRefFix implements SyntheticIntentionAction { - private final PsiReferenceExpression myRefExpr; - private static final String INPUT_VARIABLE_NAME = "INPUTVAR"; - private static final String OTHER_VARIABLE_NAME = "OTHERVAR"; - private final boolean myUnresolvedOnly; - - public RenameWrongRefFix(PsiReferenceExpression refExpr) { - this(refExpr, false); - } - - public RenameWrongRefFix(PsiReferenceExpression refExpr, boolean unresolvedOnly) { - myRefExpr = refExpr; - myUnresolvedOnly = unresolvedOnly; - } - - @Override - public LocalizeValue getText() { - return JavaQuickFixLocalize.renameWrongReferenceText(); - } - - @Override - public boolean isAvailable(Project project, Editor editor, PsiFile file) { - if (!myRefExpr.isValid() || !myRefExpr.getManager().isInProject(myRefExpr)) return false; - int offset = editor.getCaretModel().getOffset(); - PsiElement refName = myRefExpr.getReferenceNameElement(); - if (refName == null) return false; - TextRange textRange = refName.getTextRange(); - if (textRange == null || offset < textRange.getStartOffset() || - offset > textRange.getEndOffset()) { - return false; - } + private final PsiReferenceExpression myRefExpr; + private static final String INPUT_VARIABLE_NAME = "INPUTVAR"; + private static final String OTHER_VARIABLE_NAME = "OTHERVAR"; + private final boolean myUnresolvedOnly; - return !CreateFromUsageUtils.isValidReference(myRefExpr, myUnresolvedOnly); - } - - private class ReferenceNameExpression extends Expression { - class HammingComparator implements Comparator { - @Override - public int compare(LookupElement lookupItem1, LookupElement lookupItem2) { - String s1 = lookupItem1.getLookupString(); - String s2 = lookupItem2.getLookupString(); - int diff1 = 0; - for (int i = 0; i < Math.min(s1.length(), myOldReferenceName.length()); i++) { - if (s1.charAt(i) != myOldReferenceName.charAt(i)) diff1++; - } - int diff2 = 0; - for (int i = 0; i < Math.min(s2.length(), myOldReferenceName.length()); i++) { - if (s2.charAt(i) != myOldReferenceName.charAt(i)) diff2++; - } - return diff1 - diff2; - } + public RenameWrongRefFix(PsiReferenceExpression refExpr) { + this(refExpr, false); } - ReferenceNameExpression(LookupElement[] items, String oldReferenceName) { - myItems = items; - myOldReferenceName = oldReferenceName; - Arrays.sort(myItems, new HammingComparator ()); + public RenameWrongRefFix(PsiReferenceExpression refExpr, boolean unresolvedOnly) { + myRefExpr = refExpr; + myUnresolvedOnly = unresolvedOnly; } - LookupElement[] myItems; - private final String myOldReferenceName; - @Override - public Result calculateResult(ExpressionContext context) { - if (myItems == null || myItems.length == 0) { - return new TextResult(myOldReferenceName); - } - return new TextResult(myItems[0].getLookupString()); + public LocalizeValue getText() { + return JavaQuickFixLocalize.renameWrongReferenceText(); } @Override - public Result calculateQuickResult(ExpressionContext context) { - return null; - } + @RequiredReadAction + public boolean isAvailable(Project project, Editor editor, PsiFile file) { + if (!myRefExpr.isValid() || !myRefExpr.getManager().isInProject(myRefExpr)) { + return false; + } + int offset = editor.getCaretModel().getOffset(); + PsiElement refName = myRefExpr.getReferenceNameElement(); + if (refName == null) { + return false; + } + TextRange textRange = refName.getTextRange(); + if (textRange == null || offset < textRange.getStartOffset() || + offset > textRange.getEndOffset()) { + return false; + } - @Override - public LookupElement[] calculateLookupItems(ExpressionContext context) { - if (myItems == null || myItems.length == 1) return null; - return myItems; + return !CreateFromUsageUtils.isValidReference(myRefExpr, myUnresolvedOnly); } - } - - private LookupElement[] collectItems() { - Set items = new LinkedHashSet(); - boolean qualified = myRefExpr.getQualifierExpression() != null; - - if (!qualified && !(myRefExpr.getParent() instanceof PsiMethodCallExpression)) { - PsiVariable[] vars = CreateFromUsageUtils.guessMatchingVariables(myRefExpr); - for (PsiVariable var : vars) { - items.add(LookupElementBuilder.create(var.getName())); - } - } else { - class MyScopeProcessor extends BaseScopeProcessor { - ArrayList myResult = new ArrayList(); - boolean myFilterMethods; - boolean myFilterStatics = false; - - MyScopeProcessor(PsiReferenceExpression refExpression) { - myFilterMethods = refExpression.getParent() instanceof PsiMethodCallExpression; - PsiExpression qualifier = refExpression.getQualifierExpression(); - if (qualifier instanceof PsiReferenceExpression) { - PsiElement e = ((PsiReferenceExpression) qualifier).resolve(); - myFilterStatics = e instanceof PsiClass; - } else if (qualifier == null) { - PsiModifierListOwner scope = PsiTreeUtil.getParentOfType(refExpression, PsiModifierListOwner.class); - myFilterStatics = scope != null && scope.hasModifierProperty(PsiModifier.STATIC); - } + + private class ReferenceNameExpression extends Expression { + class HammingComparator implements Comparator { + @Override + public int compare(LookupElement lookupItem1, LookupElement lookupItem2) { + String s1 = lookupItem1.getLookupString(); + String s2 = lookupItem2.getLookupString(); + int diff1 = 0; + for (int i = 0; i < Math.min(s1.length(), myOldReferenceName.length()); i++) { + if (s1.charAt(i) != myOldReferenceName.charAt(i)) { + diff1++; + } + } + int diff2 = 0; + for (int i = 0; i < Math.min(s2.length(), myOldReferenceName.length()); i++) { + if (s2.charAt(i) != myOldReferenceName.charAt(i)) { + diff2++; + } + } + return diff1 - diff2; + } + } + + ReferenceNameExpression(LookupElement[] items, String oldReferenceName) { + myItems = items; + myOldReferenceName = oldReferenceName; + Arrays.sort(myItems, new HammingComparator()); } + LookupElement[] myItems; + private final String myOldReferenceName; + @Override - public boolean execute(PsiElement element, ResolveState state) { - if (element instanceof PsiNamedElement - && element instanceof PsiModifierListOwner - && myFilterMethods == element instanceof PsiMethod) { - if (((PsiModifierListOwner)element).hasModifierProperty(PsiModifier.STATIC) == myFilterStatics) { - myResult.add(element); + public Result calculateResult(ExpressionContext context) { + if (myItems == null || myItems.length == 0) { + return new TextResult(myOldReferenceName); } - } - return true; + return new TextResult(myItems[0].getLookupString()); } - public PsiElement[] getVariants () { - return PsiUtilCore.toPsiElementArray(myResult); + @Override + public Result calculateQuickResult(ExpressionContext context) { + return null; } - } - - items.add(LookupElementBuilder.create(myRefExpr.getReferenceName())); - MyScopeProcessor processor = new MyScopeProcessor(myRefExpr); - myRefExpr.processVariants(processor); - PsiElement[] variants = processor.getVariants(); - for (PsiElement variant : variants) { - items.add(LookupElementBuilder.create(((PsiNamedElement)variant).getName())); - } - } - return items.toArray(new LookupElement[items.size()]); - } - - @Override - public void invoke(Project project, Editor editor, PsiFile file) { - if (!FileModificationService.getInstance().prepareFileForWrite(file)) return; - PsiReferenceExpression[] refs = CreateFromUsageUtils.collectExpressions(myRefExpr, PsiMember.class, PsiFile.class); - PsiElement element = PsiTreeUtil.getParentOfType(myRefExpr, PsiMember.class, PsiFile.class); - LookupElement[] items = collectItems(); - ReferenceNameExpression refExpr = new ReferenceNameExpression(items, myRefExpr.getReferenceName()); - - TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(element); - for (PsiReferenceExpression expr : refs) { - if (!expr.equals(myRefExpr)) { - builder.replaceElement(expr.getReferenceNameElement(), OTHER_VARIABLE_NAME, INPUT_VARIABLE_NAME, false); - } - else { - builder.replaceElement(expr.getReferenceNameElement(), INPUT_VARIABLE_NAME, refExpr, true); - } + @Override + public LookupElement[] calculateLookupItems(ExpressionContext context) { + if (myItems == null || myItems.length == 1) { + return null; + } + return myItems; + } } - float proportion = EditorUtil.calcVerticalScrollProportion(editor); - editor.getCaretModel().moveToOffset(element.getTextRange().getStartOffset()); + @RequiredReadAction + private LookupElement[] collectItems() { + Set items = new LinkedHashSet<>(); + boolean qualified = myRefExpr.getQualifierExpression() != null; + + if (!qualified && !(myRefExpr.getParent() instanceof PsiMethodCallExpression)) { + PsiVariable[] vars = CreateFromUsageUtils.guessMatchingVariables(myRefExpr); + for (PsiVariable var : vars) { + items.add(LookupElementBuilder.create(var.getName())); + } + } + else { + class MyScopeProcessor extends BaseScopeProcessor { + List myResult = new ArrayList<>(); + boolean myFilterMethods; + boolean myFilterStatics = false; + + @RequiredReadAction + MyScopeProcessor(PsiReferenceExpression refExpression) { + myFilterMethods = refExpression.getParent() instanceof PsiMethodCallExpression; + PsiExpression qualifier = refExpression.getQualifierExpression(); + if (qualifier instanceof PsiReferenceExpression ref) { + PsiElement e = ref.resolve(); + myFilterStatics = e instanceof PsiClass; + } + else if (qualifier == null) { + PsiModifierListOwner scope = PsiTreeUtil.getParentOfType(refExpression, PsiModifierListOwner.class); + myFilterStatics = scope != null && scope.hasModifierProperty(PsiModifier.STATIC); + } + } + + @Override + public boolean execute(PsiElement element, ResolveState state) { + if (element instanceof PsiNamedElement + && element instanceof PsiModifierListOwner modifierListOwner + && myFilterMethods == element instanceof PsiMethod) { + if (modifierListOwner.hasModifierProperty(PsiModifier.STATIC) == myFilterStatics) { + myResult.add(element); + } + } + return true; + } + + public PsiElement[] getVariants() { + return PsiUtilCore.toPsiElementArray(myResult); + } + } - /*for (int i = refs.length - 1; i >= 0; i--) { - TextRange range = refs[i].getReferenceNameElement().getTextRange(); - document.deleteString(range.getStartOffset(), range.getEndOffset()); + items.add(LookupElementBuilder.create(myRefExpr.getReferenceName())); + MyScopeProcessor processor = new MyScopeProcessor(myRefExpr); + myRefExpr.processVariants(processor); + PsiElement[] variants = processor.getVariants(); + for (PsiElement variant : variants) { + items.add(LookupElementBuilder.create(((PsiNamedElement) variant).getName())); + } + } + + return items.toArray(new LookupElement[items.size()]); } -*/ - Template template = builder.buildInlineTemplate(); - editor.getCaretModel().moveToOffset(element.getTextRange().getStartOffset()); - TemplateManager.getInstance(project).startTemplate(editor, template); + @Override + @RequiredUIAccess + public void invoke(Project project, Editor editor, PsiFile file) { + if (!FileModificationService.getInstance().prepareFileForWrite(file)) { + return; + } + PsiReferenceExpression[] refs = CreateFromUsageUtils.collectExpressions(myRefExpr, PsiMember.class, PsiFile.class); + PsiElement element = PsiTreeUtil.getParentOfType(myRefExpr, PsiMember.class, PsiFile.class); + LookupElement[] items = collectItems(); + ReferenceNameExpression refExpr = new ReferenceNameExpression(items, myRefExpr.getReferenceName()); + + TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(element); + for (PsiReferenceExpression expr : refs) { + if (!expr.equals(myRefExpr)) { + builder.replaceElement(expr.getReferenceNameElement(), OTHER_VARIABLE_NAME, INPUT_VARIABLE_NAME, false); + } + else { + builder.replaceElement(expr.getReferenceNameElement(), INPUT_VARIABLE_NAME, refExpr, true); + } + } + + float proportion = AWTEditorUtil.calcVerticalScrollProportion(editor); + editor.getCaretModel().moveToOffset(element.getTextRange().getStartOffset()); + + /*for (int i = refs.length - 1; i >= 0; i--) { + TextRange range = refs[i].getReferenceNameElement().getTextRange(); + document.deleteString(range.getStartOffset(), range.getEndOffset()); + }*/ - EditorUtil.setVerticalScrollProportion(editor, proportion); - } + Template template = builder.buildInlineTemplate(); + editor.getCaretModel().moveToOffset(element.getTextRange().getStartOffset()); - @Override - public boolean startInWriteAction() { - return true; - } + TemplateManager.getInstance(project).startTemplate(editor, template); + + AWTEditorUtil.setVerticalScrollProportion(editor, proportion); + } + + @Override + public boolean startInWriteAction() { + return true; + } }