Skip to content
Merged
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
50 changes: 45 additions & 5 deletions crates/ide-completion/src/completions/env_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ pub(crate) fn complete_cargo_env_vars(
original: &ast::String,
expanded: &ast::String,
) -> Option<()> {
let is_in_env_expansion = ctx
.sema
.hir_file_for(&expanded.syntax().parent()?)
.macro_file()
.is_some_and(|it| it.is_env_or_option_env(ctx.sema.db));
let descends = ctx.sema.descend_into_macros_exact_with_file(original.syntax().clone());
let macro_file = descends.first()?.file_id.macro_file();

let is_in_env_expansion = macro_file.is_some_and(|it| it.is_env_or_option_env(ctx.sema.db));
if !is_in_env_expansion {
let call = macro_call_for_string_token(expanded)?;
let makro = ctx.sema.resolve_macro_call(&call)?;
Expand Down Expand Up @@ -116,6 +115,47 @@ fn main() {
);
}

#[test]
fn complete_in_expanded_env_macro() {
check_edit(
"CARGO_BIN_NAME",
r#"
//- minicore: env
macro_rules! bar {
($($arg:tt)*) => { $($arg)* }
}

fn main() {
let foo = bar!(env!("CA$0"));
}
"#,
r#"
macro_rules! bar {
($($arg:tt)*) => { $($arg)* }
}

fn main() {
let foo = bar!(env!("CARGO_BIN_NAME"));
}
"#,
);

check_edit(
"CARGO_BIN_NAME",
r#"
//- minicore: env, fmt
fn main() {
let foo = format_args!("{}", env!("CA$0"));
}
"#,
r#"
fn main() {
let foo = format_args!("{}", env!("CARGO_BIN_NAME"));
}
"#,
);
}

#[test]
fn doesnt_complete_in_random_strings() {
let fixture = r#"
Expand Down