mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-01 23:12:02 +00:00
Replace if with option, add detail for each env variable completion
This commit is contained in:
parent
bc7080884c
commit
55c5014b76
@ -1,8 +1,8 @@
|
||||
//! Completes environment variables defined by Cargo (https://doc.rust-lang.org/cargo/reference/environment-variables.html)
|
||||
use ide_db::syntax_helpers::node_ext::get_outer_macro_name;
|
||||
use syntax::ast::{self, IsString};
|
||||
|
||||
use syntax::{ast, AstToken, AstNode, TextRange, TextSize};
|
||||
|
||||
use crate::{context::CompletionContext, CompletionItem, CompletionItemKind};
|
||||
use crate::{CompletionItem, CompletionItemKind};
|
||||
|
||||
use super::Completions;
|
||||
const CARGO_DEFINED_VARS: &[(&str, &str)] = &[
|
||||
@ -29,34 +29,27 @@ const CARGO_DEFINED_VARS: &[(&str, &str)] = &[
|
||||
|
||||
pub(crate) fn complete_cargo_env_vars(
|
||||
acc: &mut Completions,
|
||||
ctx: &CompletionContext<'_>,
|
||||
original: &ast::String
|
||||
) {
|
||||
if !is_env_macro(original) {
|
||||
return;
|
||||
}
|
||||
expanded: &ast::String,
|
||||
) -> Option<()> {
|
||||
guard_env_macro(expanded)?;
|
||||
let range = expanded.text_range_between_quotes()?;
|
||||
|
||||
let start = ctx.original_token.text_range().start() + TextSize::from(1);
|
||||
let cursor = ctx.position.offset;
|
||||
CARGO_DEFINED_VARS.iter().for_each(|(var, detail)| {
|
||||
let mut item = CompletionItem::new(CompletionItemKind::Keyword, range, *var);
|
||||
item.detail(*detail);
|
||||
item.add_to(acc);
|
||||
});
|
||||
|
||||
CompletionItem::new(CompletionItemKind::Binding, TextRange::new(start, cursor), "CARGO").add_to(acc);
|
||||
Some(())
|
||||
}
|
||||
|
||||
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()?;
|
||||
fn guard_env_macro(string: &ast::String) -> Option<()> {
|
||||
let name = get_outer_macro_name(string)?;
|
||||
if !matches!(name.text().as_str(), "env" | "option_env") {
|
||||
return None;
|
||||
}
|
||||
|
||||
if !matches!(name.text().as_str(),
|
||||
"env" | "option_env") {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
||||
Some(())
|
||||
})()
|
||||
.is_some()
|
||||
Some(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -112,4 +105,4 @@ mod tests {
|
||||
let completions = completion_list(fixture);
|
||||
assert!(completions.is_empty(), "Completions weren't empty: {}", completions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user