-
Notifications
You must be signed in to change notification settings - Fork 28.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide an API to track a position in a document across edits #54147
Comments
@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: 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. |
Oh, that is wonderful, @DanTup! I will need exact offsets, but it looks like I'll be able to adapt that code. Thanks for sharing! |
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 - I was trying to reach to it via
but I couldn't. Any plans to make it public? |
This comment has been minimized.
This comment has been minimized.
I would propose this api instead: I needed this not only to track a selection, but multiple pinned positions, and I need to know when the user deletes the span containing the bookmark - so they need a "onremoved" event also. This implementation sucks because I have to track the "before" state of the text buffer since that information doesn't come in the TextDocumentContentChangeEvent. If TextDocumentContentChangeEvent did provide the "original text" then that would solve the problem too. |
Still no intention of adding this feature in future release ? 🤔 |
vscode-position-trackingFrom what I understand there is no API provided by VS Code, nor there is an algorithm that effectively tracks a location, whatever the scenario, using the existing VS Code API. Correct me if I'm wrong. I had some reasons for starting building such an algorithm. I tested it and improved it until I couldn't find any more situations where it wouldn't correctly update the locations. But what I would want is others to try and find situations where the updated locations are not correct. This algorithm is served under the form of a library and its API: https://www.npmjs.com/package/vscode-position-tracking?activeTab=readme And here is the library's repository: https://github.com/srares13/vscode-position-tracking No delayThere is another thing. I don't know how are the decorations drown here: #73780 , but from my testing, while I was constantly updating ranges through the vscode-position-tracking library and setting decorations based on those updated ranges, it happened real time, with no flicker. However, I noticed something, that as soon as I enabled some logs on the debug console of the development window, there was some very serious delay for me. Unfortunately, I don't know why the action of logging to debug console causes delay. Instead, if you log to the output channel of the extension (situated in the terminal's output tab of the host window / end user window), there is no issue. Library improvementsIn the future there could be added:
But I think that now the most important thing is to test the library's ability to effectively calculate the updated locations, in every scenario, according to document changes. |
I'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:
The text was updated successfully, but these errors were encountered: