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 a link to rule doc using textDocument/documentLink #274

Closed
harupy opened this issue Oct 15, 2023 · 4 comments
Closed

Provide a link to rule doc using textDocument/documentLink #274

harupy opened this issue Oct 15, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@harupy
Copy link
Contributor

harupy commented Oct 15, 2023

Is this more discoverable than hover?

Screen.Recording.2023-10-15.at.13.43.49.mov
Code change

diff --git a/ruff_lsp/server.py b/ruff_lsp/server.py
index ea7df2a..a1eb35a 100755
--- a/ruff_lsp/server.py
+++ b/ruff_lsp/server.py
@@ -23,6 +23,7 @@ from lsprotocol.types import (
     TEXT_DOCUMENT_DID_OPEN,
     TEXT_DOCUMENT_DID_SAVE,
     TEXT_DOCUMENT_FORMATTING,
+    TEXT_DOCUMENT_DOCUMENT_LINK,
     TEXT_DOCUMENT_HOVER,
     AnnotatedTextEdit,
     ClientCapabilities,
@@ -39,6 +40,8 @@ from lsprotocol.types import (
     DidOpenTextDocumentParams,
     DidSaveTextDocumentParams,
     DocumentFormattingParams,
+    DocumentLink,
+    DocumentLinkParams,
     Hover,
     HoverParams,
     InitializeParams,
@@ -397,6 +400,59 @@ async def hover(params: HoverParams) -> Hover | None:
     return None
 
 
+@LSP_SERVER.feature(TEXT_DOCUMENT_DOCUMENT_LINK)
+async def document_link(params: DocumentLinkParams) -> list[DocumentLink] | None:
+    """LSP handler for textDocument/documentLink request."""
+    document = LSP_SERVER.workspace.get_document(params.text_document.uri)
+
+    links: list[DocumentLink] = []
+    for line_index, line in enumerate(document.lines):
+        match = NOQA_REGEX.search(line)
+        if not match:
+            continue
+
+        codes = match.group("codes")
+        if not codes:
+            continue
+
+        codes_start = match.start("codes")
+        for match in CODE_REGEX.finditer(codes):
+            start, end = match.span()
+            start += codes_start
+            end += codes_start
+            code = match.group()
+            result = await _run_subcommand_on_document(
+                document,
+                VERSION_REQUIREMENT_LINTER,
+                args=[
+                    "--explain",
+                    code,
+                    "--format",
+                    "json",
+                ],
+            )
+            log_to_output(f"result.stdout: {result.stdout}")
+            if result.stdout:
+                name = json.loads(result.stdout.decode("utf-8")).get("name")
+                links.append(
+                    DocumentLink(
+                        range=Range(
+                            start=Position(
+                                line=line_index,
+                                character=start,
+                            ),
+                            end=Position(
+                                line=line_index,
+                                character=end,
+                            ),
+                        ),
+                        target=f"https://docs.astral.sh/ruff/rules/{name}",
+                    )
+                )
+
+    return links
+
+
 ###
 # Code Actions.
 ###

@charliermarsh
Copy link
Member

I think that seems good. What about you, @dhruvmanila?

@dhruvmanila
Copy link
Member

Yeah, it's good. A use case would be to have a keybinding to open the website directly instead of hover.

@dhruvmanila dhruvmanila added the enhancement New feature or request label Oct 16, 2023
@harupy
Copy link
Contributor Author

harupy commented Oct 16, 2023

@charliermarsh @dhruvmanila Thanks for the comments. Do you think the hover feature should be kept?

@dhruvmanila
Copy link
Member

Yeah, I think the hover should still be kept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants