/** Constructs an {@code AbstractCallable} suitable for wrapping a method reference. */ public static CallableBuilder methodReference( CeylonTransformer gen, Tree.Term expr, ParameterList parameterList) { CallableBuilder cb = new CallableBuilder(gen); cb.paramLists = parameterList; cb.typeModel = expr.getTypeModel(); cb.forwardCallTo = expr; return cb; }
private void extractExpressionInFile(TextChange tfc) throws CoreException { tfc.setEdit(new MultiTextEdit()); IDocument doc = tfc.getCurrentDocument(null); Tree.Term term = (Tree.Term) node; Integer start = term.getStartIndex(); int length = term.getStopIndex() - start + 1; String exp = toString(term); FindContainerVisitor fsv = new FindContainerVisitor(term); rootNode.visit(fsv); Tree.Declaration decNode = fsv.getDeclaration(); /*if (decNode instanceof Tree.Declaration) { Tree.AnnotationList anns = ((Tree.Declaration) decNode).getAnnotationList(); if (anns!=null && !anns.getAnnotations().isEmpty()) { decNode = anns.getAnnotations().get(0); } }*/ Declaration dec = decNode.getDeclarationModel(); FindLocalReferencesVisitor flrv = new FindLocalReferencesVisitor(dec); term.visit(flrv); List<TypeDeclaration> localTypes = new ArrayList<TypeDeclaration>(); for (Tree.BaseMemberExpression bme : flrv.getLocalReferences()) { addLocalType(dec, bme.getTypeModel(), localTypes, new ArrayList<ProducedType>()); } String params = ""; String args = ""; if (!flrv.getLocalReferences().isEmpty()) { for (Tree.BaseMemberExpression bme : flrv.getLocalReferences()) { params += bme.getTypeModel().getProducedTypeName() + " " + bme.getIdentifier().getText() + ", "; args += bme.getIdentifier().getText() + ", "; } params = params.substring(0, params.length() - 2); args = args.substring(0, args.length() - 2); } String indent = "\n" + getIndent(decNode, doc); String extraIndent = indent + getDefaultIndent(); String typeParams = ""; String constraints = ""; if (!localTypes.isEmpty()) { for (TypeDeclaration t : localTypes) { typeParams += t.getName() + ", "; if (!t.getSatisfiedTypes().isEmpty()) { constraints += extraIndent + getDefaultIndent() + "given " + t.getName() + " satisfies "; for (ProducedType pt : t.getSatisfiedTypes()) { constraints += pt.getProducedTypeName() + "&"; } constraints = constraints.substring(0, constraints.length() - 1); } } typeParams = "<" + typeParams.substring(0, typeParams.length() - 2) + ">"; } String type; String ending; if ("Void".equals(term.getTypeModel().getProducedTypeName())) { type = "void"; ending = ""; } else { type = explicitType || dec.isToplevel() ? term.getTypeModel().getProducedTypeName() : "function"; ending = "return "; } tfc.addEdit( new InsertEdit( decNode.getStartIndex(), type + " " + newName + typeParams + "(" + params + ")" + constraints + " {" + extraIndent + ending + exp + ";" + indent + "}" + indent + indent)); tfc.addEdit(new ReplaceEdit(start, length, newName + "(" + args + ")")); }