Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lsp_client/capability/request/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .document_symbol import WithRequestDocumentSymbol
from .hover import WithRequestHover
from .implementation import WithRequestImplementation
from .inlay_hint import WithRequestInlayHint
from .inline_value import WithRequestInlineValue
from .pull_diagnostic import WithRequestPullDiagnostic
from .reference import WithRequestReferences
Expand All @@ -22,6 +23,7 @@
WithRequestDocumentSymbol,
WithRequestHover,
WithRequestImplementation,
WithRequestInlayHint,
WithRequestInlineValue,
WithRequestPullDiagnostic,
WithRequestReferences,
Expand All @@ -37,6 +39,7 @@
"WithRequestDocumentSymbol",
"WithRequestHover",
"WithRequestImplementation",
"WithRequestInlayHint",
"WithRequestInlineValue",
"WithRequestPullDiagnostic",
"WithRequestReferences",
Expand Down
146 changes: 146 additions & 0 deletions src/lsp_client/capability/request/inlay_hint.py
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:
"""
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,
)
3 changes: 3 additions & 0 deletions src/lsp_client/capability/server_request/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

from typing import Final

from .inlay_hint_refresh import WithRespondInlayHintRefresh
from .show_document_request import WithRespondShowDocumentRequest
from .show_message_request import WithRespondShowMessageRequest
from .workspace_configuration import WithRespondConfigurationRequest
from .workspace_folders import WithRespondWorkspaceFoldersRequest

capabilities: Final = (
WithRespondConfigurationRequest,
WithRespondInlayHintRefresh,
WithRespondShowDocumentRequest,
WithRespondShowMessageRequest,
WithRespondWorkspaceFoldersRequest,
)

__all__ = [
"WithRespondConfigurationRequest",
"WithRespondInlayHintRefresh",
"WithRespondShowDocumentRequest",
"WithRespondShowMessageRequest",
"WithRespondWorkspaceFoldersRequest",
Expand Down
70 changes: 70 additions & 0 deletions src/lsp_client/capability/server_request/inlay_hint_refresh.py
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,
),
)
Loading