use of com.intellij.psi.impl.compiled.ClsMethodImpl in project intellij-community by JetBrains.
the class ClosureCompleter method runTemplate.
public static void runTemplate(List<ClosureParameterInfo> parameters, GrClosableBlock block, PsiSubstitutor substitutor, PsiMethod method, final Project project, final Editor editor) {
if (method instanceof ClsMethodImpl)
method = ((ClsMethodImpl) method).getSourceMirrorMethod();
assert block.getArrow() == null;
if (parameters.isEmpty())
return;
StringBuilder buffer = new StringBuilder();
buffer.append("{");
List<PsiType> paramTypes = ContainerUtil.newArrayList();
for (ClosureParameterInfo parameter : parameters) {
final String type = parameter.getType();
final String name = parameter.getName();
if (type != null) {
final PsiType fromText = JavaPsiFacade.getElementFactory(project).createTypeFromText(type, method);
final PsiType substituted = substitutor.substitute(fromText);
paramTypes.add(substituted);
buffer.append(substituted.getCanonicalText()).append(" ");
} else {
buffer.append("def ");
}
buffer.append(name);
buffer.append(", ");
}
buffer.replace(buffer.length() - 2, buffer.length(), " ->}");
final Document document = editor.getDocument();
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
assert file != null;
final GrClosableBlock closure = GroovyPsiElementFactory.getInstance(project).createClosureFromText(buffer.toString());
final GrClosableBlock templateClosure = (GrClosableBlock) block.replaceWithExpression(closure, false);
final TemplateBuilderImpl builder = new TemplateBuilderImpl(templateClosure);
int i = 0;
for (GrParameter p : templateClosure.getParameters()) {
final GrTypeElement typeElement = p.getTypeElementGroovy();
final PsiElement nameIdentifier = p.getNameIdentifierGroovy();
if (typeElement != null) {
final TypeConstraint[] typeConstraints = { SupertypeConstraint.create(paramTypes.get(i++)) };
final ChooseTypeExpression expression = new ChooseTypeExpression(typeConstraints, PsiManager.getInstance(project), nameIdentifier.getResolveScope());
builder.replaceElement(typeElement, expression);
} else {
final ChooseTypeExpression expression = new ChooseTypeExpression(TypeConstraint.EMPTY_ARRAY, PsiManager.getInstance(project), nameIdentifier.getResolveScope());
builder.replaceElement(p.getModifierList(), expression);
}
builder.replaceElement(nameIdentifier, new ParameterNameExpression(nameIdentifier.getText()));
}
final GrClosableBlock afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(templateClosure);
final Template template = builder.buildTemplate();
TextRange range = afterPostprocess.getTextRange();
document.deleteString(range.getStartOffset(), range.getEndOffset());
TemplateEditingListener templateListener = new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
PsiDocumentManager.getInstance(project).commitDocument(document);
final CaretModel caretModel = editor.getCaretModel();
final int offset = caretModel.getOffset();
GrClosableBlock block1 = PsiTreeUtil.findElementOfClassAtOffset(file, offset - 1, GrClosableBlock.class, false);
if (block1 != null) {
final PsiElement arrow = block1.getArrow();
if (arrow != null) {
caretModel.moveToOffset(arrow.getTextRange().getEndOffset());
}
// fix space before closure lbrace
final TextRange range1 = block1.getTextRange();
CodeStyleManager.getInstance(project).reformatRange(block1.getParent(), range1.getStartOffset() - 1, range1.getEndOffset(), true);
}
});
}
};
TemplateManager manager = TemplateManager.getInstance(project);
manager.startTemplate(editor, template, templateListener);
}
use of com.intellij.psi.impl.compiled.ClsMethodImpl in project intellij-community by JetBrains.
the class ClosureDescriptor method isMethodApplicable.
public boolean isMethodApplicable(PsiMethod method, GroovyPsiElement place) {
String name = String.valueOf(myMethod.get("name"));
if (name == null || !name.equals(method.getName()))
return false;
List<PsiType> types = new ArrayList<>();
final Object params = myMethod.get("params");
if (params instanceof Map) {
boolean first = true;
for (Object paramName : ((Map) params).keySet()) {
Object value = ((Map) params).get(paramName);
boolean isNamed = first && value instanceof List;
first = false;
String typeName = isNamed ? CommonClassNames.JAVA_UTIL_MAP : String.valueOf(value);
types.add(convertToPsiType(typeName, place));
}
} else if (params instanceof List) {
for (Object param : ((List) params)) {
PsiTypeParameterList typeParameterList = method.getTypeParameterList();
types.add(convertToPsiType(String.valueOf(param), typeParameterList != null ? typeParameterList : method));
}
}
final boolean isConstructor = Boolean.TRUE.equals(myMethod.get("constructor"));
final MethodSignature signature = MethodSignatureUtil.createMethodSignature(name, types.toArray(PsiType.createArray(types.size())), method.getTypeParameters(), PsiSubstitutor.EMPTY, isConstructor);
final GrClosureSignature closureSignature = GrClosureSignatureUtil.createSignature(signature);
if (method instanceof ClsMethodImpl)
method = ((ClsMethodImpl) method).getSourceMirrorMethod();
final PsiParameter[] parameters = method.getParameterList().getParameters();
final PsiType[] typeArray = ContainerUtil.map(parameters, parameter -> parameter.getType(), PsiType.createArray(parameters.length));
return GrClosureSignatureUtil.isSignatureApplicable(closureSignature, typeArray, place);
}
Aggregations