Skip to content

Commit 2a7dfe3

Browse files
authored
feat: optimize pull diagnostic interface and add convenience methods (#13)
* feat: optimize pull diagnostic interface and add convenience methods * fix: fix review
1 parent 78989a9 commit 2a7dfe3

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed

src/lsp_client/capability/request/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .hover import WithRequestHover
1010
from .implementation import WithRequestImplementation
1111
from .inline_value import WithRequestInlineValue
12+
from .pull_diagnostic import WithRequestPullDiagnostic
1213
from .reference import WithRequestReferences
1314
from .type_definition import WithRequestTypeDefinition
1415
from .type_hierarchy import WithRequestTypeHierarchy
@@ -22,6 +23,7 @@
2223
WithRequestHover,
2324
WithRequestImplementation,
2425
WithRequestInlineValue,
26+
WithRequestPullDiagnostic,
2527
WithRequestReferences,
2628
WithRequestTypeDefinition,
2729
WithRequestTypeHierarchy,
@@ -36,6 +38,7 @@
3638
"WithRequestHover",
3739
"WithRequestImplementation",
3840
"WithRequestInlineValue",
41+
"WithRequestPullDiagnostic",
3942
"WithRequestReferences",
4043
"WithRequestTypeDefinition",
4144
"WithRequestTypeHierarchy",
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Sequence
4+
from typing import Protocol, override, runtime_checkable
5+
6+
from loguru import logger
7+
8+
from lsp_client.jsonrpc.id import jsonrpc_uuid
9+
from lsp_client.protocol import (
10+
CapabilityClientProtocol,
11+
TextDocumentCapabilityProtocol,
12+
)
13+
from lsp_client.utils.types import AnyPath, lsp_type
14+
15+
16+
@runtime_checkable
17+
class WithRequestPullDiagnostic(
18+
TextDocumentCapabilityProtocol,
19+
CapabilityClientProtocol,
20+
Protocol,
21+
):
22+
"""
23+
`textDocument/diagnostic` - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_pullDiagnostics
24+
"""
25+
26+
@override
27+
@classmethod
28+
def methods(cls) -> Sequence[str]:
29+
return (lsp_type.TEXT_DOCUMENT_DIAGNOSTIC,)
30+
31+
@override
32+
@classmethod
33+
def register_text_document_capability(
34+
cls, cap: lsp_type.TextDocumentClientCapabilities
35+
) -> None:
36+
super().register_text_document_capability(cap)
37+
cap.diagnostic = lsp_type.DiagnosticClientCapabilities(
38+
dynamic_registration=True,
39+
related_document_support=True,
40+
related_information=True,
41+
tag_support=lsp_type.ClientDiagnosticsTagOptions(
42+
value_set=[*lsp_type.DiagnosticTag]
43+
),
44+
code_description_support=True,
45+
data_support=True,
46+
)
47+
48+
@override
49+
@classmethod
50+
def check_server_capability(
51+
cls,
52+
cap: lsp_type.ServerCapabilities,
53+
info: lsp_type.ServerInfo | None,
54+
) -> None:
55+
super().check_server_capability(cap, info)
56+
assert cap.diagnostic_provider
57+
58+
async def request_diagnostic(
59+
self,
60+
file_path: AnyPath,
61+
*,
62+
identifier: str | None = None,
63+
previous_result_id: str | None = None,
64+
) -> lsp_type.DocumentDiagnosticReport | None:
65+
"""
66+
`textDocument/diagnostic` - Request a diagnostic report for a document.
67+
"""
68+
return await self.file_request(
69+
lsp_type.DocumentDiagnosticRequest(
70+
id=jsonrpc_uuid(),
71+
params=lsp_type.DocumentDiagnosticParams(
72+
text_document=lsp_type.TextDocumentIdentifier(
73+
uri=self.as_uri(file_path)
74+
),
75+
identifier=identifier,
76+
previous_result_id=previous_result_id,
77+
),
78+
),
79+
schema=lsp_type.DocumentDiagnosticResponse,
80+
file_paths=[file_path],
81+
)
82+
83+
async def request_diagnostics(
84+
self,
85+
file_path: AnyPath,
86+
*,
87+
identifier: str | None = None,
88+
previous_result_id: str | None = None,
89+
) -> Sequence[lsp_type.Diagnostic] | None:
90+
"""
91+
Request diagnostics for a document. Returns only the list of diagnostics.
92+
"""
93+
match await self.request_diagnostic(
94+
file_path,
95+
identifier=identifier,
96+
previous_result_id=previous_result_id,
97+
):
98+
case lsp_type.RelatedFullDocumentDiagnosticReport(items=items):
99+
return items
100+
case lsp_type.FullDocumentDiagnosticReport(items=items):
101+
return items
102+
case _:
103+
logger.warning(
104+
"Unsupported diagnostic report type for file {}",
105+
file_path,
106+
)
107+
return None

src/lsp_client/clients/deno/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
WithRequestDocumentSymbol,
1616
WithRequestHover,
1717
WithRequestImplementation,
18+
WithRequestPullDiagnostic,
1819
WithRequestReferences,
1920
WithRequestTypeDefinition,
2021
WithRequestWorkspaceSymbol,
@@ -87,6 +88,7 @@ class DenoClient(
8788
WithRequestTypeDefinition,
8889
WithRequestCallHierarchy,
8990
WithRequestDocumentSymbol,
91+
WithRequestPullDiagnostic,
9092
WithRequestWorkspaceSymbol,
9193
WithReceiveLogMessage,
9294
WithReceivePublishDiagnostics,

src/lsp_client/clients/pyrefly.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
WithRequestDocumentSymbol,
2020
WithRequestHover,
2121
WithRequestImplementation,
22+
WithRequestPullDiagnostic,
2223
WithRequestReferences,
2324
WithRequestTypeDefinition,
2425
WithRequestWorkspaceSymbol,
@@ -84,6 +85,7 @@ class PyreflyClient(
8485
WithRequestDocumentSymbol,
8586
WithRequestHover,
8687
WithRequestImplementation,
88+
WithRequestPullDiagnostic,
8789
WithRequestReferences,
8890
WithRequestTypeDefinition,
8991
WithRequestWorkspaceSymbol,

src/lsp_client/clients/pyright.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
WithRequestDefinition,
1919
WithRequestDocumentSymbol,
2020
WithRequestHover,
21+
WithRequestPullDiagnostic,
2122
WithRequestReferences,
2223
WithRequestTypeDefinition,
2324
WithRequestWorkspaceSymbol,
@@ -79,6 +80,7 @@ class PyrightClient(
7980
WithRequestDefinition,
8081
WithRequestDocumentSymbol,
8182
WithRequestHover,
83+
WithRequestPullDiagnostic,
8284
WithRequestReferences,
8385
WithRequestTypeDefinition,
8486
WithRequestWorkspaceSymbol,

src/lsp_client/clients/rust_analyzer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
WithRequestDocumentSymbol,
2020
WithRequestHover,
2121
WithRequestImplementation,
22+
WithRequestPullDiagnostic,
2223
WithRequestReferences,
2324
WithRequestTypeDefinition,
2425
WithRequestWorkspaceSymbol,
@@ -80,6 +81,7 @@ class RustAnalyzerClient(
8081
WithRequestDocumentSymbol,
8182
WithRequestHover,
8283
WithRequestImplementation,
84+
WithRequestPullDiagnostic,
8385
WithRequestReferences,
8486
WithRequestTypeDefinition,
8587
WithRequestWorkspaceSymbol,

src/lsp_client/clients/ty.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
WithRequestDefinition,
1919
WithRequestDocumentSymbol,
2020
WithRequestHover,
21+
WithRequestPullDiagnostic,
2122
WithRequestReferences,
2223
WithRequestTypeDefinition,
2324
WithRequestWorkspaceSymbol,
@@ -76,6 +77,7 @@ class TyClient(
7677
WithRequestDefinition,
7778
WithRequestDocumentSymbol,
7879
WithRequestHover,
80+
WithRequestPullDiagnostic,
7981
WithRequestReferences,
8082
WithRequestTypeDefinition,
8183
WithRequestWorkspaceSymbol,

src/lsp_client/clients/typescript.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
WithRequestDocumentSymbol,
1818
WithRequestHover,
1919
WithRequestImplementation,
20+
WithRequestPullDiagnostic,
2021
WithRequestReferences,
2122
WithRequestTypeDefinition,
2223
WithRequestWorkspaceSymbol,
@@ -85,6 +86,7 @@ class TypescriptClient(
8586
WithRequestImplementation,
8687
WithRequestTypeDefinition,
8788
WithRequestDocumentSymbol,
89+
WithRequestPullDiagnostic,
8890
WithRequestWorkspaceSymbol,
8991
WithReceiveLogMessage,
9092
WithReceiveLogTrace,

0 commit comments

Comments
 (0)