Skip to content

Commit dda53f5

Browse files
committed
[SourceKit] Extend variable type request to show fully qualified types
1 parent 0361fa2 commit dda53f5

File tree

7 files changed

+53
-30
lines changed

7 files changed

+53
-30
lines changed

include/swift/Sema/IDETypeChecking.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,11 @@ namespace swift {
261261
/// The start of the printed type in a separate string buffer.
262262
uint32_t TypeOffset;
263263

264+
/// The start of the printed qualified type.
265+
uint32_t QualifiedTypeOffset;
266+
264267
VariableTypeInfo(uint32_t Offset, uint32_t Length, bool HasExplicitType,
265-
uint32_t TypeOffset);
268+
uint32_t TypeOffset, uint32_t QualifiedTypeOffset);
266269
};
267270

268271
/// Collect type information for every variable declaration in \c SF

lib/IDE/IDETypeChecking.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -824,29 +824,26 @@ class VariableTypeCollector : public SourceEntityWalker {
824824
return false;
825825
}
826826
if (auto VD = dyn_cast<VarDecl>(D)) {
827+
// Skip this declaration and its children if the type is an error type.
828+
if (VD->getType()->is<ErrorType>()) {
829+
return false;
830+
}
827831
unsigned VarOffset =
828832
SM.getLocOffsetInBuffer(DeclNameRange.getStart(), BufferId);
829833
unsigned VarLength = DeclNameRange.getByteLength();
830-
// Print the type to a temporary buffer
831-
SmallString<64> Buffer;
832-
{
833-
llvm::raw_svector_ostream OS(Buffer);
834-
PrintOptions Options;
835-
Options.SynthesizeSugarOnTypes = true;
836-
auto Ty = VD->getType();
837-
// Skip this declaration and its children if the type is an error type.
838-
if (Ty->is<ErrorType>()) {
839-
return false;
840-
}
841-
Ty->print(OS, Options);
842-
}
834+
835+
auto printOptions = PrintOptions();
836+
auto TyOffset = addToTypeBuffer(printOptions, VD);
837+
printOptions.FullyQualifiedTypes = true;
838+
auto QTyOffset = addToTypeBuffer(printOptions, VD);
839+
843840
// Transfer the type to `OS` if needed and get the offset of this string
844841
// in `OS`.
845-
auto TyOffset = getTypeOffset(Buffer.str());
846842
bool HasExplicitType =
847843
VD->getTypeReprOrParentPatternTypeRepr() != nullptr;
848844
// Add the type information to the result list.
849-
Results.emplace_back(VarOffset, VarLength, HasExplicitType, TyOffset);
845+
Results.emplace_back(VarOffset, VarLength, HasExplicitType, TyOffset,
846+
QTyOffset);
850847
}
851848
return true;
852849
}
@@ -865,12 +862,26 @@ class VariableTypeCollector : public SourceEntityWalker {
865862
// Skip this pattern and its subtree if outside the range
866863
return overlapsTotalRange(P->getSourceRange());
867864
}
865+
866+
private:
867+
// Save the type to the result buffer with desired formatting and return the
868+
// offset in the buffer.
869+
uint32_t addToTypeBuffer(PrintOptions &Options, VarDecl *VD) {
870+
SmallString<64> Buffer;
871+
llvm::raw_svector_ostream OS(Buffer);
872+
Options.SynthesizeSugarOnTypes = true;
873+
auto Ty = VD->getType();
874+
Ty->print(OS, Options);
875+
876+
return getTypeOffset(Buffer.str());
877+
}
868878
};
869879

870880
VariableTypeInfo::VariableTypeInfo(uint32_t Offset, uint32_t Length,
871-
bool HasExplicitType, uint32_t TypeOffset)
881+
bool HasExplicitType, uint32_t TypeOffset,
882+
uint32_t QualifiedTypeOffset)
872883
: Offset(Offset), Length(Length), HasExplicitType(HasExplicitType),
873-
TypeOffset(TypeOffset) {}
884+
TypeOffset(TypeOffset), QualifiedTypeOffset(QualifiedTypeOffset) {}
874885

