-
Notifications
You must be signed in to change notification settings - Fork 46
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 rule doc links #275
Conversation
Signed-off-by: harupy <hkawamura0130@gmail.com>
tooltip=f"Jump to {code} documentation", | ||
target=f"https://docs.astral.sh/ruff/rules/{name}", | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this iterate over the entire document, checking every line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it does...
"""LSP handler for textDocument/hover request.""" | ||
document = LSP_SERVER.workspace.get_text_document(params.text_document.uri) | ||
match = NOQA_REGEX.search(document.lines[params.position.line]) | ||
def _parse_codes(line: str) -> list[tuple(str, int, int)]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we have a NamedTuple
/ dataclass
/ TypedDict
instead of (str, int, int)
? It's hard to know what those represents otherwise.
document = LSP_SERVER.workspace.get_document(params.text_document.uri) | ||
|
||
links: list[DocumentLink] = [] | ||
for index, line in enumerate(document.lines): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this request applies for an entire document and not for a specific range. It makes me wonder how often this request is being sent by the client. If it's being sent multiple times then we should think of ways to optimize this as otherwise this might take some time for a large file irrespective of whether it contains the codes or not.
A few options might be:
- Applying the regex on the entire document using
re.finditer
instead of applying the regex on each line - Using
documentLink/resolve
to lazily compute the rule name. So, fortextDocument/documentLink
request, we would only compute the range and when the client explicitly requests for the link, we would run the command and update thedocumentLink
object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhruvmanila Thanks for the comment!
It makes me wonder how often this request is being sent by the client.
- When a file is opened.
- When a file is saved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
textDocument/documentLink
request is also sent on type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll close this PR :) Iterating over the entire document on type is too expensive.
Summary
Close #274
Test Plan
Manually tested