Skip to content

Commit 0f21991

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 251ce36 commit 0f21991

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
@@ -2308,17 +2308,6 @@ ImmutableTextSnapshotRef SwiftEditorDocument::getLatestSnapshot() const {
23082308
return Impl.EditableBuffer->getSnapshot();
23092309
}
23102310

2311-
std::pair<unsigned, unsigned>
2312-
SwiftEditorDocument::getLineAndColumnInBuffer(unsigned Offset) {
2313-
llvm::sys::ScopedLock L(Impl.AccessMtx);
2314-
2315-
auto SyntaxInfo = Impl.getSyntaxInfo();
2316-
auto &SM = SyntaxInfo->getSourceManager();
2317-
2318-
auto Loc = SM.getLocForOffset(SyntaxInfo->getBufferID(), Offset);
2319-
return SM.getLineAndColumnInBuffer(Loc);
2320-
}
2321-
23222311
void SwiftEditorDocument::reportDocumentStructure(SourceFile &SrcFile,
23232312
EditorConsumer &Consumer) {
23242313
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)