Skip to content

Commit 7f29b36

Browse files
authored
SourceKitd: Rename SemaToken to ResolvedCursorInfo. NFC (#11680)
1 parent 24a7499 commit 7f29b36

File tree

11 files changed

+203
-180
lines changed

11 files changed

+203
-180
lines changed

docs/refactoring/SwiftLocalRefactoring.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ specified by a cursor position in a Swift source file, such as rename refactorin
2222
In contrast, **range-based refactoring** needs a start and end position to specify
2323
its target, such as Extract Method refactoring. To facilitate the implementation
2424
of these two categories, the Swift repository provides pre-analyzed results called
25-
[SemaToken] and [RangeInfo] to answer several common questions about a cursor
25+
[ResolvedCursorInfo] and [RangeInfo] to answer several common questions about a cursor
2626
position or a range in a Swift source file.
2727

28-
For instance, [SemaToken] can tell us whether a location in the source file
28+
For instance, [ResolvedCursorInfo] can tell us whether a location in the source file
2929
points to the start of an expression and, if so, provide the corresponding compiler object of that
30-
expression. Alternatively, if the cursor points to a name, [SemaToken] gives
30+
expression. Alternatively, if the cursor points to a name, [ResolvedCursorInfo] gives
3131
us the declaration corresponding to that name. Similarly, [RangeInfo] encapsulates
3232
information about a given source range, such as whether the range has multiple entry or exit points.
3333

3434
To implement a new refactoring for Swift, we don't
3535
need to start from the raw representation of a cursor or a range position;
36-
instead, we can start with [SemaToken] and [RangeInfo] upon which a refactoring-specific
36+
instead, we can start with [ResolvedCursorInfo] and [RangeInfo] upon which a refactoring-specific
3737
analysis can be derived.
3838

3939
## Cursor-based Refactoring
@@ -48,13 +48,13 @@ Specifically, for displaying the available actions:
4848

4949
1. The user selects a location from the Xcode editor.
5050
2. Xcode makes a request to [sourcekitd] to see what available refactoring actions exist for that location.
51-
3. Each implemented refactoring action is queried with a `SemaToken` object to see if the action is applicable for that location.
51+
3. Each implemented refactoring action is queried with a `ResolvedCursorInfo` object to see if the action is applicable for that location.
5252
4. The list of applicable actions is returned as response from [sourcekitd] and displayed to the user by Xcode.
5353

5454
When the user selects one of the available actions:
5555

5656
1. Xcode makes a request to [sourcekitd] to perform the selected action on the source location.
57-
2. The specific refactoring action is queried with a `SemaToken` object, derived from the same location, to verify that the action is applicable.
57+
2. The specific refactoring action is queried with a `ResolvedCursorInfo` object, derived from the same location, to verify that the action is applicable.
5858
3. The refactoring action is asked to perform the transformation with textual source edits.
5959
4. The source edits are returned as response from [sourcekitd] and are applied by the Xcode editor.
6060

@@ -66,7 +66,7 @@ refactoring in the [RefactoringKinds.def] file with an entry like:
6666
~~~
6767
6868
`CURSOR_REFACTORING` specifies that this refactoring is initialized at a cursor
69-
location and thus will use [SemaToken] in the implementation. The first field,
69+
location and thus will use [ResolvedCursorInfo] in the implementation. The first field,
7070
`LocalizeString`, specifies the internal name of this refactoring in the Swift
7171
codebase. In this example, the class corresponding to this refactoring is named
7272
`RefactoringActionLocalizeString`. The string literal `"Localize String"` is the
@@ -90,16 +90,16 @@ of `RefactoringActionLocalizeString` in [Refactoring.cpp], as below:
9090
9191
~~~cpp
9292
1 bool RefactoringActionLocalizeString::
93-
2 isApplicable(SemaToken SemaTok) {
94-
3 if (SemaTok.Kind == SemaTokenKind::ExprStart) {
95-
4 if (auto *Literal = dyn_cast<StringLiteralExpr>(SemaTok.TrailingExpr) {
93+
2 isApplicable(ResolvedCursorInfo CursorInfo) {
94+
3 if (CursorInfo.Kind == CursorInfoKind::ExprStart) {
95+
4 if (auto *Literal = dyn_cast<StringLiteralExpr>(CursorInfo.TrailingExpr) {
9696
5 return !Literal->hasInterpolation(); // Not real API.
9797
6 }
9898
7 }
9999
8 }
100100
~~~
101101

102-
Taking a [SemaToken] object as input, it's almost trivial to check
102+
Taking a [ResolvedCursorInfo] object as input, it's almost trivial to check
103103
when to populate the available refactoring menu with
104104
“localize string”. In this case, checking that the cursor points to the start of
105105
an expression (Line 3), and the expression is a string literal (Line 4) without
@@ -108,7 +108,7 @@ interpolation (Line 5) is sufficient.
108108
Next, we need to implement how the code under the cursor should be
109109
changed if the refactoring action is applied. To do this, we
110110
have to implement the [performChange] method of `RefactoringActionLocalizeString`.
111-
In the implementation of `performChange`, we can access the same `SemaToken` object that [isApplicable] received.
111+
In the implementation of `performChange`, we can access the same `ResolvedCursorInfo` object that [isApplicable] received.
112112

113113
~~~cpp
114114
1 bool RefactoringActionLocalizeString::
@@ -143,7 +143,7 @@ a stable key of "extract.expr" for service communication purposes.
143143

144144
To teach Xcode when this refactoring should be available, we
145145
also need to implement [isApplicable] for this refactoring in [Refactoring.cpp],
146-
with the slight difference that the input is a [RangeInfo] instead of a [SemaToken] .
146+
with the slight difference that the input is a [RangeInfo] instead of a [ResolvedCursorInfo] .
147147

148148
~~~cpp
149149
1 bool RefactoringActionExtractExpr::
@@ -337,7 +337,7 @@ Swift's [issue database](https://bugs.swift.org) contains [several ideas of refa
337337
For further help with implementing refactoring transformations, please see the [documentation] or feel free to ask questions on the [swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev) mailing list.
338338

339339
[sourcekitd]: https://github.com/apple/swift/tree/master/tools/SourceKit
340-
[SemaToken]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/include/swift/IDE/Utils.h#L158
340+
[ResolvedCursorInfo]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/include/swift/IDE/Utils.h#L158
341341
[RangeInfo]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/include/swift/IDE/Utils.h#L344
342342
[performChange]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/lib/IDE/Refactoring.cpp#L599
343343
[RefactoringKinds.def]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/include/swift/IDE/RefactoringKinds.def

include/swift/IDE/Refactoring.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace swift {
2424
class SourceManager;
2525

2626
namespace ide {
27-
struct SemaToken;
27+
struct ResolvedCursorInfo;
2828

2929
enum class RefactoringKind : int8_t {
3030
None,
@@ -137,7 +137,7 @@ collectAvailableRefactorings(SourceFile *SF, RangeConfig Range,
137137
llvm::ArrayRef<DiagnosticConsumer*> DiagConsumers);
138138

139139
ArrayRef<RefactoringKind>
140-
collectAvailableRefactorings(SourceFile *SF, SemaToken Tok,
140+
collectAvailableRefactorings(SourceFile *SF, ResolvedCursorInfo CursorInfo,
141141
std::vector<RefactoringKind> &Scratch,
142142
bool ExcludeRename);
143143

include/swift/IDE/Utils.h

+37-21
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@ class XMLEscapingPrinter : public StreamPrinter {
147147
void printXML(StringRef Text);
148148
};
149149

150-
enum class SemaTokenKind {
150+
enum class CursorInfoKind {
151151
Invalid,
152152
ValueRef,
153153
ModuleRef,
154154
ExprStart,
155155
StmtStart,
156156
};
157157

158-
struct SemaToken {
159-
SemaTokenKind Kind = SemaTokenKind::Invalid;
158+
struct ResolvedCursorInfo {
159+
CursorInfoKind Kind = CursorInfoKind::Invalid;
160160
ValueDecl *ValueD = nullptr;
161161
TypeDecl *CtorTyRef = nullptr;
162162
ExtensionDecl *ExtTyRef = nullptr;
@@ -170,32 +170,48 @@ struct SemaToken {
170170
Stmt *TrailingStmt = nullptr;
171171
Expr *TrailingExpr = nullptr;
172172

173-
SemaToken() = default;
174-
SemaToken(ValueDecl *ValueD, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
175-
SourceLoc Loc, bool IsRef, Type Ty, Type ContainerType) :
176-
Kind(SemaTokenKind::ValueRef), ValueD(ValueD), CtorTyRef(CtorTyRef),
177-
ExtTyRef(ExtTyRef), Loc(Loc), IsRef(IsRef), Ty(Ty),
178-
DC(ValueD->getDeclContext()), ContainerType(ContainerType) {}
179-
SemaToken(ModuleEntity Mod, SourceLoc Loc) : Kind(SemaTokenKind::ModuleRef),
180-
Mod(Mod), Loc(Loc) { }
181-
SemaToken(Stmt *TrailingStmt) : Kind(SemaTokenKind::StmtStart),
182-
TrailingStmt(TrailingStmt) {}
183-
SemaToken(Expr* TrailingExpr) : Kind(SemaTokenKind::ExprStart),
184-
TrailingExpr(TrailingExpr) {}
173+
ResolvedCursorInfo() = default;
174+
ResolvedCursorInfo(ValueDecl *ValueD,
175+
TypeDecl *CtorTyRef,
176+
ExtensionDecl *ExtTyRef,
177+
SourceLoc Loc,
178+
bool IsRef,
179+
Type Ty,
180+
Type ContainerType) :
181+
Kind(CursorInfoKind::ValueRef),
182+
ValueD(ValueD),
183+
CtorTyRef(CtorTyRef),
184+
ExtTyRef(ExtTyRef),
185+
Loc(Loc),
186+
IsRef(IsRef),
187+
Ty(Ty),
188+
DC(ValueD->getDeclContext()),
189+
ContainerType(ContainerType) {}
190+
ResolvedCursorInfo(ModuleEntity Mod,
191+
SourceLoc Loc) :
192+
Kind(CursorInfoKind::ModuleRef),
193+
Mod(Mod),
194+
Loc(Loc) { }
195+
ResolvedCursorInfo(Stmt *TrailingStmt) :
196+
Kind(CursorInfoKind::StmtStart),
197+
TrailingStmt(TrailingStmt) {}
198+
ResolvedCursorInfo(Expr* TrailingExpr) :
199+
Kind(CursorInfoKind::ExprStart),
200+
TrailingExpr(TrailingExpr) {}
185201
bool isValid() const { return !isInvalid(); }
186-
bool isInvalid() const { return Kind == SemaTokenKind::Invalid; }
202+
bool isInvalid() const { return Kind == CursorInfoKind::Invalid; }
187203
};
188204

189-
class SemaLocResolver : public SourceEntityWalker {
205+
class CursorInfoResolver : public SourceEntityWalker {
190206
SourceFile &SrcFile;
191207
SourceLoc LocToResolve;
192-
SemaToken SemaTok;
208+
ResolvedCursorInfo CursorInfo;
193209
Type ContainerType;
194210
llvm::SmallVector<Expr*, 4> TrailingExprStack;
195211

196212
public:
197-
explicit SemaLocResolver(SourceFile &SrcFile) : SrcFile(SrcFile) { }
198-
SemaToken resolve(SourceLoc Loc);
213+
explicit CursorInfoResolver(SourceFile &SrcFile) : SrcFile(SrcFile) { }
214+
ResolvedCursorInfo resolve(SourceLoc Loc);
199215
SourceManager &getSourceMgr() const;
200216
private:
201217
bool walkToExprPre(Expr *E) override;
@@ -215,7 +231,7 @@ class SemaLocResolver : public SourceEntityWalker {
215231
bool rangeContainsLoc(SourceRange Range) const {
216232
return getSourceMgr().rangeContainsTokenLoc(Range, LocToResolve);
217233
}
218-
bool isDone() const { return SemaTok.isValid(); }
234+
bool isDone() const { return CursorInfo.isValid(); }
219235
bool tryResolve(ValueDecl *D, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
220236
SourceLoc Loc, bool IsRef, Type Ty = Type());
221237
bool tryResolve(ModuleEntity Mod, SourceLoc Loc);

0 commit comments

Comments
 (0)