Skip to content

Commit 3797b7a

Browse files
committed
[SourceKit] New request to retrieve context type information
This is a new SourceKit request which receives a position in the source file, returns possible expected types and their members which can be referenced by "implicit member expression" syntax.
1 parent 54f0fab commit 3797b7a

File tree

12 files changed

+445
-0
lines changed

12 files changed

+445
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
enum Direction {
2+
case east, west
3+
case vector(x: Int, y: Int)
4+
case distance(Int)
5+
}
6+
struct Target : OptionSet {
7+
/// Mine.
8+
static let me: Target = .init(rawValue: 1 << 0)
9+
/// Yours.
10+
static let you: Target = .init(rawValue: 1 << 1)
11+
/// Theirs.
12+
static let them: Target = .init(rawValue: 1 << 2)
13+
/// One for all.
14+
static var all: Target {
15+
return [.me, .you, .them]
16+
}
17+
}
18+
19+
class C {
20+
func foo(x: Direction) {}
21+
func foo(x: Target) {}
22+
}
23+
24+
func test(obj: C) {
25+
let _ = obj.foo(x:
26+
}
27+
28+
// RUN: %sourcekitd-test -req=typecontextinfo -pos=25:22 %s -- %s > %t.response
29+
// RUN: diff -u %s.response %t.response
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
key.results: [
3+
{
4+
key.typename: "Direction",
5+
key.typeusr: "$s17typecontext_basic9DirectionOD",
6+
key.implicitmembers: [
7+
{
8+
key.name: "east",
9+
key.sourcetext: "east",
10+
key.description: "east"
11+
},
12+
{
13+
key.name: "west",
14+
key.sourcetext: "west",
15+
key.description: "west"
16+
},
17+
{
18+
key.name: "vector(x:y:)",
19+
key.sourcetext: "vector(x: <#T##Int#>, y: <#T##Int#>)",
20+
key.description: "vector(x: Int, y: Int)"
21+
},
22+
{
23+
key.name: "distance(_:)",
24+
key.sourcetext: "distance(<#T##Int#>)",
25+
key.description: "distance(Int)"
26+
}
27+
]
28+
},
29+
{
30+
key.typename: "Target",
31+
key.typeusr: "$s17typecontext_basic6TargetVD",
32+
key.implicitmembers: [
33+
{
34+
key.name: "me",
35+
key.sourcetext: "me",
36+
key.description: "me",
37+
key.doc.brief: "Mine."
38+
},
39+
{
40+
key.name: "you",
41+
key.sourcetext: "you",
42+
key.description: "you",
43+
key.doc.brief: "Yours."
44+
},
45+
{
46+
key.name: "them",
47+
key.sourcetext: "them",
48+
key.description: "them",
49+
key.doc.brief: "Theirs."
50+
},
51+
{
52+
key.name: "all",
53+
key.sourcetext: "all",
54+
key.description: "all",
55+
key.doc.brief: "One for all."
56+
}
57+
]
58+
}
59+
]
60+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,29 @@ class DocInfoConsumer {
510510
virtual bool handleDiagnostic(const DiagnosticEntryInfo &Info) = 0;
511511
};
512512

513+
struct TypeContextInfoItem {
514+
StringRef TypeName;
515+
StringRef TypeUSR;
516+
517+
struct Member {
518+
StringRef Name;
519+
StringRef Description;
520+
StringRef SourceText;
521+
StringRef DocBrief;
522+
};
523+
ArrayRef<Member> ImplicitMembers;
524+
};
525+
526+
class TypeContextInfoConsumer {
527+
virtual void anchor();
528+
529+
public:
530+
virtual ~TypeContextInfoConsumer() {}
531+
532+
virtual void handleResult(const TypeContextInfoItem &Result) = 0;
533+
virtual void failed(StringRef ErrDescription) = 0;
534+
};
535+
513536
class LangSupport {
514537
virtual void anchor();
515538

@@ -665,6 +688,11 @@ class LangSupport {
665688
ArrayRef<const char *> Args,
666689
DocInfoConsumer &Consumer) = 0;
667690

691+
virtual void getExpressionContextInfo(llvm::MemoryBuffer *inputBuf,
692+
unsigned Offset,
693+
ArrayRef<const char *> Args,
694+
TypeContextInfoConsumer &Consumer) = 0;
695+
668696
virtual void getStatistics(StatisticsReceiver) = 0;
669697
};
670698

tools/SourceKit/lib/Core/LangSupport.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ void CodeCompletionConsumer::anchor() { }
1919
void EditorConsumer::anchor() { }
2020
void OptionsDictionary::anchor() {}
2121
void DocInfoConsumer::anchor() { }
22+
void TypeContextInfoConsumer::anchor() { }
2223
void LangSupport::anchor() { }

tools/SourceKit/lib/SwiftLang/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_sourcekit_library(SourceKitSwiftLang
88
SwiftIndexing.cpp
99
SwiftLangSupport.cpp
1010
SwiftSourceDocInfo.cpp
11+
SwiftTypeContextInfo.cpp
1112
LINK_LIBS
1213
SourceKitCore swiftDriver swiftFrontend
1314
swiftDWARFImporter swiftClangImporter swiftIDE

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ class SwiftLangSupport : public LangSupport {
524524
void findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
525525
std::function<void(ArrayRef<StringRef>, StringRef Error)> Receiver) override;
526526

527+
void getExpressionContextInfo(llvm::MemoryBuffer *inputBuf, unsigned Offset,
528+
ArrayRef<const char *> Args,
529+
TypeContextInfoConsumer &Consumer) override;
530+
527531
void getStatistics(StatisticsReceiver) override;
528532

529533
private:

0 commit comments

Comments
 (0)