KT-4849 'Join lines' should remove braces from single-statement block
#KT-4849 Fixed
This commit is contained in:
@@ -432,6 +432,7 @@
|
||||
order="before jetExpression" />
|
||||
|
||||
<joinLinesHandler implementation="org.jetbrains.jet.plugin.joinLines.JoinDeclarationAndAssignmentHandler"/>
|
||||
<joinLinesHandler implementation="org.jetbrains.jet.plugin.joinLines.JoinBlockIntoSingleStatementHandler"/>
|
||||
|
||||
<targetElementEvaluator
|
||||
language="jet"
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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.jet.plugin.joinLines
|
||||
|
||||
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
import org.jetbrains.jet.lexer.JetTokens
|
||||
import org.jetbrains.jet.lang.psi.JetBlockExpression
|
||||
import org.jetbrains.jet.lang.psi.JetContainerNode
|
||||
|
||||
public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
|
||||
override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int {
|
||||
if (file !is JetFile) return -1
|
||||
|
||||
if (start == 0) return -1
|
||||
val brace = file.findElementAt(start - 1)!!
|
||||
if (brace.getNode()!!.getElementType() != JetTokens.LBRACE) return -1
|
||||
|
||||
val block = brace.getParent() as? JetBlockExpression ?: return -1
|
||||
val statement = block.getStatements().singleOrNull() ?: return -1
|
||||
if (block.getParent() !is JetContainerNode) return -1
|
||||
if (block.getNode().getChildren(JetTokens.COMMENTS).isNotEmpty()) return -1 // otherwise we will loose comments
|
||||
|
||||
val newStatement = block.replace(statement)
|
||||
return newStatement.getTextRange()!!.getStartOffset()
|
||||
}
|
||||
|
||||
override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int)
|
||||
= - 1
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>if (a) {
|
||||
bar() // do bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
if (a) {<caret> bar() // do bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<caret>if (a) {
|
||||
bar()
|
||||
// do something else here
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
if (a) {<caret> bar()
|
||||
// do something else here
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<caret>if (a) {
|
||||
// do bar
|
||||
bar()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
if (a) {<caret> // do bar
|
||||
bar()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<caret>if (a) {
|
||||
/* do bar */
|
||||
bar()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
if (a) {<caret> /* do bar */
|
||||
bar()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>do {
|
||||
if (bar()) break
|
||||
} while (true)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
do <caret>if (bar()) break while (true)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>if (a) bar1() else {
|
||||
bar2()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
if (a) bar1() else <caret>bar2()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>for (element in collection) {
|
||||
println(element)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
for (element in collection) <caret>println(element)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<caret>fun foo() {
|
||||
bar()
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fun foo() {<caret> bar()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>if (a) {
|
||||
bar()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
if (a) <caret>bar()
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
<caret>if (a) {
|
||||
bar1()
|
||||
} else {
|
||||
bar2()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
if (a) <caret>bar1() else {
|
||||
bar2()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>run {
|
||||
bar()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
run {<caret> bar()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<caret>if (a) {
|
||||
println("a" +
|
||||
"b")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
if (a) <caret>println("a" +
|
||||
"b")
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>if (a) {
|
||||
bar1(); bar2()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
if (a) {<caret> bar1(); bar2()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>while (true) {
|
||||
if (bar()) break
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
while (true) <caret>if (bar()) break
|
||||
}
|
||||
+95
-1
@@ -32,7 +32,7 @@ import java.util.regex.Pattern;
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/joinLines")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@InnerTestClasses({JoinLinesTestGenerated.DeclarationAndAssignment.class})
|
||||
@InnerTestClasses({JoinLinesTestGenerated.DeclarationAndAssignment.class, JoinLinesTestGenerated.RemoveBraces.class})
|
||||
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
|
||||
public class JoinLinesTestGenerated extends AbstractJoinLinesTest {
|
||||
public void testAllFilesPresentInJoinLines() throws Exception {
|
||||
@@ -139,4 +139,98 @@ public class JoinLinesTestGenerated extends AbstractJoinLinesTest {
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/joinLines/removeBraces")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(org.jetbrains.jet.JUnit3RunnerWithInners.class)
|
||||
public static class RemoveBraces extends AbstractJoinLinesTest {
|
||||
public void testAllFilesPresentInRemoveBraces() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/joinLines/removeBraces"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("CommentAfterStatement.kt")
|
||||
public void testCommentAfterStatement() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/CommentAfterStatement.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("CommentAfterStatement2.kt")
|
||||
public void testCommentAfterStatement2() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/CommentAfterStatement2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("CommentBeforeStatement.kt")
|
||||
public void testCommentBeforeStatement() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/CommentBeforeStatement.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("CommentBeforeStatement2.kt")
|
||||
public void testCommentBeforeStatement2() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/CommentBeforeStatement2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("DoWhile.kt")
|
||||
public void testDoWhile() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/DoWhile.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("Else.kt")
|
||||
public void testElse() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/Else.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("For.kt")
|
||||
public void testFor() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/For.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("FunctionBody.kt")
|
||||
public void testFunctionBody() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/FunctionBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("If.kt")
|
||||
public void testIf() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/If.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("IfWithElse.kt")
|
||||
public void testIfWithElse() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/IfWithElse.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("LambdaBody.kt")
|
||||
public void testLambdaBody() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/LambdaBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NotSingleLineStatement.kt")
|
||||
public void testNotSingleLineStatement() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/NotSingleLineStatement.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("TwoStatements.kt")
|
||||
public void testTwoStatements() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/TwoStatements.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("While.kt")
|
||||
public void testWhile() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/While.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user