|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Sequence |
| 4 | +from typing import Protocol, override, runtime_checkable |
| 5 | + |
| 6 | +import asyncer |
| 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.type_guard import is_completion_items |
| 14 | +from lsp_client.utils.types import AnyPath, Position, lsp_type |
| 15 | + |
| 16 | + |
| 17 | +@runtime_checkable |
| 18 | +class WithRequestCompletion( |
| 19 | + TextDocumentCapabilityProtocol, |
| 20 | + CapabilityClientProtocol, |
| 21 | + Protocol, |
| 22 | +): |
| 23 | + """ |
| 24 | + - `textDocument/completion` - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion |
| 25 | + - `completionItem/resolve` - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItem_resolve |
| 26 | + """ |
| 27 | + |
| 28 | + @override |
| 29 | + @classmethod |
| 30 | + def methods(cls) -> Sequence[str]: |
| 31 | + return ( |
| 32 | + lsp_type.TEXT_DOCUMENT_COMPLETION, |
| 33 | + lsp_type.COMPLETION_ITEM_RESOLVE, |
| 34 | + ) |
| 35 | + |
| 36 | + @override |
| 37 | + @classmethod |
| 38 | + def register_text_document_capability( |
| 39 | + cls, cap: lsp_type.TextDocumentClientCapabilities |
| 40 | + ) -> None: |
| 41 | + cap.completion = lsp_type.CompletionClientCapabilities( |
| 42 | + completion_item=lsp_type.ClientCompletionItemOptions( |
| 43 | + snippet_support=True, |
| 44 | + resolve_support=lsp_type.ClientCompletionItemResolveOptions( |
| 45 | + properties=["documentation", "additionalTextEdits"] |
| 46 | + ), |
| 47 | + ), |
| 48 | + context_support=True, |
| 49 | + ) |
| 50 | + |
| 51 | + @override |
| 52 | + @classmethod |
| 53 | + def check_server_capability( |
| 54 | + cls, |
| 55 | + cap: lsp_type.ServerCapabilities, |
| 56 | + info: lsp_type.ServerInfo | None, |
| 57 | + ) -> None: |
| 58 | + super().check_server_capability(cap, info) |
| 59 | + assert cap.completion_provider |
| 60 | + |
| 61 | + async def request_completion( |
| 62 | + self, |
| 63 | + file_path: AnyPath, |
| 64 | + position: Position, |
| 65 | + *, |
| 66 | + trigger_character: str | None = None, |
| 67 | + trigger_kind: lsp_type.CompletionTriggerKind = lsp_type.CompletionTriggerKind.Invoked, |
| 68 | + resolve: bool = False, |
| 69 | + ) -> Sequence[lsp_type.CompletionItem]: |
| 70 | + context = lsp_type.CompletionContext( |
| 71 | + trigger_kind=trigger_kind, |
| 72 | + trigger_character=trigger_character, |
| 73 | + ) |
| 74 | + result = await self.file_request( |
| 75 | + lsp_type.CompletionRequest( |
| 76 | + id=jsonrpc_uuid(), |
| 77 | + params=lsp_type.CompletionParams( |
| 78 | + text_document=lsp_type.TextDocumentIdentifier( |
| 79 | + uri=self.as_uri(file_path) |
| 80 | + ), |
| 81 | + position=position, |
| 82 | + context=context, |
| 83 | + ), |
| 84 | + ), |
| 85 | + schema=lsp_type.CompletionResponse, |
| 86 | + file_paths=[file_path], |
| 87 | + ) |
| 88 | + |
| 89 | + match result: |
| 90 | + case lsp_type.CompletionList(items=items): |
| 91 | + res = list(items) |
| 92 | + case items if is_completion_items(items): |
| 93 | + res = list(items) |
| 94 | + case _: |
| 95 | + res = [] |
| 96 | + |
| 97 | + if resolve and res: |
| 98 | + return await self.resolve_completion_items(res) |
| 99 | + return res |
| 100 | + |
| 101 | + async def resolve_completion_items( |
| 102 | + self, |
| 103 | + items: Sequence[lsp_type.CompletionItem], |
| 104 | + ) -> Sequence[lsp_type.CompletionItem]: |
| 105 | + async with asyncer.create_task_group() as tg: |
| 106 | + tasks = [ |
| 107 | + tg.soonify(self.request_completion_resolve)(item) for item in items |
| 108 | + ] |
| 109 | + return [task.value for task in tasks] |
| 110 | + |
| 111 | + async def request_completion_resolve( |
| 112 | + self, |
| 113 | + item: lsp_type.CompletionItem, |
| 114 | + ) -> lsp_type.CompletionItem: |
| 115 | + return await self.request( |
| 116 | + lsp_type.CompletionResolveRequest( |
| 117 | + id=jsonrpc_uuid(), |
| 118 | + params=item, |
| 119 | + ), |
| 120 | + schema=lsp_type.CompletionResolveResponse, |
| 121 | + ) |
0 commit comments