875886
void swift::collectVariableType(
876887
SourceFile &SF, SourceRange Range,

tools/SourceKit/docs/Protocol.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,11 @@ type checking and the necessary compiler arguments to help resolve all dependenc
816816
```
817817
var-type-info ::=
818818
{
819-
<key.variable_offset>: (int64) // Offset of a variable identifier in the source file
820-
<key.variable_length>: (int64) // Length of a variable identifier an expression in the source file
821-
<key.variable_type>: (string) // Printed type of the variable declaration
822-
<key.variable_type_explicit> (bool) // Whether the declaration has an explicit type annotation
819+
<key.variable_offset>: (int64) // Offset of a variable identifier in the source file
820+
<key.variable_length>: (int64) // Length of a variable identifier an expression in the source file
821+
<key.variable_type>: (string) // Printed type of the variable declaration
822+
<key.variable_qualified_type>: (string) // Printed fully qualified type of the variable declaration
823+
<key.variable_type_explicit> (bool) // Whether the declaration has an explicit type annotation
823824
}
824825
```
825826

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ struct VariableType {
148148
/// The offset of the type's string representation inside
149149
/// `VariableTypesInFile.TypeBuffer`.
150150
unsigned TypeOffset;
151+
unsigned QualifiedTypeOffset;
151152
/// Whether the variable declaration has an explicit type annotation.
152153
bool HasExplicitType;
153154
};

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2643,7 +2643,9 @@ void SwiftLangSupport::collectVariableTypes(
26432643
collectVariableType(*SF, Range, Infos, OS);
26442644

26452645
for (auto Info : Infos) {
2646-
Result.Results.push_back({Info.Offset, Info.Length, Info.TypeOffset, Info.HasExplicitType});
2646+
Result.Results.push_back({Info.Offset, Info.Length, Info.TypeOffset,
2647+
Info.QualifiedTypeOffset,
2648+
Info.HasExplicitType});
26472649
}
26482650
Result.TypeBuffer = OS.str();
26492651
Receiver(RequestResult<VariableTypesInFile>::fromResult(Result));

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "sourcekitd/VariableTypeArray.h"
14-
#include "sourcekitd/CompactArray.h"
14+
#include "DictionaryKeys.h"
1515
#include "SourceKit/Core/LLVM.h"
1616
#include "SourceKit/Core/LangSupport.h"
17-
#include "SourceKit/Support/UIdent.h"
18-
#include "DictionaryKeys.h"
17+
#include "sourcekitd/CompactArray.h"
1918

2019
#include "llvm/Support/MemoryBuffer.h"
2120

@@ -32,8 +31,10 @@ class VariableTypeReader {
3231
// - Variable offset in the source buffer
3332
// - Variable length in the source buffer
3433
// - Offset of printed Variable type inside `PrintedTypes`
34+
// - Offset of printed Variable qualified type inside printedType
3535
// - Whether the variable has an explicit type annotation
36-
CompactArrayReader<unsigned, unsigned, unsigned, unsigned> EntryReader;
36+
CompactArrayReader<unsigned, unsigned, unsigned, unsigned, unsigned>
37+
EntryReader;
3738

3839
static uint64_t getHeaderValue(char *Buffer, unsigned Index) {
3940
uint64_t HeaderField;
@@ -55,7 +56,8 @@ class VariableTypeReader {
5556
VariableType Result;
5657
unsigned HasExplicitType;
5758
EntryReader.readEntries(Idx, Result.VarOffset, Result.VarLength,
58-
Result.TypeOffset, HasExplicitType);
59+
Result.TypeOffset, Result.QualifiedTypeOffset,
60+
HasExplicitType);
5961
Result.HasExplicitType = static_cast<bool>(HasExplicitType);
6062
return Result;
6163
}
@@ -79,6 +81,8 @@ class VariableTypeReader {
7981
APPLY(KeyVariableOffset, Int, Result.VarOffset);
8082
APPLY(KeyVariableLength, Int, Result.VarLength);
8183
APPLY(KeyVariableType, String, Reader.readPrintedType(Result.TypeOffset));
84+
APPLY(KeyVariableQualifiedType, String,
85+
Reader.readPrintedType(Result.QualifiedTypeOffset));
8286
APPLY(KeyVariableTypeExplicit, Bool, Result.HasExplicitType);
8387
return true;
8488
}
@@ -88,7 +92,7 @@ class VariableTypeReader {
8892
struct VariableTypeArrayBuilder::Implementation {
8993
/// A builder that builds values read by \c EntryReader in \c
9094
/// VariableTypeReader. See \c VariableTypeReader::EntryReader for more info.
91-
CompactArrayBuilder<unsigned, unsigned, unsigned, unsigned> Builder;
95+
CompactArrayBuilder<unsigned, unsigned, unsigned, unsigned, unsigned> Builder;
9296
/// A builder that builds the \c PrintedTypes string used by \c
9397
/// VariableTypeReader. See \c VariableTypeReader::PrintedTypes for more info.
9498
CompactArrayBuilder<StringRef> StrBuilder;
@@ -146,7 +150,7 @@ VariableTypeArrayBuilder::~VariableTypeArrayBuilder() { delete &Impl; }
146150
void VariableTypeArrayBuilder::add(const VariableType &VarType) {
147151
Impl.Builder.addEntry(VarType.VarOffset, VarType.VarLength,
148152
VarType.TypeOffset /*Printed type is null-terminated*/,
149-
VarType.HasExplicitType);
153+
VarType.QualifiedTypeOffset, VarType.HasExplicitType);
150154
}
151155

152156
std::unique_ptr<llvm::MemoryBuffer> VariableTypeArrayBuilder::createBuffer() {

utils/gyb_sourcekit_support/UIDs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def __init__(self, internal_name, external_name):
182182
KEY('VariableOffset', 'key.variable_offset'),
183183
KEY('VariableLength', 'key.variable_length'),
184184
KEY('VariableType', 'key.variable_type'),
185+
KEY('VariableQualifiedType', 'key.variable_qualified_type'),
185186
KEY('VariableTypeExplicit', 'key.variable_type_explicit'),
186187
KEY('CanonicalizeType', 'key.canonicalize_type'),
187188
KEY('InternalDiagnostic', 'key.internal_diagnostic'),

0 commit comments

Comments
 (0)