IDEA-173927 Support keywords autocompletion when keyboard layout is incorrect

This commit is contained in:
peter
2017-09-15 22:04:10 +02:00
parent 28be43230b
commit e5bde2ff34
6 changed files with 62 additions and 7 deletions
@@ -252,7 +252,7 @@ public class JavaCompletionContributor extends CompletionContributor {
PrefixMatcher matcher = result.getPrefixMatcher();
PsiElement parent = position.getParent();
if (JavaKeywordCompletion.addWildcardExtendsSuper(result, position)) {
if (new JavaKeywordCompletion(parameters, session).addWildcardExtendsSuper(result, position)) {
return;
}
@@ -166,6 +166,7 @@ public class JavaKeywordCompletion {
private final JavaCompletionSession mySession;
private final PsiElement myPosition;
private final String myPrefix;
private final PrefixMatcher myKeywordMatcher;
private final List<LookupElement> myResults = new ArrayList<>();
private final PsiElement myPrevLeaf;
@@ -173,6 +174,7 @@ public class JavaKeywordCompletion {
myParameters = parameters;
mySession = session;
myPrefix = session.getMatcher().getPrefix();
myKeywordMatcher = new FixingLayoutPlainMatcher(myPrefix);
myPosition = parameters.getPosition();
myPrevLeaf = prevSignificantLeaf(myPosition);
@@ -185,7 +187,7 @@ public class JavaKeywordCompletion {
}
private void addKeyword(LookupElement element) {
if (element.getLookupString().startsWith(myPrefix)) {
if (myKeywordMatcher.isStartMatch(element.getLookupString())) {
myResults.add(element);
}
}
@@ -313,10 +315,10 @@ public class JavaKeywordCompletion {
addExtendsImplements();
}
static boolean addWildcardExtendsSuper(CompletionResultSet result, PsiElement position) {
boolean addWildcardExtendsSuper(CompletionResultSet result, PsiElement position) {
if (JavaMemberNameCompletionContributor.INSIDE_TYPE_PARAMS_PATTERN.accepts(position)) {
for (String keyword : ContainerUtil.ar(PsiKeyword.EXTENDS, PsiKeyword.SUPER)) {
if (keyword.startsWith(result.getPrefixMatcher().getPrefix())) {
if (myKeywordMatcher.isStartMatch(keyword)) {
LookupElement item = BasicExpressionCompletionContributor.createKeywordLookupItem(position, keyword);
result.addElement(new OverridableSpace(item, TailType.HUMBLE_SPACE_BEFORE_WORD));
}
@@ -0,0 +1,5 @@
public class Util {
int goo() {
ку<caret>ку
}
}
@@ -132,6 +132,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase {
public void testAbstractInInterface() { doTest(1, "abstract"); }
public void testCharInAnnotatedParameter() { doTest(1, "char"); }
public void testReturnInTernary() { doTest(1, "return"); }
public void testReturnInRussian() { doTest(1, "return"); }
public void testFinalAfterParameterAnno() { doTest(2, "final", "float", "class"); }
public void testFinalAfterParameterAnno2() { doTest(2, "final", "float", "class"); }
public void testFinalAfterCase() { doTest(3, "final", "float", "class"); }
@@ -0,0 +1,48 @@
/*
* 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;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.codeStyle.FixingLayoutMatcher;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class FixingLayoutPlainMatcher extends PrefixMatcher {
@Nullable private final String myAlternativePrefix;
public FixingLayoutPlainMatcher(String prefix) {
super(prefix);
myAlternativePrefix = FixingLayoutMatcher.fixLayout(prefix);
}
@Override
public boolean isStartMatch(String name) {
return StringUtil.startsWithIgnoreCase(name, getPrefix()) ||
myAlternativePrefix != null && StringUtil.startsWithIgnoreCase(name, myAlternativePrefix);
}
@Override
public boolean prefixMatches(@NotNull String name) {
return StringUtil.containsIgnoreCase(name, getPrefix()) ||
myAlternativePrefix != null && StringUtil.containsIgnoreCase(name, myAlternativePrefix);
}
@NotNull
@Override
public PrefixMatcher cloneWithPrefix(@NotNull String prefix) {
return new FixingLayoutPlainMatcher(prefix);
}
}
@@ -33,13 +33,12 @@ public class FixingLayoutMatcher extends MinusculeMatcher {
public FixingLayoutMatcher(@NotNull String pattern, @NotNull NameUtil.MatchingCaseSensitivity options, String hardSeparators) {
super(pattern, options, hardSeparators);
String s = fixPattern(pattern);
String s = fixLayout(pattern);
myFixedMatcher = s == null ? null : new MinusculeMatcher(s, options, hardSeparators);
}
@Nullable
private static String fixPattern(String pattern) {
public static String fixLayout(String pattern) {
boolean hasLetters = false;
boolean onlyWrongLetters = true;
for (int i = 0; i < pattern.length(); i++) {