11from __future__ import annotations
22
33from functools import cached_property
4- from typing import Protocol , Sequence , override , runtime_checkable
4+ from typing import Sequence , override
55
66import asyncer
77from attrs import define
88from lsp_client .capability .request import (
99 WithRequestDeclaration ,
1010 WithRequestDefinition ,
11- WithRequestDocumentSymbol ,
12- WithRequestHover ,
1311 WithRequestTypeDefinition ,
1412)
1513from lsprotocol .types import Location
1614
17- from lsap .exception import UnsupportedCapabilityError
1815from lsap .schema .definition import DefinitionRequest , DefinitionResponse
1916from lsap .schema .models import SymbolCodeInfo
17+ from lsap .utils .capability import ensure_capability
2018
21- from .abc import Capability , ClientProtocol
19+ from .abc import Capability
2220from .locate import LocateCapability
2321from .symbol import SymbolCapability
2422
2523
26- @runtime_checkable
27- class DefinitionClient (
28- WithRequestDefinition ,
29- WithRequestDocumentSymbol ,
30- WithRequestHover ,
31- ClientProtocol ,
32- Protocol ,
33- ): ...
34-
35-
3624@define
37- class DefinitionCapability (
38- Capability [DefinitionClient , DefinitionRequest , DefinitionResponse ]
39- ):
25+ class DefinitionCapability (Capability [DefinitionRequest , DefinitionResponse ]):
4026 @cached_property
4127 def locate (self ) -> LocateCapability :
4228 return LocateCapability (self .client )
@@ -55,31 +41,25 @@ async def __call__(self, req: DefinitionRequest) -> DefinitionResponse | None:
5541 locations : Sequence [Location ] | None = None
5642 match req .mode :
5743 case "definition" :
58- locations = await self . client . request_definition_locations (
59- file_path , lsp_pos
60- )
44+ locations = await ensure_capability (
45+ self . client , WithRequestDefinition
46+ ). request_definition_locations ( file_path , lsp_pos )
6147 case "declaration" :
62- if not isinstance (self .client , WithRequestDeclaration ):
63- raise UnsupportedCapabilityError (
64- "Client does not support 'textDocument/declaration'. "
65- "To find declarations, you can: "
66- "1) Use 'definition' mode (most language servers treat them similarly); "
67- "2) For C/C++, check corresponding header files manually."
68- )
69- locations = await self .client .request_declaration_locations (
70- file_path , lsp_pos
71- )
48+ locations = await ensure_capability (
49+ self .client ,
50+ WithRequestDeclaration ,
51+ error = "To find declarations, you can: "
52+ "1) Use 'definition' mode (most language servers treat them similarly); "
53+ "2) For C/C++, check corresponding header files manually." ,
54+ ).request_declaration_locations (file_path , lsp_pos )
7255 case "type_definition" :
73- if not isinstance (self .client , WithRequestTypeDefinition ):
74- raise UnsupportedCapabilityError (
75- "Client does not support 'textDocument/typeDefinition'. "
76- "To find type definitions, you can: "
77- "1) Use 'definition' on the type name itself if visible; "
78- "2) Use 'hover' to see the type name and then search for it."
79- )
80- locations = await self .client .request_type_definition_locations (
81- file_path , lsp_pos
82- )
56+ locations = await ensure_capability (
57+ self .client ,
58+ WithRequestTypeDefinition ,
59+ error = "To find type definitions, you can: "
60+ "1) Use 'definition' on the type name itself if visible; "
61+ "2) Use 'hover' to see the type name and then search for it." ,
62+ ).request_type_definition_locations (file_path , lsp_pos )
8363
8464 if not locations :
8565 return None
0 commit comments