2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.template.postfix.completion;
18 import com.intellij.codeInsight.completion.CompletionParameters;
19 import com.intellij.codeInsight.completion.CompletionProvider;
20 import com.intellij.codeInsight.completion.CompletionResultSet;
21 import com.intellij.codeInsight.completion.PrefixMatcher;
22 import com.intellij.codeInsight.template.CustomTemplateCallback;
23 import com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor;
24 import com.intellij.codeInsight.template.postfix.settings.PostfixTemplatesSettings;
25 import com.intellij.codeInsight.template.postfix.templates.PostfixLiveTemplate;
26 import com.intellij.patterns.StandardPatterns;
27 import com.intellij.util.ProcessingContext;
28 import org.jetbrains.annotations.NotNull;
30 import static com.intellij.codeInsight.template.postfix.completion.PostfixTemplateCompletionContributor.getPostfixLiveTemplate;
32 class PostfixTemplatesCompletionProvider extends CompletionProvider<CompletionParameters> {
34 protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
35 if (!isCompletionEnabled(parameters) || LiveTemplateCompletionContributor.shouldShowAllTemplates() ||
36 parameters.getEditor().getCaretModel().getCaretCount() != 1) {
38 * disabled or covered with {@link com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor}
43 PostfixLiveTemplate postfixLiveTemplate = getPostfixLiveTemplate(parameters.getOriginalFile(), parameters.getEditor());
44 if (postfixLiveTemplate != null) {
45 postfixLiveTemplate.addCompletions(parameters, result.withPrefixMatcher(new MyPrefixMatcher(result.getPrefixMatcher().getPrefix())));
46 String possibleKey = postfixLiveTemplate.computeTemplateKeyWithoutContextChecking(
47 new CustomTemplateCallback(parameters.getEditor(), parameters.getOriginalFile()));
48 if (possibleKey != null) {
49 result = result.withPrefixMatcher(possibleKey);
50 result.restartCompletionOnPrefixChange(
51 StandardPatterns.string().oneOf(postfixLiveTemplate.getAllTemplateKeys(parameters.getOriginalFile(), parameters.getOffset())));
56 private static boolean isCompletionEnabled(@NotNull CompletionParameters parameters) {
57 if (!parameters.isAutoPopup()) {
61 PostfixTemplatesSettings settings = PostfixTemplatesSettings.getInstance();
62 if (settings == null || !settings.isPostfixTemplatesEnabled() || !settings.isTemplatesCompletionEnabled()) {
69 private static class MyPrefixMatcher extends PrefixMatcher {
70 protected MyPrefixMatcher(String prefix) {
75 public boolean prefixMatches(@NotNull String name) {
76 return name.equalsIgnoreCase(myPrefix);
81 public PrefixMatcher cloneWithPrefix(@NotNull String prefix) {
82 return new MyPrefixMatcher(prefix);