Skip to content

Commit 1bd0d97

Browse files
committed
[SourceKit] Add option for returning fully qualified types in variable type request
1 parent 0efc3ea commit 1bd0d97

File tree

8 files changed

+46
-18
lines changed

8 files changed

+46
-18
lines changed

include/swift/Sema/IDETypeChecking.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ namespace swift {
264264
/// \c VariableTypeInfos will index into the string that backs this
265265
/// stream.
266266
void collectVariableType(SourceFile &SF, SourceRange Range,
267+
bool FullyQualified,
267268
std::vector<VariableTypeInfo> &VariableTypeInfos,
268269
llvm::raw_ostream &OS);
269270

lib/IDE/IDETypeChecking.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ class VariableTypeCollector : public SourceEntityWalker {
761761
/// The range in which variable types are to be collected.
762762
SourceRange TotalRange;
763763

764+
// Specified by the client whether we should print fully qualified types
765+
const bool FullyQualified;
766+
764767
/// The output vector for VariableTypeInfos emitted during traversal.
765768
std::vector<VariableTypeInfo> &Results;
766769

@@ -793,10 +796,12 @@ class VariableTypeCollector : public SourceEntityWalker {
793796

794797
public:
795798
VariableTypeCollector(const SourceFile &SF, SourceRange Range,
799+
bool FullyQualified,
796800
std::vector<VariableTypeInfo> &Results,
797801
llvm::raw_ostream &OS)
798802
: SM(SF.getASTContext().SourceMgr), BufferId(*SF.getBufferID()),
799-
TotalRange(Range), Results(Results), OS(OS) {}
803+
TotalRange(Range), FullyQualified(FullyQualified), Results(Results),
804+
OS(OS) {}
800805

801806
bool walkToDeclPre(Decl *D, CharSourceRange DeclNameRange) override {
802807
if (DeclNameRange.isInvalid()) {
@@ -816,6 +821,7 @@ class VariableTypeCollector : public SourceEntityWalker {
816821
llvm::raw_svector_ostream OS(Buffer);
817822
PrintOptions Options;
818823
Options.SynthesizeSugarOnTypes = true;
824+
Options.FullyQualifiedTypes = FullyQualified;
819825
auto Ty = VD->getType();
820826
// Skip this declaration and its children if the type is an error type.
821827
if (Ty->is<ErrorType>()) {
@@ -856,9 +862,10 @@ VariableTypeInfo::VariableTypeInfo(uint32_t Offset, uint32_t Length,
856862
TypeOffset(TypeOffset) {}
857863

858864
void swift::collectVariableType(
859-
SourceFile &SF, SourceRange Range,
865+
SourceFile &SF, SourceRange Range, bool FullyQualified,
860866
std::vector<VariableTypeInfo> &VariableTypeInfos, llvm::raw_ostream &OS) {
861-
VariableTypeCollector Walker(SF, Range, VariableTypeInfos, OS);
867+
VariableTypeCollector Walker(SF, Range, FullyQualified, VariableTypeInfos,
868+
OS);
862869
Walker.walk(SF);
863870
}
864871

test/SourceKit/VariableType/basic.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ let `else` = 3
3333
// CHECK: (15:7, 15:8): (Int) -> Int (explicit type: 1)
3434
// CHECK: (19:7, 19:12): Int (explicit type: 0)
3535
// CHECK: (22:5, 22:11): Int (explicit type: 0)
36+
37+
// RUN: %sourcekitd-test -req=collect-var-type -req-opts=fully_qualified=true %s -- %s | %FileCheck %s --check-prefix CHECK-FULLY-QUALIFIED
38+
// CHECK-FULLY-QUALIFIED: (1:5, 1:6): Swift.Int (explicit type: 1)
39+
// CHECK-FULLY-QUALIFIED: (2:5, 2:6): Swift.String (explicit type: 0)
40+
// CHECK-FULLY-QUALIFIED: (4:5, 4:8): [Swift.String] (explicit type: 0)
41+
// CHECK-FULLY-QUALIFIED: (7:7, 7:8): Swift.String (explicit type: 1)
42+
// CHECK-FULLY-QUALIFIED: (8:7, 8:8): Swift.String (explicit type: 0)
43+
// CHECK-FULLY-QUALIFIED: (12:7, 12:8): Swift.Double (explicit type: 0)
44+
// CHECK-FULLY-QUALIFIED: (13:7, 13:8): [basic.A] (explicit type: 0)
45+
// CHECK-FULLY-QUALIFIED: (14:7, 14:8): [Swift.Int : Swift.Int] (explicit type: 1)
46+
// CHECK-FULLY-QUALIFIED: (15:7, 15:8): (Swift.Int) -> Swift.Int (explicit type: 1)
47+
// CHECK-FULLY-QUALIFIED: (19:7, 19:12): Swift.Int (explicit type: 0)
48+
// CHECK-FULLY-QUALIFIED: (22:5, 22:11): Swift.Int (explicit type: 0)

tools/SourceKit/docs/Protocol.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -796,13 +796,14 @@ type checking and the necessary compiler arguments to help resolve all dependenc
796796

797797
```
798798
{
799-
<key.request>: (UID) <source.request.variable.type>,
800-
<key.sourcefile>: (string) // Absolute path to the file.
801-
<key.compilerargs>: [string*] // Array of zero or more strings for the compiler arguments,
802-
// e.g ["-sdk", "/path/to/sdk"]. If key.sourcefile is provided,
803-
// these must include the path to that file.
804-
[opt] <key.offset>: (int64) // Offset of the requested range. Defaults to zero.
805-
[opt] <key.length>: (int64) // Length of the requested range. Defaults to the entire file.
799+
<key.request>: (UID) <source.request.variable.type>,
800+
<key.sourcefile>: (string) // Absolute path to the file.
801+
<key.compilerargs>: [string*] // Array of zero or more strings for the compiler arguments,
802+
// e.g ["-sdk", "/path/to/sdk"]. If key.sourcefile is provided,
803+
// these must include the path to that file.
804+
[opt] <key.offset>: (int64) // Offset of the requested range. Defaults to zero.
805+
[opt] <key.length>: (int64) // Length of the requested range. Defaults to the entire file.
806+
[opt] <key.fully_qualified>: (bool) // True when fully qualified type should be returned. Defaults to False.
806807
}
807808
```
808809

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ class LangSupport {
921921
/// the entire document are collected.
922922
virtual void collectVariableTypes(
923923
StringRef FileName, ArrayRef<const char *> Args,
924-
Optional<unsigned> Offset, Optional<unsigned> Length,
924+
Optional<unsigned> Offset, Optional<unsigned> Length, bool FullyQualified,
925925
SourceKitCancellationToken CancellationToken,
926926
std::function<void(const RequestResult<VariableTypesInFile> &)>
927927
Receiver) = 0;

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ class SwiftLangSupport : public LangSupport {
693693

694694
void collectVariableTypes(
695695
StringRef FileName, ArrayRef<const char *> Args,
696-
Optional<unsigned> Offset, Optional<unsigned> Length,
696+
Optional<unsigned> Offset, Optional<unsigned> Length, bool FullyQualified,
697697
SourceKitCancellationToken CancellationToken,
698698
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver)
699699
override;

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,7 +2591,8 @@ void SwiftLangSupport::collectExpressionTypes(
25912591

25922592
void SwiftLangSupport::collectVariableTypes(
25932593
StringRef FileName, ArrayRef<const char *> Args, Optional<unsigned> Offset,
2594-
Optional<unsigned> Length, SourceKitCancellationToken CancellationToken,
2594+
Optional<unsigned> Length, bool FullyQualified,
2595+
SourceKitCancellationToken CancellationToken,
25952596
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver) {
25962597
std::string Error;
25972598
SwiftInvocationRef Invok =
@@ -2608,13 +2609,16 @@ void SwiftLangSupport::collectVariableTypes(
26082609
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver;
26092610
Optional<unsigned> Offset;
26102611
Optional<unsigned> Length;
2612+
bool FullyQualified;
26112613

26122614
public:
26132615
VariableTypeCollectorASTConsumer(
26142616
std::function<void(const RequestResult<VariableTypesInFile> &)>
26152617
Receiver,
2616-
Optional<unsigned> Offset, Optional<unsigned> Length)
2617-
: Receiver(std::move(Receiver)), Offset(Offset), Length(Length) {}
2618+
Optional<unsigned> Offset, Optional<unsigned> Length,
2619+
bool FullyQualified)
2620+
: Receiver(std::move(Receiver)), Offset(Offset), Length(Length),
2621+
FullyQualified(FullyQualified) {}
26182622

26192623
void handlePrimaryAST(ASTUnitRef AstUnit) override {
26202624
auto &CompInst = AstUnit->getCompilerInstance();
@@ -2638,7 +2642,7 @@ void SwiftLangSupport::collectVariableTypes(
26382642
llvm::raw_string_ostream OS(TypeBuffer);
26392643
VariableTypesInFile Result;
26402644

2641-
collectVariableType(*SF, Range, Infos, OS);
2645+
collectVariableType(*SF, Range, FullyQualified, Infos, OS);
26422646

26432647
for (auto Info : Infos) {
26442648
Result.Results.push_back({Info.Offset, Info.Length, Info.TypeOffset, Info.HasExplicitType});
@@ -2657,7 +2661,7 @@ void SwiftLangSupport::collectVariableTypes(
26572661
};
26582662

26592663
auto Collector = std::make_shared<VariableTypeCollectorASTConsumer>(
2660-
Receiver, Offset, Length);
2664+
Receiver, Offset, Length, FullyQualified);
26612665
/// FIXME: When request cancellation is implemented and Xcode adopts it,
26622666
/// don't use 'OncePerASTToken'.
26632667
static const char OncePerASTToken = 0;

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,8 +1251,10 @@ static void handleSemanticRequest(
12511251
[](int64_t v) -> unsigned { return v; });
12521252
Optional<unsigned> Length = Req.getOptionalInt64(KeyLength).map(
12531253
[](int64_t v) -> unsigned { return v; });
1254+
int64_t FullyQualified = false;
1255+
Req.getInt64(KeyFullyQualified, FullyQualified, /*isOptional=*/true);
12541256
return Lang.collectVariableTypes(
1255-
*SourceFile, Args, Offset, Length, CancellationToken,
1257+
*SourceFile, Args, Offset, Length, FullyQualified, CancellationToken,
12561258
[Rec](const RequestResult<VariableTypesInFile> &Result) {
12571259
reportVariableTypeInfo(Result, Rec);
12581260
});

0 commit comments

Comments
 (0)