Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7789062
commit 7c360a0
Showing
10 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...brains/jet/plugin/codeInsight/surroundWith/statement/KotlinFunctionLiteralSurrounder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2010-2013 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.codeInsight.surroundWith.statement; | ||
|
||
import com.intellij.codeInsight.CodeInsightUtilBase; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiElement; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.jetbrains.jet.lang.psi.*; | ||
import org.jetbrains.jet.plugin.JetBundle; | ||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.KotlinSurrounderUtils; | ||
|
||
public class KotlinFunctionLiteralSurrounder extends KotlinStatementsSurrounder { | ||
@Nullable | ||
@Override | ||
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) { | ||
// TODO extract variables declaration | ||
|
||
JetCallExpression callExpression = (JetCallExpression) JetPsiFactory.createExpression(project, "run {\n}"); | ||
callExpression = (JetCallExpression) container.addAfter(callExpression, statements[statements.length - 1]); | ||
container.addBefore(JetPsiFactory.createWhiteSpace(project), callExpression); | ||
|
||
JetFunctionLiteralExpression bodyExpression = (JetFunctionLiteralExpression) callExpression.getFunctionLiteralArguments().get(0); | ||
assert bodyExpression != null : "Body expression should exists for " + callExpression.getText(); | ||
JetBlockExpression blockExpression = bodyExpression.getBodyExpression(); | ||
assert blockExpression != null : "JetBlockExpression should exists for " + callExpression.getText(); | ||
//Add statements in function literal block | ||
KotlinSurrounderUtils.addStatementsInBlock(blockExpression, statements); | ||
|
||
//Delete statements from original code | ||
container.deleteChildRange(statements[0], statements[statements.length - 1]); | ||
|
||
callExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(callExpression); | ||
|
||
JetExpression literalName = callExpression.getCalleeExpression(); | ||
assert literalName != null : "Run expression should have callee expression " + callExpression.getText(); | ||
return literalName.getTextRange(); | ||
} | ||
|
||
@Override | ||
public String getTemplateDescription() { | ||
return JetBundle.message("surround.with.function.template"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fun foo() { | ||
<selection>"aaa" | ||
"aaa"</selection> | ||
} |
6 changes: 6 additions & 0 deletions
6
idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt.after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fun foo() { | ||
<selection>run</selection> { | ||
"aaa" | ||
"aaa" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fun foo() { | ||
<caret>"aaa" | ||
} |
5 changes: 5 additions & 0 deletions
5
idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt.after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fun foo() { | ||
<selection>run</selection> { | ||
"aaa" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...tbrains/jet/plugin/codeInsight/surroundWith/SurroundWithFunctionLiteralTestGenerated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2010-2013 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.codeInsight.surroundWith; | ||
|
||
import junit.framework.Assert; | ||
import junit.framework.Test; | ||
import junit.framework.TestSuite; | ||
|
||
import java.io.File; | ||
import java.util.regex.Pattern; | ||
import org.jetbrains.jet.JetTestUtils; | ||
import org.jetbrains.jet.test.InnerTestClasses; | ||
import org.jetbrains.jet.test.TestMetadata; | ||
|
||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTest; | ||
|
||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ | ||
@SuppressWarnings("all") | ||
@TestMetadata("idea/testData/codeInsight/surroundWith/functionLiteral") | ||
public class SurroundWithFunctionLiteralTestGenerated extends AbstractSurroundWithTest { | ||
public void testAllFilesPresentInFunctionLiteral() throws Exception { | ||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/functionLiteral"), Pattern.compile("^(.+)\\.kt$"), true); | ||
} | ||
|
||
@TestMetadata("multiStatement.kt") | ||
public void testMultiStatement() throws Exception { | ||
doTestWithFunctionLiteralSurrounder("idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt"); | ||
} | ||
|
||
@TestMetadata("singleStatement.kt") | ||
public void testSingleStatement() throws Exception { | ||
doTestWithFunctionLiteralSurrounder("idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt"); | ||
} | ||
|
||
} |