-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement Inlay Hint capabilities #12
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
84bfa36
feat: implement textDocument/inlayHint and workspace/inlayHint/refres…
observerw 3cc1099
Update src/lsp_client/capability/request/inlay_hint.py
observerw 9df2010
Update src/lsp_client/capability/request/inlay_hint.py
observerw d3cdcbb
fix: update type
observerw f8aa7a1
Merge branch 'main' into feature/inlay-hint
observerw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from typing import Protocol, override, runtime_checkable | ||
|
|
||
| import asyncer | ||
|
|
||
| from lsp_client.jsonrpc.id import jsonrpc_uuid | ||
| from lsp_client.protocol import ( | ||
| CapabilityClientProtocol, | ||
| TextDocumentCapabilityProtocol, | ||
| ) | ||
| from lsp_client.utils.types import AnyPath, Range, lsp_type | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class WithRequestInlayHint( | ||
| TextDocumentCapabilityProtocol, | ||
| CapabilityClientProtocol, | ||
| Protocol, | ||
| ): | ||
| """ | ||
| `textDocument/inlayHint` - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint | ||
| """ | ||
|
|
||
| @override | ||
| @classmethod | ||
| def methods(cls) -> Sequence[str]: | ||
| return ( | ||
| lsp_type.TEXT_DOCUMENT_INLAY_HINT, | ||
| lsp_type.INLAY_HINT_RESOLVE, | ||
| ) | ||
|
|
||
| @override | ||
| @classmethod | ||
| def register_text_document_capability( | ||
| cls, cap: lsp_type.TextDocumentClientCapabilities | ||
| ) -> None: | ||
| cap.inlay_hint = lsp_type.InlayHintClientCapabilities( | ||
| dynamic_registration=True, | ||
| resolve_support=lsp_type.ClientInlayHintResolveOptions( | ||
| properties=[ | ||
| "tooltip", | ||
| "location", | ||
| "label.tooltip", | ||
| "label.location", | ||
| "textEdits", | ||
| ] | ||
| ), | ||
| ) | ||
|
|
||
| @override | ||
| @classmethod | ||
| def check_server_capability( | ||
| cls, | ||
| cap: lsp_type.ServerCapabilities, | ||
| info: lsp_type.ServerInfo | None, | ||
| ) -> None: | ||
| super().check_server_capability(cap, info) | ||
| assert cap.inlay_hint_provider | ||
|
|
||
| def get_inlay_hint_label( | ||
| self, hint: lsp_type.InlayHint | lsp_type.InlayHintLabelPart | ||
| ) -> str: | ||
| """Extract the text label from an InlayHint or InlayHintLabelPart.""" | ||
| match hint: | ||
| case lsp_type.InlayHintLabelPart(value=value): | ||
| return value | ||
| case lsp_type.InlayHint(label=str() as label): | ||
| return label | ||
| case lsp_type.InlayHint(label=parts): | ||
| return "".join(part.value for part in parts) | ||
| case _: | ||
| raise TypeError(f"Unexpected type for inlay hint label: {type(hint)}") | ||
|
|
||
| async def request_inlay_hint( | ||
| self, | ||
| file_path: AnyPath, | ||
| range: Range, | ||
| *, | ||
| resolve: bool = False, | ||
| ) -> Sequence[lsp_type.InlayHint] | None: | ||
| """ | ||
| Request inlay hints for a given file and range. | ||
|
|
||
| This sends a `textDocument/inlayHint` request for the specified document range. | ||
| If ``resolve`` is True, each returned inlay hint is further resolved using | ||
| :meth:`request_inlay_hint_resolve` to populate optional properties such as | ||
| tooltip, locations, and text edits. | ||
|
|
||
| :param file_path: Path to the file for which inlay hints are requested. | ||
| :param range: LSP range within the document to compute inlay hints for. | ||
| :param resolve: Whether to resolve each returned inlay hint for additional | ||
| details supported by the server. | ||
| :return: A sequence of :class:`lsp_type.InlayHint` instances if the server | ||
| returns hints, or ``None`` if no hints are provided. | ||
| """ | ||
| hints = await self.file_request( | ||
| lsp_type.InlayHintRequest( | ||
| id=jsonrpc_uuid(), | ||
| params=lsp_type.InlayHintParams( | ||
| text_document=lsp_type.TextDocumentIdentifier( | ||
| uri=self.as_uri(file_path) | ||
| ), | ||
| range=range, | ||
| ), | ||
| ), | ||
| schema=lsp_type.InlayHintResponse, | ||
| file_paths=[file_path], | ||
| ) | ||
|
|
||
| if resolve and hints: | ||
| async with asyncer.create_task_group() as tg: | ||
| tasks = [ | ||
| tg.soonify(self.request_inlay_hint_resolve)(hint) for hint in hints | ||
| ] | ||
| return [task.value for task in tasks] | ||
|
|
||
| return hints | ||
|
|
||
| async def request_inlay_hint_resolve( | ||
| self, | ||
| hint: lsp_type.InlayHint, | ||
| ) -> lsp_type.InlayHint: | ||
observerw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Resolve additional details for a previously returned inlay hint. | ||
|
|
||
| This sends an LSP ``inlayHint/resolve`` request to the server for the | ||
| given ``hint``. Servers may initially return inlay hints with only a | ||
| subset of properties populated and require a subsequent resolve | ||
| request to fill in optional fields such as tooltips, locations, or | ||
| text edits. | ||
|
|
||
| :param hint: An :class:`lsp_type.InlayHint` instance obtained from a | ||
| prior ``textDocument/inlayHint`` request that should be fully | ||
| resolved by the server. | ||
| :return: A new :class:`lsp_type.InlayHint` containing any additional | ||
| data supplied by the server. | ||
| """ | ||
| return await self.request( | ||
| lsp_type.InlayHintResolveRequest( | ||
| id=jsonrpc_uuid(), | ||
| params=hint, | ||
| ), | ||
| schema=lsp_type.InlayHintResolveResponse, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/lsp_client/capability/server_request/inlay_hint_refresh.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from typing import Protocol, override, runtime_checkable | ||
|
|
||
| from lsp_client.protocol import ( | ||
| CapabilityClientProtocol, | ||
| ServerRequestHook, | ||
| ServerRequestHookProtocol, | ||
| ServerRequestHookRegistry, | ||
| WorkspaceCapabilityProtocol, | ||
| ) | ||
| from lsp_client.utils.types import lsp_type | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class WithRespondInlayHintRefresh( | ||
| WorkspaceCapabilityProtocol, | ||
| ServerRequestHookProtocol, | ||
| CapabilityClientProtocol, | ||
| Protocol, | ||
| ): | ||
| """ | ||
| `workspace/inlayHint/refresh` - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_inlayHint_refresh | ||
| """ | ||
|
|
||
| @override | ||
| @classmethod | ||
| def methods(cls) -> Sequence[str]: | ||
| return (lsp_type.WORKSPACE_INLAY_HINT_REFRESH,) | ||
|
|
||
| @override | ||
| @classmethod | ||
| def register_workspace_capability( | ||
| cls, cap: lsp_type.WorkspaceClientCapabilities | ||
| ) -> None: | ||
| super().register_workspace_capability(cap) | ||
| cap.inlay_hint = lsp_type.InlayHintWorkspaceClientCapabilities( | ||
| refresh_support=True | ||
| ) | ||
|
|
||
| @override | ||
| @classmethod | ||
| def check_server_capability( | ||
| cls, | ||
| cap: lsp_type.ServerCapabilities, | ||
| info: lsp_type.ServerInfo | None, | ||
| ) -> None: | ||
| super().check_server_capability(cap, info) | ||
|
|
||
| async def respond_inlay_hint_refresh( | ||
| self, req: lsp_type.InlayHintRefreshRequest | ||
| ) -> lsp_type.InlayHintRefreshResponse: | ||
| return lsp_type.InlayHintRefreshResponse( | ||
| id=req.id, | ||
| result=None, | ||
| ) | ||
|
|
||
| @override | ||
| def register_server_request_hooks( | ||
| self, registry: ServerRequestHookRegistry | ||
| ) -> None: | ||
| super().register_server_request_hooks(registry) | ||
| registry.register( | ||
| lsp_type.WORKSPACE_INLAY_HINT_REFRESH, | ||
| ServerRequestHook( | ||
| cls=lsp_type.InlayHintRefreshRequest, | ||
| execute=self.respond_inlay_hint_refresh, | ||
| ), | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.