Introduce Variable: Skip leading/trailing comments inside selection
#KT-13054 Fixed
This commit is contained in:
@@ -53,6 +53,7 @@ import org.jetbrains.kotlin.idea.refactoring.introduce.IntroduceUtilKt;
|
||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers;
|
||||
import org.jetbrains.kotlin.psi.*;
|
||||
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
|
||||
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt;
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
|
||||
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils;
|
||||
@@ -350,9 +351,17 @@ public class KotlinRefactoringUtil {
|
||||
if (editor.getSelectionModel().hasSelection()) {
|
||||
int selectionStart = editor.getSelectionModel().getSelectionStart();
|
||||
int selectionEnd = editor.getSelectionModel().getSelectionEnd();
|
||||
String text = file.getText();
|
||||
while (selectionStart < selectionEnd && Character.isSpaceChar(text.charAt(selectionStart))) ++selectionStart;
|
||||
while (selectionStart < selectionEnd && Character.isSpaceChar(text.charAt(selectionEnd - 1))) --selectionEnd;
|
||||
|
||||
PsiElement firstElement = file.findElementAt(selectionStart);
|
||||
PsiElement lastElement = file.findElementAt(selectionEnd - 1);
|
||||
|
||||
if (PsiTreeUtil.getParentOfType(firstElement, KtLiteralStringTemplateEntry.class, KtEscapeStringTemplateEntry.class) == null
|
||||
&& PsiTreeUtil.getParentOfType(lastElement, KtLiteralStringTemplateEntry.class, KtEscapeStringTemplateEntry.class) == null) {
|
||||
firstElement = PsiUtilsKt.getNextSiblingIgnoringWhitespaceAndComments(firstElement, true);
|
||||
lastElement = PsiUtilsKt.getPrevSiblingIgnoringWhitespaceAndComments(lastElement, true);
|
||||
selectionStart = firstElement.getTextRange().getStartOffset();
|
||||
selectionEnd = lastElement.getTextRange().getEndOffset();
|
||||
}
|
||||
|
||||
for (CodeInsightUtils.ElementKind elementKind : elementKinds) {
|
||||
PsiElement element = findElement(file, selectionStart, selectionEnd, failOnEmptySuggestion, elementKind);
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(x: Boolean) {
|
||||
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
foo(<selection>/* 1 */true/* 2 */</selection>)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo(x: Boolean) {
|
||||
|
||||
}
|
||||
|
||||
fun bar() {
|
||||
val x = true
|
||||
foo(/* 1 */x/* 2 */)
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fun foo(param: Int): String {
|
||||
val x = "atrue123"
|
||||
val y = "aTRUE123"
|
||||
val z = true
|
||||
val u = "123 true 456"
|
||||
return "ab<selection> true </selection>def"
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo(param: Int): String {
|
||||
val x = "atrue123"
|
||||
val y = "aTRUE123"
|
||||
val z = true
|
||||
val s = " true "
|
||||
val u = "123${s}456"
|
||||
return "ab${s}def"
|
||||
}
|
||||
+12
@@ -61,6 +61,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
|
||||
doIntroduceVariableTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("commentSkipping.kt")
|
||||
public void testCommentSkipping() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/commentSkipping.kt");
|
||||
doIntroduceVariableTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ComplexCallee.kt")
|
||||
public void testComplexCallee() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/ComplexCallee.kt");
|
||||
@@ -735,6 +741,12 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
|
||||
doIntroduceVariableTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("extractTrueWithSpaces.kt")
|
||||
public void testExtractTrueWithSpaces() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/stringTemplates/extractTrueWithSpaces.kt");
|
||||
doIntroduceVariableTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("fullContent.kt")
|
||||
public void testFullContent() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/stringTemplates/fullContent.kt");
|
||||
|
||||
Reference in New Issue
Block a user