Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! Completes environment variables defined by Cargo (https://doc.rust-lang.org/cargo/reference/environment-variables.html)

use syntax::{ast, AstToken, AstNode, TextRange, TextSize};

use crate::{context::CompletionContext, CompletionItem, CompletionItemKind};

use super::Completions;

pub(crate) fn complete_cargo_env_vars(
    acc: &mut Completions,
    ctx: &CompletionContext<'_>,
    original: &ast::String
) {
    if !is_env_macro(original) {
        return;
    }

    let start = ctx.original_token.text_range().start() + TextSize::from(1);
    let cursor = ctx.position.offset;

    CompletionItem::new(CompletionItemKind::Binding, TextRange::new(start, cursor), "CARGO").add_to(acc);
}

fn is_env_macro(string: &ast::String) -> bool {
    //todo: replace copypaste from format_string with separate function
    (|| {
        let macro_call = string.syntax().parent_ancestors().find_map(ast::MacroCall::cast)?;
        let name = macro_call.path()?.segment()?.name_ref()?;

        if !matches!(name.text().as_str(), 
        "env" | "option_env") {
            return None;
        }


        Some(())
    })()
    .is_some()
}

#[cfg(test)]
mod tests {
    use expect_test::{expect, Expect};
    use crate::tests::{check_edit};

    #[test]
    fn completes_env_variables() {
        check_edit("CARGO", 
        r#"
            fn main() {
                let foo = env!("CA$0);
            }
        "#
        ,r#"
            fn main() {
                let foo = env!("CARGO);
            }
        "#)
    }
}