Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions crates/ide-completion/src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,30 @@ impl Completions {
item.add_to(self, ctx.db);
}

pub(crate) fn add_keyword_with_colon(
&mut self,
ctx: &CompletionContext<'_, '_>,
keyword: &'static str,
) {
debug_assert!(!keyword.ends_with("::"));
let label = syntax::format_smolstr!("{keyword}::");
let mut item = CompletionItem::new(
CompletionItemKind::Keyword,
ctx.source_range(),
label.clone(),
ctx.edition,
);
if ctx.token.next_token().is_some_and(|it| it.kind() == syntax::T![::]) {
item.insert_text(keyword);
}
item.add_to(self, ctx.db);
}

pub(crate) fn add_nameref_keywords_with_colon(&mut self, ctx: &CompletionContext<'_, '_>) {
["self::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
["self", "crate"].into_iter().for_each(|kw| self.add_keyword_with_colon(ctx, kw));

if ctx.depth_from_crate_root > 0 {
self.add_keyword(ctx, "super::");
self.add_keyword_with_colon(ctx, "super");
}
}

Expand Down Expand Up @@ -152,7 +171,7 @@ impl Completions {
&& len > 0
&& len < ctx.depth_from_crate_root
{
self.add_keyword(ctx, "super::");
self.add_keyword_with_colon(ctx, "super");
}
}

Expand Down
8 changes: 7 additions & 1 deletion crates/ide-completion/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,18 @@ pub(crate) fn render_type_keyword_snippet(
ctx: &CompletionContext<'_, '_>,
path_ctx: &PathCompletionCtx<'_>,
label: &str,
snippet: &str,
mut snippet: &str,
) -> Builder {
let source_range = ctx.source_range();
let mut item =
CompletionItem::new(CompletionItemKind::Keyword, source_range, label, ctx.edition);

if let Some(keyword) = snippet.strip_suffix("::")
&& ctx.token.next_token().is_some_and(|it| it.kind() == syntax::T![::])
{
snippet = keyword;
}

let insert_text = if !snippet.contains('$') {
item.insert_text(snippet);
snippet
Expand Down
23 changes: 23 additions & 0 deletions crates/ide-completion/src/tests/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,29 @@ fn foo() { i!(module) }"#,
);
}

#[test]
fn complete_self_kw() {
check_edit("self::", r#"fn foo() { $0 }"#, r#"fn foo() { self:: }"#);

check_edit("self::", r#"fn foo() -> $0 {}"#, r#"fn foo() -> self:: {}"#);

check_edit("self::", r#"fn foo() { $0::bar }"#, r#"fn foo() { self::bar }"#);

check_edit("self::", r#"fn foo() -> $0::bar {}"#, r#"fn foo() -> self::bar {}"#);

check_edit("self::", r#"fn foo() { $0bar::baz }"#, r#"fn foo() { self::bar::baz }"#);

check_edit(
"self::",
r#"
macro_rules! i { ($i:ident) => { $i::bar } }
fn foo() { i!($0) }"#,
r#"
macro_rules! i { ($i:ident) => { $i::bar } }
fn foo() { i!(self) }"#,
);
}

#[test]
fn else_completion_after_if() {
check(
Expand Down
Loading