Skip to content

SourceKitd: Rename SemaToken to ResolvedCursorInfo. NFC #11680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions docs/refactoring/SwiftLocalRefactoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ specified by a cursor position in a Swift source file, such as rename refactorin
In contrast, **range-based refactoring** needs a start and end position to specify
its target, such as Extract Method refactoring. To facilitate the implementation
of these two categories, the Swift repository provides pre-analyzed results called
[SemaToken] and [RangeInfo] to answer several common questions about a cursor
[ResolvedCursorInfo] and [RangeInfo] to answer several common questions about a cursor
position or a range in a Swift source file.

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

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

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

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

When the user selects one of the available actions:

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

Expand All @@ -66,7 +66,7 @@ refactoring in the [RefactoringKinds.def] file with an entry like:
~~~

`CURSOR_REFACTORING` specifies that this refactoring is initialized at a cursor
location and thus will use [SemaToken] in the implementation. The first field,
location and thus will use [ResolvedCursorInfo] in the implementation. The first field,
`LocalizeString`, specifies the internal name of this refactoring in the Swift
codebase. In this example, the class corresponding to this refactoring is named
`RefactoringActionLocalizeString`. The string literal `"Localize String"` is the
Expand All @@ -90,16 +90,16 @@ of `RefactoringActionLocalizeString` in [Refactoring.cpp], as below:

