|
| 1 | +import uuid |
1 | 2 | from typing import List, Optional |
2 | 3 |
|
3 | 4 | from ..lsp_types import ( |
4 | 5 | URI, |
5 | 6 | LogMessageParams, |
6 | 7 | MessageActionItem, |
7 | 8 | MessageType, |
| 9 | + ProgressParams, |
| 10 | + ProgressToken, |
8 | 11 | Range, |
9 | 12 | ShowDocumentParams, |
10 | 13 | ShowDocumentResult, |
11 | 14 | ShowMessageParams, |
12 | 15 | ShowMessageRequestParams, |
| 16 | + WorkDoneProgressBase, |
| 17 | + WorkDoneProgressBegin, |
| 18 | + WorkDoneProgressCancelParams, |
| 19 | + WorkDoneProgressCreateParams, |
| 20 | + WorkDoneProgressEnd, |
| 21 | + WorkDoneProgressReport, |
13 | 22 | ) |
14 | 23 | from .protocol_part import LanguageServerProtocolPart |
15 | 24 |
|
@@ -44,3 +53,91 @@ async def show_document( |
44 | 53 | ShowDocumentResult, |
45 | 54 | ) |
46 | 55 | ).success |
| 56 | + |
| 57 | + async def create_progress(self) -> Optional[ProgressToken]: |
| 58 | + |
| 59 | + if ( |
| 60 | + self.parent.client_capabilities |
| 61 | + and self.parent.client_capabilities.window |
| 62 | + and self.parent.client_capabilities.window.work_done_progress |
| 63 | + ): |
| 64 | + token = str(uuid.uuid4()) |
| 65 | + await self.parent.send_request_async("window/workDoneProgress/create", WorkDoneProgressCreateParams(token)) |
| 66 | + return token |
| 67 | + |
| 68 | + return None |
| 69 | + |
| 70 | + def progress_cancel(self, token: Optional[ProgressToken]) -> None: |
| 71 | + if ( |
| 72 | + token is not None |
| 73 | + and self.parent.client_capabilities |
| 74 | + and self.parent.client_capabilities.window |
| 75 | + and self.parent.client_capabilities.window.work_done_progress |
| 76 | + ): |
| 77 | + self.parent.send_notification("window/workDoneProgress/cancel", WorkDoneProgressCancelParams(token)) |
| 78 | + |
| 79 | + def _progress(self, token: Optional[ProgressToken], value: WorkDoneProgressBase) -> None: |
| 80 | + if ( |
| 81 | + token is not None |
| 82 | + and self.parent.client_capabilities |
| 83 | + and self.parent.client_capabilities.window |
| 84 | + and self.parent.client_capabilities.window.work_done_progress |
| 85 | + ): |
| 86 | + self.parent.send_notification("$/progress", ProgressParams(token, value)) |
| 87 | + |
| 88 | + _default_title = "Dummy" |
| 89 | + |
| 90 | + def progress_begin( |
| 91 | + self, |
| 92 | + token: Optional[ProgressToken], |
| 93 | + message: Optional[str] = None, |
| 94 | + percentage: Optional[int] = None, |
| 95 | + cancellable: Optional[bool] = None, |
| 96 | + title: Optional[str] = None, |
| 97 | + ) -> None: |
| 98 | + if ( |
| 99 | + token is not None |
| 100 | + and self.parent.client_capabilities |
| 101 | + and self.parent.client_capabilities.window |
| 102 | + and self.parent.client_capabilities.window.work_done_progress |
| 103 | + ): |
| 104 | + self._progress( |
| 105 | + token, |
| 106 | + WorkDoneProgressBegin( |
| 107 | + title or self.parent.name or self._default_title, message, percentage, cancellable |
| 108 | + ), |
| 109 | + ) |
| 110 | + |
| 111 | + def progress_report( |
| 112 | + self, |
| 113 | + token: Optional[ProgressToken], |
| 114 | + message: Optional[str] = None, |
| 115 | + percentage: Optional[int] = None, |
| 116 | + cancellable: Optional[bool] = None, |
| 117 | + title: Optional[str] = None, |
| 118 | + ) -> None: |
| 119 | + if ( |
| 120 | + token is not None |
| 121 | + and self.parent.client_capabilities |
| 122 | + and self.parent.client_capabilities.window |
| 123 | + and self.parent.client_capabilities.window.work_done_progress |
| 124 | + ): |
| 125 | + self._progress( |
| 126 | + token, |
| 127 | + WorkDoneProgressReport( |
| 128 | + title or self.parent.name or self._default_title, message, percentage, cancellable |
| 129 | + ), |
| 130 | + ) |
| 131 | + |
| 132 | + def progress_end( |
| 133 | + self, |
| 134 | + token: Optional[ProgressToken], |
| 135 | + message: Optional[str] = None, |
| 136 | + ) -> None: |
| 137 | + if ( |
| 138 | + token is not None |
| 139 | + and self.parent.client_capabilities |
| 140 | + and self.parent.client_capabilities.window |
| 141 | + and self.parent.client_capabilities.window.work_done_progress |
| 142 | + ): |
| 143 | + self._progress(token, WorkDoneProgressEnd(message)) |
0 commit comments