]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/completions/env_vars.rs
Fix formatting for cargo vars list
[rust.git] / crates / ide-completion / src / completions / env_vars.rs
1 //! Completes environment variables defined by Cargo (https://doc.rust-lang.org/cargo/reference/environment-variables.html)
2 use hir::Semantics;
3 use ide_db::{syntax_helpers::node_ext::macro_call_for_string_token, RootDatabase};
4 use syntax::ast::{self, IsString};
5
6 use crate::{context::CompletionContext, CompletionItem, CompletionItemKind, completions::Completions};
7
8 const CARGO_DEFINED_VARS: &[(&str, &str)] = &[
9     ("CARGO","Path to the cargo binary performing the build"),
10     ("CARGO_MANIFEST_DIR","The directory containing the manifest of your package"),
11     ("CARGO_PKG_VERSION","The full version of your package"),
12     ("CARGO_PKG_VERSION_MAJOR","The major version of your package"),
13     ("CARGO_PKG_VERSION_MINOR","The minor version of your package"),
14     ("CARGO_PKG_VERSION_PATCH","The patch version of your package"),
15     ("CARGO_PKG_VERSION_PRE","The pre-release version of your package"),
16     ("CARGO_PKG_AUTHORS","Colon separated list of authors from the manifest of your package"),
17     ("CARGO_PKG_NAME","The name of your package"),
18     ("CARGO_PKG_DESCRIPTION","The description from the manifest of your package"),
19     ("CARGO_PKG_HOMEPAGE","The home page from the manifest of your package"),
20     ("CARGO_PKG_REPOSITORY","The repository from the manifest of your package"),
21     ("CARGO_PKG_LICENSE","The license from the manifest of your package"),
22     ("CARGO_PKG_LICENSE_FILE","The license file from the manifest of your package"),
23     ("CARGO_PKG_RUST_VERSION","The Rust version from the manifest of your package. Note that this is the minimum Rust version supported by the package, not the current Rust version"),
24     ("CARGO_CRATE_NAME","The name of the crate that is currently being compiled"),
25     ("CARGO_BIN_NAME","The name of the binary that is currently being compiled (if it is a binary). This name does not include any file extension, such as .exe"),
26     ("CARGO_PRIMARY_PACKAGE","This environment variable will be set if the package being built is primary. Primary packages are the ones the user selected on the command-line, either with -p flags or the defaults based on the current directory and the default workspace members. This environment variable will not be set when building dependencies. This is only set when compiling the package (not when running binaries or tests)"),
27     ("CARGO_TARGET_TMPDIR","Only set when building integration test or benchmark code. This is a path to a directory inside the target directory where integration tests or benchmarks are free to put any data needed by the tests/benches. Cargo initially creates this directory but doesn't manage its content in any way, this is the responsibility of the test code")
28 ];
29
30 pub(crate) fn complete_cargo_env_vars(
31     acc: &mut Completions,
32     ctx: &CompletionContext<'_>,
33     expanded: &ast::String,
34 ) -> Option<()> {
35     guard_env_macro(expanded, &ctx.sema)?;
36     let range = expanded.text_range_between_quotes()?;
37
38     CARGO_DEFINED_VARS.iter().for_each(|(var, detail)| {
39         let mut item = CompletionItem::new(CompletionItemKind::Keyword, range, var);
40         item.detail(*detail);
41         item.add_to(acc);
42     });
43
44     Some(())
45 }
46
47 fn guard_env_macro(
48     string: &ast::String,
49     semantics: &Semantics<'_, RootDatabase>,
50 ) -> Option<()> {
51     let call = macro_call_for_string_token(string)?;
52     let name = call.path()?.segment()?.name_ref()?;
53     let makro = semantics.resolve_macro_call(&call)?;
54     let db = semantics.db;
55
56     match name.text().as_str() {
57         "env" | "option_env" if makro.kind(db) == hir::MacroKind::BuiltIn => Some(()),
58         _ => None,
59     }
60 }
61
62 #[cfg(test)]
63 mod tests {
64     use crate::tests::{check_edit, completion_list};
65
66     fn check(macro_name: &str) {
67         check_edit(
68             "CARGO_BIN_NAME",
69             &format!(
70                 r#"
71             #[rustc_builtin_macro]
72             macro_rules! {} {{
73                 ($var:literal) => {{ 0 }}
74             }}
75
76             fn main() {{
77                 let foo = {}!("CAR$0");
78             }}
79         "#,
80                 macro_name, macro_name
81             ),
82             &format!(
83                 r#"
84             #[rustc_builtin_macro]
85             macro_rules! {} {{
86                 ($var:literal) => {{ 0 }}
87             }}
88
89             fn main() {{
90                 let foo = {}!("CARGO_BIN_NAME");
91             }}
92         "#,
93                 macro_name, macro_name
94             ),
95         );
96     }
97     #[test]
98     fn completes_env_variable_in_env() {
99         check("env")
100     }
101
102     #[test]
103     fn completes_env_variable_in_option_env() {
104         check("option_env");
105     }
106
107     #[test]
108     fn doesnt_complete_in_random_strings() {
109         let fixture = r#"
110             fn main() {
111                 let foo = "CA$0";
112             }
113         "#;
114
115         let completions = completion_list(fixture);
116         assert!(completions.is_empty(), "Completions weren't empty: {}", completions);
117     }
118
119     #[test]
120     fn doesnt_complete_in_random_macro() {
121         let fixture = r#"
122             macro_rules! bar {
123                 ($($arg:tt)*) => { 0 }
124             }
125
126             fn main() {
127                 let foo = bar!("CA$0");
128
129             }
130         "#;
131
132         let completions = completion_list(fixture);
133         assert!(completions.is_empty(), "Completions weren't empty: {}", completions);
134     }
135
136     #[test]
137     fn doesnt_complete_for_shadowed_macro() {
138         let fixture = r#"
139             macro_rules! env {
140                 ($var:literal) => { 0 }
141             }
142
143             fn main() {
144                 let foo = env!("CA$0");
145             }
146         "#;
147
148         let completions = completion_list(fixture);
149         assert!(completions.is_empty(), "Completions weren't empty: {}", completions)
150     }
151 }