Java: Intention that wraps list/set/map with Collections.unmodifiable - handle the caret at the end of the expression (IDEA-93154)

This commit is contained in:
Pavel Dolgov
2019-02-21 18:03:24 +03:00
parent 0267b104b3
commit 96b9ab4f80
3 changed files with 39 additions and 7 deletions
@@ -2,7 +2,6 @@
package com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
@@ -24,13 +23,16 @@ import static com.intellij.util.ObjectUtils.tryCast;
/**
* @author Pavel.Dolgov
*/
public class WrapWithUnmodifiableAction extends PsiElementBaseIntentionAction {
public class WrapWithUnmodifiableAction extends BaseIntentionAction {
private static final String JAVA_UTIL_SORTED_SET = "java.util.SortedSet";
private static final String JAVA_UTIL_SORTED_MAP = "java.util.SortedMap";
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
PsiExpression expression = getParentExpression(element);
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (editor == null || file == null || !canModify(file)) {
return;
}
PsiExpression expression = getParentExpression(editor, file);
if (expression != null) {
PsiClass psiClass = PsiUtil.resolveClassInClassTypeOnly(expression.getType());
if (psiClass != null) {
@@ -59,8 +61,14 @@ public class WrapWithUnmodifiableAction extends PsiElementBaseIntentionAction {
}
}
private static PsiExpression getParentExpression(@NotNull PsiElement element) {
@Nullable
private static PsiExpression getParentExpression(@NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
PsiExpression expression = PsiTreeUtil.getNonStrictParentOfType(element, PsiExpression.class);
if (expression == null) {
element = file.findElementAt(editor.getCaretModel().getOffset() - 1);
expression = PsiTreeUtil.getNonStrictParentOfType(element, PsiExpression.class);
}
if (expression != null) {
PsiMethodCallExpression methodCall = tryCast(expression.getParent(), PsiMethodCallExpression.class);
if (methodCall != null && methodCall.getMethodExpression() == expression) {
@@ -78,8 +86,11 @@ public class WrapWithUnmodifiableAction extends PsiElementBaseIntentionAction {
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
PsiExpression expression = getParentExpression(element);
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (editor == null || file == null || !canModify(file)) {
return false;
}
PsiExpression expression = getParentExpression(editor, file);
if (expression != null) {
if (isUnmodifiable(expression)) {
return false;
@@ -0,0 +1,11 @@
// "Wrap with unmodifiable set" "true"
import java.util.Collections;
import java.util.Set;
import java.util.HashSet;
class C {
Set<String> test() {
Set<String> result = new HashSet<>();
return Collections.unmodifiableSet(result);
}
}
@@ -0,0 +1,10 @@
// "Wrap with unmodifiable set" "true"
import java.util.Set;
import java.util.HashSet;
class C {
Set<String> test() {
Set<String> result = new HashSet<>();
return result<caret>;
}
}