mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Add CollectVariableType request to SourceKit
- Add VariableTypeCollector This new SourceEntityWalker collects types from variable declarations. - Add SwiftLangSupport::collectVariableTypes - Implement CollectVariableType request - Provide information about explicit types in CollectVarType - Fix HasExplicitType in VariableTypeArray - Fix typo - Implement ranged CollectVariableTypes requests - Use offset/length params for CollectVariableType in sourcekitd-test - Address a bunch of PR suggestions - Remove CanonicalType from VariableTypeCollector This turned out not to be needed (for now). - Improve doc comment on VariableTypeCollector::getTypeOffsets - Remove unused CanonicalTy variable - Remove out-of-date comment - Run clang-format on the CollectVariableType implementation - Fix some minor style issues - Use emplace_back while collecting variable infos - Pass CollectVariableType range to VariableTypeCollector - Use capitalized variable names in VariableTypeArray ...as recommended by the LLVM coding standards - Use PrintOptions for type printing in VariableTypeCollector - Return void for collectVariableType This seems to be cleaner stylistically. - Avoid visiting subranges of invalid range in VariableTypeCollector - Use std::string for type buffer in VariableTypeCollectorASTConsumer - Use plural for PrintedType in VariableTypeArray - Remove unused fields in VariableTypeArrayBuilder - Add suggested doc comments to VariableTypeArray - Remove unused VariableTypeInfo.TypeLength - Fix typo of ostream in VariableTypeCollectorASTConsumer - Fix typo - Document Offset and Length semantics in collectVariableTypes
This commit is contained in:
@@ -2368,3 +2368,62 @@ void SwiftLangSupport::collectExpressionTypes(StringRef FileName,
|
||||
&OncePerASTToken,
|
||||
llvm::vfs::getRealFileSystem());
|
||||
}
|
||||
|
||||
void SwiftLangSupport::collectVariableTypes(
|
||||
StringRef FileName, ArrayRef<const char *> Args, Optional<unsigned> Offset,
|
||||
Optional<unsigned> Length,
|
||||
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver) {
|
||||
std::string Error;
|
||||
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, FileName, Error);
|
||||
if (!Invok) {
|
||||
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
|
||||
Receiver(RequestResult<VariableTypesInFile>::fromError(Error));
|
||||
return;
|
||||
}
|
||||
assert(Invok);
|
||||
|
||||
class VariableTypeCollectorASTConsumer : public SwiftASTConsumer {
|
||||
private:
|
||||
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver;
|
||||
Optional<unsigned> Offset;
|
||||
Optional<unsigned> Length;
|
||||
|
||||
public:
|
||||
VariableTypeCollectorASTConsumer(
|
||||
std::function<void(const RequestResult<VariableTypesInFile> &)>
|
||||
Receiver,
|
||||
Optional<unsigned> Offset, Optional<unsigned> Length)
|
||||
: Receiver(std::move(Receiver)), Offset(Offset), Length(Length) {}
|
||||
|
||||
void handlePrimaryAST(ASTUnitRef AstUnit) override {
|
||||
auto *SF = AstUnit->getCompilerInstance().getPrimarySourceFile();
|
||||
std::vector<VariableTypeInfo> Infos;
|
||||
std::string TypeBuffer;
|
||||
llvm::raw_string_ostream OS(TypeBuffer);
|
||||
VariableTypesInFile Result;
|
||||
collectVariableType(*SF, Offset, Length, Infos, OS);
|
||||
for (auto Info : Infos) {
|
||||
Result.Results.push_back({Info.Offset, Info.Length, Info.TypeOffset, Info.HasExplicitType});
|
||||
}
|
||||
Result.TypeBuffer = OS.str();
|
||||
Receiver(RequestResult<VariableTypesInFile>::fromResult(Result));
|
||||
}
|
||||
|
||||
void cancelled() override {
|
||||
Receiver(RequestResult<VariableTypesInFile>::cancelled());
|
||||
}
|
||||
|
||||
void failed(StringRef Error) override {
|
||||
Receiver(RequestResult<VariableTypesInFile>::fromError(Error));
|
||||
}
|
||||
};
|
||||
|
||||
auto Collector = std::make_shared<VariableTypeCollectorASTConsumer>(
|
||||
Receiver, Offset, Length);
|
||||
/// FIXME: When request cancellation is implemented and Xcode adopts it,
|
||||
/// don't use 'OncePerASTToken'.
|
||||
static const char OncePerASTToken = 0;
|
||||
getASTManager()->processASTAsync(Invok, std::move(Collector),
|
||||
&OncePerASTToken,
|
||||
llvm::vfs::getRealFileSystem());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user