|
| 1 | +# METADATA |
| 2 | +# description: | |
| 3 | +# handler for the LSP "initialize" request, which is the first request sent by the client to the server in order |
| 4 | +# to initialize the connection and establish the capabilities of both client and server |
| 5 | +# related_resources: |
| 6 | +# - https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize |
| 7 | +# schemas: |
| 8 | +# - input: schema.regal.lsp.common |
| 9 | +# - input.params: schema.regal.lsp.initialize |
| 10 | +# scope: subpackages |
| 11 | +package regal.lsp.initialize |
| 12 | + |
| 13 | +# METADATA |
| 14 | +# entrypoint: true |
| 15 | +result.response.serverInfo := _serverInfo |
| 16 | + |
| 17 | +_serverInfo.name := "Regal" |
| 18 | + |
| 19 | +default _serverInfo.version := "unknown" |
| 20 | + |
| 21 | +_serverInfo.version := input.regal.server.version |
| 22 | + |
| 23 | +# METADATA |
| 24 | +# description: The capabilities of Regal's language server, as defined in the LSP specification |
| 25 | +result.response.capabilities := _capabilities |
| 26 | + |
| 27 | +_capabilities.textDocumentSync := { |
| 28 | + "openClose": true, |
| 29 | + # For now, send full document on change, but this is something we should improve. |
| 30 | + # See https://github.com/open-policy-agent/regal/issues/1651 |
| 31 | + "change": 1, |
| 32 | + "save": {"includeText": true}, |
| 33 | +} |
| 34 | + |
| 35 | +_capabilities.diagnosticProvider := { |
| 36 | + "identifier": "rego", |
| 37 | + "interFileDependencies": true, |
| 38 | + "workspaceDiagnostics": true, |
| 39 | +} |
| 40 | + |
| 41 | +_capabilities.workspace.fileOperations[operation].filters := filters if { |
| 42 | + filters := [{ |
| 43 | + "scheme": "file", |
| 44 | + "pattern": {"glob": "**/*.rego"}, |
| 45 | + }] |
| 46 | + some operation in ["didCreate", "didRename", "didDelete"] |
| 47 | +} |
| 48 | + |
| 49 | +## NOTE(anders): The language server protocol doesn't go into detail about what this is meant to |
| 50 | +## entail, and there's nothing else in the request/response payloads that carry workspace folder |
| 51 | +## information. The best source I've found on the this topic is this example repo from VS Code, |
| 52 | +## where they have the client start one instance of the server per workspace folder: |
| 53 | +## https://github.com/microsoft/vscode-extension-samples/tree/main/lsp-multi-server-sample |
| 54 | +## That seems like a reasonable approach to take, and means we won't have to deal with workspace |
| 55 | +## folders throughout the rest of the codebase. But the question then is — what is the point of |
| 56 | +## this capability, and what does it mean to say we support it? Clearly we don't in the server as |
| 57 | +## *there is no way* to support it here. |
| 58 | +_capabilities.workspace.workspaceFolders.supported := true |
| 59 | + |
| 60 | +_capabilities.inlayHintProvider := { |
| 61 | + # inlayHint/resolve request supported |
| 62 | + "resolveProvider": true, |
| 63 | +} |
| 64 | + |
| 65 | +_capabilities.hoverProvider := true |
| 66 | + |
| 67 | +_capabilities.signatureHelpProvider := { |
| 68 | + # In additional to the client's default trigger characters for signature help |
| 69 | + "triggerCharacters": ["(", ","] |
| 70 | +} |
| 71 | + |
| 72 | +_capabilities.codeActionProvider := { |
| 73 | + # Currently supported code action kinds |
| 74 | + "codeActionKinds": [ |
| 75 | + "quickfix", |
| 76 | + "source" |
| 77 | + ], |
| 78 | +} |
| 79 | + |
| 80 | +_capabilities.executeCommandProvider.commands := _commands |
| 81 | + |
| 82 | +_commands contains "regal.eval" |
| 83 | +_commands contains "regal.fix.opa-fmt" |
| 84 | +_commands contains "regal.fix.use-rego-v1" |
| 85 | +_commands contains "regal.fix.use-assignment-operator" |
| 86 | +_commands contains "regal.fix.no-whitespace-comment" |
| 87 | +_commands contains "regal.fix.directory-package-mismatch" |
| 88 | +_commands contains "regal.fix.non-raw-regex-pattern" |
| 89 | +_commands contains "regal.fix.prefer-equals-comparison" |
| 90 | +_commands contains "regal.fix.constant-condition" |
| 91 | +_commands contains "regal.fix.redundant-existence-check" |
| 92 | +_commands contains "regal.config.disable-rule" |
| 93 | +_commands contains "regal.explorer" if input.regal.server.feature_flags.explorer_provider |
| 94 | +_commands contains "regal.debug" if input.regal.server.feature_flags.debug_provider |
| 95 | + |
| 96 | +_capabilities.documentFormattingProvider := true |
| 97 | + |
| 98 | +_capabilities.foldingRangeProvider := true |
| 99 | + |
| 100 | +_capabilities.definitionProvider := true |
| 101 | + |
| 102 | +_capabilities.documentSymbolProvider := true |
| 103 | + |
| 104 | +_capabilities.workspaceSymbolProvider := true |
| 105 | + |
| 106 | +_capabilities.completionProvider := { |
| 107 | + "triggerCharacters": [ |
| 108 | + ":", # to suggest := |
| 109 | + ".", # for refs |
| 110 | + ], |
| 111 | + "resolveProvider": true, |
| 112 | + "completionItem": {"labelDetailsSupport": true}, |
| 113 | +} |
| 114 | + |
| 115 | +_capabilities.codeLensProvider := { |
| 116 | + # codeLens/resolve to be implemented |
| 117 | + "resolveProvider": false, |
| 118 | +} |
| 119 | + |
| 120 | +_capabilities.documentLinkProvider := { |
| 121 | + # documentLink/resolve to be implemented |
| 122 | + "resolveProvider": false, |
| 123 | +} |
| 124 | + |
| 125 | +_capabilities.documentHighlightProvider := true |
| 126 | + |
| 127 | +_capabilities.selectionRangeProvider := true |
| 128 | + |
| 129 | +_capabilities.linkedEditingRangeProvider := true |
| 130 | + |
| 131 | +_capabilities.semanticTokensProvider := { |
| 132 | + "legend": { |
| 133 | + "tokenTypes": [ |
| 134 | + "namespace", |
| 135 | + "variable", |
| 136 | + "import", |
| 137 | + "keyword", |
| 138 | + ], |
| 139 | + "tokenModifiers": [ |
| 140 | + "declaration", |
| 141 | + "definition", |
| 142 | + "reference", |
| 143 | + ], |
| 144 | + }, |
| 145 | + "full": true, |
| 146 | +} |
| 147 | + |
| 148 | +# Note: 'experimental' is LSP terminology. We are using these to mean |
| 149 | +# custom additions that are ready for use, but not in the base spec. |
| 150 | +_capabilities.experimental.explorerProvider := input.regal.server.feature_flags.explorer_provider |
| 151 | +_capabilities.experimental.inlineEvalProvider := input.regal.server.feature_flags.inline_evaluation_provider |
| 152 | +_capabilities.experimental.debugProvider := input.regal.server.feature_flags.debug_provider |
| 153 | +_capabilities.experimental.opaTestProvider := input.regal.server.feature_flags.opa_test_provider |
| 154 | + |
| 155 | +# METADATA |
| 156 | +# description: The server's identifier for the client, based on the clientInfo sent in the request |
| 157 | +# scope: document |
| 158 | +default result.regal.client.identifier := 0 |
| 159 | + |
| 160 | +result.regal.client.identifier := _client_identifier(input.params.clientInfo.name) |
| 161 | + |
| 162 | +_client_identifier("Visual Studio Code") := 1 |
| 163 | +_client_identifier("go test") := 2 |
| 164 | +_client_identifier("Zed") := 3 |
| 165 | +_client_identifier("Neovim") := 4 |
| 166 | +_client_identifier(name) := 5 if contains(name, "IntelliJ") |
| 167 | + |
| 168 | +# METADATA |
| 169 | +# description: The initialization options sent by the client, or an empty object if not provided |
| 170 | +# scope: document |
| 171 | +default result.regal.client.initializationOptions := {} |
| 172 | + |
| 173 | +result.regal.client.initializationOptions := input.params.initializationOptions |
| 174 | + |
| 175 | +# METADATA |
| 176 | +# description: The capabilities of the client, as sent in the initialize request |
| 177 | +# scope: document |
| 178 | +result.regal.client.capabilities := input.params.capabilities |
| 179 | + |
| 180 | +# METADATA |
| 181 | +# description: The root URI of the workspace, as provided by the client |
| 182 | +result.regal.workspace.uri := input.params.rootUri if { |
| 183 | + input.params.rootUri != "" |
| 184 | +} else := input.params.workspaceFolders[0].uri |
| 185 | + |
| 186 | +# METADATA |
| 187 | +# description: Any warnings to log from initialization |
| 188 | +# scope: document |
| 189 | +result.regal.warnings contains $"multiple workspace folders provided, only the first one will be used: {uri}" if { |
| 190 | + count(input.params.workspaceFolders) > 1 |
| 191 | + |
| 192 | + uri := input.params.workspaceFolders[0].uri |
| 193 | +} |
0 commit comments