Complete functions with single parameter on typing '{'

For KT-4049 When completing a function that takes a lambda and immediately typing '{', swallow the '{'
 #KT-4049 Fixed
This commit is contained in:
Nikolay Krasko
2013-10-15 17:22:08 +04:00
parent 31a4d91122
commit 0c49b91098
9 changed files with 90 additions and 50 deletions
@@ -0,0 +1,9 @@
<root>
<item
name='com.intellij.codeInsight.lookup.CharFilter com.intellij.codeInsight.lookup.CharFilter.Result acceptChar(char, int, com.intellij.codeInsight.lookup.Lookup) 2'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.intellij.codeInsight.lookup.Lookup com.intellij.openapi.editor.Editor getEditor()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
+1 -1
View File
@@ -202,7 +202,7 @@
<completion.skip implementation="org.jetbrains.jet.plugin.liveTemplates.JetLiveTemplateCompletionContributor$Skipper"
id="skipLiveTemplate"/>
<lookup.charFilter implementation="org.jetbrains.jet.plugin.completion.JetReferenceCharFilter"/>
<lookup.charFilter implementation="org.jetbrains.jet.plugin.completion.JetCompletionCharFilter"/>
<psi.referenceContributor language="jet" order="after JetCompletionContributor"
implementation="org.jetbrains.jet.plugin.references.JetReferenceContributor"/>
@@ -37,14 +37,13 @@ import org.jetbrains.jet.lang.resolve.lazy.KotlinCodeAnalyzer;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.plugin.JetDescriptorIconProvider;
import org.jetbrains.jet.plugin.completion.handlers.*;
import org.jetbrains.jet.plugin.completion.handlers.JetClassInsertHandler;
import org.jetbrains.jet.plugin.completion.handlers.JetJavaClassInsertHandler;
import org.jetbrains.jet.renderer.DescriptorRenderer;
import java.util.List;
import static org.jetbrains.jet.plugin.completion.handlers.JetFunctionInsertHandler.EMPTY_FUNCTION_HANDLER;
import static org.jetbrains.jet.plugin.completion.handlers.JetFunctionInsertHandler.PARAMS_BRACES_FUNCTION_HANDLER;
import static org.jetbrains.jet.plugin.completion.handlers.JetFunctionInsertHandler.PARAMS_PARENTHESIS_FUNCTION_HANDLER;
import static org.jetbrains.jet.plugin.completion.handlers.JetFunctionInsertHandler.*;
public final class DescriptorLookupConverter {
private DescriptorLookupConverter() {}
@@ -102,7 +101,7 @@ public final class DescriptorLookupConverter {
}
@Nullable
private static InsertHandler<LookupElement> getInsertHandler(@NotNull DeclarationDescriptor descriptor) {
public static InsertHandler<LookupElement> getInsertHandler(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof FunctionDescriptor) {
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
@@ -0,0 +1,63 @@
/*
* 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.completion
import com.intellij.codeInsight.lookup.CharFilter
import com.intellij.codeInsight.lookup.Lookup
import com.intellij.codeInsight.lookup.CharFilter.Result
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.plugin.completion.handlers.JetFunctionInsertHandler
public open class JetCompletionCharFilter() : CharFilter() {
public override fun acceptChar(c : Char, prefixLength : Int, lookup : Lookup) : Result? {
if (lookup.getPsiFile() !is JetFile) return null
fun checkHideLookupForRangeOperator(): Result? {
if (c == '.' && prefixLength == 0 && !lookup.isSelectionTouched()) {
val caret = lookup.getEditor().getCaretModel().getOffset()
if (caret > 0 && (lookup.getEditor().getDocument().getCharsSequence().charAt(caret - 1)) == '.') {
return Result.HIDE_LOOKUP
}
}
return null
}
fun checkFinishCompletionForOpenBrace(): Result? {
if (c == '{') {
val currentItem = lookup.getCurrentItem()
if (currentItem != null && (currentItem.getObject() is JetLookupObject)) {
val lookupObject = (currentItem.getObject() as JetLookupObject)
val descriptor = lookupObject.getDescriptor()
if (descriptor != null) {
val handler = DescriptorLookupConverter.getInsertHandler(descriptor)
if (handler == JetFunctionInsertHandler.PARAMS_BRACES_FUNCTION_HANDLER) {
return Result.SELECT_ITEM_AND_FINISH_LOOKUP
}
}
}
}
return null
}
return checkHideLookupForRangeOperator() ?:
checkFinishCompletionForOpenBrace() ?:
null
}
}
@@ -1,43 +0,0 @@
/*
* 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.completion;
import com.intellij.codeInsight.lookup.CharFilter;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
public class JetReferenceCharFilter extends CharFilter {
@Override
@Nullable
public Result acceptChar(char c, int prefixLength, Lookup lookup) {
PsiFile psiFile = lookup.getPsiFile();
if (!(psiFile instanceof JetFile)) {
return null;
}
if (c == '.' && prefixLength == 0 && !lookup.isSelectionTouched()) {
int caret = lookup.getEditor().getCaretModel().getOffset();
if (caret > 0 && lookup.getEditor().getDocument().getCharsSequence().charAt(caret - 1) == '.') {
return Result.HIDE_LOOKUP;
}
}
return null;
}
}
@@ -90,7 +90,7 @@ public class JetFunctionInsertHandler(val caretPosition : CaretPosition, val bra
var inBracketsShift = 0
if (openingBracketIndex == -1) {
if (braces) {
if (completionChar == ' ') {
if (completionChar == ' ' || completionChar == '{') {
context.setAddCompletionChar(false)
}
@@ -0,0 +1,5 @@
fun some(f: () -> Unit) { f() }
fun test() {
some<caret>
}
@@ -0,0 +1,5 @@
fun some(f: () -> Unit) { f() }
fun test() {
some { <caret> }
}
@@ -90,6 +90,8 @@ public class CompletionHandlerTest() : JetLightCodeInsightFixtureTestCase() {
fun testTabInsertInSimpleName() = doTest(CompletionType.BASIC, 0, "vvvvv", null, '\t')
fun testInsertFunctionWithSingleParameterWithBrace() = doTest(CompletionType.BASIC, 0, "some", null, '{')
var fixture by Delegates.notNull<JavaCodeInsightTestFixture>()
protected override fun setUp() {