-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathCompletionContextFinder.cpp
168 lines (147 loc) · 5.64 KB
/
CompletionContextFinder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//===--- CompletionContextFinder.cpp --------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Sema/CompletionContextFinder.h"
#include "swift/Basic/Assertions.h"
#include "swift/Parse/Lexer.h"
#include "swift/Sema/SyntacticElementTarget.h"
using namespace swift;
using namespace constraints;
using Fallback = CompletionContextFinder::Fallback;
CompletionContextFinder::CompletionContextFinder(
SyntacticElementTarget target, DeclContext *DC)
: InitialExpr(target.getAsExpr()), InitialDC(DC) {
assert(DC);
target.walk(*this);
}
ASTWalker::PreWalkResult<Expr *>
CompletionContextFinder::walkToExprPre(Expr *E) {
if (auto *closure = dyn_cast<ClosureExpr>(E)) {
// NOTE: We're querying hasSingleExpressionBody before the single-expression
// body transform has happened, so this won't take e.g SingleValueStmtExprs
// into account.
Contexts.push_back({closure->hasSingleExpressionBody()
? ContextKind::SingleStmtClosure
: ContextKind::MultiStmtClosure,
closure});
}
if (isa<InterpolatedStringLiteralExpr>(E)) {
Contexts.push_back({ContextKind::StringInterpolation, E});
}
if (isa<ApplyExpr>(E) || isa<SequenceExpr>(E) ||
isa<SingleValueStmtExpr>(E)) {
Contexts.push_back({ContextKind::FallbackExpression, E});
}
if (auto *Error = dyn_cast<ErrorExpr>(E)) {
Contexts.push_back({ContextKind::ErrorExpression, E});
if (auto *OrigExpr = Error->getOriginalExpr()) {
OrigExpr->walk(*this);
if (hasCompletionExpr())
return Action::Stop();
}
}
if (auto *CCE = dyn_cast<CodeCompletionExpr>(E)) {
CompletionNode = CCE;
return Action::Stop();
}
if (auto *KeyPath = dyn_cast<KeyPathExpr>(E)) {
for (auto &component : KeyPath->getComponents()) {
if (component.getKind() == KeyPathExpr::Component::Kind::CodeCompletion) {
CompletionNode = KeyPath;
return Action::Stop();
}
}
// Code completion in key paths is modelled by a code completion component
// Don't walk the key path's parsed expressions.
return Action::SkipNode(E);
}
return Action::Continue(E);
}
ASTWalker::PostWalkResult<Expr *>
CompletionContextFinder::walkToExprPost(Expr *E) {
if (isa<ClosureExpr>(E) || isa<InterpolatedStringLiteralExpr>(E) ||
isa<ApplyExpr>(E) || isa<SequenceExpr>(E) || isa<ErrorExpr>(E) ||
isa<SingleValueStmtExpr>(E)) {
assert(Contexts.back().E == E);
Contexts.pop_back();
}
return Action::Continue(E);
}
ASTWalker::PreWalkAction CompletionContextFinder::walkToDeclPre(Decl *D) {
// Look through any decl if we're looking for any viable fallback expression.
if (ForFallback)
return Action::Continue();
// Otherwise, follow the same rule as the ConstraintSystem, where only
// nested PatternBindingDecls are solved as part of the system. Local decls
// are handled by TypeCheckASTNodeAtLocRequest.
return Action::VisitNodeIf(isa<PatternBindingDecl>(D));
}
size_t CompletionContextFinder::getKeyPathCompletionComponentIndex() const {
size_t ComponentIndex = 0;
auto Components = getKeyPathContainingCompletionComponent()->getComponents();
for (auto &Component : Components) {
if (Component.getKind() == KeyPathExpr::Component::Kind::CodeCompletion) {
break;
} else {
ComponentIndex++;
}
}
assert(ComponentIndex < Components.size() &&
"No completion component in the key path?");
return ComponentIndex;
}
std::optional<Fallback>
CompletionContextFinder::getFallbackCompletionExpr() const {
if (!hasCompletionExpr()) {
// Creating a fallback expression only makes sense if we are completing in
// an expression, not when we're completing in a key path.
return std::nullopt;
}
std::optional<Fallback> fallback;
bool separatePrecheck = false;
DeclContext *fallbackDC = InitialDC;
// Find the outermost fallback expression within the innermost error
// expression or multi-statement closure, keeping track of its decl context.
for (auto context : Contexts) {
switch (context.Kind) {
case ContextKind::StringInterpolation:
LLVM_FALLTHROUGH;
case ContextKind::FallbackExpression:
if (!fallback && context.E != InitialExpr)
fallback = Fallback{context.E, fallbackDC, separatePrecheck};
continue;
case ContextKind::SingleStmtClosure:
if (!fallback && context.E != InitialExpr)
fallback = Fallback{context.E, fallbackDC, separatePrecheck};
fallbackDC = cast<AbstractClosureExpr>(context.E);
continue;
case ContextKind::MultiStmtClosure:
fallbackDC = cast<AbstractClosureExpr>(context.E);
LLVM_FALLTHROUGH;
case ContextKind::ErrorExpression:;
fallback = std::nullopt;
separatePrecheck = true;
continue;
}
}
if (fallback)
return fallback;
if (getCompletionExpr() != InitialExpr)
return Fallback{getCompletionExpr(), fallbackDC, separatePrecheck};
return std::nullopt;
}
bool swift::containsIDEInspectionTarget(SourceRange range,
const SourceManager &SourceMgr) {
if (range.isInvalid())
return false;
auto charRange = Lexer::getCharSourceRangeFromSourceRange(SourceMgr, range);
return SourceMgr.rangeContainsIDEInspectionTarget(charRange);
}