From 809cc220edd5316bee37880584041d41461391b4 Mon Sep 17 00:00:00 2001 From: Joscha Alisch Date: Sat, 23 Dec 2017 15:27:41 +0100 Subject: [PATCH] Select lambda after other args when it's outside argument list (KT-21214) #KT-21214 Fixed --- idea/src/META-INF/plugin.xml | 1 + ...tlinCallExpressionWithLambdaSelectioner.kt | 42 +++++++++++++++++++ .../wordSelection/LambdaArgument1/0.kt | 5 +++ .../wordSelection/LambdaArgument1/1.kt | 5 +++ .../wordSelection/LambdaArgument1/2.kt | 5 +++ .../wordSelection/LambdaArgument1/3.kt | 5 +++ .../wordSelection/LambdaArgument2/0.kt | 5 +++ .../wordSelection/LambdaArgument2/1.kt | 5 +++ .../wordSelection/LambdaArgument2/2.kt | 5 +++ .../wordSelection/LambdaArgument3/0.kt | 5 +++ .../wordSelection/LambdaArgument3/1.kt | 5 +++ .../wordSelection/LambdaArgument3/2.kt | 5 +++ .../wordSelection/LambdaArgument4/0.kt | 5 +++ .../wordSelection/LambdaArgument4/1.kt | 5 +++ .../wordSelection/LambdaArgument4/2.kt | 5 +++ .../wordSelection/LambdaArgument4/3.kt | 5 +++ .../kotlin/idea/WordSelectionTest.java | 13 ++++++ 17 files changed, 126 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinCallExpressionWithLambdaSelectioner.kt create mode 100644 idea/testData/wordSelection/LambdaArgument1/0.kt create mode 100644 idea/testData/wordSelection/LambdaArgument1/1.kt create mode 100644 idea/testData/wordSelection/LambdaArgument1/2.kt create mode 100644 idea/testData/wordSelection/LambdaArgument1/3.kt create mode 100644 idea/testData/wordSelection/LambdaArgument2/0.kt create mode 100644 idea/testData/wordSelection/LambdaArgument2/1.kt create mode 100644 idea/testData/wordSelection/LambdaArgument2/2.kt create mode 100644 idea/testData/wordSelection/LambdaArgument3/0.kt create mode 100644 idea/testData/wordSelection/LambdaArgument3/1.kt create mode 100644 idea/testData/wordSelection/LambdaArgument3/2.kt create mode 100644 idea/testData/wordSelection/LambdaArgument4/0.kt create mode 100644 idea/testData/wordSelection/LambdaArgument4/1.kt create mode 100644 idea/testData/wordSelection/LambdaArgument4/2.kt create mode 100644 idea/testData/wordSelection/LambdaArgument4/3.kt diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 81ac1aa421d..f756c18ce67 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -552,6 +552,7 @@ + diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinCallExpressionWithLambdaSelectioner.kt b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinCallExpressionWithLambdaSelectioner.kt new file mode 100644 index 00000000000..35819b0d312 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/editor/wordSelection/KotlinCallExpressionWithLambdaSelectioner.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.editor.wordSelection + +import com.intellij.codeInsight.editorActions.ExtendWordSelectionHandlerBase +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.psi.KtCallExpression +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.startOffset + +class KotlinCallExpressionWithLambdaSelectioner : ExtendWordSelectionHandlerBase() { + + override fun canSelect(e: PsiElement): Boolean + = e is KtCallExpression && e.hasLambda() + + override fun select(e: PsiElement?, editorText: CharSequence?, cursorOffset: Int, editor: Editor?): List? { + if (e !is KtCallExpression) return null + + val endOffset = e.valueArgumentList?.endOffset ?: return null + return listOf(TextRange(e.startOffset, endOffset)) + } + + private fun KtCallExpression.hasLambda(): Boolean + = lambdaArguments.isNotEmpty() + +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument1/0.kt b/idea/testData/wordSelection/LambdaArgument1/0.kt new file mode 100644 index 00000000000..a935cc7397a --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument1/0.kt @@ -0,0 +1,5 @@ +fun foo() { + foo(1, 2) { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument1/1.kt b/idea/testData/wordSelection/LambdaArgument1/1.kt new file mode 100644 index 00000000000..ace8c950bef --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument1/1.kt @@ -0,0 +1,5 @@ +fun foo() { + foo(1, 2) { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument1/2.kt b/idea/testData/wordSelection/LambdaArgument1/2.kt new file mode 100644 index 00000000000..585c5a24dbd --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument1/2.kt @@ -0,0 +1,5 @@ +fun foo() { + foo(1, 2) { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument1/3.kt b/idea/testData/wordSelection/LambdaArgument1/3.kt new file mode 100644 index 00000000000..3127ad43707 --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument1/3.kt @@ -0,0 +1,5 @@ +fun foo() { + foo(1, 2) { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument2/0.kt b/idea/testData/wordSelection/LambdaArgument2/0.kt new file mode 100644 index 00000000000..f1479a8ed62 --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument2/0.kt @@ -0,0 +1,5 @@ +fun foo() { + foo { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument2/1.kt b/idea/testData/wordSelection/LambdaArgument2/1.kt new file mode 100644 index 00000000000..9654dd73e4a --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument2/1.kt @@ -0,0 +1,5 @@ +fun foo() { + foo { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument2/2.kt b/idea/testData/wordSelection/LambdaArgument2/2.kt new file mode 100644 index 00000000000..dda75144a16 --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument2/2.kt @@ -0,0 +1,5 @@ +fun foo() { + foo { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument3/0.kt b/idea/testData/wordSelection/LambdaArgument3/0.kt new file mode 100644 index 00000000000..36acaba6c22 --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument3/0.kt @@ -0,0 +1,5 @@ +fun foo() { + foo({ + 1 + }) +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument3/1.kt b/idea/testData/wordSelection/LambdaArgument3/1.kt new file mode 100644 index 00000000000..dc5c033cf2d --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument3/1.kt @@ -0,0 +1,5 @@ +fun foo() { + foo({ + 1 + }) +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument3/2.kt b/idea/testData/wordSelection/LambdaArgument3/2.kt new file mode 100644 index 00000000000..981f5c512be --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument3/2.kt @@ -0,0 +1,5 @@ +fun foo() { + foo({ + 1 + }) +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument4/0.kt b/idea/testData/wordSelection/LambdaArgument4/0.kt new file mode 100644 index 00000000000..bde45ca21bc --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument4/0.kt @@ -0,0 +1,5 @@ +fun foo() { + foo() { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument4/1.kt b/idea/testData/wordSelection/LambdaArgument4/1.kt new file mode 100644 index 00000000000..c037bb546c5 --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument4/1.kt @@ -0,0 +1,5 @@ +fun foo() { + foo() { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument4/2.kt b/idea/testData/wordSelection/LambdaArgument4/2.kt new file mode 100644 index 00000000000..f8af0d037ec --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument4/2.kt @@ -0,0 +1,5 @@ +fun foo() { + foo() { + 1 + } +} \ No newline at end of file diff --git a/idea/testData/wordSelection/LambdaArgument4/3.kt b/idea/testData/wordSelection/LambdaArgument4/3.kt new file mode 100644 index 00000000000..8a0d605e077 --- /dev/null +++ b/idea/testData/wordSelection/LambdaArgument4/3.kt @@ -0,0 +1,5 @@ +fun foo() { + foo() { + 1 + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/WordSelectionTest.java b/idea/tests/org/jetbrains/kotlin/idea/WordSelectionTest.java index 714f40d6f61..534d58f9251 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/WordSelectionTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/WordSelectionTest.java @@ -108,6 +108,19 @@ public class WordSelectionTest extends KotlinLightCodeInsightFixtureTestCase { doTest(); } + public void testLambdaArgument1() { + doTest(); + } + public void testLambdaArgument2() { + doTest(); + } + public void testLambdaArgument3() { + doTest(); + } + public void testLambdaArgument4() { + doTest(); + } + public void testArrayBrackets() { doTest(); }