feat: add logic and tests for position at offset#563
feat: add logic and tests for position at offset#563meymchen wants to merge 2 commits intoopenlawlibrary:mainfrom
Conversation
|
Great! Thank you ❤️. Could you format with Black so that we can see just the changes you've made when reviewing the diff? |
|
Great, thanks. What do you think about putting most of this code in I must admit I don't know of a usecase for this function, which could just be my own inexperience. But what do you think about a sentence describing a simple usecase in the function's doc comment? |
| def _get_line_offsets(self) -> List[int]: | ||
| if self._line_offsets is None: | ||
| self._line_offsets = self._compute_line_offsets(self.source, True) | ||
| return self._line_offsets |
There was a problem hiding this comment.
I think this is missing cache invalidation.
My reading of the pygls code suggests pygls updates the TextDocument.source in-place if the client sends a delta for textDocument/didChange notifications but this cache would persist causing the line offsets to be out of sync with TextDocument.source.
A version check will suffice for documents open in the client. For server opened document, I am not aware of a good way to determine if the cache can be reused (since TextDocument.source will silently open and re-read the file every time).
There was a problem hiding this comment.
You are absolutely right. I rewrite the text document in my owned project and the dependencies is missing here. I'll update this in the next few days.
Currently the If we moved this logic to The downside is that Personally, I think I am net for making it stateless in general with my current world view, since the |
|
Thanks as always @nthykier, very good points. Let's keep |
Description (e.g. "Related to ...", etc.)
A
position_at_offsetmethod is added toTextDocument, inspired by a nice work here.Related tests are also implemented.
Code review checklist (for code reviewer to complete)
Automated linters
You can run the lints that are run on CI locally with: