mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
use builder interface for completion item
This commit is contained in:
parent
b0ff6176ed
commit
b5c5995bf1
4 changed files with 40 additions and 59 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -734,7 +734,7 @@ dependencies = [
|
||||||
"serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde_derive 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde_derive 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"smol_str 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"test_utils 0.1.0",
|
"test_utils 0.1.0",
|
||||||
"text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -959,7 +959,7 @@ version = "0.1.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"smol_str 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"text_unit 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -1092,7 +1092,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smol_str"
|
name = "smol_str"
|
||||||
version = "0.1.7"
|
version = "0.1.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -1565,7 +1565,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
"checksum sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51b9d1f3b5de8a167ab06834a7c883bd197f2191e1dda1a22d9ccfeedbf9aded"
|
"checksum sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51b9d1f3b5de8a167ab06834a7c883bd197f2191e1dda1a22d9ccfeedbf9aded"
|
||||||
"checksum slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373"
|
"checksum slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373"
|
||||||
"checksum smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b73ea3738b47563803ef814925e69be00799a8c07420be8b996f8e98fb2336db"
|
"checksum smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b73ea3738b47563803ef814925e69be00799a8c07420be8b996f8e98fb2336db"
|
||||||
"checksum smol_str 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f3ed6f19b800d76574926e458d5f8e2dbea86c2b58c08d33a982448f09ac8d0c"
|
"checksum smol_str 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "486a74e9b9fc53373808f7a17e10fc728adcb1fbe272292271d8bea61175e181"
|
||||||
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
|
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
|
||||||
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
|
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
|
||||||
"checksum superslice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b50b13d42370e0f5fc62eafdd5c2d20065eaf5458dab215ff3e20e63eea96b30"
|
"checksum superslice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b50b13d42370e0f5fc62eafdd5c2d20065eaf5458dab215ff3e20e63eea96b30"
|
||||||
|
|
|
@ -428,7 +428,7 @@ mod tests {
|
||||||
<|>
|
<|>
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
r##"[CompletionItem { label: "Test function", lookup: Some("tfn"), snippet: Some("#[test]\nfn ${1:feature}() {\n$0\n}") },
|
r##"[CompletionItem { label: "Test function", lookup: Some("tfn"), snippet: Some("#[test]\nfn ${1:feature}() {\n $0\n}") },
|
||||||
CompletionItem { label: "pub(crate)", lookup: None, snippet: Some("pub(crate) $0") }]"##,
|
CompletionItem { label: "pub(crate)", lookup: None, snippet: Some("pub(crate) $0") }]"##,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ impl CompletionItem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub(crate) struct Builder {
|
pub(crate) struct Builder {
|
||||||
label: String,
|
label: String,
|
||||||
lookup: Option<String>,
|
lookup: Option<String>,
|
||||||
|
@ -41,4 +42,8 @@ impl Builder {
|
||||||
self.lookup = Some(lookup.into());
|
self.lookup = Some(lookup.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
pub fn snippet(mut self, snippet: impl Into<String>) -> Builder {
|
||||||
|
self.snippet = Some(snippet.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,23 +125,13 @@ fn classify_name_ref(name_ref: ast::NameRef) -> Option<NameRefKind> {
|
||||||
|
|
||||||
fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) {
|
fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) {
|
||||||
let mut shadowed = FxHashSet::default();
|
let mut shadowed = FxHashSet::default();
|
||||||
acc.extend(
|
|
||||||
scopes
|
scopes
|
||||||
.scope_chain(name_ref.syntax())
|
.scope_chain(name_ref.syntax())
|
||||||
.flat_map(|scope| scopes.entries(scope).iter())
|
.flat_map(|scope| scopes.entries(scope).iter())
|
||||||
.filter(|entry| shadowed.insert(entry.name()))
|
.filter(|entry| shadowed.insert(entry.name()))
|
||||||
.map(|entry| CompletionItem {
|
.for_each(|entry| CompletionItem::new(entry.name().to_string()).add_to(acc));
|
||||||
label: entry.name().to_string(),
|
|
||||||
lookup: None,
|
|
||||||
snippet: None,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
if scopes.self_param.is_some() {
|
if scopes.self_param.is_some() {
|
||||||
acc.push(CompletionItem {
|
CompletionItem::new("self").add_to(acc);
|
||||||
label: "self".to_string(),
|
|
||||||
lookup: None,
|
|
||||||
snippet: None,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,32 +154,26 @@ fn complete_path(
|
||||||
_ => return Ok(()),
|
_ => return Ok(()),
|
||||||
};
|
};
|
||||||
let module_scope = target_module.scope(db)?;
|
let module_scope = target_module.scope(db)?;
|
||||||
let completions = module_scope.entries().map(|(name, _res)| CompletionItem {
|
module_scope
|
||||||
label: name.to_string(),
|
.entries()
|
||||||
lookup: None,
|
.for_each(|(name, _res)| CompletionItem::new(name.to_string()).add_to(acc));
|
||||||
snippet: None,
|
|
||||||
});
|
|
||||||
acc.extend(completions);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn complete_mod_item_snippets(acc: &mut Vec<CompletionItem>) {
|
fn complete_mod_item_snippets(acc: &mut Vec<CompletionItem>) {
|
||||||
acc.push(CompletionItem {
|
CompletionItem::new("Test function")
|
||||||
label: "Test function".to_string(),
|
.lookup_by("tfn")
|
||||||
lookup: Some("tfn".to_string()),
|
.snippet(
|
||||||
snippet: Some(
|
"\
|
||||||
"#[test]\n\
|
#[test]
|
||||||
fn ${1:feature}() {\n\
|
fn ${1:feature}() {
|
||||||
$0\n\
|
$0
|
||||||
}"
|
}",
|
||||||
.to_string(),
|
)
|
||||||
),
|
.add_to(acc);
|
||||||
});
|
CompletionItem::new("pub(crate)")
|
||||||
acc.push(CompletionItem {
|
.snippet("pub(crate) $0")
|
||||||
label: "pub(crate)".to_string(),
|
.add_to(acc);
|
||||||
lookup: None,
|
|
||||||
snippet: Some("pub(crate) $0".to_string()),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn complete_expr_keywords(
|
fn complete_expr_keywords(
|
||||||
|
@ -270,23 +254,15 @@ fn complete_return(fn_def: ast::FnDef, name_ref: ast::NameRef) -> Option<Complet
|
||||||
Some(keyword("return", snip))
|
Some(keyword("return", snip))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn keyword(kw: &str, snip: &str) -> CompletionItem {
|
fn keyword(kw: &str, snippet: &str) -> CompletionItem {
|
||||||
CompletionItem {
|
CompletionItem::new(kw).snippet(snippet).build()
|
||||||
label: kw.to_string(),
|
|
||||||
lookup: None,
|
|
||||||
snippet: Some(snip.to_string()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn complete_expr_snippets(acc: &mut Vec<CompletionItem>) {
|
fn complete_expr_snippets(acc: &mut Vec<CompletionItem>) {
|
||||||
acc.push(CompletionItem {
|
CompletionItem::new("pd")
|
||||||
label: "pd".to_string(),
|
.snippet("eprintln!(\"$0 = {:?}\", $0);")
|
||||||
lookup: None,
|
.add_to(acc);
|
||||||
snippet: Some("eprintln!(\"$0 = {:?}\", $0);".to_string()),
|
CompletionItem::new("ppd")
|
||||||
});
|
.snippet("eprintln!(\"$0 = {:#?}\", $0);")
|
||||||
acc.push(CompletionItem {
|
.add_to(acc);
|
||||||
label: "ppd".to_string(),
|
|
||||||
lookup: None,
|
|
||||||
snippet: Some("eprintln!(\"$0 = {:#?}\", $0);".to_string()),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue