mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
Merge #5225
5225: Alight details in comkplation list r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
c815d5b496
9 changed files with 531 additions and 1196 deletions
|
@ -2,6 +2,9 @@ mod completion_config;
|
|||
mod completion_item;
|
||||
mod completion_context;
|
||||
mod presentation;
|
||||
mod patterns;
|
||||
#[cfg(test)]
|
||||
mod test_utils;
|
||||
|
||||
mod complete_attribute;
|
||||
mod complete_dot;
|
||||
|
@ -15,9 +18,6 @@ mod complete_unqualified_path;
|
|||
mod complete_postfix;
|
||||
mod complete_macro_in_item_position;
|
||||
mod complete_trait_impl;
|
||||
mod patterns;
|
||||
#[cfg(test)]
|
||||
mod test_utils;
|
||||
|
||||
use ra_ide_db::RootDatabase;
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ fn foo(s: S) { s.<|> }
|
|||
"#,
|
||||
expect![[r#"
|
||||
me bar() fn bar(&self)
|
||||
fd foo u32
|
||||
fd foo u32
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ impl S {
|
|||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
me foo() fn foo(self)
|
||||
me foo() fn foo(self)
|
||||
fd the_field (u32,)
|
||||
"#]],
|
||||
)
|
||||
|
@ -114,7 +114,7 @@ impl A {
|
|||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
me foo() fn foo(&self)
|
||||
me foo() fn foo(&self)
|
||||
fd the_field (u32, i32)
|
||||
"#]],
|
||||
)
|
||||
|
@ -148,7 +148,7 @@ fn foo(a: inner::A) { a.<|> }
|
|||
"#,
|
||||
expect![[r#"
|
||||
fd crate_field u32
|
||||
fd pub_field u32
|
||||
fd pub_field u32
|
||||
fd super_field u32
|
||||
"#]],
|
||||
);
|
||||
|
|
|
@ -216,17 +216,17 @@ mod tests {
|
|||
check(
|
||||
r"use a::<|>",
|
||||
expect![[r#"
|
||||
kw self
|
||||
kw super::
|
||||
"#]],
|
||||
kw self
|
||||
kw super::
|
||||
"#]],
|
||||
);
|
||||
|
||||
check(
|
||||
r"use a::{b, <|>}",
|
||||
expect![[r#"
|
||||
kw self
|
||||
kw super::
|
||||
"#]],
|
||||
kw self
|
||||
kw super::
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ fn foo() {
|
|||
expect![[r#"
|
||||
st Bar
|
||||
en E
|
||||
ev X ()
|
||||
ev X ()
|
||||
ct Z
|
||||
md m
|
||||
"#]],
|
||||
|
|
|
@ -260,14 +260,14 @@ fn main() {
|
|||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
sn box Box::new(expr)
|
||||
sn call function(expr)
|
||||
sn dbg dbg!(expr)
|
||||
sn if if expr {}
|
||||
sn box Box::new(expr)
|
||||
sn call function(expr)
|
||||
sn dbg dbg!(expr)
|
||||
sn if if expr {}
|
||||
sn match match expr {}
|
||||
sn not !expr
|
||||
sn ref &expr
|
||||
sn refm &mut expr
|
||||
sn not !expr
|
||||
sn ref &expr
|
||||
sn refm &mut expr
|
||||
sn while while expr {}
|
||||
"#]],
|
||||
);
|
||||
|
@ -283,12 +283,12 @@ fn main() {
|
|||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
sn box Box::new(expr)
|
||||
sn call function(expr)
|
||||
sn dbg dbg!(expr)
|
||||
sn box Box::new(expr)
|
||||
sn call function(expr)
|
||||
sn dbg dbg!(expr)
|
||||
sn match match expr {}
|
||||
sn ref &expr
|
||||
sn refm &mut expr
|
||||
sn ref &expr
|
||||
sn refm &mut expr
|
||||
"#]],
|
||||
)
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -70,95 +70,47 @@ fn ${1:feature}() {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
|
||||
use insta::assert_debug_snapshot;
|
||||
use expect::{expect, Expect};
|
||||
|
||||
fn do_snippet_completion(code: &str) -> Vec<CompletionItem> {
|
||||
do_completion(code, CompletionKind::Snippet)
|
||||
use crate::completion::{test_utils::completion_list, CompletionKind};
|
||||
|
||||
fn check(ra_fixture: &str, expect: Expect) {
|
||||
let actual = completion_list(ra_fixture, CompletionKind::Snippet);
|
||||
expect.assert_eq(&actual)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_snippets_in_expressions() {
|
||||
assert_debug_snapshot!(
|
||||
do_snippet_completion(r"fn foo(x: i32) { <|> }"),
|
||||
@r###"
|
||||
[
|
||||
CompletionItem {
|
||||
label: "pd",
|
||||
source_range: 17..17,
|
||||
delete: 17..17,
|
||||
insert: "eprintln!(\"$0 = {:?}\", $0);",
|
||||
kind: Snippet,
|
||||
},
|
||||
CompletionItem {
|
||||
label: "ppd",
|
||||
source_range: 17..17,
|
||||
delete: 17..17,
|
||||
insert: "eprintln!(\"$0 = {:#?}\", $0);",
|
||||
kind: Snippet,
|
||||
},
|
||||
]
|
||||
"###
|
||||
);
|
||||
check(
|
||||
r#"fn foo(x: i32) { <|> }"#,
|
||||
expect![[r#"
|
||||
sn pd
|
||||
sn ppd
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_not_complete_snippets_in_path() {
|
||||
assert_debug_snapshot!(
|
||||
do_snippet_completion(r"fn foo(x: i32) { ::foo<|> }"),
|
||||
@"[]"
|
||||
);
|
||||
assert_debug_snapshot!(
|
||||
do_snippet_completion(r"fn foo(x: i32) { ::<|> }"),
|
||||
@"[]"
|
||||
);
|
||||
check(r#"fn foo(x: i32) { ::foo<|> }"#, expect![[""]]);
|
||||
check(r#"fn foo(x: i32) { ::<|> }"#, expect![[""]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_snippets_in_items() {
|
||||
assert_debug_snapshot!(
|
||||
do_snippet_completion(
|
||||
r"
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
<|>
|
||||
}
|
||||
"
|
||||
),
|
||||
@r###"
|
||||
[
|
||||
CompletionItem {
|
||||
label: "Test function",
|
||||
source_range: 29..29,
|
||||
delete: 29..29,
|
||||
insert: "#[test]\nfn ${1:feature}() {\n $0\n}",
|
||||
kind: Snippet,
|
||||
lookup: "tfn",
|
||||
},
|
||||
CompletionItem {
|
||||
label: "Test module",
|
||||
source_range: 29..29,
|
||||
delete: 29..29,
|
||||
insert: "#[cfg(test)]\nmod tests {\n use super::*;\n\n #[test]\n fn ${1:test_name}() {\n $0\n }\n}",
|
||||
kind: Snippet,
|
||||
lookup: "tmod",
|
||||
},
|
||||
CompletionItem {
|
||||
label: "macro_rules",
|
||||
source_range: 29..29,
|
||||
delete: 29..29,
|
||||
insert: "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}",
|
||||
kind: Snippet,
|
||||
},
|
||||
CompletionItem {
|
||||
label: "pub(crate)",
|
||||
source_range: 29..29,
|
||||
delete: 29..29,
|
||||
insert: "pub(crate) $0",
|
||||
kind: Snippet,
|
||||
},
|
||||
]
|
||||
"###
|
||||
);
|
||||
check(
|
||||
r#"
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
<|>
|
||||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
sn Test function
|
||||
sn Test module
|
||||
sn macro_rules
|
||||
sn pub(crate)
|
||||
"#]],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -683,6 +683,57 @@ impl S {
|
|||
},
|
||||
]
|
||||
"#]],
|
||||
);
|
||||
|
||||
check(
|
||||
r#"
|
||||
use self::my<|>;
|
||||
|
||||
/// mod docs
|
||||
mod my { }
|
||||
|
||||
/// enum docs
|
||||
enum E {
|
||||
/// variant docs
|
||||
V
|
||||
}
|
||||
use self::E::*;
|
||||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
CompletionItem {
|
||||
label: "E",
|
||||
source_range: 10..12,
|
||||
delete: 10..12,
|
||||
insert: "E",
|
||||
kind: Enum,
|
||||
documentation: Documentation(
|
||||
"enum docs",
|
||||
),
|
||||
},
|
||||
CompletionItem {
|
||||
label: "V",
|
||||
source_range: 10..12,
|
||||
delete: 10..12,
|
||||
insert: "V",
|
||||
kind: EnumVariant,
|
||||
detail: "()",
|
||||
documentation: Documentation(
|
||||
"variant docs",
|
||||
),
|
||||
},
|
||||
CompletionItem {
|
||||
label: "my",
|
||||
source_range: 10..12,
|
||||
delete: 10..12,
|
||||
insert: "my",
|
||||
kind: Module,
|
||||
documentation: Documentation(
|
||||
"mod docs",
|
||||
),
|
||||
},
|
||||
]
|
||||
"#]],
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -43,12 +43,21 @@ pub(crate) fn completion_list_with_config(
|
|||
.filter(|c| c.completion_kind == kind)
|
||||
.collect();
|
||||
kind_completions.sort_by_key(|c| c.label().to_owned());
|
||||
let label_width = kind_completions
|
||||
.iter()
|
||||
.map(|it| monospace_width(it.label()))
|
||||
.max()
|
||||
.unwrap_or_default()
|
||||
.min(16);
|
||||
kind_completions
|
||||
.into_iter()
|
||||
.map(|it| {
|
||||
let mut buf = format!("{} {}", it.kind().unwrap().tag(), it.label());
|
||||
let tag = it.kind().unwrap().tag();
|
||||
let var_name = format!("{} {}", tag, it.label());
|
||||
let mut buf = var_name;
|
||||
if let Some(detail) = it.detail() {
|
||||
format_to!(buf, " {}", detail);
|
||||
let width = label_width.saturating_sub(monospace_width(it.label()));
|
||||
format_to!(buf, "{:width$} {}", "", detail, width = width);
|
||||
}
|
||||
format_to!(buf, "\n");
|
||||
buf
|
||||
|
@ -56,6 +65,10 @@ pub(crate) fn completion_list_with_config(
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn monospace_width(s: &str) -> usize {
|
||||
s.chars().count()
|
||||
}
|
||||
|
||||
pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||
check_edit_with_config(what, ra_fixture_before, ra_fixture_after, &CompletionConfig::default())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue