Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Provide rule doc links #275

wants to merge 1 commit into from

Conversation

harupy
Copy link
Contributor

@harupy harupy commented Oct 16, 2023

Summary

Close #274

Test Plan

Manually tested

Signed-off-by: harupy <hkawamura0130@gmail.com>
tooltip=f"Jump to {code} documentation",
target=f"https://docs.astral.sh/ruff/rules/{name}",
)
)
Copy link
Member

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?

Copy link
Contributor Author

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)]:
Copy link
Member

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):
Copy link
Member

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, for textDocument/documentLink request, we would only compute the range and when the client explicitly requests for the link, we would run the command and update the documentLink object.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@harupy harupy Oct 18, 2023

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.

Copy link
Contributor Author

@harupy harupy Oct 18, 2023

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.

@harupy harupy closed this Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a link to rule doc using textDocument/documentLink
3 participants