mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract: skip comments when no statement is selected but expression can be retrieved (IDEA-91985)
This commit is contained in:
@@ -34,7 +34,10 @@ import com.intellij.psi.impl.source.PsiDiamondTypeElementImpl;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.searches.ClassInheritorsSearch;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.psi.util.proximity.PsiProximityComparator;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.Consumer;
|
||||
@@ -61,6 +64,18 @@ public class CodeInsightUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (expression == null && findStatementsInRange(file, startOffset, endOffset).length == 0) {
|
||||
PsiElement element = PsiTreeUtil.skipSiblingsBackward(file.findElementAt(endOffset), PsiWhiteSpace.class);
|
||||
if (element != null) {
|
||||
element = PsiTreeUtil.skipSiblingsBackward(element.getLastChild(), PsiWhiteSpace.class, PsiComment.class);
|
||||
if (element != null) {
|
||||
final int newEndOffset = element.getTextRange().getEndOffset();
|
||||
if (newEndOffset < endOffset) {
|
||||
expression = findExpressionInRange(file, startOffset, newEndOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (expression instanceof PsiReferenceExpression && expression.getParent() instanceof PsiMethodCallExpression) return null;
|
||||
return expression;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
String arg = <selection>args[0]; //comment</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
String arg = newMethod(args[0]); //comment
|
||||
}
|
||||
|
||||
private static String newMethod(String arg) {
|
||||
return arg;
|
||||
}
|
||||
}
|
||||
@@ -554,6 +554,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testSkipComments() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTestDisabledParam() throws PrepareFailedException {
|
||||
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
|
||||
settings.ELSE_ON_NEW_LINE = true;
|
||||
|
||||
Reference in New Issue
Block a user