mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
preserve comments: Objects.requireNotNull replacement
This commit is contained in:
+9
@@ -64,6 +64,15 @@ public class PsiReplacementUtil {
|
||||
return styleManager.reformat(replacementExp);
|
||||
}
|
||||
|
||||
public static PsiElement replaceExpressionAndShorten(@NotNull PsiExpression expression, @NotNull @NonNls String newExpressionText, CommentTracker tracker) {
|
||||
final Project project = expression.getProject();
|
||||
final PsiElement replacementExp = tracker.replaceAndRestoreComments(expression, newExpressionText);
|
||||
final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
|
||||
javaCodeStyleManager.shortenClassReferences(replacementExp);
|
||||
final CodeStyleManager styleManager = CodeStyleManager.getInstance(project);
|
||||
return styleManager.reformat(replacementExp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Consider to use {@link #replaceStatement(PsiExpression, String, CommentTracker)} to preserve comments
|
||||
*/
|
||||
|
||||
+8
-2
@@ -171,7 +171,10 @@ public class IfCanBeAssertionInspection extends BaseInspection {
|
||||
if (!(condition instanceof PsiBinaryExpression)) return null;
|
||||
PsiExpression nullComparedExpression = ExpressionUtils.getValueComparedWithNull((PsiBinaryExpression)condition);
|
||||
if (nullComparedExpression == null) return null;
|
||||
return new Replacer(text -> PsiReplacementUtil.replaceStatementAndShortenClassNames(ifStatement, text + ";"), nullComparedExpression, null);
|
||||
CommentTracker tracker = new CommentTracker();
|
||||
return new Replacer(text -> PsiReplacementUtil.replaceStatementAndShortenClassNames(ifStatement, text + ";", tracker),
|
||||
tracker.markUnchanged(nullComparedExpression),
|
||||
null);
|
||||
} else {
|
||||
PsiReferenceExpression ref = ObjectUtils.tryCast(descriptor.getPsiElement().getParent(), PsiReferenceExpression.class);
|
||||
if (ref == null) return null;
|
||||
@@ -185,7 +188,10 @@ public class IfCanBeAssertionInspection extends BaseInspection {
|
||||
}
|
||||
PsiExpression[] args = methodCall.getArgumentList().getExpressions();
|
||||
if (args.length > 2) return null;
|
||||
return new Replacer(text -> PsiReplacementUtil.replaceExpressionAndShorten(methodCall, text), args[0], args.length == 2 ? args[1] : null);
|
||||
CommentTracker tracker = new CommentTracker();
|
||||
return new Replacer(text -> PsiReplacementUtil.replaceExpressionAndShorten(methodCall, text, tracker),
|
||||
tracker.markUnchanged(args[0]),
|
||||
args.length == 2 ? tracker.markUnchanged(args[1]) : null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -19,6 +19,7 @@ import java.util.Objects;
|
||||
|
||||
class Parentheses {
|
||||
void s(String s) {
|
||||
//keep me
|
||||
Objects.requireNonNull(s);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -18,7 +18,7 @@ package com.siyeh.ipp.asserttoif.if_to_assert;
|
||||
class Parentheses {
|
||||
void s(String s) {
|
||||
i<caret>f (s == null) {
|
||||
throw (new NullPointerException(("s")));
|
||||
throw (new NullPointerException(("s")));//keep me
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user