Deduplicate
This commit is contained in:
parent
522f66545f
commit
2a60b8452e
@ -111,6 +111,26 @@ impl Completions {
|
|||||||
["self", "super", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
|
["self", "super", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn add_keyword_snippet(&mut self, ctx: &CompletionContext, kw: &str, snippet: &str) {
|
||||||
|
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
|
||||||
|
|
||||||
|
match ctx.config.snippet_cap {
|
||||||
|
Some(cap) => {
|
||||||
|
if snippet.ends_with('}') && ctx.incomplete_let {
|
||||||
|
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
|
||||||
|
cov_mark::hit!(let_semi);
|
||||||
|
item.insert_snippet(cap, format!("{};", snippet));
|
||||||
|
} else {
|
||||||
|
item.insert_snippet(cap, snippet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
item.insert_text(if snippet.contains('$') { kw } else { snippet });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
item.add_to(self);
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
|
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
|
||||||
ctx.process_all_names(&mut |name, res| match res {
|
ctx.process_all_names(&mut |name, res| match res {
|
||||||
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
|
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
|
||||||
|
@ -178,8 +178,7 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
|
|||||||
});
|
});
|
||||||
|
|
||||||
if !is_func_update {
|
if !is_func_update {
|
||||||
let mut add_keyword =
|
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
|
||||||
|kw, snippet| super::keyword::add_keyword(acc, ctx, kw, snippet);
|
|
||||||
|
|
||||||
if ctx.expects_expression() {
|
if ctx.expects_expression() {
|
||||||
if !in_block_expr {
|
if !in_block_expr {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::{IdentContext, NameContext, NameKind, NameRefContext, PathCompletionCtx, PathKind},
|
context::{IdentContext, NameContext, NameKind, NameRefContext, PathCompletionCtx, PathKind},
|
||||||
CompletionContext, CompletionItem, CompletionItemKind, Completions,
|
CompletionContext, Completions,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
@ -22,7 +22,7 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext
|
|||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
if ctx.qualifier_ctx.vis_node.is_none() {
|
if ctx.qualifier_ctx.vis_node.is_none() {
|
||||||
let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet);
|
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
|
||||||
add_keyword("pub(crate)", "pub(crate)");
|
add_keyword("pub(crate)", "pub(crate)");
|
||||||
add_keyword("pub(super)", "pub(super)");
|
add_keyword("pub(super)", "pub(super)");
|
||||||
add_keyword("pub", "pub");
|
add_keyword("pub", "pub");
|
||||||
@ -31,23 +31,3 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext
|
|||||||
_ => return,
|
_ => return,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) {
|
|
||||||
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
|
|
||||||
|
|
||||||
match ctx.config.snippet_cap {
|
|
||||||
Some(cap) => {
|
|
||||||
if snippet.ends_with('}') && ctx.incomplete_let {
|
|
||||||
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
|
|
||||||
cov_mark::hit!(let_semi);
|
|
||||||
item.insert_snippet(cap, format!("{};", snippet));
|
|
||||||
} else {
|
|
||||||
item.insert_snippet(cap, snippet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
item.insert_text(if snippet.contains('$') { kw } else { snippet });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
item.add_to(acc);
|
|
||||||
}
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
completions::module_or_fn_macro,
|
completions::module_or_fn_macro,
|
||||||
context::{ItemListKind, PathCompletionCtx, PathKind, PathQualifierCtx},
|
context::{ItemListKind, PathCompletionCtx, PathKind, PathQualifierCtx},
|
||||||
CompletionContext, CompletionItem, CompletionItemKind, Completions,
|
CompletionContext, Completions,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
@ -24,7 +24,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
|
|||||||
}) => (is_absolute_path, qualifier, None),
|
}) => (is_absolute_path, qualifier, None),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet);
|
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
|
||||||
|
|
||||||
let in_item_list = matches!(kind, Some(ItemListKind::SourceFile | ItemListKind::Module) | None);
|
let in_item_list = matches!(kind, Some(ItemListKind::SourceFile | ItemListKind::Module) | None);
|
||||||
let in_assoc_non_trait_impl = matches!(kind, Some(ItemListKind::Impl | ItemListKind::Trait));
|
let in_assoc_non_trait_impl = matches!(kind, Some(ItemListKind::Impl | ItemListKind::Trait));
|
||||||
@ -121,23 +121,3 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
|
|||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) {
|
|
||||||
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
|
|
||||||
|
|
||||||
match ctx.config.snippet_cap {
|
|
||||||
Some(cap) => {
|
|
||||||
if snippet.ends_with('}') && ctx.incomplete_let {
|
|
||||||
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
|
|
||||||
cov_mark::hit!(let_semi);
|
|
||||||
item.insert_snippet(cap, format!("{};", snippet));
|
|
||||||
} else {
|
|
||||||
item.insert_snippet(cap, snippet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
item.insert_text(if snippet.contains('$') { kw } else { snippet });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
item.add_to(acc);
|
|
||||||
}
|
|
||||||
|
@ -4,9 +4,7 @@
|
|||||||
|
|
||||||
use syntax::ast::Item;
|
use syntax::ast::Item;
|
||||||
|
|
||||||
use crate::{
|
use crate::{context::NameRefContext, CompletionContext, Completions};
|
||||||
context::NameRefContext, CompletionContext, CompletionItem, CompletionItemKind, Completions,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
let item = match ctx.nameref_ctx() {
|
let item = match ctx.nameref_ctx() {
|
||||||
@ -18,7 +16,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
|||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet);
|
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
|
||||||
|
|
||||||
match item {
|
match item {
|
||||||
Item::Impl(it) => {
|
Item::Impl(it) => {
|
||||||
@ -39,26 +37,6 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) {
|
|
||||||
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
|
|
||||||
|
|
||||||
match ctx.config.snippet_cap {
|
|
||||||
Some(cap) => {
|
|
||||||
if snippet.ends_with('}') && ctx.incomplete_let {
|
|
||||||
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
|
|
||||||
cov_mark::hit!(let_semi);
|
|
||||||
item.insert_snippet(cap, format!("{};", snippet));
|
|
||||||
} else {
|
|
||||||
item.insert_snippet(cap, snippet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
item.insert_text(if snippet.contains('$') { kw } else { snippet });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
item.add_to(acc);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user