1 //! Completes environment variables defined by Cargo (https://doc.rust-lang.org/cargo/reference/environment-variables.html)
3 use syntax::{ast, AstToken, AstNode, TextRange, TextSize};
5 use crate::{context::CompletionContext, CompletionItem, CompletionItemKind};
7 use super::Completions;
9 pub(crate) fn complete_cargo_env_vars(
10 acc: &mut Completions,
11 ctx: &CompletionContext<'_>,
12 original: &ast::String
14 if !is_env_macro(original) {
18 let start = ctx.original_token.text_range().start() + TextSize::from(1);
19 let cursor = ctx.position.offset;
21 CompletionItem::new(CompletionItemKind::Binding, TextRange::new(start, cursor), "CARGO").add_to(acc);
24 fn is_env_macro(string: &ast::String) -> bool {
25 //todo: replace copypaste from format_string with separate function
27 let macro_call = string.syntax().parent_ancestors().find_map(ast::MacroCall::cast)?;
28 let name = macro_call.path()?.segment()?.name_ref()?;
30 if !matches!(name.text().as_str(),
31 "env" | "option_env") {
43 use expect_test::{expect, Expect};
44 use crate::tests::{check_edit};
47 fn completes_env_variables() {
51 let foo = env!("CA$0);
56 let foo = env!("CARGO);