Skip to content

Commit 357c4e5

Browse files
committed
[SourceKit] Map line and column using the latest snapshot
During a cursor info request, the resolved offset was mapped to line and column using the buffer inside `SwiftDocumentSyntaxInfo`. However, prior to 54683ca, `editorReplaceText` was not updating the syntax info. This meant that snapshots would be more up to date than the buffer in the syntax info, allowing for the possibility of an invalid offset. While 54683ca would also fix this, snapshots actually have a `getLineAndColumn` anyway. Use that instead of grabbing the buffer from syntax info. Resolves rdar://78161348.
1 parent 5a387d7 commit 357c4e5

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: touch %t/empty.swift
4+
// RUN: echo "func foo() {}" >> %t/func.swift
5+
6+
// Edit previously did not update the syntax info. Cursor info was using its
7+
// buffer to calculate line and column (before rdar://78161348).
8+
// RUN: %sourcekitd-test \
9+
// RUN: -req=open -text-input %t/empty.swift %t/func.swift -- %t/func.swift == \
10+
// RUN: -req=edit -offset=0 -length=0 -replace="func foo() {}" -req-opts=enablesyntaxmap=0,enablesubstructure=0,enablediagnostics=0 %t/func.swift -- %t/func.swift == \
11+
// RUN: -req=cursor -offset=5 %t/func.swift -- %t/func.swift

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,17 +2300,6 @@ ImmutableTextSnapshotRef SwiftEditorDocument::getLatestSnapshot() const {
23002300
return Impl.EditableBuffer->getSnapshot();
23012301
}
23022302

2303-
std::pair<unsigned, unsigned>
2304-
SwiftEditorDocument::getLineAndColumnInBuffer(unsigned Offset) {
2305-
llvm::sys::ScopedLock L(Impl.AccessMtx);
2306-
2307-
auto SyntaxInfo = Impl.getSyntaxInfo();
2308-
auto &SM = SyntaxInfo->getSourceManager();
2309-
2310-
auto Loc = SM.getLocForOffset(SyntaxInfo->getBufferID(), Offset);
2311-
return SM.getLineAndColumnInBuffer(Loc);
2312-
}
2313-
23142303
void SwiftEditorDocument::reportDocumentStructure(SourceFile &SrcFile,
23152304
EditorConsumer &Consumer) {
23162305
ide::SyntaxModelContext ModelContext(SrcFile);

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ class SwiftEditorDocument :
105105
void removeCachedAST();
106106

107107
ImmutableTextSnapshotRef getLatestSnapshot() const;
108-
std::pair<unsigned, unsigned> getLineAndColumnInBuffer(unsigned Offset);
109108

110109
void resetSyntaxInfo(ImmutableTextSnapshotRef Snapshot,
111110
SwiftLangSupport &Lang, bool BuildSyntaxTree,

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ static void mapLocToLatestSnapshot(
639639
}
640640

641641
std::tie(Location.Line, Location.Column) =
642-
EditorDoc->getLineAndColumnInBuffer(Location.Offset);
642+
LatestSnap->getBuffer()->getLineAndColumn(Location.Offset);
643643
}
644644

645645

0 commit comments

Comments
 (0)