Skip to content

Commit 9a0a772

Browse files
committed
[IDE] Disable a few code completion specific paths for solver-based CursorInfo
The lexer will be responsible for knowing whether we have a code completion token, everything else will also work for other IDE inspection features. The changes start to really make sense once I rename CodeCompletion -> IDEInspection in a lot of places.
1 parent e777d37 commit 9a0a772

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class Parser {
192192
}
193193

194194
bool isCodeCompletionFirstPass() const {
195-
return L->isCodeCompletion() && !CodeCompletion;
195+
return SourceMgr.hasCodeCompletionBuffer() && !CodeCompletion;
196196
}
197197

198198
bool allowTopLevelCode() const;

lib/Parse/Lexer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,13 @@ void Lexer::initialize(unsigned Offset, unsigned EndOffset) {
209209
// Initialize code completion.
210210
if (BufferID == SourceMgr.getCodeCompletionBufferID()) {
211211
const char *Ptr = BufferStart + SourceMgr.getCodeCompletionOffset();
212-
if (Ptr >= BufferStart && Ptr <= BufferEnd)
212+
// If the pointer points to a null byte, it's the null byte that was
213+
// inserted to mark the code completion token. If the IDE inspection offset
214+
// points to a normal character, no code completion token should be
215+
// inserted.
216+
if (Ptr >= BufferStart && Ptr <= BufferEnd && *Ptr == '\0') {
213217
CodeCompletionPtr = Ptr;
218+
}
214219
}
215220

216221
ArtificialEOF = BufferStart + EndOffset;

lib/Parse/ParseExpr.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,9 +1388,7 @@ Parser::parseExprPostfixSuffix(ParserResult<Expr> Result, bool isExprBasic,
13881388
assert(activeElements.size() == 1 && activeElements[0].is<Expr *>());
13891389
auto expr = activeElements[0].get<Expr *>();
13901390
ParserStatus status(ICD);
1391-
if (SourceMgr.getCodeCompletionLoc().isValid() &&
1392-
SourceMgr.rangeContainsTokenLoc(expr->getSourceRange(),
1393-
SourceMgr.getCodeCompletionLoc()))
1391+
if (SourceMgr.rangeContainsCodeCompletionLoc(expr->getSourceRange()) && L->isCodeCompletion())
13941392
status.setHasCodeCompletion();
13951393
hasBindOptional |= exprsWithBindOptional.contains(expr);
13961394
Result = makeParserResult(status, expr);
@@ -2818,9 +2816,12 @@ ParserResult<Expr> Parser::parseExprClosure() {
28182816
// Ignore 'CodeCompletionDelayedDeclState' inside closures.
28192817
// Completions inside functions body inside closures at top level should
28202818
// be considered top-level completions.
2821-
if (State->hasCodeCompletionDelayedDeclState())
2819+
if (State->hasCodeCompletionDelayedDeclState()) {
28222820
(void)State->takeCodeCompletionDelayedDeclState();
2823-
Status.setHasCodeCompletionAndIsError();
2821+
}
2822+
if (L->isCodeCompletion()) {
2823+
Status.setHasCodeCompletionAndIsError();
2824+
}
28242825
}
28252826

28262827
// Parse the closing '}'.

lib/Parse/ParsePattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ ParserResult<Pattern> Parser::parseTypedPattern() {
10291029
// as typed patterns, such as "var x: [Int]()".
10301030
// Disable this tentative parse when in code-completion mode, otherwise
10311031
// code-completion may enter the delayed-decl state twice.
1032-
if (Tok.isFollowingLParen() && !L->isCodeCompletion()) {
1032+
if (Tok.isFollowingLParen() && !SourceMgr.hasCodeCompletionBuffer()) {
10331033
CancellableBacktrackingScope backtrack(*this);
10341034

10351035
// Create a local context if needed so we can parse trailing closures.

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5297,7 +5297,7 @@ bool ConstraintSystem::repairFailures(
52975297
// other (explicit) argument's so source range containment alone isn't
52985298
// sufficient.
52995299
bool isSynthesizedArg = arg->isImplicit() && isa<DeclRefExpr>(arg);
5300-
if (!isSynthesizedArg && containsCodeCompletionLoc(arg) &&
5300+
if (!isSynthesizedArg && isForCodeCompletion() && containsCodeCompletionLoc(arg) &&
53015301
!lhs->isVoid() && !lhs->isUninhabited())
53025302
return true;
53035303
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t/split)
2+
// RUN: %{python} %utils/split_file.py -o %t/split %s
3+
// RUN: %empty-directory(%t/build)
4+
// RUN: %sourcekitd-test -req=cursor -pos=5:9 %t/split/MovieRow.swift -- %t/split/MovieRow.swift %t/split/Color.swift | %FileCheck %s
5+
6+
// BEGIN Color.swift
7+
8+
extension Invalid {}
9+
10+
// BEGIN MovieRow.swift
11+
12+
struct Bar {}
13+
14+
fileprivate let bar: Bar = {
15+
let bar = Bar()
16+
return bar
17+
}()
18+
19+
20+
// CHECK: source.lang.swift.decl.var.local (5:9-5:12)
21+
// CHECK-NEXT: bar
22+
// CHECK: RELATED BEGIN
23+
// CHECK-NEXT: <RelatedName usr="s:4main3bar33_F48676AE0C86F007C79C860E40EDA2D3LLAA3BarVvp">bar</RelatedName>
24+
// CHECK-NEXT: RELATED END

0 commit comments

Comments
 (0)