Extend Selection: whole literal with braces is selected after parameters (KT-13420)

#KT-13420 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-04-04 07:12:43 +03:00
committed by Nikolay Krasko
parent 05269cea2f
commit 5aa0b7d2aa
6 changed files with 26 additions and 6 deletions
@@ -32,7 +32,7 @@ import org.jetbrains.kotlin.psi.psiUtil.siblings
*/
class KotlinStatementGroupSelectioner : ExtendWordSelectionHandlerBase() {
override fun canSelect(e: PsiElement): Boolean {
if (e !is KtExpression && e !is KtWhenEntry && e !is PsiComment) return false
if (e !is KtExpression && e !is KtWhenEntry && e !is KtParameterList && e !is PsiComment) return false
val parent = e.parent
return parent is KtBlockExpression || parent is KtWhenExpression || parent is KtFunctionLiteral
}
@@ -43,8 +43,7 @@ class KotlinStatementGroupSelectioner : ExtendWordSelectionHandlerBase() {
val startElement = e.siblings(forward = false, withItself = false)
.firstOrNull {
// find preceding '{' or blank line
it is LeafPsiElement && it.elementType == KtTokens.LBRACE ||
it is PsiWhiteSpace && it.getText()!!.count { it == '\n' } > 1
it is LeafPsiElement && it.elementType == KtTokens.LBRACE || it is PsiWhiteSpace && it.getText()!!.count { it == '\n' } > 1
}
?.siblings(forward = true, withItself = false)
?.dropWhile { it is PsiWhiteSpace } // and take first non-whitespace element after it
@@ -53,14 +52,13 @@ class KotlinStatementGroupSelectioner : ExtendWordSelectionHandlerBase() {
val endElement = e.siblings(forward = true, withItself = false)
.firstOrNull {
// find next '}' or blank line
it is LeafPsiElement && it.elementType == KtTokens.RBRACE ||
it is PsiWhiteSpace && it.getText()!!.count { it == '\n' } > 1
it is LeafPsiElement && it.elementType == KtTokens.RBRACE || it is PsiWhiteSpace && it.getText()!!.count { it == '\n' } > 1
}
?.siblings(forward = false, withItself = false)
?.dropWhile { it is PsiWhiteSpace } // and take first non-whitespace element before it
?.firstOrNull() ?: parent.lastChild!!
return ExtendWordSelectionHandlerBase.expandToWholeLine(
return expandToWholeLine(
editorText,
TextRange(
startElement.textRange!!.startOffset,
@@ -0,0 +1,5 @@
fun foo(f: (Int) -> Int) {}
fun test() {
foo { <caret>it -> it + 1 }
}
@@ -0,0 +1,5 @@
fun foo(f: (Int) -> Int) {}
fun test() {
foo { <caret><selection>it</selection> -> it + 1 }
}
@@ -0,0 +1,5 @@
fun foo(f: (Int) -> Int) {}
fun test() {
foo { <caret><selection>it -> it + 1</selection> }
}
@@ -0,0 +1,5 @@
fun foo(f: (Int) -> Int) {}
fun test() {
foo <selection>{ <caret>it -> it + 1 }</selection>
}
@@ -49,6 +49,8 @@ public class WordSelectionTest extends KotlinLightCodeInsightFixtureTestCase {
doTest();
}
public void testValueParametersInLambda() { doTest(); }
public void testDocComment() { doTest(); }
public void testDocCommentOneLine() { doTest(); }