Skip to content

Commit b9e2c33

Browse files
committed
Fix jump tracking when document changed externally
Previously `git checkout` would mess up your jumps
1 parent eafa396 commit b9e2c33

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

extensionBase.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,13 @@ export async function activate(context: vscode.ExtensionContext, handleLocal: bo
144144
Logger.trace(`\t-${x.rangeLength}, +'${x.text}'`);
145145
}
146146

147-
if (event.contentChanges.length === 1) {
148-
const change = event.contentChanges[0];
149-
150-
const anyLinesDeleted = change.range.start.line !== change.range.end.line;
151-
152-
if (anyLinesDeleted && change.text === '') {
147+
for (const change of event.contentChanges) {
148+
if (change.rangeLength > 0) {
153149
globalState.jumpTracker.handleTextDeleted(event.document, change.range);
154-
} else if (!anyLinesDeleted && change.text.includes('\n')) {
155-
globalState.jumpTracker.handleTextAdded(event.document, change.range, change.text);
156-
} else {
157-
// TODO: What to do here?
158150
}
159-
} else {
160-
// TODO: In this case, we should probably loop over the content changes...
151+
if (change.text.length > 0) {
152+
globalState.jumpTracker.handleTextAdded(event.document, change.range.start, change.text);
153+
}
161154
}
162155

163156
const mh = ModeHandlerMap.get(event.document.uri);

src/jumps/jumpTracker.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,25 @@ export class JumpTracker {
237237
* Update existing jumps when lines were added to a document.
238238
*
239239
* @param document - Document that was changed, typically a vscode.TextDocument.
240-
* @param range - Location where the text was added.
240+
* @param position - Location where the text was added.
241241
* @param text - Text containing one or more newline characters.
242242
*/
243-
public handleTextAdded(document: { fileName: string }, range: vscode.Range, text: string): void {
243+
public handleTextAdded(
244+
document: { fileName: string },
245+
position: vscode.Position,
246+
text: string,
247+
): void {
244248
// Get distance from newlines in the text added.
245249
// Unlike handleTextDeleted, the range parameter distance between start/end is generally zero,
246250
// just showing where the text was added.
247251
const distance = text.split('').filter((c) => c === '\n').length;
252+
if (distance === 0) {
253+
return;
254+
}
248255

249256
for (const [i, jump] of this._jumps.entries()) {
250257
const jumpIsAfterAddedText =
251-
jump.fileName === document.fileName && jump.position.line > range.start.line;
258+
jump.fileName === document.fileName && jump.position.line > position.line;
252259

253260
if (jumpIsAfterAddedText) {
254261
const newPosition = new Position(jump.position.line + distance, jump.position.character);

test/jumpTracker.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,5 +440,7 @@ suite('Record and navigate jumps', () => {
440440
jumps: ['start', 'end'],
441441
});
442442
});
443+
444+
// TODO: Test that jumps are adjusted properly when document is modified externally
443445
});
444446
});

0 commit comments

Comments
 (0)