forked from extern/nushell
Allow NU_LIBS_DIR and friends to be const (#8310)
# Description Allow NU_LIBS_DIR and friends to be const they can be updated within the same parse pass. This will allow us to remove having multiple config files eventually. Small implementation detail: I've changed `call.parser_info` to a hashmap with string keys, so the information can have names rather than indices, and we don't have to worry too much about the order in which we put things into it. Closes https://github.com/nushell/nushell/issues/8422 # User-Facing Changes In a single file, users can now do stuff like ``` const NU_LIBS_DIR = ['/some/path/here'] source script.nu ``` and the source statement will use the value of NU_LIBS_DIR declared the line before. Currently, if there is no `NU_LIBS_DIR` const, then we fallback to using the value of the `NU_LIBS_DIR` env-var, so there are no breaking changes (unless someone named a const NU_LIBS_DIR for some reason).  # Tests + Formatting ~~TODO: write tests~~ Done # After Submitting ~~TODO: update docs~~ Will do when we update default_env.nu/merge default_env.nu into default_config.nu.
This commit is contained in:
@ -7,6 +7,7 @@ use crate::{
|
||||
ParseError, Token, TokenContents,
|
||||
};
|
||||
|
||||
use nu_engine::DIR_VAR_PARSER_INFO;
|
||||
use nu_protocol::{
|
||||
ast::{
|
||||
Argument, Assignment, Bits, Block, Boolean, Call, CellPath, Comparison, Expr, Expression,
|
||||
@ -19,10 +20,10 @@ use nu_protocol::{
|
||||
};
|
||||
|
||||
use crate::parse_keywords::{
|
||||
is_unaliasable_parser_keyword, parse_alias, parse_def, parse_def_predecl,
|
||||
find_dirs_var, is_unaliasable_parser_keyword, parse_alias, parse_def, parse_def_predecl,
|
||||
parse_export_in_block, parse_extern, parse_for, parse_hide, parse_keyword, parse_let_or_const,
|
||||
parse_module, parse_old_alias, parse_overlay_hide, parse_overlay_new, parse_overlay_use,
|
||||
parse_source, parse_use, parse_where, parse_where_expr,
|
||||
parse_source, parse_use, parse_where, parse_where_expr, LIB_DIRS_VAR,
|
||||
};
|
||||
|
||||
use itertools::Itertools;
|
||||
@ -781,6 +782,25 @@ pub struct ParsedInternalCall {
|
||||
pub error: Option<ParseError>,
|
||||
}
|
||||
|
||||
fn attach_parser_info_builtin(working_set: &StateWorkingSet, name: &str, call: &mut Call) {
|
||||
match name {
|
||||
"use" | "overlay use" | "source-env" | "nu-check" => {
|
||||
if let Some(var_id) = find_dirs_var(working_set, LIB_DIRS_VAR) {
|
||||
call.set_parser_info(
|
||||
DIR_VAR_PARSER_INFO.to_owned(),
|
||||
Expression {
|
||||
expr: Expr::Var(var_id),
|
||||
span: call.head,
|
||||
ty: Type::Any,
|
||||
custom_completion: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_internal_call(
|
||||
working_set: &mut StateWorkingSet,
|
||||
command_span: Span,
|
||||
@ -800,6 +820,10 @@ pub fn parse_internal_call(
|
||||
let signature = decl.signature();
|
||||
let output = signature.output_type.clone();
|
||||
|
||||
if decl.is_builtin() {
|
||||
attach_parser_info_builtin(working_set, decl.name(), &mut call);
|
||||
}
|
||||
|
||||
// The index into the positional parameter in the definition
|
||||
let mut positional_idx = 0;
|
||||
|
||||
@ -5312,7 +5336,7 @@ pub fn parse_expression(
|
||||
arguments,
|
||||
redirect_stdout: true,
|
||||
redirect_stderr: false,
|
||||
parser_info: vec![],
|
||||
parser_info: HashMap::new(),
|
||||
}));
|
||||
|
||||
(
|
||||
@ -6158,7 +6182,7 @@ fn wrap_expr_with_collect(working_set: &mut StateWorkingSet, expr: &Expression)
|
||||
decl_id,
|
||||
redirect_stdout: true,
|
||||
redirect_stderr: false,
|
||||
parser_info: vec![],
|
||||
parser_info: HashMap::new(),
|
||||
})),
|
||||
span,
|
||||
ty: Type::String,
|
||||
|
Reference in New Issue
Block a user