Skip to content

Commit 6888ca2

Browse files
committed
feat: optimize pull diagnostic interface and add convenience methods
1 parent 38bb3d2 commit 6888ca2

File tree

8 files changed

+114
-0
lines changed

8 files changed

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