Provide an API to track a position in a document across edits
See original GitHub issueI’ve come across a few places where this’d be handy lately. Most recently, I run some tests for my user and I get back positions in the document of where each test is - this allows them to click on the test in the runner/results to jump directly to it.
Unfortunately, if the user modifies their test file then this location information becomes out of date and now jumps the user to the wrong location when they click it. This can be really common - you run your tests; 5 of them fail; you start working through them - working on the first test shifts the position of the tests below such that it’s now difficult to jump to them from the test list.
VS Code is presumably already tracking things like this for things like decorations so it’d be nice if we could use it too. For example, imagine an API like this:
// Get some token that represents a live position in the document
const loc = document.trackLocation(position);
// User makes some edits
// Request the new position
console.log(document.positionOf(loc));
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:7 (4 by maintainers)
Top GitHub Comments
I see that internally there is a method that we would really appreciate - https://github.com/microsoft/vscode/blob/master/src/vs/editor/browser/widget/codeEditorWidget.ts#L1118 -
getLineDecorations
but it is not exposed to public usage.I was trying to reach to it via
but I couldn’t.
Any plans to make it public?
@PEZ Sure - the code is in an experimental feature that nobody currently uses so I can’t guarantee how well it works, but it’s here:
https://github.com/Dart-Code/Dart-Code/blob/d996c73d6a455135b8e532ac266ef1f33704b0e7/src/decorations/hot_reload_coverage_decorations.ts#L54
Of interest are probably lines 73-83 which enumerates the changes and 130-145 which updates the internal model based on the changes. We track ranges of lines, so we offset any that were after the edit, leave any that were before, and drop any that are eclipsed by the edit. If you need exact offsets, you’d need to improve this somewhat.