don't let live templates prevent case-insensitive matching items

don't accept live template variants by commas and other strange characters
This commit is contained in:
peter
2011-04-06 13:02:44 +02:00
parent 6a12b9c4de
commit 885ec01362
6 changed files with 110 additions and 12 deletions
@@ -631,4 +631,24 @@ public interface Test {
assert !lookup
}
public void testTemplateSelection() {
myFixture.configureByText("a.java", """
class Foo {
int ITER = 2;
int itea = 2;
{
it<caret>
}
}
""")
type 'e'
assertOrderedEquals myFixture.lookupElementStrings, "itea"
type 'r'
assertOrderedEquals myFixture.lookupElementStrings, "iter", "ITER", "Iterable", "Iterator"
type ','
assert !lookup
assert myFixture.editor.document.text.contains('iter,')
}
}
@@ -16,6 +16,7 @@
package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.template.impl.LiveTemplateLookupElement;
import com.intellij.patterns.PatternCondition;
import com.intellij.patterns.StandardPatterns;
import com.intellij.util.Consumer;
@@ -63,7 +64,7 @@ public class RelaxedMatchingContributor extends CompletionContributor {
}
CompletionParameters relaxed;
if (elements.isEmpty() && parameters.getInvocationCount() == 0) {
if (parameters.getInvocationCount() == 0 && (elements.isEmpty() || elements.size() == 1 && elements.iterator().next() instanceof LiveTemplateLookupElement)) {
relaxed = parameters.withRelaxedMatching();
}
else if (parameters.getInvocationCount() >= 2) {
@@ -0,0 +1,33 @@
/*
* Copyright 2000-2011 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.template.impl;
import com.intellij.codeInsight.lookup.CharFilter;
import com.intellij.codeInsight.lookup.Lookup;
/**
* @author peter
*/
public class LiveTemplateCharFilter extends CharFilter {
@Override
public Result acceptChar(char c, int prefixLength, Lookup lookup) {
if (lookup.getCurrentItem() instanceof LiveTemplateLookupElement && c != ' ') {
return Result.HIDE_LOOKUP;
}
return null;
}
}
@@ -16,9 +16,6 @@
package com.intellij.codeInsight.template.impl;
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.openapi.util.Condition;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiFile;
@@ -48,14 +45,7 @@ public class LiveTemplateCompletionContributor extends CompletionContributor {
final String prefix = result.getPrefixMatcher().getPrefix();
final TemplateImpl template = findApplicableTemplate(file, offset, prefix);
if (template != null) {
result.addElement(LookupElementBuilder.create(prefix).setTypeText(template.getDescription()).setInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset());
context.setAddCompletionChar(false);
TemplateManager.getInstance(context.getProject()).startTemplate(context.getEditor(), template);
}
}));
result.addElement(new LiveTemplateLookupElement(prefix, template));
} else {
for (final TemplateImpl possible : listApplicableTemplates(file, offset)) {
result.restartCompletionOnPrefixChange(possible.getKey());
@@ -0,0 +1,53 @@
/*
* Copyright 2000-2011 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.template.impl;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.codeInsight.template.TemplateManager;
import org.jetbrains.annotations.NotNull;
/**
* @author peter
*/
public class LiveTemplateLookupElement extends LookupElement {
private final String myPrefix;
private final TemplateImpl myTemplate;
public LiveTemplateLookupElement(String prefix, TemplateImpl template) {
myPrefix = prefix;
myTemplate = template;
}
@NotNull
@Override
public String getLookupString() {
return myPrefix;
}
@Override
public void renderElement(LookupElementPresentation presentation) {
super.renderElement(presentation);
presentation.setTypeText(myTemplate.getDescription());
}
@Override
public void handleInsert(InsertionContext context) {
context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset());
context.setAddCompletionChar(false);
TemplateManager.getInstance(context.getProject()).startTemplate(context.getEditor(), myTemplate);
}
}
@@ -402,6 +402,7 @@
<psi.referenceContributor implementation="com.intellij.psi.PsiReferenceContributorImpl"/>
<lookup.charFilter implementation="com.intellij.codeInsight.template.impl.LiveTemplateCharFilter" order="first" id="liveTemplate"/>
<lookup.charFilter implementation="com.intellij.codeInsight.completion.CompletionCharFilter" order="last" id="completion"/>
<lookup.charFilter implementation="com.intellij.refactoring.IdentifierCharFilter" id="identifier"/>
<lookup.charFilter implementation="com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceCharFilter" id="fileRef" order="before completion"/>