~~~cpp
1 bool RefactoringActionLocalizeString::
2 isApplicable(SemaToken SemaTok) {
3 if (SemaTok.Kind == SemaTokenKind::ExprStart) {
4 if (auto *Literal = dyn_cast<StringLiteralExpr>(SemaTok.TrailingExpr) {
2 isApplicable(ResolvedCursorInfo CursorInfo) {
3 if (CursorInfo.Kind == CursorInfoKind::ExprStart) {
4 if (auto *Literal = dyn_cast<StringLiteralExpr>(CursorInfo.TrailingExpr) {
5 return !Literal->hasInterpolation(); // Not real API.
6 }
7 }
8 }
~~~

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

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

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

~~~cpp
1 bool RefactoringActionExtractExpr::
Expand Down Expand Up @@ -337,7 +337,7 @@ Swift's [issue database](https://bugs.swift.org) contains [several ideas of refa
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.

[sourcekitd]: https://github.com/apple/swift/tree/master/tools/SourceKit
[SemaToken]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/include/swift/IDE/Utils.h#L158
[ResolvedCursorInfo]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/include/swift/IDE/Utils.h#L158
[RangeInfo]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/include/swift/IDE/Utils.h#L344
[performChange]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/lib/IDE/Refactoring.cpp#L599
[RefactoringKinds.def]: https://github.com/apple/swift/blob/60a91bb7360dde5ce9531889e0ed10a2edbc961a/include/swift/IDE/RefactoringKinds.def
Expand Down
4 changes: 2 additions & 2 deletions include/swift/IDE/Refactoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace swift {
class SourceManager;

namespace ide {
struct SemaToken;
struct ResolvedCursorInfo;

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

ArrayRef<RefactoringKind>
collectAvailableRefactorings(SourceFile *SF, SemaToken Tok,
collectAvailableRefactorings(SourceFile *SF, ResolvedCursorInfo CursorInfo,
std::vector<RefactoringKind> &Scratch,
bool ExcludeRename);

Expand Down
58 changes: 37 additions & 21 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ class XMLEscapingPrinter : public StreamPrinter {
void printXML(StringRef Text);
};

enum class SemaTokenKind {
enum class CursorInfoKind {
Invalid,
ValueRef,
ModuleRef,
ExprStart,
StmtStart,
};

struct SemaToken {
SemaTokenKind Kind = SemaTokenKind::Invalid;
struct ResolvedCursorInfo {
CursorInfoKind Kind = CursorInfoKind::Invalid;
ValueDecl *ValueD = nullptr;
TypeDecl *CtorTyRef = nullptr;
ExtensionDecl *ExtTyRef = nullptr;
Expand All @@ -170,32 +170,48 @@ struct SemaToken {
Stmt *TrailingStmt = nullptr;
Expr *TrailingExpr = nullptr;

SemaToken() = default;
SemaToken(ValueDecl *ValueD, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
SourceLoc Loc, bool IsRef, Type Ty, Type ContainerType) :
Kind(SemaTokenKind::ValueRef), ValueD(ValueD), CtorTyRef(CtorTyRef),
ExtTyRef(ExtTyRef), Loc(Loc), IsRef(IsRef), Ty(Ty),
DC(ValueD->getDeclContext()), ContainerType(ContainerType) {}
SemaToken(ModuleEntity Mod, SourceLoc Loc) : Kind(SemaTokenKind::ModuleRef),
Mod(Mod), Loc(Loc) { }
SemaToken(Stmt *TrailingStmt) : Kind(SemaTokenKind::StmtStart),
TrailingStmt(TrailingStmt) {}
SemaToken(Expr* TrailingExpr) : Kind(SemaTokenKind::ExprStart),
TrailingExpr(TrailingExpr) {}
ResolvedCursorInfo() = default;
ResolvedCursorInfo(ValueDecl *ValueD,
TypeDecl *CtorTyRef,
ExtensionDecl *ExtTyRef,
SourceLoc Loc,
bool IsRef,
Type Ty,
Type ContainerType) :
Kind(CursorInfoKind::ValueRef),
ValueD(ValueD),
CtorTyRef(CtorTyRef),
ExtTyRef(ExtTyRef),
Loc(Loc),
IsRef(IsRef),
Ty(Ty),
DC(ValueD->getDeclContext()),
ContainerType(ContainerType) {}
ResolvedCursorInfo(ModuleEntity Mod,
SourceLoc Loc) :
Kind(CursorInfoKind::ModuleRef),
Mod(Mod),
Loc(Loc) { }
ResolvedCursorInfo(Stmt *TrailingStmt) :
Kind(CursorInfoKind::StmtStart),
TrailingStmt(TrailingStmt) {}
ResolvedCursorInfo(Expr* TrailingExpr) :
Kind(CursorInfoKind::ExprStart),
TrailingExpr(TrailingExpr) {}
bool isValid() const { return !isInvalid(); }
bool isInvalid() const { return Kind == SemaTokenKind::Invalid; }
bool isInvalid() const { return Kind == CursorInfoKind::Invalid; }
};

class SemaLocResolver : public SourceEntityWalker {
class CursorInfoResolver : public SourceEntityWalker {
SourceFile &SrcFile;
SourceLoc LocToResolve;
SemaToken SemaTok;
ResolvedCursorInfo CursorInfo;
Type ContainerType;
llvm::SmallVector<Expr*, 4> TrailingExprStack;

public:
explicit SemaLocResolver(SourceFile &SrcFile) : SrcFile(SrcFile) { }
SemaToken resolve(SourceLoc Loc);
explicit CursorInfoResolver(SourceFile &SrcFile) : SrcFile(SrcFile) { }
ResolvedCursorInfo resolve(SourceLoc Loc);
SourceManager &getSourceMgr() const;
private:
bool walkToExprPre(Expr *E) override;
Expand All @@ -215,7 +231,7 @@ class SemaLocResolver : public SourceEntityWalker {
bool rangeContainsLoc(SourceRange Range) const {
return getSourceMgr().rangeContainsTokenLoc(Range, LocToResolve);
}
bool isDone() const { return SemaTok.isValid(); }
bool isDone() const { return CursorInfo.isValid(); }
bool tryResolve(ValueDecl *D, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
SourceLoc Loc, bool IsRef, Type Ty = Type());
bool tryResolve(ModuleEntity Mod, SourceLoc Loc);
Expand Down
Loading