2 lsp_ext.rs hash: 28a9d5a24b7ca396
4 If you need to change the above hash to make the test pass, please check if you
5 need to adjust this doc as well and ping this issue:
7 https://github.com/rust-analyzer/rust-analyzer/issues/4604
13 This document describes LSP extensions used by rust-analyzer.
14 It's a best effort document, when in doubt, consult the source (and send a PR with clarification ;-) ).
15 We aim to upstream all non Rust-specific extensions to the protocol, but this is not a top priority.
16 All capabilities are enabled via `experimental` field of `ClientCapabilities` or `ServerCapabilities`.
17 Requests which we hope to upstream live under `experimental/` namespace.
18 Requests, which are likely to always remain specific to `rust-analyzer` are under `rust-analyzer/` namespace.
20 If you want to be notified about the changes to this document, subscribe to [#4604](https://github.com/rust-analyzer/rust-analyzer/issues/4604).
24 rust-analyzer supports clangd's extension for opting into UTF-8 as the coordinate space for offsets (by default, LSP uses UTF-16 offsets).
26 https://clangd.llvm.org/extensions.html#utf-8-offsets
28 ## `initializationOptions`
30 For `initializationOptions`, `rust-analyzer` expects `"rust-analyzer"` section of the configuration.
31 That is, `rust-analyzer` usually sends `"workspace/configuration"` request with `{ "items": ["rust-analyzer"] }` payload.
32 `initializationOptions` should contain the same data that would be in the first item of the result.
33 If a language client does not know about `rust-analyzer`'s configuration options it can get sensible defaults by doing any of the following:
34 * Not sending `initializationOptions`
35 * Send `"initializationOptions": null`
36 * Send `"initializationOptions": {}`
40 **Issue:** https://github.com/microsoft/language-server-protocol/issues/724
42 **Experimental Client Capability:** `{ "snippetTextEdit": boolean }`
44 If this capability is set, `WorkspaceEdit`s returned from `codeAction` requests might contain `SnippetTextEdit`s instead of usual `TextEdit`s:
47 interface SnippetTextEdit extends TextEdit {
48 insertTextFormat?: InsertTextFormat;
49 annotationId?: ChangeAnnotationIdentifier;
54 export interface TextDocumentEdit {
55 textDocument: OptionalVersionedTextDocumentIdentifier;
56 edits: (TextEdit | SnippetTextEdit)[];
60 When applying such code action, the editor should insert snippet, with tab stops and placeholder.
61 At the moment, rust-analyzer guarantees that only a single edit will have `InsertTextFormat.Snippet`.
65 "Add `derive`" code action transforms `struct S;` into `#[derive($0)] struct S;`
67 ### Unresolved Questions
69 * Where exactly are `SnippetTextEdit`s allowed (only in code actions at the moment)?
70 * Can snippets span multiple files (so far, no)?
72 ## `CodeAction` Groups
74 **Issue:** https://github.com/microsoft/language-server-protocol/issues/994
76 **Experimental Client Capability:** `{ "codeActionGroup": boolean }`
78 If this capability is set, `CodeAction` returned from the server contain an additional field, `group`:
81 interface CodeAction {
88 All code-actions with the same `group` should be grouped under single (extendable) entry in lightbulb menu.
89 The set of actions `[ { title: "foo" }, { group: "frobnicate", title: "bar" }, { group: "frobnicate", title: "baz" }]` should be rendered as
102 Alternatively, selecting `frobnicate` could present a user with an additional menu to choose between `bar` and `baz`.
108 let x: Entry/*cursor here*/ = todo!();
112 Invoking code action at this position will yield two code actions for importing `Entry` from either `collections::HashMap` or `collection::BTreeMap`, grouped under a single "import" group.
114 ### Unresolved Questions
116 * Is a fixed two-level structure enough?
117 * Should we devise a general way to encode custom interaction protocols for GUI refactorings?
121 **Issue:** https://github.com/microsoft/language-server-protocol/issues/1002
123 **Experimental Server Capability:** `{ "parentModule": boolean }`
125 This request is sent from client to server to handle "Goto Parent Module" editor action.
127 **Method:** `experimental/parentModule`
129 **Request:** `TextDocumentPositionParams`
131 **Response:** `Location | Location[] | LocationLink[] | null`
144 `experimental/parentModule` returns a single `Link` to the `mod foo;` declaration.
146 ### Unresolved Question
148 * An alternative would be to use a more general "gotoSuper" request, which would work for super methods, super classes and super modules.
149 This is the approach IntelliJ Rust is taking.
150 However, experience shows that super module (which generally has a feeling of navigation between files) should be separate.
151 If you want super module, but the cursor happens to be inside an overridden function, the behavior with single "gotoSuper" request is surprising.
155 **Issue:** https://github.com/microsoft/language-server-protocol/issues/992
157 **Experimental Server Capability:** `{ "joinLines": boolean }`
159 This request is sent from client to server to handle "Join Lines" editor action.
161 **Method:** `experimental/joinLines`
166 interface JoinLinesParams {
167 textDocument: TextDocumentIdentifier,
168 /// Currently active selections/cursor offsets.
169 /// This is an array to support multiple cursors.
174 **Response:** `TextEdit[]`
180 /*cursor here*/let x = {
186 `experimental/joinLines` yields (curly braces are automagically removed)
194 ### Unresolved Question
196 * What is the position of the cursor after `joinLines`?
197 Currently, this is left to editor's discretion, but it might be useful to specify on the server via snippets.
198 However, it then becomes unclear how it works with multi cursor.
202 **Issue:** https://github.com/microsoft/language-server-protocol/issues/1001
204 **Experimental Server Capability:** `{ "onEnter": boolean }`
206 This request is sent from client to server to handle <kbd>Enter</kbd> keypress.
208 **Method:** `experimental/onEnter`
210 **Request:**: `TextDocumentPositionParams`
222 // Some /*cursor here*/ docs
227 `experimental/onEnter` returns the following snippet
237 The primary goal of `onEnter` is to handle automatic indentation when opening a new line.
238 This is not yet implemented.
239 The secondary goal is to handle fixing up syntax, like continuing doc strings and comments, and escaping `\n` in string literals.
241 As proper cursor positioning is raison-d'etat for `onEnter`, it uses `SnippetTextEdit`.
243 ### Unresolved Question
245 * How to deal with synchronicity of the request?
246 One option is to require the client to block until the server returns the response.
247 Another option is to do a OT-style merging of edits from client and server.
248 A third option is to do a record-replay: client applies heuristic on enter immediately, then applies all user's keypresses.
249 When the server is ready with the response, the client rollbacks all the changes and applies the recorded actions on top of the correct response.
250 * How to deal with multiple carets?
251 * Should we extend this to arbitrary typed events and not just `onEnter`?
253 ## Structural Search Replace (SSR)
255 **Experimental Server Capability:** `{ "ssr": boolean }`
257 This request is sent from client to server to handle structural search replace -- automated syntax tree based transformation of the source.
259 **Method:** `experimental/ssr`
264 interface SsrParams {
266 /// The specific syntax is specified outside of the protocol.
268 /// If true, only check the syntax of the query and don't compute the actual edit.
270 /// The current text document. This and `position` will be used to determine in what scope
271 /// paths in `query` should be resolved.
272 textDocument: lc.TextDocumentIdentifier;
273 /// Position where SSR was invoked.
274 position: lc.Position;
286 SSR with query `foo($a, $b) ==>> ($a).foo($b)` will transform, eg `foo(y + 5, z)` into `(y + 5).foo(z)`.
288 ### Unresolved Question
290 * Probably needs search without replace mode
291 * Needs a way to limit the scope to certain files.
295 **Issue:** https://github.com/microsoft/language-server-protocol/issues/999
297 **Experimental Server Capability:** `{ "matchingBrace": boolean }`
299 This request is sent from client to server to handle "Matching Brace" editor action.
301 **Method:** `experimental/matchingBrace`
306 interface MatchingBraceParams {
307 textDocument: TextDocumentIdentifier,
308 /// Position for each cursor
309 positions: Position[],
323 let x: Vec<()>/*cursor here*/ = vec![]
327 `experimental/matchingBrace` yields the position of `<`.
328 In many cases, matching braces can be handled by the editor.
329 However, some cases (like disambiguating between generics and comparison operations) need a real parser.
330 Moreover, it would be cool if editors didn't need to implement even basic language parsing
332 ### Unresolved Question
334 * Should we return a nested brace structure, to allow paredit-like actions of jump *out* of the current brace pair?
335 This is how `SelectionRange` request works.
336 * Alternatively, should we perhaps flag certain `SelectionRange`s as being brace pairs?
340 **Issue:** https://github.com/microsoft/language-server-protocol/issues/944
342 **Experimental Server Capability:** `{ "runnables": { "kinds": string[] } }`
344 This request is sent from client to server to get the list of things that can be run (tests, binaries, `cargo check -p`).
346 **Method:** `experimental/runnables`
351 interface RunnablesParams {
352 textDocument: TextDocumentIdentifier;
353 /// If null, compute runnables for the whole file.
358 **Response:** `Runnable[]`
363 /// If this Runnable is associated with a specific function/module, etc, the location of this item
364 location?: LocationLink;
365 /// Running things is necessary technology specific, `kind` needs to be advertised via server capabilities,
366 // the type of `args` is specific to `kind`. The actual running is handled by the client.
372 rust-analyzer supports only one `kind`, `"cargo"`. The `args` for `"cargo"` look like this:
376 workspaceRoot?: string;
378 cargoExtraArgs: string[];
379 executableArgs: string[];
380 expectTest?: boolean;
381 overrideCargo?: string;
385 ## Open External Documentation
387 This request is sent from client to server to get a URL to documentation for the symbol under the cursor, if available.
389 **Method** `experimental/externalDocs`
391 **Request:**: `TextDocumentPositionParams`
393 **Response** `string | null`
398 **Method:** `rust-analyzer/analyzerStatus`
403 interface AnalyzerStatusParams {
404 /// If specified, show dependencies of the current file.
405 textDocument?: TextDocumentIdentifier;
409 **Response:** `string`
411 Returns internal status message, mostly for debugging purposes.
415 **Method:** `rust-analyzer/reloadWorkspace`
421 Reloads project information (that is, re-executes `cargo metadata`).
425 **Experimental Client Capability:** `{ "serverStatusNotification": boolean }`
427 **Method:** `experimental/serverStatus`
432 interface ServerStatusParams {
433 /// `ok` means that the server is completely functional.
435 /// `warning` means that the server is partially functional.
436 /// It can answer correctly to most requests, but some results
437 /// might be wrong due to, for example, some missing dependencies.
439 /// `error` means that the server is not functional. For example,
440 /// there's a fatal build configuration problem. The server might
441 /// still give correct answers to simple requests, but most results
442 /// will be incomplete or wrong.
443 health: "ok" | "warning" | "error",
444 /// Is there any pending background work which might change the status?
445 /// For example, are dependencies being downloaded?
447 /// Explanatory message to show on hover.
452 This notification is sent from server to client.
453 The client can use it to display *persistent* status to the user (in modline).
454 It is similar to the `showMessage`, but is intended for stares rather than point-in-time events.
456 Note that this functionality is intended primarily to inform the end user about the state of the server.
457 In particular, it's valid for the client to completely ignore this extension.
458 Clients are discouraged from but are allowed to use the `health` status to decide if it's worth sending a request to the server.
462 **Method:** `rust-analyzer/syntaxTree`
467 interface SyntaxTeeParams {
468 textDocument: TextDocumentIdentifier,
473 **Response:** `string`
475 Returns textual representation of a parse tree for the file/selected region.
476 Primarily for debugging, but very useful for all people working on rust-analyzer itself.
480 **Method:** `rust-analyzer/viewHir`
482 **Request:** `TextDocumentPositionParams`
484 **Response:** `string`
486 Returns a textual representation of the HIR of the function containing the cursor.
487 For debugging or when working on rust-analyzer itself.
491 **Method:** `rust-analyzer/expandMacro`
496 interface ExpandMacroParams {
497 textDocument: TextDocumentIdentifier,
505 interface ExpandedMacro {
511 Expands macro call at a given position.
515 **Method:** `rust-analyzer/inlayHints`
517 This request is sent from client to server to render "inlay hints" -- virtual text inserted into editor to show things like inferred types.
518 Generally, the client should re-query inlay hints after every modification.
519 Note that we plan to move this request to `experimental/inlayHints`, as it is not really Rust-specific, but the current API is not necessary the right one.
520 Upstream issues: https://github.com/microsoft/language-server-protocol/issues/956 , https://github.com/rust-analyzer/rust-analyzer/issues/2797
525 interface InlayHintsParams {
526 textDocument: TextDocumentIdentifier,
530 **Response:** `InlayHint[]`
533 interface InlayHint {
534 kind: "TypeHint" | "ParameterHint" | "ChainingHint",
542 **Experimental Client Capability:** `{ "hoverActions": boolean }`
544 If this capability is set, `Hover` request returned from the server might contain an additional field, `actions`:
549 actions?: CommandLinkGroup[];
552 interface CommandLink extends Command {
554 * A tooltip for the command, when represented in the UI.
559 interface CommandLinkGroup {
561 commands: CommandLink[];
565 Such actions on the client side are appended to a hover bottom as command links:
567 +-----------------------------+
570 +-----------------------------+
571 | _Action1_ | _Action2_ | <- first group, no TITLE
572 +-----------------------------+
573 | TITLE _Action1_ | _Action2_ | <- second group
574 +-----------------------------+
580 **Issue:** https://github.com/rust-analyzer/rust-analyzer/issues/6462
582 This request is sent from client to server to open the current project's Cargo.toml
584 **Method:** `experimental/openCargoToml`
586 **Request:** `OpenCargoTomlParams`
588 **Response:** `Location | null`
601 `experimental/openCargoToml` returns a single `Link` to the start of the `[package]` keyword.
605 This request is sent from client to server to get the list of tests for the specified position.
607 **Method:** `rust-analyzer/relatedTests`
609 **Request:** `TextDocumentPositionParams`
611 **Response:** `TestInfo[]`
621 **Issue:** https://github.com/rust-analyzer/rust-analyzer/issues/6823
623 This request is sent from client to server to move item under cursor or selection in some direction.
625 **Method:** `experimental/moveItem`
627 **Request:** `MoveItemParams`
629 **Response:** `SnippetTextEdit[]`
632 export interface MoveItemParams {
633 textDocument: lc.TextDocumentIdentifier,
638 export const enum Direction {