[SourceKit] Implement a new SourceKit request to generate the interface for a given type. rdar://27306890 (#3586)

This patch allows SourceKit to generate the interface for a given type specified by its mangled name.
Type interface is refined decl printing with type parameters localized and unusable members hidden.

Required field:
   "key.request": "source.request.editor.open.interface.swifttype"
   "key.usr": the mangled name of the given type.
This commit is contained in:
Xi Ge
2016-07-18 18:05:47 -07:00
committed by GitHub
parent 53a3a65d2c
commit 2a860100a5
10 changed files with 245 additions and 23 deletions

View File

@@ -147,27 +147,38 @@ getUnderlyingClangModuleForImport(ImportDecl *Import) {
return nullptr;
}
static void printTypeNameToString(Type Ty, std::string &Text) {
SmallString<128> Buffer;
llvm::raw_svector_ostream OS(Buffer);
Ty->print(OS);
Text = OS.str();
}
bool swift::ide::
printTypeInterface(ModuleDecl *M, Type Ty, ASTPrinter &Printer,
std::string &Error) {
if (!Ty)
return false;
std::string &TypeName, std::string &Error) {
if (!Ty) {
if (Error.empty())
Error = "type cannot be null.";
return true;
}
Ty = Ty->getRValueType();
if (auto ND = Ty->getNominalOrBoundGenericNominal()) {
PrintOptions Options = PrintOptions::printTypeInterface(Ty.getPointer(), M);
ND->print(Printer, Options);
return true;
printTypeNameToString(Ty, TypeName);
return false;
}
Error = "cannot find declaration of type.";
return false;
return true;
}
bool swift::ide::
printTypeInterface(ModuleDecl *M, StringRef TypeUSR, ASTPrinter &Printer,
std::string &Error) {
std::string &TypeName, std::string &Error) {
return printTypeInterface(M, getTypeFromMangledSymbolname(M->getASTContext(),
TypeUSR, Error),
Printer, Error);
Printer, TypeName, Error);
}
void swift::ide::printModuleInterface(Module *M, Optional<StringRef> Group,