diff --git a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java index 50ad3510465..9cabe5838a2 100644 --- a/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java +++ b/idea/src/org/jetbrains/jet/plugin/completion/handlers/JetFunctionInsertHandler.java @@ -23,8 +23,11 @@ import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; @@ -35,6 +38,7 @@ import org.jetbrains.jet.lang.psi.JetQualifiedExpression; import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.plugin.completion.JetLookupObject; +import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings; import org.jetbrains.jet.plugin.quickfix.ImportInsertHelper; /** @@ -100,8 +104,14 @@ public class JetFunctionInsertHandler implements InsertHandler { if (openingBracketIndex == -1) { // Insert ()/{} if it's not already exist if (braces) { - document.insertString(endOffset, " {}"); - openingBracketIndex = endOffset + 1; + if (isInsertSpacesInOneLineFunctionEnabled(offsetElement.getProject())) { + document.insertString(endOffset, " { }"); + openingBracketIndex = endOffset + 2; + } + else { + document.insertString(endOffset, " {}"); + openingBracketIndex = endOffset + 1; + } } else { document.insertString(endOffset, "()"); @@ -125,6 +135,13 @@ public class JetFunctionInsertHandler implements InsertHandler { PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument()); } + private static boolean isInsertSpacesInOneLineFunctionEnabled(Project project) { + CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project); + JetCodeStyleSettings jetSettings = settings.getCustomSettings(JetCodeStyleSettings.class); + + return jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD; + } + private static void addImport(final InsertionContext context, final @NotNull LookupElement item) { ApplicationManager.getApplication().runReadAction(new Runnable() { @Override diff --git a/idea/testData/completion/handlers/FunctionLiteralInsertWhenNoSpacesForBraces.kt b/idea/testData/completion/handlers/FunctionLiteralInsertWhenNoSpacesForBraces.kt new file mode 100644 index 00000000000..dc92137c7c7 --- /dev/null +++ b/idea/testData/completion/handlers/FunctionLiteralInsertWhenNoSpacesForBraces.kt @@ -0,0 +1,5 @@ +fun jet.Array.filter(predicate : (T) -> jet.Boolean) : java.util.List = throw UnsupportedOperationException() + +fun main(args: Array) { + args.fil +} diff --git a/idea/testData/completion/handlers/FunctionLiteralInsertWhenNoSpacesForBraces.kt.after b/idea/testData/completion/handlers/FunctionLiteralInsertWhenNoSpacesForBraces.kt.after new file mode 100644 index 00000000000..83984db0e82 --- /dev/null +++ b/idea/testData/completion/handlers/FunctionLiteralInsertWhenNoSpacesForBraces.kt.after @@ -0,0 +1,5 @@ +fun jet.Array.filter(predicate : (T) -> jet.Boolean) : java.util.List = throw UnsupportedOperationException() + +fun main(args: Array) { + args.filter {} +} diff --git a/idea/testData/completion/handlers/HigherOrderFunction.kt.after b/idea/testData/completion/handlers/HigherOrderFunction.kt.after index 83984db0e82..daeaa796a9e 100644 --- a/idea/testData/completion/handlers/HigherOrderFunction.kt.after +++ b/idea/testData/completion/handlers/HigherOrderFunction.kt.after @@ -1,5 +1,5 @@ fun jet.Array.filter(predicate : (T) -> jet.Boolean) : java.util.List = throw UnsupportedOperationException() fun main(args: Array) { - args.filter {} + args.filter { } } diff --git a/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java b/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java index f54728abc05..e1a092f032d 100644 --- a/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java +++ b/idea/tests/org/jetbrains/jet/completion/handlers/CompletionHandlerTest.java @@ -23,9 +23,12 @@ import com.intellij.codeInsight.lookup.LookupElementPresentation; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.codeInsight.lookup.impl.LookupImpl; import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import junit.framework.Assert; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.plugin.PluginTestCaseBase; +import org.jetbrains.jet.plugin.formatter.JetCodeStyleSettings; import java.io.File; @@ -76,6 +79,18 @@ public class CompletionHandlerTest extends LightCompletionTestCase { doTest(); } + public void testFunctionLiteralInsertWhenNoSpacesForBraces() { + CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); + JetCodeStyleSettings jetSettings = settings.getCustomSettings(JetCodeStyleSettings.class); + + try { + jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = false; + doTest(); + } finally { + jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true; + } + } + public void testHigherOrderFunction() { doTest(); }