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
|
//===--- SwiftConformingMethodList.cpp ------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "SwiftASTManager.h"
#include "SwiftEditorDiagConsumer.h"
#include "SwiftLangSupport.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/IDE/ConformingMethodList.h"
#include "swift/IDETool/IDEInspectionInstance.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Comment.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Type.h"
using namespace SourceKit;
using namespace swift;
using namespace ide;
static void
translateConformingMethodListOptions(OptionsDictionary &from,
ConformingMethodList::Options &to) {
// ConformingMethodList doesn't receive any options at this point.
}
static void
deliverResults(SourceKit::ConformingMethodListConsumer &SKConsumer,
CancellableResult<ConformingMethodListResults> Result) {
switch (Result.getKind()) {
case CancellableResultKind::Success: {
SKConsumer.setReusingASTContext(Result->DidReuseAST);
if (!Result->Result) {
// If we have no results, don't call SKConsumer.handleResult which causes
// empty results to be delivered.
break;
}
SmallString<512> SS;
llvm::raw_svector_ostream OS(SS);
unsigned TypeNameBegin = SS.size();
Result->Result->ExprType.print(OS);
unsigned TypeNameLength = SS.size() - TypeNameBegin;
unsigned TypeUSRBegin = SS.size();
SwiftLangSupport::printTypeUSR(Result->Result->ExprType, OS);
unsigned TypeUSRLength = SS.size() - TypeUSRBegin;
struct MemberInfo {
size_t DeclNameBegin = 0;
size_t DeclNameLength = 0;
size_t TypeNameBegin = 0;
size_t TypeNameLength = 0;
size_t TypeUSRBegin = 0;
size_t TypeUSRLength = 0;
size_t DescriptionBegin = 0;
size_t DescriptionLength = 0;
size_t SourceTextBegin = 0;
size_t SourceTextLength = 0;
StringRef BriefComment;
MemberInfo() {}
};
SmallVector<MemberInfo, 8> Members;
for (auto member : Result->Result->Members) {
Members.emplace_back();
auto &memberElem = Members.back();
auto funcTy = cast<FuncDecl>(member)->getMethodInterfaceType();
funcTy = Result->Result->ExprType->getTypeOfMember(
Result->Result->DC->getParentModule(), member, funcTy);
auto resultTy = funcTy->castTo<FunctionType>()->getResult();
// Name.
memberElem.DeclNameBegin = SS.size();
member->getName().print(OS);
memberElem.DeclNameLength = SS.size() - memberElem.DeclNameBegin;
// Type name.
memberElem.TypeNameBegin = SS.size();
resultTy.print(OS);
memberElem.TypeNameLength = SS.size() - memberElem.TypeNameBegin;
// Type USR.
memberElem.TypeUSRBegin = SS.size();
SwiftLangSupport::printTypeUSR(resultTy, OS);
memberElem.TypeUSRLength = SS.size() - memberElem.TypeUSRBegin;
// Description.
memberElem.DescriptionBegin = SS.size();
SwiftLangSupport::printMemberDeclDescription(
member, Result->Result->ExprType, /*usePlaceholder=*/false, OS);
memberElem.DescriptionLength = SS.size() - memberElem.DescriptionBegin;
// Sourcetext.
memberElem.SourceTextBegin = SS.size();
SwiftLangSupport::printMemberDeclDescription(
member, Result->Result->ExprType, /*usePlaceholder=*/true, OS);
memberElem.SourceTextLength = SS.size() - memberElem.SourceTextBegin;
// DocBrief.
auto MaybeClangNode = member->getClangNode();
if (MaybeClangNode) {
if (auto *D = MaybeClangNode.getAsDecl()) {
const auto &ClangContext = D->getASTContext();
if (const clang::RawComment *RC =
ClangContext.getRawCommentForAnyRedecl(D))
memberElem.BriefComment = RC->getBriefText(ClangContext);
}
} else {
memberElem.BriefComment = member->getSemanticBriefComment();
}
}
SourceKit::ConformingMethodListResult SKResult;
SmallVector<SourceKit::ConformingMethodListResult::Member, 8> SKMembers;
for (auto info : Members) {
StringRef Name(SS.begin() + info.DeclNameBegin, info.DeclNameLength);
StringRef TypeName(SS.begin() + info.TypeNameBegin, info.TypeNameLength);
StringRef TypeUSR(SS.begin() + info.TypeUSRBegin, info.TypeUSRLength);
StringRef Description(SS.begin() + info.DescriptionBegin,
info.DescriptionLength);
StringRef SourceText(SS.begin() + info.SourceTextBegin,
info.SourceTextLength);
SKMembers.push_back({Name, TypeName, TypeUSR, Description, SourceText,
info.BriefComment});
}
SKResult.TypeName = StringRef(SS.begin() + TypeNameBegin, TypeNameLength);
SKResult.TypeUSR = StringRef(SS.begin() + TypeUSRBegin, TypeUSRLength);
SKResult.Members = SKMembers;
SKConsumer.handleResult(SKResult);
break;
}
case CancellableResultKind::Failure:
SKConsumer.failed(Result.getError());
break;
case CancellableResultKind::Cancelled:
SKConsumer.cancelled();
break;
}
}
void SwiftLangSupport::getConformingMethodList(
llvm::MemoryBuffer *UnresolvedInputFile, unsigned Offset,
OptionsDictionary *optionsDict, ArrayRef<const char *> Args,
ArrayRef<const char *> ExpectedTypeNames,
SourceKitCancellationToken CancellationToken,
SourceKit::ConformingMethodListConsumer &SKConsumer,
std::optional<VFSOptions> vfsOptions) {
std::string error;
// FIXME: the use of None as primary file is to match the fact we do not read
// the document contents using the editor documents infrastructure.
auto fileSystem =
getFileSystem(vfsOptions, /*primaryFile=*/std::nullopt, error);
if (!fileSystem) {
return SKConsumer.failed(error);
}
ConformingMethodList::Options options;
if (optionsDict) {
translateConformingMethodListOptions(*optionsDict, options);
}
performWithParamsToCompletionLikeOperation(
UnresolvedInputFile, Offset, /*InsertCodeCompletionToken=*/true, Args,
fileSystem, CancellationToken,
[&](CancellableResult<CompletionLikeOperationParams> ParmsResult) {
ParmsResult.mapAsync<ConformingMethodListResults>(
[&](auto &Params, auto DeliverTransformed) {
getIDEInspectionInstance()->conformingMethodList(
Params.Invocation, Args, fileSystem, Params.completionBuffer,
Offset, Params.DiagC, ExpectedTypeNames,
Params.CancellationFlag, DeliverTransformed);
},
[&](auto Result) { deliverResults(SKConsumer, Result); });
});
}
|