rust/crates
bors[bot] 19dd1fd4d4
Merge #7904
7904: Improved completion sorting r=JoshMcguigan a=JoshMcguigan

I was working on extending #3954 to apply completion scores in more places (I'll have another PR open for that soon) when I discovered that actually completion sorting was not working for me at all in `coc.nvim`. This led me down a bit of a rabbit hole of how coc and vs code each sort completion items.

Before this PR, rust-analyzer was setting the `sortText` field on completion items to `None` if we hadn't applied any completion score for that item, or to the label of the item with a leading whitespace character if we had applied any completion score. Completion score is defined in rust-analyzer as an enum with two variants, `TypeMatch` and `TypeAndNameMatch`. 

In vs code the above strategy works, because if `sortText` isn't set [they default it to the label](b4ead4ed66). However, coc [does not do this](e211e36147/src/completion/complete.ts (L245)).

I was going to file a bug report against coc, but I read the [LSP spec for the `sortText` field](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion) and I feel like it is ambiguous and coc could claim what they do is a valid interpretation of the spec.

Further, the existing rust-analyzer behavior of prepending a leading whitespace character for completion items with any completion score does not handle sorting `TypeAndNameMatch` completions above `TypeMatch` completions. They were both being treated the same.

The first change this PR makes is to set the `sortText` field to either "1" for `TypeAndNameMatch` completions, "2" for `TypeMatch` completions, or "3" for completions which are neither of those. This change works around the potential ambiguity in the LSP spec and fixes completion sorting for users of coc. It also allows `TypeAndNameMatch` items to be sorted above just `TypeMatch` items (of course both of these will be sorted above completion items without a score). 

The second change this PR makes is to use the actual completion scores for ref matches. The existing code ignored the actual score and always assumed these would be a high priority completion item.

#### Before

Here coc just sorts based on how close the items are in the file.

![image](https://user-images.githubusercontent.com/22216761/110249880-46063580-7f2d-11eb-9233-91a2bbd48238.png)

#### After

Here we correctly get `zzz` first, since that is both a type and name match. Then we get `ccc` which is just a type match.

![image](https://user-images.githubusercontent.com/22216761/110249883-4e5e7080-7f2d-11eb-9269-a3bc133fdee7.png)


Co-authored-by: Josh Mcguigan <joshmcg88@gmail.com>
2021-03-12 14:23:32 +00:00
..
base_db Fixed remaining references to AnalysisChange (now: Change) 2021-02-28 12:57:41 +01:00
cfg Upgrade expect-test to 1.1 2021-01-03 14:43:29 +01:00
flycheck Bump cargo_metadata 2021-03-02 14:27:29 +02:00
hir Avoid double text edits when renaming mod declaration 2021-03-10 15:49:01 +01:00
hir_def Prefer names from outer DefMap over extern prelude 2021-03-10 16:33:18 +01:00
hir_expand Use expect-test for builtin macro/derive tests 2021-03-10 21:05:02 +01:00
hir_ty Prefer names from outer DefMap over extern prelude 2021-03-10 16:33:18 +01:00
ide remove unused CompletionScore enum 2021-03-12 06:16:04 -08:00
ide_assists fix: add semicolon after type ascription 2021-03-11 10:36:45 +00:00
ide_completion update relevance score u8 -> u32 2021-03-12 06:16:04 -08:00
ide_db Compilation speed 2021-03-09 22:30:58 +03:00
ide_ssr Merge #7961 2021-03-10 17:06:11 +00:00
mbe Compilation speed 2021-03-09 22:30:58 +03:00
parser Fix remaining references to cargo xtask codegen 2021-03-12 15:10:33 +01:00
paths Document paths items 2021-01-22 15:38:33 +01:00
proc_macro_api use doc-comments 2021-03-10 04:54:31 +08:00
proc_macro_srv Compilation speed 2021-03-09 22:30:58 +03:00
proc_macro_test Add description for crates that will be published 2020-08-24 13:07:22 +02:00
profile add more counts 2021-01-27 12:39:19 +03:00
project_model Never run cargo check on the rustc source 2021-03-08 16:42:18 +00:00
rust-analyzer update relevance score u8 -> u32 2021-03-12 06:16:04 -08:00
stdx Cleanup decl_check 2021-02-05 16:09:45 +01:00
syntax cargo update and lexer 2021-03-10 13:47:12 -05:00
test_utils Use upstream cov-mark 2021-03-08 22:19:44 +02:00
text_edit Avoid turning completion objects into builders 2020-11-16 23:16:41 +02:00
toolchain Add description for crates that will be published 2020-08-24 13:07:22 +02:00
tt Refactor TokenBuffer for reduc cloning 2021-01-05 02:00:46 +08:00
vfs Fix slow tests sometimes failing 2021-02-12 16:31:16 +01:00
vfs-notify Fix slow tests sometimes failing 2021-02-12 16:31:16 +01:00