From fe0ead80c0bd419fa4d7b6f6014f86fd10a154b4 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 8 May 2017 18:54:54 +0200 Subject: [PATCH] let's enable completion autopopup when typing digits and see if anyone complains (IDEA-123325) --- .../CustomFileTypeAutopopupTest.groovy | 34 +++++++++++++++++++ .../completion/JavaAutoPopupTest.groovy | 7 ++++ .../CustomFileTypeCompletionContributor.java | 18 +++------- .../CompletionAutoPopupHandler.java | 2 +- 4 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/completion/CustomFileTypeAutopopupTest.groovy diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/CustomFileTypeAutopopupTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/CustomFileTypeAutopopupTest.groovy new file mode 100644 index 000000000000..866f248881a6 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/CustomFileTypeAutopopupTest.groovy @@ -0,0 +1,34 @@ +/* + * Copyright 2000-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 com.intellij.codeInsight.completion + +/** + * @author peter + */ +class CustomFileTypeAutopopupTest extends CompletionAutoPopupTestCase { + void "test no autopopup when typing just digit in a custom file type"() { + myFixture.configureByText 'a.hs', 'a42 = 42\n }}' + type '4' + assert !lookup + } + + void "test show autopopup when typing digit after letter"() { + myFixture.configureByText 'a.hs', 'a42 = 42\na }}' + type '4' + assert lookup + } + +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy index fa9575330c42..a7b65ea0ad8d 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy @@ -1833,4 +1833,11 @@ ita myFixture.assertPreferredCompletionItems 0, 'KimeFamilyRange' } + + void "test show autopopup when typing digit after letter"() { + myFixture.configureByText 'a.java', 'class Foo {{ int a42; a }}' + type '4' + assert lookup + } + } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/CustomFileTypeCompletionContributor.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/CustomFileTypeCompletionContributor.java index 4294848318ea..0a3243b29e85 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/CustomFileTypeCompletionContributor.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/CustomFileTypeCompletionContributor.java @@ -23,7 +23,6 @@ import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType; import com.intellij.openapi.project.DumbAware; import com.intellij.psi.CustomHighlighterTokenType; -import com.intellij.psi.PsiElement; import com.intellij.psi.tree.IElementType; import com.intellij.util.ProcessingContext; import org.jetbrains.annotations.NotNull; @@ -56,7 +55,11 @@ public class CustomFileTypeCompletionContributor extends CompletionContributor i } SyntaxTable syntaxTable = ((CustomSyntaxTableFileType)fileType).getSyntaxTable(); - String prefix = findPrefix(parameters.getPosition(), parameters.getOffset()); + String prefix = CompletionUtil.findJavaIdentifierPrefix(parameters); + if (prefix.isEmpty() && parameters.isAutoPopup()) { + return; + } + CompletionResultSet resultSetWithPrefix = result.withPrefixMatcher(prefix); addVariants(resultSetWithPrefix, syntaxTable.getKeywords1()); @@ -90,15 +93,4 @@ public class CustomFileTypeCompletionContributor extends CompletionContributor i } } - private static String findPrefix(PsiElement insertedElement, int offset) { - String text = insertedElement.getText(); - int offsetInElement = offset - insertedElement.getTextOffset(); - int start = offsetInElement - 1; - while(start >=0 ) { - if(!Character.isJavaIdentifierStart(text.charAt(start))) break; - --start; - } - return text.substring(start+1, offsetInElement).trim(); - } - } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java index 36eb43e2cba9..4e4c510528dc 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/CompletionAutoPopupHandler.java @@ -59,7 +59,7 @@ public class CompletionAutoPopupHandler extends TypedHandlerDelegate { return Result.STOP; } - if (Character.isLetter(charTyped) || charTyped == '_') { + if (Character.isLetterOrDigit(charTyped) || charTyped == '_') { AutoPopupController.getInstance(project).scheduleAutoPopup(editor); return Result.STOP; }