diff options
author | Aleksey Kladov <[email protected]> | 2019-01-20 12:43:43 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-21 08:27:01 +0000 |
commit | c33202ec8245786932795478d650b686b136f913 (patch) | |
tree | c10fde8aa1f8f2e5587b75997a910ef17dc37452 | |
parent | 6789e5a6e788c06ab4605bf642b545243ee9ae7d (diff) |
complete completion section
-rw-r--r-- | guide.md | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -524,6 +524,40 @@ message as a request for completion and [schedule it on the threadpool]. This is | |||
524 | the place where we [catch] canceled error if, immediately after completion, the | 524 | the place where we [catch] canceled error if, immediately after completion, the |
525 | client sends some modification. | 525 | client sends some modification. |
526 | 526 | ||
527 | In [the handler] we deserialize LSP request into rust-analyzer specific data | ||
528 | types (by converting a file url into a numeric `FileId`), [ask analysis for | ||
529 | completion] and serializer results to LSP. | ||
530 | |||
531 | [Completion implementation] is finally the place where we start doing the actual | ||
532 | work. The first step is to collection `CompletionContext` -- a struct which | ||
533 | describes the cursor position in terms of Rust syntax and semantics. For | ||
534 | example, `function_syntax: Option<&'a ast::FnDef>` stores a reference to | ||
535 | enclosing function *syntax*, while `function: Option<hir::Function>` is the | ||
536 | `Def` for this function. | ||
537 | |||
538 | To construct the context, we first do an ["IntelliJ Trick"]: we insert a dummy | ||
539 | identifier at the cursor's position and parse this modified file, to get a | ||
540 | reasonably looking syntax tree. Then we do a bunch of "classification" routines | ||
541 | to figure out the context. For example, we [find ancestor fn node] and we get a | ||
542 | [semantic model] for it (using the lossy `source_binder` infrastructure). | ||
543 | |||
544 | The second step is to run a [series of independent completion routines]. Let's | ||
545 | take a closer look at [`complete_dot`], which completes fields and methods in | ||
546 | `foo.bar|`. First we extract a semantic function and a syntactic receiver | ||
547 | expression out of the `Context`. Then we run type-inference for this single | ||
548 | function and map our syntactic expression to `ExprId`. Using the id, we figure | ||
549 | out the type of the receiver expression. Then we add all fields & methods from | ||
550 | the type to completion. | ||
551 | |||
527 | [receiving a message]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_lsp_server/src/main_loop.rs#L203 | 552 | [receiving a message]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_lsp_server/src/main_loop.rs#L203 |
528 | [schedule it on the threadpool]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_lsp_server/src/main_loop.rs#L428 | 553 | [schedule it on the threadpool]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_lsp_server/src/main_loop.rs#L428 |
529 | [catch]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_lsp_server/src/main_loop.rs#L436-L442 | 554 | [catch]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_lsp_server/src/main_loop.rs#L436-L442 |
555 | [the handler]: https://salsa.zulipchat.com/#narrow/stream/181542-rfcs.2Fsalsa-query-group/topic/design.20next.20steps | ||
556 | [ask analysis for completion]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_ide_api/src/lib.rs#L439-L444 | ||
557 | [Completion implementation]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_ide_api/src/completion.rs#L46-L62 | ||
558 | [`CompletionContext`]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_ide_api/src/completion/completion_context.rs#L14-L37 | ||
559 | ["IntelliJ Trick"]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_ide_api/src/completion/completion_context.rs#L72-L75 | ||
560 | [find ancestor fn node]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_ide_api/src/completion/completion_context.rs#L116-L120 | ||
561 | [semantic model]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_ide_api/src/completion/completion_context.rs#L123 | ||
562 | [series of independent completion routines]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_ide_api/src/completion.rs#L52-L59 | ||
563 | [`complete_dot`]: https://github.com/rust-analyzer/rust-analyzer/blob/guide-2019-01/crates/ra_ide_api/src/completion/complete_dot.rs#L6-L22 | ||