aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-20 12:43:43 +0000
committerAleksey Kladov <[email protected]>2019-01-21 08:27:01 +0000
commitc33202ec8245786932795478d650b686b136f913 (patch)
treec10fde8aa1f8f2e5587b75997a910ef17dc37452
parent6789e5a6e788c06ab4605bf642b545243ee9ae7d (diff)
complete completion section
-rw-r--r--guide.md34
1 files changed, 34 insertions, 0 deletions
diff --git a/guide.md b/guide.md
index f0a2d60b8..12db0a82d 100644
--- a/guide.md
+++ b/guide.md
@@ -524,6 +524,40 @@ message as a request for completion and [schedule it on the threadpool]. This is
524the place where we [catch] canceled error if, immediately after completion, the 524the place where we [catch] canceled error if, immediately after completion, the
525client sends some modification. 525client sends some modification.
526 526
527In [the handler] we deserialize LSP request into rust-analyzer specific data
528types (by converting a file url into a numeric `FileId`), [ask analysis for
529completion] and serializer results to LSP.
530
531[Completion implementation] is finally the place where we start doing the actual
532work. The first step is to collection `CompletionContext` -- a struct which
533describes the cursor position in terms of Rust syntax and semantics. For
534example, `function_syntax: Option<&'a ast::FnDef>` stores a reference to
535enclosing function *syntax*, while `function: Option<hir::Function>` is the
536`Def` for this function.
537
538To construct the context, we first do an ["IntelliJ Trick"]: we insert a dummy
539identifier at the cursor's position and parse this modified file, to get a
540reasonably looking syntax tree. Then we do a bunch of "classification" routines
541to 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
544The second step is to run a [series of independent completion routines]. Let's
545take 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
547expression out of the `Context`. Then we run type-inference for this single
548function and map our syntactic expression to `ExprId`. Using the id, we figure
549out the type of the receiver expression. Then we add all fields & methods from
550the 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