You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/refactoring/SwiftLocalRefactoring.md
+14-14
Original file line number
Diff line number
Diff line change
@@ -22,18 +22,18 @@ specified by a cursor position in a Swift source file, such as rename refactorin
22
22
In contrast, **range-based refactoring** needs a start and end position to specify
23
23
its target, such as Extract Method refactoring. To facilitate the implementation
24
24
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
26
26
position or a range in a Swift source file.
27
27
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
29
29
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
31
31
us the declaration corresponding to that name. Similarly, [RangeInfo] encapsulates
32
32
information about a given source range, such as whether the range has multiple entry or exit points.
33
33
34
34
To implement a new refactoring for Swift, we don't
35
35
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
37
37
analysis can be derived.
38
38
39
39
## Cursor-based Refactoring
@@ -48,13 +48,13 @@ Specifically, for displaying the available actions:
48
48
49
49
1. The user selects a location from the Xcode editor.
50
50
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.
52
52
4. The list of applicable actions is returned as response from [sourcekitd] and displayed to the user by Xcode.
53
53
54
54
When the user selects one of the available actions:
55
55
56
56
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.
58
58
3. The refactoring action is asked to perform the transformation with textual source edits.
59
59
4. The source edits are returned as response from [sourcekitd] and are applied by the Xcode editor.
60
60
@@ -66,7 +66,7 @@ refactoring in the [RefactoringKinds.def] file with an entry like:
66
66
~~~
67
67
68
68
`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,
70
70
`LocalizeString`, specifies the internal name of this refactoring in the Swift
71
71
codebase. In this example, the class corresponding to this refactoring is named
72
72
`RefactoringActionLocalizeString`. The string literal `"Localize String"` is the
@@ -90,16 +90,16 @@ of `RefactoringActionLocalizeString` in [Refactoring.cpp], as below:
90
90
91
91
~~~cpp
92
92
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) {
96
96
5 return !Literal->hasInterpolation(); // Not real API.
97
97
6 }
98
98
7 }
99
99
8 }
100
100
~~~
101
101
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
103
103
when to populate the available refactoring menu with
104
104
“localize string”. In this case, checking that the cursor points to the start of
105
105
an expression (Line 3), and the expression is a string literal (Line 4) without
@@ -108,7 +108,7 @@ interpolation (Line 5) is sufficient.
108
108
Next, we need to implement how the code under the cursor should be
109
109
changed if the refactoring action is applied. To do this, we
110
110
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.
112
112
113
113
~~~cpp
114
114
1bool RefactoringActionLocalizeString::
@@ -143,7 +143,7 @@ a stable key of "extract.expr" for service communication purposes.
143
143
144
144
To teach Xcode when this refactoring should be available, we
145
145
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] .
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.
0 commit comments