/*
 * Copyright 2000-2009 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.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.ui.StringComboboxEditor;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

/**
 * @author peter
 */
public class ComboEditorCompletionContributor extends CompletionContributor implements DumbAware {

  public static final Key<Boolean> CONTINUE_RUN_COMPLETION = Key.create("CONTINUE_RUN_COMPLETION");

  @Override
  public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    if (parameters.getInvocationCount() == 0) {
      return;
    }

    final PsiFile file = parameters.getOriginalFile();
    final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
    if (document != null) {
      JComboBox comboBox = document.getUserData(StringComboboxEditor.COMBO_BOX_KEY);
      if (comboBox != null) {
        String substring = document.getText().substring(0, parameters.getOffset());
        boolean plainPrefixMatcher = Boolean.TRUE.equals(document.getUserData(StringComboboxEditor.USE_PLAIN_PREFIX_MATCHER));
        final CompletionResultSet resultSet = plainPrefixMatcher ?
                                              result.withPrefixMatcher(new PlainPrefixMatcher(substring)) :
                                              result.withPrefixMatcher(substring);
        final int count = comboBox.getItemCount();
        for (int i = 0; i < count; i++) {
          final Object o = comboBox.getItemAt(i);
          if (o instanceof String) {
            resultSet.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create((String)o).withInsertHandler(
              new InsertHandler<LookupElement>() {
                @Override
                public void handleInsert(final InsertionContext context, final LookupElement item) {
                  final Document document = context.getEditor().getDocument();
                  document.deleteString(context.getEditor().getCaretModel().getOffset(), document.getTextLength());
                }
              }), count-i));
          }
        }
        if (!Boolean.TRUE.equals(document.getUserData(CONTINUE_RUN_COMPLETION))) {
          result.stopHere();
        }
      }
    }
  }
}

@@ -1,13 +1,10 @@
 /
-*%0A *
+/
  Copyrig
@@ -17,10 +17,10 @@
 0-20
-09
+18
  Jet
@@ -36,566 +36,109 @@
 r.o.
-%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */
+ Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 %0Apac
@@ -837,16 +837,139 @@
 ETION%22);
+%0A  private static final Key%3CLookupElementProvider%3E LOOKUP_ELEMENT_PROVIDER_KEY = Key.create(%22LOOKUP_ELEMENT_PROVIDER_KEY%22);
 %0A%0A  @Ove
@@ -1939,16 +1939,150 @@
 tring);%0A
+        final LookupElementProvider lookupElementProvider = LOOKUP_ELEMENT_PROVIDER_KEY.get(document, LookupElementProvider.DEFAULT);%0A
         
@@ -2324,17 +2324,17 @@
 riority(
-L
+l
 ookupEle
@@ -2341,52 +2341,192 @@
 ment
-Builder.create((String)o).withInsertHandler(
+Provider.getLookupElement((String)o), count - i));%0A          %7D%0A        %7D%0A        if (!Boolean.TRUE.equals(document.getUserData(CONTINUE_RUN_COMPLETION))) %7B%0A          result.stopHere();
 %0A   
@@ -2534,188 +2534,692 @@
     
+%7D%0A
       
-new InsertHandler%3CLookupElement%3E() %7B%0A                @Override%0A                public void handleInsert(final InsertionContext context, final LookupElement item) %7B%0A          
+%7D%0A    %7D%0A  %7D%0A%0A  public static void installLookupElementProvider(@NotNull final Document document, @NotNull final LookupElementProvider provider) %7B%0A    LOOKUP_ELEMENT_PROVIDER_KEY.set(document, provider);%0A  %7D%0A%0A  public interface LookupElementProvider %7B%0A    DefaultLookupElementProvider DEFAULT = new DefaultLookupElementProvider();%0A    LookupElement getLookupElement(@NotNull String lookupString);%0A  %7D%0A%0A  public static class DefaultLookupElementProvider implements LookupElementProvider %7B%0A    @Override%0A    public LookupElementBuilder getLookupElement(@NotNull String lookupString) %7B%0A      return LookupElementBuilder.create(lookupString).withInsertHandler((context, item) -%3E %7B%0A
     
@@ -3283,26 +3283,16 @@
 ment();%0A
-          
         
@@ -3399,200 +3399,11 @@
     
-          %7D%0A              %7D), count-i));%0A          %7D%0A        %7D%0A        if (!Boolean.TRUE.equals(document.getUserData(CONTINUE_RUN_COMPLETION))) %7B%0A          result.stopHere();%0A        %7D%0A      %7D
+%7D);
 %0A   

extend ComboEditorCompletionContributor with LookupElementProvider