/*
 * 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.LookupElementDecorator;
import com.intellij.openapi.util.ClassConditionKey;

/**
 * Use only when you want to control lookup sorting & preference in simple cases when you have control over ALL the items in lookup.
 * When this is not the case, or sorting is too complex to be handled by single scalar,
 * please use relevance ({@link CompletionService#RELEVANCE_KEY}) & sorting ({@link CompletionService#SORTING_KEY}) weighers.
 *
 * @author peter
 */
public class PrioritizedLookupElement<T extends LookupElement> extends LookupElementDecorator<T> {
  public static final ClassConditionKey<PrioritizedLookupElement> CLASS_CONDITION_KEY = ClassConditionKey.create(PrioritizedLookupElement.class);
  private final double myPriority;
  private final int myGrouping;
  private final int myExplicitProximity;

  private PrioritizedLookupElement(T delegate, double priority, int grouping) {
    this(delegate, priority, grouping, 0);
  }

  private PrioritizedLookupElement(T delegate, double priority, int grouping, int explicitProximity) {
    super(delegate);
    myPriority = priority;
    myGrouping = grouping;
    myExplicitProximity = explicitProximity;
  }

  public double getPriority() {
    return myPriority;
  }

  public int getGrouping() {
    return myGrouping;
  }

  public int getExplicitProximity() {
    return myExplicitProximity;
  }

  public static LookupElement withPriority(LookupElement element, double priority) {
    final PrioritizedLookupElement prioritized = element.as(CLASS_CONDITION_KEY);
    final LookupElement finalElement = prioritized == null ? element : prioritized.getDelegate();
    final int grouping = prioritized == null ? 0 : prioritized.getGrouping();
    return new PrioritizedLookupElement<>(finalElement, priority, grouping);
  }

  public static LookupElement withGrouping(LookupElement element, int grouping) {
    final PrioritizedLookupElement prioritized = element.as(CLASS_CONDITION_KEY);
    LookupElement finalElement = prioritized == null ? element : prioritized.getDelegate();
    final double priority = prioritized == null ? 0 : prioritized.getPriority();
    return new PrioritizedLookupElement<>(finalElement, priority, grouping);
  }

  public static LookupElement withExplicitProximity(LookupElement element, int explicitProximity) {
    final PrioritizedLookupElement prioritized = element.as(CLASS_CONDITION_KEY);
    final double priority = prioritized == null ? 0 : prioritized.getPriority();
    final int grouping = prioritized == null ? 0 : prioritized.getGrouping();
    final LookupElement finalElement = prioritized == null ? element : prioritized.getDelegate();
    return new PrioritizedLookupElement<>(finalElement, priority, grouping, explicitProximity);
  }
}

@@ -20,10 +20,10 @@
 0-20
-09
+17
  Jet
@@ -39,560 +39,107 @@
 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 Licens
+ Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE fil
 e.%0A 
@@ -919,16 +919,20 @@
 ON_KEY =
+%0A   
  ClassCo
@@ -982,16 +982,17 @@
 class);%0A
+%0A
   privat
@@ -1022,40 +1022,8 @@
 ty;%0A
-  private final int myGrouping;%0A
   pr
@@ -1059,17 +1059,16 @@
 ximity;%0A
-%0A
   privat
@@ -1073,124 +1073,29 @@
 ate 
-PrioritizedLookupElement(T delegate, double priority, int grouping) %7B%0A    this(delegate, priority, grouping, 0);%0A  %7D
+final int myGrouping;
 %0A%0A  
@@ -1159,22 +1159,8 @@
 ity,
- int grouping,
  int
@@ -1169,32 +1169,46 @@
 xplicitProximity
+, int grouping
 ) %7B%0A    super(de
@@ -1247,35 +1247,8 @@
 ty;%0A
-    myGrouping = grouping;%0A
     
@@ -1280,32 +1280,59 @@
 licitProximity;%0A
+    myGrouping = grouping;%0A
   %7D%0A%0A  public do
@@ -1769,32 +1769,120 @@
 .getDelegate();%0A
+    final int proximity = prioritized == null ? 0 : prioritized.getExplicitProximity();%0A
     final int gr
@@ -2000,32 +2000,43 @@
 ement, priority,
+ proximity,
  grouping);%0A  %7D%0A
@@ -2369,24 +2369,112 @@
 Priority();%0A
+    final int proximity = prioritized == null ? 0 : prioritized.getExplicitProximity();%0A
     return n
@@ -2518,32 +2518,43 @@
 ement, priority,
+ proximity,
  grouping);%0A  %7D%0A
@@ -3062,18 +3062,8 @@
 ity,
- grouping,
  exp
@@ -3076,16 +3076,26 @@
 roximity
+, grouping
 );%0A  %7D%0A%7D

allow PrioritizedLookupElement to have all 3 features: