2 lsp_ext.rs hash: c6568e4035333f3a
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 the `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 ## Configuration in `initializationOptions`
30 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/567
32 The `initializationOptions` field of the `InitializeParams` of the initialization request should contain the `"rust-analyzer"` section of the configuration.
34 `rust-analyzer` normally sends a `"workspace/configuration"` request with `{ "items": ["rust-analyzer"] }` payload.
35 However, the server can't do this during initialization.
36 At the same time some essential configuration parameters are needed early on, before servicing requests.
37 For this reason, we ask that `initializationOptions` contains the configuration, as if the server did make a `"workspace/configuration"` request.
39 If a language client does not know about `rust-analyzer`'s configuration options it can get sensible defaults by doing any of the following:
40 * Not sending `initializationOptions`
41 * Sending `"initializationOptions": null`
42 * Sending `"initializationOptions": {}`
46 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/724
48 **Experimental Client Capability:** `{ "snippetTextEdit": boolean }`
50 If this capability is set, `WorkspaceEdit`s returned from `codeAction` requests might contain `SnippetTextEdit`s instead of usual `TextEdit`s:
53 interface SnippetTextEdit extends TextEdit {
54 insertTextFormat?: InsertTextFormat;
55 annotationId?: ChangeAnnotationIdentifier;
60 export interface TextDocumentEdit {
61 textDocument: OptionalVersionedTextDocumentIdentifier;
62 edits: (TextEdit | SnippetTextEdit)[];
66 When applying such code action, the editor should insert snippet, with tab stops and placeholder.
67 At the moment, rust-analyzer guarantees that only a single edit will have `InsertTextFormat.Snippet`.
71 "Add `derive`" code action transforms `struct S;` into `#[derive($0)] struct S;`
73 ### Unresolved Questions
75 * Where exactly are `SnippetTextEdit`s allowed (only in code actions at the moment)?
76 * Can snippets span multiple files (so far, no)?
78 ## `CodeAction` Groups
80 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/994
82 **Experimental Client Capability:** `{ "codeActionGroup": boolean }`
84 If this capability is set, `CodeAction`s returned from the server contain an additional field, `group`:
87 interface CodeAction {
94 All code-actions with the same `group` should be grouped under single (extendable) entry in lightbulb menu.
95 The set of actions `[ { title: "foo" }, { group: "frobnicate", title: "bar" }, { group: "frobnicate", title: "baz" }]` should be rendered as
101 +-------------+-----+
102 | frobnicate >| bar |
103 +-------------+-----+
108 Alternatively, selecting `frobnicate` could present a user with an additional menu to choose between `bar` and `baz`.
114 let x: Entry/*cursor here*/ = todo!();
118 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.
120 ### Unresolved Questions
122 * Is a fixed two-level structure enough?
123 * Should we devise a general way to encode custom interaction protocols for GUI refactorings?
127 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/1002
129 **Experimental Server Capability:** `{ "parentModule": boolean }`
131 This request is sent from client to server to handle "Goto Parent Module" editor action.
133 **Method:** `experimental/parentModule`
135 **Request:** `TextDocumentPositionParams`
137 **Response:** `Location | Location[] | LocationLink[] | null`
150 `experimental/parentModule` returns a single `Link` to the `mod foo;` declaration.
152 ### Unresolved Question
154 * An alternative would be to use a more general "gotoSuper" request, which would work for super methods, super classes and super modules.
155 This is the approach IntelliJ Rust is taking.
156 However, experience shows that super module (which generally has a feeling of navigation between files) should be separate.
157 If you want super module, but the cursor happens to be inside an overridden function, the behavior with single "gotoSuper" request is surprising.
161 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/992
163 **Experimental Server Capability:** `{ "joinLines": boolean }`
165 This request is sent from client to server to handle "Join Lines" editor action.
167 **Method:** `experimental/joinLines`
172 interface JoinLinesParams {
173 textDocument: TextDocumentIdentifier,
174 /// Currently active selections/cursor offsets.
175 /// This is an array to support multiple cursors.
180 **Response:** `TextEdit[]`
186 /*cursor here*/let x = {
192 `experimental/joinLines` yields (curly braces are automagically removed)
200 ### Unresolved Question
202 * What is the position of the cursor after `joinLines`?
203 Currently, this is left to editor's discretion, but it might be useful to specify on the server via snippets.
204 However, it then becomes unclear how it works with multi cursor.
208 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/1001
210 **Experimental Server Capability:** `{ "onEnter": boolean }`
212 This request is sent from client to server to handle the <kbd>Enter</kbd> key press.
214 **Method:** `experimental/onEnter`
216 **Request:**: `TextDocumentPositionParams`
228 // Some /*cursor here*/ docs
233 `experimental/onEnter` returns the following snippet
243 The primary goal of `onEnter` is to handle automatic indentation when opening a new line.
244 This is not yet implemented.
245 The secondary goal is to handle fixing up syntax, like continuing doc strings and comments, and escaping `\n` in string literals.
247 As proper cursor positioning is raison-d'etat for `onEnter`, it uses `SnippetTextEdit`.
249 ### Unresolved Question
251 * How to deal with synchronicity of the request?
252 One option is to require the client to block until the server returns the response.
253 Another option is to do a OT-style merging of edits from client and server.
254 A third option is to do a record-replay: client applies heuristic on enter immediately, then applies all user's keypresses.
255 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.
256 * How to deal with multiple carets?
257 * Should we extend this to arbitrary typed events and not just `onEnter`?
259 ## Structural Search Replace (SSR)
261 **Experimental Server Capability:** `{ "ssr": boolean }`
263 This request is sent from client to server to handle structural search replace -- automated syntax tree based transformation of the source.
265 **Method:** `experimental/ssr`
270 interface SsrParams {
272 /// The specific syntax is specified outside of the protocol.
274 /// If true, only check the syntax of the query and don't compute the actual edit.
276 /// The current text document. This and `position` will be used to determine in what scope
277 /// paths in `query` should be resolved.
278 textDocument: TextDocumentIdentifier;
279 /// Position where SSR was invoked.
292 SSR with query `foo($a, $b) ==>> ($a).foo($b)` will transform, eg `foo(y + 5, z)` into `(y + 5).foo(z)`.
294 ### Unresolved Question
296 * Probably needs search without replace mode
297 * Needs a way to limit the scope to certain files.
301 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/999
303 **Experimental Server Capability:** `{ "matchingBrace": boolean }`
305 This request is sent from client to server to handle "Matching Brace" editor action.
307 **Method:** `experimental/matchingBrace`
312 interface MatchingBraceParams {
313 textDocument: TextDocumentIdentifier,
314 /// Position for each cursor
315 positions: Position[],
329 let x: Vec<()>/*cursor here*/ = vec![]
333 `experimental/matchingBrace` yields the position of `<`.
334 In many cases, matching braces can be handled by the editor.
335 However, some cases (like disambiguating between generics and comparison operations) need a real parser.
336 Moreover, it would be cool if editors didn't need to implement even basic language parsing
338 ### Unresolved Question
340 * Should we return a nested brace structure, to allow paredit-like actions of jump *out* of the current brace pair?
341 This is how `SelectionRange` request works.
342 * Alternatively, should we perhaps flag certain `SelectionRange`s as being brace pairs?
346 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/944
348 **Experimental Server Capability:** `{ "runnables": { "kinds": string[] } }`
350 This request is sent from client to server to get the list of things that can be run (tests, binaries, `cargo check -p`).
352 **Method:** `experimental/runnables`
357 interface RunnablesParams {
358 textDocument: TextDocumentIdentifier;
359 /// If null, compute runnables for the whole file.
364 **Response:** `Runnable[]`
369 /// If this Runnable is associated with a specific function/module, etc, the location of this item
370 location?: LocationLink;
371 /// Running things is necessary technology specific, `kind` needs to be advertised via server capabilities,
372 // the type of `args` is specific to `kind`. The actual running is handled by the client.
378 rust-analyzer supports only one `kind`, `"cargo"`. The `args` for `"cargo"` look like this:
382 workspaceRoot?: string;
384 cargoExtraArgs: string[];
385 executableArgs: string[];
386 expectTest?: boolean;
387 overrideCargo?: string;
391 ## Open External Documentation
393 This request is sent from client to server to get a URL to documentation for the symbol under the cursor, if available.
395 **Method** `experimental/externalDocs`
397 **Request:**: `TextDocumentPositionParams`
399 **Response** `string | null`
404 **Method:** `rust-analyzer/analyzerStatus`
409 interface AnalyzerStatusParams {
410 /// If specified, show dependencies of the current file.
411 textDocument?: TextDocumentIdentifier;
415 **Response:** `string`
417 Returns internal status message, mostly for debugging purposes.
421 **Method:** `rust-analyzer/reloadWorkspace`
427 Reloads project information (that is, re-executes `cargo metadata`).
431 **Experimental Client Capability:** `{ "serverStatusNotification": boolean }`
433 **Method:** `experimental/serverStatus`
438 interface ServerStatusParams {
439 /// `ok` means that the server is completely functional.
441 /// `warning` means that the server is partially functional.
442 /// It can answer correctly to most requests, but some results
443 /// might be wrong due to, for example, some missing dependencies.
445 /// `error` means that the server is not functional. For example,
446 /// there's a fatal build configuration problem. The server might
447 /// still give correct answers to simple requests, but most results
448 /// will be incomplete or wrong.
449 health: "ok" | "warning" | "error",
450 /// Is there any pending background work which might change the status?
451 /// For example, are dependencies being downloaded?
453 /// Explanatory message to show on hover.
458 This notification is sent from server to client.
459 The client can use it to display *persistent* status to the user (in modline).
460 It is similar to the `showMessage`, but is intended for stares rather than point-in-time events.
462 Note that this functionality is intended primarily to inform the end user about the state of the server.
463 In particular, it's valid for the client to completely ignore this extension.
464 Clients are discouraged from but are allowed to use the `health` status to decide if it's worth sending a request to the server.
468 **Method:** `rust-analyzer/syntaxTree`
473 interface SyntaxTreeParams {
474 textDocument: TextDocumentIdentifier,
479 **Response:** `string`
481 Returns textual representation of a parse tree for the file/selected region.
482 Primarily for debugging, but very useful for all people working on rust-analyzer itself.
486 **Method:** `rust-analyzer/viewHir`
488 **Request:** `TextDocumentPositionParams`
490 **Response:** `string`
492 Returns a textual representation of the HIR of the function containing the cursor.
493 For debugging or when working on rust-analyzer itself.
497 **Method:** `rust-analyzer/viewItemTree`
502 interface ViewItemTreeParams {
503 textDocument: TextDocumentIdentifier,
507 **Response:** `string`
509 Returns a textual representation of the `ItemTree` of the currently open file, for debugging.
513 **Method:** `rust-analyzer/viewCrateGraph`
518 interface ViewCrateGraphParams {
523 **Response:** `string`
525 Renders rust-analyzer's crate graph as an SVG image.
527 If `full` is `true`, the graph includes non-workspace crates (crates.io dependencies as well as sysroot crates).
531 **Method:** `rust-analyzer/expandMacro`
536 interface ExpandMacroParams {
537 textDocument: TextDocumentIdentifier,
545 interface ExpandedMacro {
551 Expands macro call at a given position.
555 **Method:** `rust-analyzer/inlayHints`
557 This request is sent from client to server to render "inlay hints" -- virtual text inserted into editor to show things like inferred types.
558 Generally, the client should re-query inlay hints after every modification.
559 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.
560 Upstream issues: https://github.com/microsoft/language-server-protocol/issues/956 , https://github.com/rust-analyzer/rust-analyzer/issues/2797
565 interface InlayHintsParams {
566 textDocument: TextDocumentIdentifier,
570 **Response:** `InlayHint[]`
573 interface InlayHint {
574 kind: "TypeHint" | "ParameterHint" | "ChainingHint",
582 **Experimental Client Capability:** `{ "hoverActions": boolean }`
584 If this capability is set, `Hover` request returned from the server might contain an additional field, `actions`:
589 actions?: CommandLinkGroup[];
592 interface CommandLink extends Command {
594 * A tooltip for the command, when represented in the UI.
599 interface CommandLinkGroup {
601 commands: CommandLink[];
605 Such actions on the client side are appended to a hover bottom as command links:
607 +-----------------------------+
610 +-----------------------------+
611 | _Action1_ | _Action2_ | <- first group, no TITLE
612 +-----------------------------+
613 | TITLE _Action1_ | _Action2_ | <- second group
614 +-----------------------------+
620 **Upstream Issue:** https://github.com/rust-analyzer/rust-analyzer/issues/6462
622 **Experimental Server Capability:** `{ "openCargoToml": boolean }`
624 This request is sent from client to server to open the current project's Cargo.toml
626 **Method:** `experimental/openCargoToml`
628 **Request:** `OpenCargoTomlParams`
630 **Response:** `Location | null`
643 `experimental/openCargoToml` returns a single `Link` to the start of the `[package]` keyword.
647 This request is sent from client to server to get the list of tests for the specified position.
649 **Method:** `rust-analyzer/relatedTests`
651 **Request:** `TextDocumentPositionParams`
653 **Response:** `TestInfo[]`
663 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/377
665 **Experimental Server Capability:** { "hoverRange": boolean }
667 This extension allows passing a `Range` as a `position` field of `HoverParams`.
668 The primary use-case is to use the hover request to show the type of the expression currently selected.
671 interface HoverParams extends WorkDoneProgressParams {
672 textDocument: TextDocumentIdentifier;
673 position: Range | Position;
676 Whenever the client sends a `Range`, it is understood as the current selection and any hover included in the range will show the type of the expression if possible.
682 let expression = $01 + 2 * 3$0;
686 Triggering a hover inside the selection above will show a result of `i32`.
690 **Upstream Issue:** https://github.com/rust-analyzer/rust-analyzer/issues/6823
692 This request is sent from client to server to move item under cursor or selection in some direction.
694 **Method:** `experimental/moveItem`
696 **Request:** `MoveItemParams`
698 **Response:** `SnippetTextEdit[]`
701 export interface MoveItemParams {
702 textDocument: TextDocumentIdentifier,
707 export const enum Direction {
713 ## Workspace Symbols Filtering
715 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/941
717 **Experimental Server Capability:** `{ "workspaceSymbolScopeKindFiltering": boolean }`
719 Extends the existing `workspace/symbol` request with ability to filter symbols by broad scope and kind of symbol.
720 If this capability is set, `workspace/symbol` parameter gains two new optional fields:
724 interface WorkspaceSymbolParams {
726 * Return only the symbols defined in the specified scope.
728 searchScope?: WorkspaceSymbolSearchScope;
730 * Return only the symbols of specified kinds.
732 searchKind?: WorkspaceSymbolSearchKind;
736 const enum WorkspaceSymbolSearchScope {
737 Workspace = "workspace",
738 WorkspaceAndDependencies = "workspaceAndDependencies"
741 const enum WorkspaceSymbolSearchKind {
742 OnlyTypes = "onlyTypes",
743 AllSymbols = "allSymbols"
749 **Upstream Issue:** https://github.com/microsoft/language-server-protocol/issues/642
751 **Experimental Client Capability:** `{ "commands?": ClientCommandOptions }`
753 Certain LSP types originating on the server, notably code lenses, embed commands.
754 Commands can be serviced either by the server or by the client.
755 However, the server doesn't know which commands are available on the client.
757 This extensions allows the client to communicate this info.
761 export interface ClientCommandOptions {
763 * The commands to be executed on the client