about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbtwotwo <10519967+btwotwo@users.noreply.github.com>2022-10-05 00:02:03 +0200
committerbtwotwo <10519967+btwotwo@users.noreply.github.com>2022-10-06 16:32:19 +0200
commit75f4c54d8c1304966dff2e1d45cff88ac96a9d0f (patch)
tree0932f43760636104e6fb3a4e74235bebde2e2d38
parenta415fb4c4e00da1b4138f5a7787ae9838e8ab576 (diff)
downloadrust-75f4c54d8c1304966dff2e1d45cff88ac96a9d0f.tar.gz
rust-75f4c54d8c1304966dff2e1d45cff88ac96a9d0f.zip
Add stub for cargo environment variables auto completion
-rw-r--r--crates/ide-completion/src/completions.rs1
-rw-r--r--crates/ide-completion/src/completions/env_vars.rs60
-rw-r--r--crates/ide-completion/src/lib.rs1
3 files changed, 62 insertions, 0 deletions
diff --git a/crates/ide-completion/src/completions.rs b/crates/ide-completion/src/completions.rs
index 97b90c62dd7..296dfc14250 100644
--- a/crates/ide-completion/src/completions.rs
+++ b/crates/ide-completion/src/completions.rs
@@ -19,6 +19,7 @@ pub(crate) mod snippet;
 pub(crate) mod r#type;
 pub(crate) mod use_;
 pub(crate) mod vis;
+pub(crate) mod env_vars;
 
 use std::iter;
 
diff --git a/crates/ide-completion/src/completions/env_vars.rs b/crates/ide-completion/src/completions/env_vars.rs
new file mode 100644
index 00000000000..6cfbd0f922f
--- /dev/null
+++ b/crates/ide-completion/src/completions/env_vars.rs
@@ -0,0 +1,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);
+            }
+        "#)
+    }
+}
\ No newline at end of file
diff --git a/crates/ide-completion/src/lib.rs b/crates/ide-completion/src/lib.rs
index 8d21f4fce0a..cd9da0d3dcf 100644
--- a/crates/ide-completion/src/lib.rs
+++ b/crates/ide-completion/src/lib.rs
@@ -183,6 +183,7 @@ pub fn completions(
             CompletionAnalysis::String { original, expanded: Some(expanded) } => {
                 completions::extern_abi::complete_extern_abi(acc, ctx, expanded);
                 completions::format_string::format_string(acc, ctx, original, expanded);
+                completions::env_vars::complete_cargo_env_vars(acc, ctx, original)
             }
             CompletionAnalysis::UnexpandedAttrTT {
                 colon_prefix,