diff options
Diffstat (limited to 'docs/dev')
-rw-r--r-- | docs/dev/README.md | 6 | ||||
-rw-r--r-- | docs/dev/debugging.md | 14 | ||||
-rw-r--r-- | docs/dev/lsp-extensions.md | 86 | ||||
-rw-r--r-- | docs/dev/syntax.md | 42 |
4 files changed, 117 insertions, 31 deletions
diff --git a/docs/dev/README.md b/docs/dev/README.md index f230dc1db..65cc9fc12 100644 --- a/docs/dev/README.md +++ b/docs/dev/README.md | |||
@@ -74,7 +74,7 @@ relevant test and execute it (VS Code includes an action for running a single | |||
74 | test). | 74 | test). |
75 | 75 | ||
76 | However, launching a VS Code instance with locally build language server is | 76 | However, launching a VS Code instance with locally build language server is |
77 | possible. There's **"Run Extension (Dev Server)"** launch configuration for this. | 77 | possible. There's **"Run Extension (Debug Build)"** launch configuration for this. |
78 | 78 | ||
79 | In general, I use one of the following workflows for fixing bugs and | 79 | In general, I use one of the following workflows for fixing bugs and |
80 | implementing features. | 80 | implementing features. |
@@ -86,7 +86,7 @@ then just do printf-driven development/debugging. As a sanity check after I'm | |||
86 | done, I use `cargo xtask install --server` and **Reload Window** action in VS | 86 | done, I use `cargo xtask install --server` and **Reload Window** action in VS |
87 | Code to sanity check that the thing works as I expect. | 87 | Code to sanity check that the thing works as I expect. |
88 | 88 | ||
89 | If the problem concerns only the VS Code extension, I use **Run Extension** | 89 | If the problem concerns only the VS Code extension, I use **Run Installed Extension** |
90 | launch configuration from `launch.json`. Notably, this uses the usual | 90 | launch configuration from `launch.json`. Notably, this uses the usual |
91 | `rust-analyzer` binary from `PATH`. For this it is important to have the following | 91 | `rust-analyzer` binary from `PATH`. For this it is important to have the following |
92 | in `setting.json` file: | 92 | in `setting.json` file: |
@@ -134,7 +134,7 @@ To log all communication between the server and the client, there are two choice | |||
134 | 134 | ||
135 | * you can log on the server side, by running something like | 135 | * you can log on the server side, by running something like |
136 | ``` | 136 | ``` |
137 | env RUST_LOG=gen_lsp_server=trace code . | 137 | env RA_LOG=gen_lsp_server=trace code . |
138 | ``` | 138 | ``` |
139 | 139 | ||
140 | * you can log on the client side, by enabling `"rust-analyzer.trace.server": | 140 | * you can log on the client side, by enabling `"rust-analyzer.trace.server": |
diff --git a/docs/dev/debugging.md b/docs/dev/debugging.md index bece6a572..59a83f7d7 100644 --- a/docs/dev/debugging.md +++ b/docs/dev/debugging.md | |||
@@ -22,11 +22,11 @@ where **only** the `rust-analyzer` extension being debugged is enabled. | |||
22 | 22 | ||
23 | ## Debug TypeScript VSCode extension | 23 | ## Debug TypeScript VSCode extension |
24 | 24 | ||
25 | - `Run Extension` - runs the extension with the globally installed `rust-analyzer` binary. | 25 | - `Run Installed Extension` - runs the extension with the globally installed `rust-analyzer` binary. |
26 | - `Run Extension (Dev Server)` - runs extension with the locally built LSP server (`target/debug/rust-analyzer`). | 26 | - `Run Extension (Debug Build)` - runs extension with the locally built LSP server (`target/debug/rust-analyzer`). |
27 | 27 | ||
28 | TypeScript debugging is configured to watch your source edits and recompile. | 28 | TypeScript debugging is configured to watch your source edits and recompile. |
29 | To apply changes to an already running debug process press <kbd>Ctrl+Shift+P</kbd> and run the following command in your `[Extension Development Host]` | 29 | To apply changes to an already running debug process, press <kbd>Ctrl+Shift+P</kbd> and run the following command in your `[Extension Development Host]` |
30 | 30 | ||
31 | ``` | 31 | ``` |
32 | > Developer: Reload Window | 32 | > Developer: Reload Window |
@@ -47,7 +47,7 @@ To apply changes to an already running debug process press <kbd>Ctrl+Shift+P</kb | |||
47 | debug = 2 | 47 | debug = 2 |
48 | ``` | 48 | ``` |
49 | 49 | ||
50 | - Select `Run Extension (Dev Server)` to run your locally built `target/debug/rust-analyzer`. | 50 | - Select `Run Extension (Debug Build)` to run your locally built `target/debug/rust-analyzer`. |
51 | 51 | ||
52 | - In the original VSCode window once again select the `Attach To Server` debug configuration. | 52 | - In the original VSCode window once again select the `Attach To Server` debug configuration. |
53 | 53 | ||
@@ -76,11 +76,11 @@ Make sure you open a rust file in the `[Extension Development Host]` and try aga | |||
76 | 76 | ||
77 | Make sure you have run `echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope`. | 77 | Make sure you have run `echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope`. |
78 | 78 | ||
79 | By default this should reset back to 1 everytime you log in. | 79 | By default this should reset back to 1 every time you log in. |
80 | 80 | ||
81 | ### Breakpoints are never being hit | 81 | ### Breakpoints are never being hit |
82 | 82 | ||
83 | Check your version of `lldb` if it's version 6 and lower use the `classic` adapter type. | 83 | Check your version of `lldb`. If it's version 6 and lower, use the `classic` adapter type. |
84 | It's `lldb.adapterType` in settings file. | 84 | It's `lldb.adapterType` in settings file. |
85 | 85 | ||
86 | If you're running `lldb` version 7 change the lldb adapter type to `bundled` or `native`. | 86 | If you're running `lldb` version 7, change the lldb adapter type to `bundled` or `native`. |
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md new file mode 100644 index 000000000..0e3a0af1c --- /dev/null +++ b/docs/dev/lsp-extensions.md | |||
@@ -0,0 +1,86 @@ | |||
1 | # LSP Extensions | ||
2 | |||
3 | This document describes LSP extensions used by rust-analyzer. | ||
4 | It's a best effort document, when in doubt, consult the source (and send a PR with clarification ;-) ). | ||
5 | We aim to upstream all non Rust-specific extensions to the protocol, but this is not a top priority. | ||
6 | All capabilities are enabled via `experimental` field of `ClientCapabilities`. | ||
7 | |||
8 | ## `SnippetTextEdit` | ||
9 | |||
10 | **Client Capability:** `{ "snippetTextEdit": boolean }` | ||
11 | |||
12 | If this capability is set, `WorkspaceEdit`s returned from `codeAction` requests might contain `SnippetTextEdit`s instead of usual `TextEdit`s: | ||
13 | |||
14 | ```typescript | ||
15 | interface SnippetTextEdit extends TextEdit { | ||
16 | insertTextFormat?: InsertTextFormat; | ||
17 | } | ||
18 | ``` | ||
19 | |||
20 | ```typescript | ||
21 | export interface TextDocumentEdit { | ||
22 | textDocument: VersionedTextDocumentIdentifier; | ||
23 | edits: (TextEdit | SnippetTextEdit)[]; | ||
24 | } | ||
25 | ``` | ||
26 | |||
27 | When applying such code action, the editor should insert snippet, with tab stops and placeholder. | ||
28 | At the moment, rust-analyzer guarantees that only a single edit will have `InsertTextFormat.Snippet`. | ||
29 | |||
30 | ### Example | ||
31 | |||
32 | "Add `derive`" code action transforms `struct S;` into `#[derive($0)] struct S;` | ||
33 | |||
34 | ### Unresolved Questions | ||
35 | |||
36 | * Where exactly are `SnippetTextEdit`s allowed (only in code actions at the moment)? | ||
37 | * Can snippets span multiple files (so far, no)? | ||
38 | |||
39 | ## `joinLines` | ||
40 | |||
41 | **Server Capability:** `{ "joinLines": boolean }` | ||
42 | |||
43 | This request is send from client to server to handle "Join Lines" editor action. | ||
44 | |||
45 | **Method:** `experimental/JoinLines` | ||
46 | |||
47 | **Request:** | ||
48 | |||
49 | ```typescript | ||
50 | interface JoinLinesParams { | ||
51 | textDocument: TextDocumentIdentifier, | ||
52 | /// Currently active selections/cursor offsets. | ||
53 | /// This is an array to support multiple cursors. | ||
54 | ranges: Range[], | ||
55 | } | ||
56 | ``` | ||
57 | |||
58 | **Response:** | ||
59 | |||
60 | ```typescript | ||
61 | TextEdit[] | ||
62 | ``` | ||
63 | |||
64 | ### Example | ||
65 | |||
66 | ```rust | ||
67 | fn main() { | ||
68 | /*cursor here*/let x = { | ||
69 | 92 | ||
70 | }; | ||
71 | } | ||
72 | ``` | ||
73 | |||
74 | `experimental/joinLines` yields (curly braces are automagiacally removed) | ||
75 | |||
76 | ```rust | ||
77 | fn main() { | ||
78 | let x = 92; | ||
79 | } | ||
80 | ``` | ||
81 | |||
82 | ### Unresolved Question | ||
83 | |||
84 | * What is the position of the cursor after `joinLines`? | ||
85 | Currently this is left to editor's discretion, but it might be useful to specify on the server via snippets. | ||
86 | However, it then becomes unclear how it works with multi cursor. | ||
diff --git a/docs/dev/syntax.md b/docs/dev/syntax.md index 33973ffec..c2864bbbc 100644 --- a/docs/dev/syntax.md +++ b/docs/dev/syntax.md | |||
@@ -64,7 +64,7 @@ struct Token { | |||
64 | } | 64 | } |
65 | ``` | 65 | ``` |
66 | 66 | ||
67 | All the difference bettwen the above sketch and the real implementation are strictly due to optimizations. | 67 | All the difference between the above sketch and the real implementation are strictly due to optimizations. |
68 | 68 | ||
69 | Points of note: | 69 | Points of note: |
70 | * The tree is untyped. Each node has a "type tag", `SyntaxKind`. | 70 | * The tree is untyped. Each node has a "type tag", `SyntaxKind`. |
@@ -72,7 +72,7 @@ Points of note: | |||
72 | * Trivia and non-trivia tokens are not distinguished on the type level. | 72 | * Trivia and non-trivia tokens are not distinguished on the type level. |
73 | * Each token carries its full text. | 73 | * Each token carries its full text. |
74 | * The original text can be recovered by concatenating the texts of all tokens in order. | 74 | * The original text can be recovered by concatenating the texts of all tokens in order. |
75 | * Accessing a child of particular type (for example, parameter list of a function) generarly involves linerary traversing the children, looking for a specific `kind`. | 75 | * Accessing a child of particular type (for example, parameter list of a function) generally involves linerary traversing the children, looking for a specific `kind`. |
76 | * Modifying the tree is roughly `O(depth)`. | 76 | * Modifying the tree is roughly `O(depth)`. |
77 | We don't make special efforts to guarantree that the depth is not liner, but, in practice, syntax trees are branchy and shallow. | 77 | We don't make special efforts to guarantree that the depth is not liner, but, in practice, syntax trees are branchy and shallow. |
78 | * If mandatory (grammar wise) node is missing from the input, it's just missing from the tree. | 78 | * If mandatory (grammar wise) node is missing from the input, it's just missing from the tree. |
@@ -123,7 +123,7 @@ To more compactly store the children, we box *both* interior nodes and tokens, a | |||
123 | `Either<Arc<Node>, Arc<Token>>` as a single pointer with a tag in the last bit. | 123 | `Either<Arc<Node>, Arc<Token>>` as a single pointer with a tag in the last bit. |
124 | 124 | ||
125 | To avoid allocating EVERY SINGLE TOKEN on the heap, syntax trees use interning. | 125 | To avoid allocating EVERY SINGLE TOKEN on the heap, syntax trees use interning. |
126 | Because the tree is fully imutable, it's valid to structuraly share subtrees. | 126 | Because the tree is fully immutable, it's valid to structurally share subtrees. |
127 | For example, in `1 + 1`, there will be a *single* token for `1` with ref count 2; the same goes for the ` ` whitespace token. | 127 | For example, in `1 + 1`, there will be a *single* token for `1` with ref count 2; the same goes for the ` ` whitespace token. |
128 | Interior nodes are shared as well (for example in `(1 + 1) * (1 + 1)`). | 128 | Interior nodes are shared as well (for example in `(1 + 1) * (1 + 1)`). |
129 | 129 | ||
@@ -134,8 +134,8 @@ Currently, the interner is created per-file, but it will be easy to use a per-th | |||
134 | 134 | ||
135 | We use a `TextSize`, a newtyped `u32`, to store the length of the text. | 135 | We use a `TextSize`, a newtyped `u32`, to store the length of the text. |
136 | 136 | ||
137 | We currently use `SmolStr`, an small object optimized string to store text. | 137 | We currently use `SmolStr`, a small object optimized string to store text. |
138 | This was mostly relevant *before* we implmented tree interning, to avoid allocating common keywords and identifiers. We should switch to storing text data alongside the interned tokens. | 138 | This was mostly relevant *before* we implemented tree interning, to avoid allocating common keywords and identifiers. We should switch to storing text data alongside the interned tokens. |
139 | 139 | ||
140 | #### Alternative designs | 140 | #### Alternative designs |
141 | 141 | ||
@@ -162,12 +162,12 @@ Explicit trivia nodes, like in `rowan`, are used by IntelliJ. | |||
162 | 162 | ||
163 | ##### Accessing Children | 163 | ##### Accessing Children |
164 | 164 | ||
165 | As noted before, accesing a specific child in the node requires a linear traversal of the children (though we can skip tokens, beacuse the tag is encoded in the pointer itself). | 165 | As noted before, accessing a specific child in the node requires a linear traversal of the children (though we can skip tokens, because the tag is encoded in the pointer itself). |
166 | It is possible to recover O(1) access with another representation. | 166 | It is possible to recover O(1) access with another representation. |
167 | We explicitly store optional and missing (required by the grammar, but not present) nodes. | 167 | We explicitly store optional and missing (required by the grammar, but not present) nodes. |
168 | That is, we use `Option<Node>` for children. | 168 | That is, we use `Option<Node>` for children. |
169 | We also remove trivia tokens from the tree. | 169 | We also remove trivia tokens from the tree. |
170 | This way, each child kind genrerally occupies a fixed position in a parent, and we can use index access to fetch it. | 170 | This way, each child kind generally occupies a fixed position in a parent, and we can use index access to fetch it. |
171 | The cost is that we now need to allocate space for all not-present optional nodes. | 171 | The cost is that we now need to allocate space for all not-present optional nodes. |
172 | So, `fn foo() {}` will have slots for visibility, unsafeness, attributes, abi and return type. | 172 | So, `fn foo() {}` will have slots for visibility, unsafeness, attributes, abi and return type. |
173 | 173 | ||
@@ -193,7 +193,7 @@ Modeling this with immutable trees is possible, but annoying. | |||
193 | ### Syntax Nodes | 193 | ### Syntax Nodes |
194 | 194 | ||
195 | A function green tree is not super-convenient to use. | 195 | A function green tree is not super-convenient to use. |
196 | The biggest problem is acessing parents (there are no parent pointers!). | 196 | The biggest problem is accessing parents (there are no parent pointers!). |
197 | But there are also "identify" issues. | 197 | But there are also "identify" issues. |
198 | Let's say you want to write a code which builds a list of expressions in a file: `fn collect_exrepssions(file: GreenNode) -> HashSet<GreenNode>`. | 198 | Let's say you want to write a code which builds a list of expressions in a file: `fn collect_exrepssions(file: GreenNode) -> HashSet<GreenNode>`. |
199 | For the input like | 199 | For the input like |
@@ -207,7 +207,7 @@ fn main() { | |||
207 | } | 207 | } |
208 | ``` | 208 | ``` |
209 | 209 | ||
210 | both copies of the `x + 2` expression are representing by equal (and, with interning in mind, actualy the same) green nodes. | 210 | both copies of the `x + 2` expression are representing by equal (and, with interning in mind, actually the same) green nodes. |
211 | Green trees just can't differentiate between the two. | 211 | Green trees just can't differentiate between the two. |
212 | 212 | ||
213 | `SyntaxNode` adds parent pointers and identify semantics to green nodes. | 213 | `SyntaxNode` adds parent pointers and identify semantics to green nodes. |
@@ -285,9 +285,9 @@ They also point to the parent (and, consequently, to the root) with an owning `R | |||
285 | In other words, one needs *one* arc bump when initiating a traversal. | 285 | In other words, one needs *one* arc bump when initiating a traversal. |
286 | 286 | ||
287 | To get rid of allocations, `rowan` takes advantage of `SyntaxNode: !Sync` and uses a thread-local free list of `SyntaxNode`s. | 287 | To get rid of allocations, `rowan` takes advantage of `SyntaxNode: !Sync` and uses a thread-local free list of `SyntaxNode`s. |
288 | In a typical traversal, you only directly hold a few `SyntaxNode`s at a time (and their ancesstors indirectly), so a free list proportional to the depth of the tree removes all allocations in a typical case. | 288 | In a typical traversal, you only directly hold a few `SyntaxNode`s at a time (and their ancestors indirectly), so a free list proportional to the depth of the tree removes all allocations in a typical case. |
289 | 289 | ||
290 | So, while traversal is not exactly incrementing a pointer, it's still prety cheep: tls + rc bump! | 290 | So, while traversal is not exactly incrementing a pointer, it's still pretty cheap: TLS + rc bump! |
291 | 291 | ||
292 | Traversal also yields (cheap) owned nodes, which improves ergonomics quite a bit. | 292 | Traversal also yields (cheap) owned nodes, which improves ergonomics quite a bit. |
293 | 293 | ||
@@ -308,15 +308,15 @@ struct SyntaxData { | |||
308 | } | 308 | } |
309 | ``` | 309 | ``` |
310 | 310 | ||
311 | This allows using true pointer equality for comparision of identities of `SyntaxNodes`. | 311 | This allows using true pointer equality for comparison of identities of `SyntaxNodes`. |
312 | rust-analyzer used to have this design as well, but since we've switch to cursors. | 312 | rust-analyzer used to have this design as well, but we've since switched to cursors. |
313 | The main problem with memoizing the red nodes is that it more than doubles the memory requirenments for fully realized syntax trees. | 313 | The main problem with memoizing the red nodes is that it more than doubles the memory requirements for fully realized syntax trees. |
314 | In contrast, cursors generally retain only a path to the root. | 314 | In contrast, cursors generally retain only a path to the root. |
315 | C# combats increased memory usage by using weak references. | 315 | C# combats increased memory usage by using weak references. |
316 | 316 | ||
317 | ### AST | 317 | ### AST |
318 | 318 | ||
319 | `GreenTree`s are untyped and homogeneous, because it makes accomodating error nodes, arbitrary whitespace and comments natural, and because it makes possible to write generic tree traversals. | 319 | `GreenTree`s are untyped and homogeneous, because it makes accommodating error nodes, arbitrary whitespace and comments natural, and because it makes possible to write generic tree traversals. |
320 | However, when working with a specific node, like a function definition, one would want a strongly typed API. | 320 | However, when working with a specific node, like a function definition, one would want a strongly typed API. |
321 | 321 | ||
322 | This is what is provided by the AST layer. AST nodes are transparent wrappers over untyped syntax nodes: | 322 | This is what is provided by the AST layer. AST nodes are transparent wrappers over untyped syntax nodes: |
@@ -397,7 +397,7 @@ impl HasVisbility for FnDef { | |||
397 | Points of note: | 397 | Points of note: |
398 | 398 | ||
399 | * Like `SyntaxNode`s, AST nodes are cheap to clone pointer-sized owned values. | 399 | * Like `SyntaxNode`s, AST nodes are cheap to clone pointer-sized owned values. |
400 | * All "fields" are optional, to accomodate incomplete and/or erroneous source code. | 400 | * All "fields" are optional, to accommodate incomplete and/or erroneous source code. |
401 | * It's always possible to go from an ast node to an untyped `SyntaxNode`. | 401 | * It's always possible to go from an ast node to an untyped `SyntaxNode`. |
402 | * It's possible to go in the opposite direction with a checked cast. | 402 | * It's possible to go in the opposite direction with a checked cast. |
403 | * `enum`s allow modeling of arbitrary intersecting subsets of AST types. | 403 | * `enum`s allow modeling of arbitrary intersecting subsets of AST types. |
@@ -437,13 +437,13 @@ impl GreenNodeBuilder { | |||
437 | } | 437 | } |
438 | ``` | 438 | ``` |
439 | 439 | ||
440 | The parser, ultimatelly, needs to invoke the `GreenNodeBuilder`. | 440 | The parser, ultimately, needs to invoke the `GreenNodeBuilder`. |
441 | There are two principal sources of inputs for the parser: | 441 | There are two principal sources of inputs for the parser: |
442 | * source text, which contains trivia tokens (whitespace and comments) | 442 | * source text, which contains trivia tokens (whitespace and comments) |
443 | * token trees from macros, which lack trivia | 443 | * token trees from macros, which lack trivia |
444 | 444 | ||
445 | Additionaly, input tokens do not correspond 1-to-1 with output tokens. | 445 | Additionally, input tokens do not correspond 1-to-1 with output tokens. |
446 | For example, two consequtive `>` tokens might be glued, by the parser, into a single `>>`. | 446 | For example, two consecutive `>` tokens might be glued, by the parser, into a single `>>`. |
447 | 447 | ||
448 | For these reasons, the parser crate defines a callback interfaces for both input tokens and output trees. | 448 | For these reasons, the parser crate defines a callback interfaces for both input tokens and output trees. |
449 | The explicit glue layer then bridges various gaps. | 449 | The explicit glue layer then bridges various gaps. |
@@ -491,7 +491,7 @@ Syntax errors are not stored directly in the tree. | |||
491 | The primary motivation for this is that syntax tree is not necessary produced by the parser, it may also be assembled manually from pieces (which happens all the time in refactorings). | 491 | The primary motivation for this is that syntax tree is not necessary produced by the parser, it may also be assembled manually from pieces (which happens all the time in refactorings). |
492 | Instead, parser reports errors to an error sink, which stores them in a `Vec`. | 492 | Instead, parser reports errors to an error sink, which stores them in a `Vec`. |
493 | If possible, errors are not reported during parsing and are postponed for a separate validation step. | 493 | If possible, errors are not reported during parsing and are postponed for a separate validation step. |
494 | For example, parser accepts visibility modifiers on trait methods, but then a separate tree traversal flags all such visibilites as erroneous. | 494 | For example, parser accepts visibility modifiers on trait methods, but then a separate tree traversal flags all such visibilities as erroneous. |
495 | 495 | ||
496 | ### Macros | 496 | ### Macros |
497 | 497 | ||
@@ -501,7 +501,7 @@ Specifically, `TreeSink` constructs the tree in lockstep with draining the origi | |||
501 | In the process, it records which tokens of the tree correspond to which tokens of the input, by using text ranges to identify syntax tokens. | 501 | In the process, it records which tokens of the tree correspond to which tokens of the input, by using text ranges to identify syntax tokens. |
502 | The end result is that parsing an expanded code yields a syntax tree and a mapping of text-ranges of the tree to original tokens. | 502 | The end result is that parsing an expanded code yields a syntax tree and a mapping of text-ranges of the tree to original tokens. |
503 | 503 | ||
504 | To deal with precedence in cases like `$expr * 1`, we use special invisible parenthesis, which are explicitelly handled by the parser | 504 | To deal with precedence in cases like `$expr * 1`, we use special invisible parenthesis, which are explicitly handled by the parser |
505 | 505 | ||
506 | ### Whitespace & Comments | 506 | ### Whitespace & Comments |
507 | 507 | ||