/
DescriptorLookupConverter.java
219 lines (185 loc) · 9.58 KB
/
DescriptorLookupConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2010-2013 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 org.jetbrains.jet.plugin.completion;
import com.google.common.collect.Lists;
import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.completion.JavaMethodCallElement;
import com.intellij.codeInsight.completion.JavaPsiClassReferenceElement;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.codeInsight.lookup.VariableLookupItem;
import com.intellij.openapi.util.Iconable;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JavaResolverPsiUtils;
import org.jetbrains.jet.lang.resolve.lazy.KotlinCodeAnalyzer;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.plugin.JetDescriptorIconProvider;
import org.jetbrains.jet.plugin.completion.handlers.JetClassInsertHandler;
import org.jetbrains.jet.plugin.completion.handlers.JetJavaClassInsertHandler;
import org.jetbrains.jet.renderer.DescriptorRenderer;
import java.util.List;
import static org.jetbrains.jet.plugin.completion.handlers.JetFunctionInsertHandler.*;
public final class DescriptorLookupConverter {
private DescriptorLookupConverter() {}
@NotNull
public static LookupElement createLookupElement(@NotNull KotlinCodeAnalyzer analyzer,
@NotNull DeclarationDescriptor descriptor, @Nullable PsiElement declaration) {
if (declaration != null) {
LookupElement javaLookupElement = createJavaLookupElementIfPossible(declaration, descriptor);
if (javaLookupElement != null) {
return javaLookupElement;
}
}
LookupElementBuilder element = LookupElementBuilder.create(
new JetLookupObject(descriptor, analyzer, declaration), descriptor.getName().asString());
String presentableText = descriptor.getName().asString();
String typeText = "";
String tailText = "";
if (descriptor instanceof FunctionDescriptor) {
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
JetType returnType = functionDescriptor.getReturnType();
typeText = returnType != null ? DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(returnType) : "";
presentableText += DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderFunctionParameters(functionDescriptor);
boolean extensionFunction = functionDescriptor.getReceiverParameter() != null;
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
if (containingDeclaration != null && extensionFunction) {
tailText += " for " + DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(functionDescriptor.getReceiverParameter().getType());
tailText += " in " + DescriptorUtils.getFqName(containingDeclaration);
}
}
else if (descriptor instanceof VariableDescriptor) {
JetType outType = ((VariableDescriptor) descriptor).getType();
typeText = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(outType);
}
else if (descriptor instanceof ClassDescriptor) {
DeclarationDescriptor declaredIn = descriptor.getContainingDeclaration();
assert declaredIn != null;
tailText = " (" + DescriptorUtils.getFqName(declaredIn) + ")";
}
else {
typeText = DescriptorRenderer.SHORT_NAMES_IN_TYPES.render(descriptor);
}
element = element.withInsertHandler(getInsertHandler(descriptor));
element = element.withTailText(tailText, true).withTypeText(typeText).withPresentableText(presentableText);
element = element.withIcon(JetDescriptorIconProvider.getIcon(descriptor, Iconable.ICON_FLAG_VISIBILITY));
element = element.withStrikeoutness(KotlinBuiltIns.getInstance().isDeprecated(descriptor));
return element;
}
@Nullable
public static InsertHandler<LookupElement> getInsertHandler(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof FunctionDescriptor) {
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
if (functionDescriptor.getValueParameters().isEmpty()) {
return EMPTY_FUNCTION_HANDLER;
}
if (functionDescriptor.getValueParameters().size() == 1 &&
KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(functionDescriptor.getValueParameters().get(0).getType())) {
return PARAMS_BRACES_FUNCTION_HANDLER;
}
return PARAMS_PARENTHESIS_FUNCTION_HANDLER;
}
if (descriptor instanceof ClassDescriptor) {
return JetClassInsertHandler.INSTANCE;
}
return null;
}
@Nullable
private static LookupElement createJavaLookupElementIfPossible(@NotNull PsiElement declaration, @NotNull DeclarationDescriptor descriptor) {
if (declaration instanceof PsiClass) {
PsiClass psiClass = (PsiClass) declaration;
if (!JavaResolverPsiUtils.isCompiledKotlinClassOrPackageClass(psiClass)) {
return setCustomInsertHandler(new JavaPsiClassReferenceElement(psiClass));
}
}
if (declaration instanceof PsiMember) {
PsiClass containingClass = ((PsiMember) declaration).getContainingClass();
if (containingClass != null && !JavaResolverPsiUtils.isCompiledKotlinClassOrPackageClass(containingClass)) {
if (declaration instanceof PsiMethod) {
InsertHandler<LookupElement> handler = getInsertHandler(descriptor);
assert handler != null:
"Special kotlin handler is expected for function: " + declaration.getText() +
" and descriptor: " + DescriptorRenderer.FQNAMES_IN_TYPES.render(descriptor);
return new JavaMethodCallElementWithCustomHandler(declaration).setInsertHandler(handler);
}
if (declaration instanceof PsiField) {
return new JavaVariableLookupItemWithCustomHandler(declaration);
}
}
}
return null;
}
public static LookupElement setCustomInsertHandler(JavaPsiClassReferenceElement javaPsiReferenceElement) {
return javaPsiReferenceElement.setInsertHandler(JetJavaClassInsertHandler.INSTANCE);
}
@NotNull
public static LookupElement createLookupElement(
@NotNull KotlinCodeAnalyzer analyzer,
@NotNull BindingContext bindingContext,
@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof CallableMemberDescriptor) {
descriptor = DescriptorUtils.unwrapFakeOverride((CallableMemberDescriptor) descriptor);
}
return createLookupElement(analyzer, descriptor, BindingContextUtils.descriptorToDeclaration(bindingContext, descriptor));
}
public static LookupElement[] collectLookupElements(
@NotNull KotlinCodeAnalyzer analyzer,
@NotNull BindingContext bindingContext,
@NotNull Iterable<DeclarationDescriptor> descriptors) {
List<LookupElement> result = Lists.newArrayList();
for (DeclarationDescriptor descriptor : descriptors) {
result.add(createLookupElement(analyzer, bindingContext, descriptor));
}
return result.toArray(new LookupElement[result.size()]);
}
private static class JavaMethodCallElementWithCustomHandler extends JavaMethodCallElement {
public JavaMethodCallElementWithCustomHandler(PsiElement declaration) {
super((PsiMethod) declaration);
}
@Override
public void handleInsert(InsertionContext context) {
InsertHandler<? extends LookupElement> handler = getInsertHandler();
if (handler != null) {
//noinspection unchecked
((InsertHandler)handler).handleInsert(context, this);
return;
}
super.handleInsert(context);
}
}
private static class JavaVariableLookupItemWithCustomHandler extends VariableLookupItem {
public JavaVariableLookupItemWithCustomHandler(PsiElement declaration) {
super((PsiField) declaration);
}
@Override
public void handleInsert(InsertionContext context) {
InsertHandler<? extends LookupElement> handler = getInsertHandler();
if (handler != null) {
//noinspection unchecked
((InsertHandler)handler).handleInsert(context, this);
return;
}
super.handleInsert(context);
}
}
}