| 1 |
#pragma once |
| 2 |
#include <LibLsp/lsp/general/lsClientCapabilities.h> |
| 3 |
#include <LibLsp/lsp/utils.h> |
| 4 |
#include <memory> |
| 5 |
#include <vector> |
| 6 |
#include <string> |
| 7 |
|
| 8 |
class ClientPreferences |
| 9 |
{ |
| 10 |
public: |
| 11 |
std::shared_ptr<lsWorkspaceClientCapabilites> workspace; |
| 12 |
lsTextDocumentClientCapabilities textDocument; |
| 13 |
|
| 14 |
ClientPreferences(lsClientCapabilities const& capabilities) |
| 15 |
{ |
| 16 |
v3supported = capabilities.textDocument.has_value(); |
| 17 |
if (v3supported) |
| 18 |
{ |
| 19 |
textDocument = capabilities.textDocument.value(); |
| 20 |
} |
| 21 |
if (capabilities.workspace) |
| 22 |
{ |
| 23 |
workspace = std::make_shared<lsWorkspaceClientCapabilites>(capabilities.workspace.value()); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
bool v3supported = false; |
| 28 |
|
| 29 |
bool isSignatureHelpSupported() |
| 30 |
{ |
| 31 |
|
| 32 |
return v3supported && (textDocument.signatureHelp); |
| 33 |
} |
| 34 |
bool isWorkspaceDidChangeConfigurationSupported() const |
| 35 |
{ |
| 36 |
return workspace && isDynamicRegistrationSupported(workspace->didChangeConfiguration); |
| 37 |
} |
| 38 |
bool isWorkspaceFoldersSupported() |
| 39 |
{ |
| 40 |
return workspace != nullptr && isTrue(workspace->workspaceFolders); |
| 41 |
} |
| 42 |
|
| 43 |
bool isCompletionDynamicRegistered() |
| 44 |
{ |
| 45 |
return v3supported && isDynamicRegistrationSupported(textDocument.completion); |
| 46 |
} |
| 47 |
|
| 48 |
bool isCompletionSnippetsSupported() |
| 49 |
{ |
| 50 |
//@formatter:off |
| 51 |
if (!v3supported || !textDocument.completion) |
| 52 |
{ |
| 53 |
return false; |
| 54 |
} |
| 55 |
auto const& completion = textDocument.completion.value(); |
| 56 |
if (completion.completionItem) |
| 57 |
{ |
| 58 |
return isTrue(completion.completionItem.value().snippetSupport); |
| 59 |
} |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
bool isV3Supported() |
| 64 |
{ |
| 65 |
return v3supported; |
| 66 |
} |
| 67 |
|
| 68 |
bool isFormattingDynamicRegistrationSupported() |
| 69 |
{ |
| 70 |
return v3supported && isDynamicRegistrationSupported(textDocument.formatting); |
| 71 |
} |
| 72 |
|
| 73 |
bool isRangeFormattingDynamicRegistrationSupported() |
| 74 |
{ |
| 75 |
return v3supported && isDynamicRegistrationSupported(textDocument.rangeFormatting); |
| 76 |
} |
| 77 |
|
| 78 |
bool isOnTypeFormattingDynamicRegistrationSupported() |
| 79 |
{ |
| 80 |
return v3supported && isDynamicRegistrationSupported(textDocument.onTypeFormatting); |
| 81 |
} |
| 82 |
|
| 83 |
bool isCodeLensDynamicRegistrationSupported() |
| 84 |
{ |
| 85 |
return v3supported && isDynamicRegistrationSupported(textDocument.codeLens); |
| 86 |
} |
| 87 |
|
| 88 |
bool isSignatureHelpDynamicRegistrationSupported() |
| 89 |
{ |
| 90 |
return v3supported && isDynamicRegistrationSupported(textDocument.signatureHelp); |
| 91 |
} |
| 92 |
template<typename T> |
| 93 |
static bool isDynamicRegistrationSupported(optional<T>& capability) |
| 94 |
{ |
| 95 |
if (capability) |
| 96 |
{ |
| 97 |
return (capability.value().dynamicRegistration.value()); |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
bool isTrue(optional<bool> const& value) |
| 103 |
{ |
| 104 |
if (value) |
| 105 |
{ |
| 106 |
return *value; |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
return false; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
bool isRenameDynamicRegistrationSupported() |
| 115 |
{ |
| 116 |
return v3supported && isDynamicRegistrationSupported(textDocument.rename); |
| 117 |
} |
| 118 |
|
| 119 |
bool isExecuteCommandDynamicRegistrationSupported() |
| 120 |
{ |
| 121 |
return v3supported && workspace != nullptr && isDynamicRegistrationSupported(workspace->executeCommand); |
| 122 |
} |
| 123 |
|
| 124 |
bool isWorkspaceSymbolDynamicRegistered() |
| 125 |
{ |
| 126 |
return v3supported && workspace != nullptr && isDynamicRegistrationSupported(workspace->symbol); |
| 127 |
} |
| 128 |
|
| 129 |
bool isWorkspaceChangeWatchedFilesDynamicRegistered() |
| 130 |
{ |
| 131 |
return v3supported && workspace != nullptr && isDynamicRegistrationSupported(workspace->didChangeWatchedFiles); |
| 132 |
} |
| 133 |
|
| 134 |
bool isDocumentSymbolDynamicRegistered() |
| 135 |
{ |
| 136 |
return v3supported && isDynamicRegistrationSupported(textDocument.documentSymbol); |
| 137 |
} |
| 138 |
|
| 139 |
bool isCodeActionDynamicRegistered() |
| 140 |
{ |
| 141 |
return v3supported && isDynamicRegistrationSupported(textDocument.codeAction); |
| 142 |
} |
| 143 |
|
| 144 |
bool isDefinitionDynamicRegistered() |
| 145 |
{ |
| 146 |
return v3supported && isDynamicRegistrationSupported(textDocument.definition); |
| 147 |
} |
| 148 |
|
| 149 |
bool isTypeDefinitionDynamicRegistered() |
| 150 |
{ |
| 151 |
return v3supported && isDynamicRegistrationSupported(textDocument.typeDefinition); |
| 152 |
} |
| 153 |
|
| 154 |
bool isHoverDynamicRegistered() |
| 155 |
{ |
| 156 |
return v3supported && isDynamicRegistrationSupported(textDocument.hover); |
| 157 |
} |
| 158 |
|
| 159 |
bool isReferencesDynamicRegistered() |
| 160 |
{ |
| 161 |
return v3supported && isDynamicRegistrationSupported(textDocument.references); |
| 162 |
} |
| 163 |
|
| 164 |
bool isDocumentHighlightDynamicRegistered() |
| 165 |
{ |
| 166 |
return v3supported && isDynamicRegistrationSupported(textDocument.documentHighlight); |
| 167 |
} |
| 168 |
|
| 169 |
bool isDocumentLinkDynamicRegistered() |
| 170 |
{ |
| 171 |
return v3supported && isDynamicRegistrationSupported(textDocument.documentLink); |
| 172 |
} |
| 173 |
|
| 174 |
bool isFoldgingRangeDynamicRegistered() |
| 175 |
{ |
| 176 |
return v3supported && isDynamicRegistrationSupported(textDocument.foldingRange); |
| 177 |
} |
| 178 |
|
| 179 |
bool isInlayHintDynamicRegistered() |
| 180 |
{ |
| 181 |
return v3supported && isDynamicRegistrationSupported(textDocument.inlayHint); |
| 182 |
} |
| 183 |
|
| 184 |
bool isImplementationDynamicRegistered() |
| 185 |
{ |
| 186 |
return v3supported && isDynamicRegistrationSupported(textDocument.implementation); |
| 187 |
} |
| 188 |
|
| 189 |
bool isSelectionRangeDynamicRegistered() |
| 190 |
{ |
| 191 |
return v3supported && isDynamicRegistrationSupported(textDocument.selectionRange); |
| 192 |
} |
| 193 |
|
| 194 |
bool isWillSaveRegistered() |
| 195 |
{ |
| 196 |
return v3supported && isTrue(textDocument.synchronization.willSave); |
| 197 |
} |
| 198 |
|
| 199 |
bool isWillSaveWaitUntilRegistered() |
| 200 |
{ |
| 201 |
return v3supported && isTrue(textDocument.synchronization.willSaveWaitUntil); |
| 202 |
} |
| 203 |
|
| 204 |
bool isWorkspaceApplyEditSupported() |
| 205 |
{ |
| 206 |
return workspace != nullptr && isTrue(workspace->applyEdit); |
| 207 |
} |
| 208 |
|
| 209 |
bool isSupportsCompletionDocumentationMarkdown() |
| 210 |
{ |
| 211 |
|
| 212 |
if (!v3supported || !textDocument.completion) |
| 213 |
{ |
| 214 |
return false; |
| 215 |
} |
| 216 |
auto const& completion = textDocument.completion.value(); |
| 217 |
if (completion.completionItem) |
| 218 |
{ |
| 219 |
auto& documentationFormat = completion.completionItem.value().documentationFormat; |
| 220 |
if (documentationFormat) |
| 221 |
{ |
| 222 |
auto& data = documentationFormat.value(); |
| 223 |
for (auto& it : data) |
| 224 |
{ |
| 225 |
if (it == "markdown") |
| 226 |
{ |
| 227 |
return true; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
bool isWorkspaceEditResourceChangesSupported() |
| 236 |
{ |
| 237 |
if (!workspace) |
| 238 |
{ |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
if (workspace->workspaceEdit) |
| 243 |
{ |
| 244 |
return isTrue(workspace->workspaceEdit.value().resourceChanges); |
| 245 |
} |
| 246 |
return false; |
| 247 |
} |
| 248 |
static bool contains(std::vector<std::string> const& v, std::string const& target) |
| 249 |
{ |
| 250 |
for (auto& it : v) |
| 251 |
{ |
| 252 |
if (it == target) |
| 253 |
{ |
| 254 |
return true; |
| 255 |
} |
| 256 |
} |
| 257 |
return false; |
| 258 |
} |
| 259 |
bool isResourceOperationSupported() const |
| 260 |
{ |
| 261 |
if (!workspace) |
| 262 |
{ |
| 263 |
return false; |
| 264 |
} |
| 265 |
if (!workspace->workspaceEdit) |
| 266 |
{ |
| 267 |
return false; |
| 268 |
} |
| 269 |
auto& it = (workspace->workspaceEdit.value()); |
| 270 |
if (!it.resourceOperations) |
| 271 |
{ |
| 272 |
return false; |
| 273 |
} |
| 274 |
auto const& resourceOperations = it.resourceOperations.value(); |
| 275 |
return contains(resourceOperations, "create") && contains(resourceOperations, "rename") |
| 276 |
&& contains(resourceOperations, "delete"); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* {@code true} if the client has explicitly set the |
| 281 |
* {@code textDocument.documentSymbol.hierarchicalDocumentSymbolSupport} to |
| 282 |
* {@code true} when initializing the LS. Otherwise, {@code false}. |
| 283 |
*/ |
| 284 |
bool isHierarchicalDocumentSymbolSupported() |
| 285 |
{ |
| 286 |
if (!v3supported || !textDocument.documentSymbol) |
| 287 |
{ |
| 288 |
return false; |
| 289 |
} |
| 290 |
return isTrue(textDocument.documentSymbol.value().hierarchicalDocumentSymbolSupport); |
| 291 |
} |
| 292 |
|
| 293 |
bool isSemanticHighlightingSupported() |
| 294 |
{ |
| 295 |
//@formatter:off |
| 296 |
if (!v3supported || !textDocument.semanticHighlightingCapabilities) |
| 297 |
{ |
| 298 |
return false; |
| 299 |
} |
| 300 |
return isTrue(textDocument.semanticHighlightingCapabilities.value().semanticHighlighting); |
| 301 |
//@formatter:on |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* {@code true} if the client has explicitly set the |
| 306 |
* {@code textDocument.codeAction.codeActionLiteralSupport.codeActionKind.valueSet} |
| 307 |
* value. Otherwise, {@code false}. |
| 308 |
*/ |
| 309 |
bool isSupportedCodeActionKind(std::string const& kind) |
| 310 |
{ |
| 311 |
if (!v3supported || !textDocument.codeAction) |
| 312 |
{ |
| 313 |
return false; |
| 314 |
} |
| 315 |
//@formatter:off |
| 316 |
auto const& codeAction = textDocument.codeAction.value(); |
| 317 |
if (codeAction.codeActionLiteralSupport) |
| 318 |
{ |
| 319 |
auto const& codeActionKind = codeAction.codeActionLiteralSupport.value().codeActionKind; |
| 320 |
if (codeActionKind) |
| 321 |
{ |
| 322 |
auto const& valueSet = codeActionKind.value().valueSet; |
| 323 |
if (valueSet) |
| 324 |
{ |
| 325 |
for (auto& k : valueSet.value()) |
| 326 |
{ |
| 327 |
if (lsp::StartsWith(kind, k)) |
| 328 |
{ |
| 329 |
return true; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
return false; |
| 336 |
|
| 337 |
//@formatter:on |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* {@code true} if the client has explicitly set the |
| 342 |
* {@code textDocument.publishDiagnostics.tagSupport} to |
| 343 |
* {@code true} when initializing the LS. Otherwise, {@code false}. |
| 344 |
*/ |
| 345 |
bool isDiagnosticTagSupported() |
| 346 |
{ |
| 347 |
if (!v3supported || !textDocument.publishDiagnostics) |
| 348 |
{ |
| 349 |
return false; |
| 350 |
} |
| 351 |
auto const& publishDiagnostics = textDocument.publishDiagnostics.value(); |
| 352 |
if (publishDiagnostics.tagSupport) |
| 353 |
{ |
| 354 |
isTagSupported(publishDiagnostics.tagSupport); |
| 355 |
} |
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
bool isTagSupported(optional<std::pair<optional<bool>, optional<DiagnosticsTagSupport>>> const& tagSupport) |
| 360 |
{ |
| 361 |
if (tagSupport) |
| 362 |
{ |
| 363 |
auto& v = tagSupport.value(); |
| 364 |
if (v.first) |
| 365 |
{ |
| 366 |
return v.first.value(); |
| 367 |
} |
| 368 |
if (v.second) |
| 369 |
{ |
| 370 |
return !v.second.value().valueSet.empty(); |
| 371 |
} |
| 372 |
} |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
bool isCallHierarchyDynamicRegistered() |
| 377 |
{ |
| 378 |
return v3supported && isDynamicRegistrationSupported(textDocument.callHierarchy); |
| 379 |
} |
| 380 |
}; |