2023-07-14 21:51:28 +00:00
use crate ::{
parse_block ,
parser_path ::ParserPath ,
type_check ::{ check_block_input_output , type_compatible } ,
} ;
2023-05-12 14:10:40 +00:00
use itertools ::Itertools ;
2022-12-10 17:23:24 +00:00
use log ::trace ;
2022-01-05 00:26:01 +00:00
use nu_path ::canonicalize_with ;
2021-09-26 18:39:19 +00:00
use nu_protocol ::{
2021-11-15 23:16:06 +00:00
ast ::{
2022-04-09 02:55:02 +00:00
Argument , Block , Call , Expr , Expression , ImportPattern , ImportPatternHead ,
2023-04-07 18:09:38 +00:00
ImportPatternMember , Pipeline , PipelineElement ,
2021-11-15 23:16:06 +00:00
} ,
2022-05-07 19:39:22 +00:00
engine ::{ StateWorkingSet , DEFAULT_OVERLAY_NAME } ,
2023-12-04 19:13:47 +00:00
eval_const ::eval_constant ,
2023-10-02 18:13:31 +00:00
span , Alias , BlockId , DeclId , Exportable , Module , ModuleId , ParseError , PositionalArg ,
2023-05-06 18:39:54 +00:00
ResolvedImportPattern , Span , Spanned , SyntaxShape , Type , VarId ,
2021-09-26 18:39:19 +00:00
} ;
revert: move to ahash (#9464)
This PR reverts https://github.com/nushell/nushell/pull/9391
We try not to revert PRs like this, though after discussion with the
Nushell team, we decided to revert this one.
The main reason is that Nushell, as a codebase, isn't ready for these
kinds of optimisations. It's in the part of the development cycle where
our main focus should be on improving the algorithms inside of Nushell
itself. Once we have matured our algorithms, then we can look for
opportunities to switch out technologies we're using for alternate
forms.
Much of Nushell still has lots of opportunities for tuning the codebase,
paying down technical debt, and making the codebase generally cleaner
and more robust. This should be the focus. Performance improvements
should flow out of that work.
Said another, optimisation that isn't part of tuning the codebase is
premature at this stage. We need to focus on doing the hard work of
making the engine, parser, etc better.
# User-Facing Changes
Reverts the HashMap -> ahash change.
cc @FilipAndersson245
2023-06-18 03:27:57 +00:00
use std ::collections ::{ HashMap , HashSet } ;
2022-03-12 20:12:15 +00:00
use std ::path ::{ Path , PathBuf } ;
2023-04-05 16:56:48 +00:00
pub const LIB_DIRS_VAR : & str = " NU_LIB_DIRS " ;
2022-03-12 20:12:15 +00:00
#[ cfg(feature = " plugin " ) ]
2023-04-05 16:56:48 +00:00
pub const PLUGIN_DIRS_VAR : & str = " NU_PLUGIN_DIRS " ;
2021-09-26 18:39:19 +00:00
use crate ::{
2023-04-06 21:40:53 +00:00
is_math_expression_like ,
2022-02-11 18:38:10 +00:00
known_external ::KnownExternal ,
2022-11-18 21:46:48 +00:00
lex ,
2022-12-22 11:41:44 +00:00
lite_parser ::{ lite_parse , LiteCommand , LiteElement } ,
2021-09-26 18:39:19 +00:00
parser ::{
2023-04-06 21:40:53 +00:00
check_call , check_name , garbage , garbage_pipeline , parse , parse_call , parse_expression ,
2023-10-07 13:42:09 +00:00
parse_full_signature , parse_import_pattern , parse_internal_call , parse_multispan_value ,
2023-04-06 21:40:53 +00:00
parse_string , parse_value , parse_var_with_opt_type , trim_quotes , ParsedInternalCall ,
2021-09-26 18:39:19 +00:00
} ,
2023-04-07 00:35:45 +00:00
unescape_unquote_string , Token , TokenContents ,
2021-09-26 18:39:19 +00:00
} ;
2023-03-10 21:20:31 +00:00
/// These parser keywords can be aliased
pub const ALIASABLE_PARSER_KEYWORDS : & [ & [ u8 ] ] = & [ b " overlay hide " , b " overlay new " , b " overlay use " ] ;
2023-11-29 17:29:07 +00:00
pub const RESERVED_VARIABLE_NAMES : [ & str ; 3 ] = [ " in " , " nu " , " env " ] ;
2023-03-10 21:20:31 +00:00
/// These parser keywords cannot be aliased (either not possible, or support not yet added)
pub const UNALIASABLE_PARSER_KEYWORDS : & [ & [ u8 ] ] = & [
b " export " ,
b " def " ,
b " export def " ,
b " for " ,
b " extern " ,
b " export extern " ,
b " alias " ,
b " export alias " ,
b " export-env " ,
b " module " ,
b " use " ,
b " export use " ,
b " hide " ,
// b"overlay",
// b"overlay hide",
// b"overlay new",
// b"overlay use",
b " let " ,
b " const " ,
b " mut " ,
b " source " ,
b " where " ,
b " register " ,
] ;
/// Check whether spans start with a parser keyword that can be aliased
pub fn is_unaliasable_parser_keyword ( working_set : & StateWorkingSet , spans : & [ Span ] ) -> bool {
// try two words
2023-11-17 15:15:55 +00:00
if let ( Some ( span1 ) , Some ( span2 ) ) = ( spans . first ( ) , spans . get ( 1 ) ) {
2023-03-10 21:20:31 +00:00
let cmd_name = working_set . get_span_contents ( span ( & [ * span1 , * span2 ] ) ) ;
return UNALIASABLE_PARSER_KEYWORDS . contains ( & cmd_name ) ;
}
// try one word
2023-11-17 15:15:55 +00:00
if let Some ( span1 ) = spans . first ( ) {
2023-03-10 21:20:31 +00:00
let cmd_name = working_set . get_span_contents ( * span1 ) ;
UNALIASABLE_PARSER_KEYWORDS . contains ( & cmd_name )
} else {
false
}
}
/// This is a new more compact method of calling parse_xxx() functions without repeating the
/// parse_call() in each function. Remaining keywords can be moved here.
pub fn parse_keyword (
working_set : & mut StateWorkingSet ,
lite_command : & LiteCommand ,
is_subexpression : bool ,
2023-04-07 00:35:45 +00:00
) -> Pipeline {
let call_expr = parse_call (
2023-03-10 21:20:31 +00:00
working_set ,
& lite_command . parts ,
lite_command . parts [ 0 ] ,
is_subexpression ,
) ;
2023-04-07 00:35:45 +00:00
// if err.is_some() {
// return (Pipeline::from_vec(vec![call_expr]), err);
// }
2023-03-10 21:20:31 +00:00
if let Expression {
expr : Expr ::Call ( call ) ,
..
} = call_expr . clone ( )
{
// Apply parse keyword side effects
let cmd = working_set . get_decl ( call . decl_id ) ;
2023-05-03 11:08:54 +00:00
// check help flag first.
if call . named_iter ( ) . any ( | ( flag , _ , _ ) | flag . item = = " help " ) {
let call_span = call . span ( ) ;
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
}
2023-03-10 21:20:31 +00:00
match cmd . name ( ) {
" overlay hide " = > parse_overlay_hide ( working_set , call ) ,
" overlay new " = > parse_overlay_new ( working_set , call ) ,
2023-04-07 18:09:38 +00:00
" overlay use " = > parse_overlay_use ( working_set , call ) ,
2023-04-07 00:35:45 +00:00
_ = > Pipeline ::from_vec ( vec! [ call_expr ] ) ,
2023-03-10 21:20:31 +00:00
}
} else {
2023-04-07 00:35:45 +00:00
Pipeline ::from_vec ( vec! [ call_expr ] )
2023-03-10 21:20:31 +00:00
}
}
2023-04-07 18:09:38 +00:00
pub fn parse_def_predecl ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) {
2023-10-02 18:13:31 +00:00
let mut pos = 0 ;
2021-09-26 18:39:19 +00:00
2023-10-02 18:13:31 +00:00
let def_type_name = if spans . len ( ) > = 3 {
// definition can't have only two spans, minimum is 3, e.g., 'extern spam []'
let first_word = working_set . get_span_contents ( spans [ 0 ] ) ;
if first_word = = b " export " {
pos + = 2 ;
} else {
pos + = 1 ;
}
working_set . get_span_contents ( spans [ pos - 1 ] ) . to_vec ( )
2021-09-28 17:29:38 +00:00
} else {
2023-10-02 18:13:31 +00:00
return ;
2021-09-28 17:29:38 +00:00
} ;
2023-11-19 15:25:09 +00:00
if def_type_name ! = b " def " & & def_type_name ! = b " extern " {
2023-10-02 18:13:31 +00:00
return ;
}
// Now, pos should point at the next span after the def-like call.
// Skip all potential flags, like --env, --wrapped or --help:
while pos < spans . len ( )
& & working_set
. get_span_contents ( spans [ pos ] )
. starts_with ( & [ b '-' ] )
{
pos + = 1 ;
}
if pos > = spans . len ( ) {
// This can happen if the call ends with a flag, e.g., 'def --help'
return ;
}
// Now, pos should point at the command name.
let name_pos = pos ;
let Some ( name ) = parse_string ( working_set , spans [ name_pos ] ) . as_string ( ) else {
return ;
} ;
if name . contains ( '#' )
| | name . contains ( '^' )
| | name . parse ::< bytesize ::ByteSize > ( ) . is_ok ( )
| | name . parse ::< f64 > ( ) . is_ok ( )
{
working_set . error ( ParseError ::CommandDefNotValid ( spans [ name_pos ] ) ) ;
return ;
}
// Find signature
let mut signature_pos = None ;
while pos < spans . len ( ) {
if working_set
. get_span_contents ( spans [ pos ] )
. starts_with ( & [ b '[' ] )
| | working_set
. get_span_contents ( spans [ pos ] )
. starts_with ( & [ b '(' ] )
2023-04-07 00:35:45 +00:00
{
2023-10-02 18:13:31 +00:00
signature_pos = Some ( pos ) ;
2023-10-08 10:58:26 +00:00
break ;
2023-04-07 00:35:45 +00:00
}
2022-12-22 20:31:34 +00:00
2023-10-02 18:13:31 +00:00
pos + = 1 ;
}
2021-09-26 18:39:19 +00:00
2023-10-02 18:13:31 +00:00
let Some ( signature_pos ) = signature_pos else {
return ;
} ;
let mut allow_unknown_args = false ;
for span in spans {
if working_set . get_span_contents ( * span ) = = b " --wrapped " & & def_type_name = = b " def " {
allow_unknown_args = true ;
2021-09-26 18:39:19 +00:00
}
2023-10-02 18:13:31 +00:00
}
2022-12-22 20:31:34 +00:00
2023-10-02 18:13:31 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
2023-08-18 17:45:33 +00:00
2023-10-02 18:13:31 +00:00
working_set . enter_scope ( ) ;
// FIXME: because parse_signature will update the scope with the variables it sees
// we end up parsing the signature twice per def. The first time is during the predecl
// so that we can see the types that are part of the signature, which we need for parsing.
// The second time is when we actually parse the body itworking_set.
// We can't reuse the first time because the variables that are created during parse_signature
// are lost when we exit the scope below.
2023-10-07 13:42:09 +00:00
let sig = parse_full_signature ( working_set , & spans [ signature_pos .. ] ) ;
2023-10-02 18:13:31 +00:00
working_set . parse_errors . truncate ( starting_error_count ) ;
working_set . exit_scope ( ) ;
let Some ( mut signature ) = sig . as_signature ( ) else {
return ;
} ;
2022-02-11 18:38:10 +00:00
2023-10-02 18:13:31 +00:00
signature . name = name ;
if allow_unknown_args {
signature . allows_unknown_args = true ;
}
let decl = signature . predeclare ( ) ;
if working_set . add_predecl ( decl ) . is_some ( ) {
working_set . error ( ParseError ::DuplicateCommandDef ( spans [ name_pos ] ) ) ;
2021-09-26 18:39:19 +00:00
}
}
2023-04-07 18:09:38 +00:00
pub fn parse_for ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Expression {
2022-01-12 04:06:56 +00:00
// Checking that the function is used with the correct name
// Maybe this is not necessary but it is a sanity check
if working_set . get_span_contents ( spans [ 0 ] ) ! = b " for " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Wrong call name for 'for' function " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage ( spans [ 0 ] ) ;
2022-01-12 04:06:56 +00:00
}
// Parsing the spans and checking that they match the register signature
// Using a parsed call makes more sense than checking for how many spans are in the call
// Also, by creating a call, it can be checked if it matches the declaration signature
2023-07-13 19:05:03 +00:00
let ( call , call_span ) = match working_set . find_decl ( b " for " ) {
2022-01-12 04:06:56 +00:00
None = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: for declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage ( spans [ 0 ] ) ;
2022-01-12 04:06:56 +00:00
}
Some ( decl_id ) = > {
working_set . enter_scope ( ) ;
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & spans [ 1 .. ] , decl_id ) ;
2022-06-12 19:18:00 +00:00
2022-01-12 04:06:56 +00:00
working_set . exit_scope ( ) ;
let call_span = span ( spans ) ;
let decl = working_set . get_decl ( decl_id ) ;
let sig = decl . signature ( ) ;
2023-04-18 08:19:08 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & sig , & call ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return garbage ( spans [ 0 ] ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2023-04-18 08:19:08 +00:00
return Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : output ,
custom_completion : None ,
} ;
}
2022-01-12 04:06:56 +00:00
// Let's get our block and make sure it has the right signature
2022-04-09 02:55:02 +00:00
if let Some ( arg ) = call . positional_nth ( 2 ) {
2022-01-12 04:06:56 +00:00
match arg {
Expression {
expr : Expr ::Block ( block_id ) ,
..
}
| Expression {
expr : Expr ::RowCondition ( block_id ) ,
..
} = > {
let block = working_set . get_block_mut ( * block_id ) ;
2023-04-18 08:19:08 +00:00
block . signature = Box ::new ( sig ) ;
2022-01-12 04:06:56 +00:00
}
_ = > { }
}
}
( call , call_span )
}
} ;
// All positional arguments must be in the call positional vector by this point
2022-04-09 02:55:02 +00:00
let var_decl = call . positional_nth ( 0 ) . expect ( " for call already checked " ) ;
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
let iteration_expr = call . positional_nth ( 1 ) . expect ( " for call already checked " ) ;
2022-04-09 02:55:02 +00:00
let block = call . positional_nth ( 2 ) . expect ( " for call already checked " ) ;
2022-01-12 04:06:56 +00:00
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
let iteration_expr_ty = iteration_expr . ty . clone ( ) ;
// Figure out the type of the variable the `for` uses for iteration
let var_type = match iteration_expr_ty {
Type ::List ( x ) = > * x ,
Type ::Table ( x ) = > Type ::Record ( x ) ,
x = > x ,
} ;
2022-01-12 04:06:56 +00:00
if let ( Some ( var_id ) , Some ( block_id ) ) = ( & var_decl . as_var ( ) , block . as_block ( ) ) {
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
working_set . set_variable_type ( * var_id , var_type . clone ( ) ) ;
2022-01-12 04:06:56 +00:00
let block = working_set . get_block_mut ( block_id ) ;
block . signature . required_positional . insert (
0 ,
PositionalArg {
name : String ::new ( ) ,
desc : String ::new ( ) ,
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
shape : var_type . to_shape ( ) ,
2022-01-12 04:06:56 +00:00
var_id : Some ( * var_id ) ,
2022-03-07 20:08:56 +00:00
default_value : None ,
2022-01-12 04:06:56 +00:00
} ,
) ;
}
2023-04-07 00:35:45 +00:00
Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
ty : Type ::Nothing ,
2023-04-07 00:35:45 +00:00
custom_completion : None ,
}
2022-01-12 04:06:56 +00:00
}
2023-11-29 17:29:07 +00:00
/// If `name` is a keyword, emit an error.
fn verify_not_reserved_variable_name ( working_set : & mut StateWorkingSet , name : & str , span : Span ) {
if RESERVED_VARIABLE_NAMES . contains ( & name ) {
working_set . error ( ParseError ::NameIsBuiltinVar ( name . to_string ( ) , span ) )
}
}
2023-10-02 18:13:31 +00:00
// Returns also the parsed command name and ID
2021-09-26 18:39:19 +00:00
pub fn parse_def (
working_set : & mut StateWorkingSet ,
2022-01-22 18:24:47 +00:00
lite_command : & LiteCommand ,
2023-01-22 19:34:15 +00:00
module_name : Option < & [ u8 ] > ,
2023-10-02 18:13:31 +00:00
) -> ( Pipeline , Option < ( Vec < u8 > , DeclId ) > ) {
2022-01-22 18:24:47 +00:00
let spans = & lite_command . parts [ .. ] ;
2022-12-30 15:44:37 +00:00
let ( usage , extra_usage ) = working_set . build_usage ( & lite_command . comments ) ;
2022-01-22 18:24:47 +00:00
2021-12-27 19:13:52 +00:00
// Checking that the function is used with the correct name
// Maybe this is not necessary but it is a sanity check
2022-08-22 21:19:47 +00:00
// Note: "export def" is treated the same as "def"
2022-01-29 20:45:46 +00:00
2022-08-22 21:19:47 +00:00
let ( name_span , split_id ) =
if spans . len ( ) > 1 & & working_set . get_span_contents ( spans [ 0 ] ) = = b " export " {
( spans [ 1 ] , 2 )
} else {
( spans [ 0 ] , 1 )
} ;
let def_call = working_set . get_span_contents ( name_span ) . to_vec ( ) ;
2023-11-19 15:25:09 +00:00
if def_call ! = b " def " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Wrong call name for def function " . into ( ) ,
span ( spans ) ,
) ) ;
2023-10-02 18:13:31 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2021-12-27 19:13:52 +00:00
}
2021-09-27 01:03:50 +00:00
2021-12-27 19:13:52 +00:00
// Parsing the spans and checking that they match the register signature
// Using a parsed call makes more sense than checking for how many spans are in the call
// Also, by creating a call, it can be checked if it matches the declaration signature
2023-07-13 19:05:03 +00:00
let ( call , call_span ) = match working_set . find_decl ( & def_call ) {
2021-12-27 19:13:52 +00:00
None = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: def declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
2023-10-02 18:13:31 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2021-12-27 19:13:52 +00:00
}
Some ( decl_id ) = > {
working_set . enter_scope ( ) ;
2022-08-22 21:19:47 +00:00
let ( command_spans , rest_spans ) = spans . split_at ( split_id ) ;
2023-05-12 14:10:40 +00:00
2023-10-02 18:13:31 +00:00
// Find the first span that is not a flag
let mut decl_name_span = None ;
for span in rest_spans {
if ! working_set . get_span_contents ( * span ) . starts_with ( & [ b '-' ] ) {
decl_name_span = Some ( * span ) ;
break ;
}
}
if let Some ( name_span ) = decl_name_span {
// Check whether name contains [] or () -- possible missing space error
2023-05-12 14:10:40 +00:00
if let Some ( err ) = detect_params_in_name (
working_set ,
2023-10-02 18:13:31 +00:00
name_span ,
2023-05-12 14:10:40 +00:00
String ::from_utf8_lossy ( & def_call ) . as_ref ( ) ,
) {
working_set . error ( err ) ;
2023-10-02 18:13:31 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2023-05-12 14:10:40 +00:00
}
}
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , span ( command_spans ) , rest_spans , decl_id ) ;
2023-04-07 00:35:45 +00:00
// This is to preserve the order of the errors so that
// the check errors below come first
let mut new_errors = working_set . parse_errors [ starting_error_count .. ] . to_vec ( ) ;
working_set . parse_errors . truncate ( starting_error_count ) ;
2022-06-12 19:18:00 +00:00
2021-12-27 19:13:52 +00:00
working_set . exit_scope ( ) ;
2021-09-27 01:03:50 +00:00
2021-12-27 19:13:52 +00:00
let call_span = span ( spans ) ;
let decl = working_set . get_decl ( decl_id ) ;
2022-01-12 04:06:56 +00:00
let sig = decl . signature ( ) ;
// Let's get our block and make sure it has the right signature
2022-04-09 02:55:02 +00:00
if let Some ( arg ) = call . positional_nth ( 2 ) {
2022-01-12 04:06:56 +00:00
match arg {
Expression {
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
expr : Expr ::Closure ( block_id ) ,
..
2022-01-12 04:06:56 +00:00
} = > {
let block = working_set . get_block_mut ( * block_id ) ;
block . signature = Box ::new ( sig . clone ( ) ) ;
}
2024-02-03 11:20:40 +00:00
_ = > working_set . parse_errors . push ( ParseError ::Expected (
" definition body closure { ... } " ,
arg . span ,
) ) ,
2022-01-12 04:06:56 +00:00
}
}
2021-09-27 01:03:50 +00:00
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & sig , & call ) ;
working_set . parse_errors . append ( & mut new_errors ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2023-10-02 18:13:31 +00:00
return (
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : output ,
custom_completion : None ,
} ] ) ,
None ,
) ;
2021-12-27 19:13:52 +00:00
}
2021-09-27 01:03:50 +00:00
2021-12-27 19:13:52 +00:00
( call , call_span )
}
} ;
2021-09-27 01:03:50 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( has_env ) = has_flag_const ( working_set , & call , " env " ) else {
2024-01-11 15:19:48 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
} ;
2024-02-03 11:20:40 +00:00
let Ok ( has_wrapped ) = has_flag_const ( working_set , & call , " wrapped " ) else {
2024-01-11 15:19:48 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
} ;
2023-10-02 18:13:31 +00:00
2021-12-27 19:13:52 +00:00
// All positional arguments must be in the call positional vector by this point
2022-04-09 02:55:02 +00:00
let name_expr = call . positional_nth ( 0 ) . expect ( " def call already checked " ) ;
let sig = call . positional_nth ( 1 ) . expect ( " def call already checked " ) ;
let block = call . positional_nth ( 2 ) . expect ( " def call already checked " ) ;
2021-09-27 01:03:50 +00:00
2023-01-22 19:34:15 +00:00
let name = if let Some ( name ) = name_expr . as_string ( ) {
if let Some ( mod_name ) = module_name {
if name . as_bytes ( ) = = mod_name {
let name_expr_span = name_expr . span ;
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::NamedAsModule (
" command " . to_string ( ) ,
name ,
2023-05-06 18:39:54 +00:00
" main " . to_string ( ) ,
2023-04-07 00:35:45 +00:00
name_expr_span ,
) ) ;
2023-10-02 18:13:31 +00:00
return (
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ,
None ,
) ;
2023-01-22 19:34:15 +00:00
}
}
name
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" Could not get string from string expression " . into ( ) ,
name_expr . span ,
) ) ;
2023-10-02 18:13:31 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2023-01-22 19:34:15 +00:00
} ;
2023-10-02 18:13:31 +00:00
let mut result = None ;
2023-01-22 19:34:15 +00:00
if let ( Some ( mut signature ) , Some ( block_id ) ) = ( sig . as_signature ( ) , block . as_block ( ) ) {
2023-11-29 17:29:07 +00:00
for arg_name in & signature . required_positional {
verify_not_reserved_variable_name ( working_set , & arg_name . name , sig . span ) ;
}
for arg_name in & signature . optional_positional {
verify_not_reserved_variable_name ( working_set , & arg_name . name , sig . span ) ;
}
for arg_name in & signature . rest_positional {
verify_not_reserved_variable_name ( working_set , & arg_name . name , sig . span ) ;
}
for flag_name in & signature . get_names ( ) {
verify_not_reserved_variable_name ( working_set , flag_name , sig . span ) ;
}
2023-10-02 18:13:31 +00:00
if has_wrapped {
if let Some ( rest ) = & signature . rest_positional {
if let Some ( var_id ) = rest . var_id {
let rest_var = & working_set . get_variable ( var_id ) ;
if rest_var . ty ! = Type ::Any & & rest_var . ty ! = Type ::List ( Box ::new ( Type ::String ) )
{
working_set . error ( ParseError ::TypeMismatchHelp (
Type ::List ( Box ::new ( Type ::String ) ) ,
rest_var . ty . clone ( ) ,
rest_var . declaration_span ,
format! ( " ...rest-like positional argument used in 'def --wrapped' supports only strings. Change the type annotation of ... {} to 'string'. " , & rest . name ) ) ) ;
return (
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ,
result ,
) ;
}
}
} else {
working_set . error ( ParseError ::MissingPositional ( " ...rest-like positional argument " . to_string ( ) , name_expr . span , " def --wrapped must have a ...rest-like positional argument. Add '...rest: string' to the command's signature. " . to_string ( ) ) ) ;
return (
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ,
result ,
) ;
}
}
2022-02-21 21:05:20 +00:00
if let Some ( decl_id ) = working_set . find_predecl ( name . as_bytes ( ) ) {
2021-12-27 19:13:52 +00:00
let declaration = working_set . get_decl_mut ( decl_id ) ;
signature . name = name . clone ( ) ;
2023-12-05 19:04:36 +00:00
if ! has_wrapped {
* signature = signature . add_help ( ) ;
}
2022-01-22 18:24:47 +00:00
signature . usage = usage ;
2022-12-30 15:44:37 +00:00
signature . extra_usage = extra_usage ;
2023-10-02 18:13:31 +00:00
signature . allows_unknown_args = has_wrapped ;
2022-01-12 04:06:56 +00:00
* declaration = signature . clone ( ) . into_block_command ( block_id ) ;
2023-06-08 16:49:58 +00:00
let block = working_set . get_block_mut ( block_id ) ;
2022-01-12 04:06:56 +00:00
block . signature = signature ;
2023-11-19 15:25:09 +00:00
block . redirect_env = has_env ;
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
2023-07-14 21:51:28 +00:00
if block . signature . input_output_types . is_empty ( ) {
block
. signature
. input_output_types
. push ( ( Type ::Any , Type ::Any ) ) ;
}
let block = working_set . get_block ( block_id ) ;
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
2023-07-14 21:51:28 +00:00
let typecheck_errors = check_block_input_output ( working_set , block ) ;
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
2023-07-14 21:51:28 +00:00
working_set
. parse_errors
. extend_from_slice ( & typecheck_errors ) ;
2023-10-02 18:13:31 +00:00
result = Some ( ( name . as_bytes ( ) . to_vec ( ) , decl_id ) ) ;
2021-09-27 01:03:50 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" Predeclaration failed to add declaration " . into ( ) ,
name_expr . span ,
) ) ;
2021-12-27 19:13:52 +00:00
} ;
}
2021-09-27 01:03:50 +00:00
2023-01-22 19:34:15 +00:00
// It's OK if it returns None: The decl was already merged in previous parse pass.
working_set . merge_predecl ( name . as_bytes ( ) ) ;
2021-12-27 19:13:52 +00:00
2023-10-02 18:13:31 +00:00
(
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ,
result ,
)
2021-09-26 18:39:19 +00:00
}
2022-02-11 18:38:10 +00:00
pub fn parse_extern (
working_set : & mut StateWorkingSet ,
lite_command : & LiteCommand ,
2023-01-22 19:34:15 +00:00
module_name : Option < & [ u8 ] > ,
2023-04-07 00:35:45 +00:00
) -> Pipeline {
2022-08-22 21:19:47 +00:00
let spans = & lite_command . parts ;
2022-02-11 18:38:10 +00:00
2022-12-30 15:44:37 +00:00
let ( usage , extra_usage ) = working_set . build_usage ( & lite_command . comments ) ;
2022-02-11 18:38:10 +00:00
// Checking that the function is used with the correct name
// Maybe this is not necessary but it is a sanity check
2023-08-18 17:45:33 +00:00
let ( name_span , split_id ) =
if spans . len ( ) > 1 & & ( working_set . get_span_contents ( spans [ 0 ] ) = = b " export " ) {
( spans [ 1 ] , 2 )
} else {
( spans [ 0 ] , 1 )
} ;
2022-08-22 21:19:47 +00:00
let extern_call = working_set . get_span_contents ( name_span ) . to_vec ( ) ;
2023-11-16 22:44:28 +00:00
if extern_call ! = b " extern " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
2023-11-16 22:44:28 +00:00
" internal error: Wrong call name for extern command " . into ( ) ,
2023-04-07 00:35:45 +00:00
span ( spans ) ,
) ) ;
return garbage_pipeline ( spans ) ;
2022-02-11 18:38:10 +00:00
}
// Parsing the spans and checking that they match the register signature
// Using a parsed call makes more sense than checking for how many spans are in the call
// Also, by creating a call, it can be checked if it matches the declaration signature
2023-07-13 19:05:03 +00:00
let ( call , call_span ) = match working_set . find_decl ( & extern_call ) {
2022-02-11 18:38:10 +00:00
None = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: def declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage_pipeline ( spans ) ;
2022-02-11 18:38:10 +00:00
}
Some ( decl_id ) = > {
working_set . enter_scope ( ) ;
2022-08-22 21:19:47 +00:00
let ( command_spans , rest_spans ) = spans . split_at ( split_id ) ;
2023-11-17 15:15:55 +00:00
if let Some ( name_span ) = rest_spans . first ( ) {
2023-05-12 14:10:40 +00:00
if let Some ( err ) = detect_params_in_name (
working_set ,
* name_span ,
String ::from_utf8_lossy ( & extern_call ) . as_ref ( ) ,
) {
working_set . error ( err ) ;
return garbage_pipeline ( spans ) ;
}
}
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , .. } =
parse_internal_call ( working_set , span ( command_spans ) , rest_spans , decl_id ) ;
2022-02-11 18:38:10 +00:00
working_set . exit_scope ( ) ;
let call_span = span ( spans ) ;
//let decl = working_set.get_decl(decl_id);
//let sig = decl.signature();
( call , call_span )
}
} ;
2022-04-09 02:55:02 +00:00
let name_expr = call . positional_nth ( 0 ) ;
let sig = call . positional_nth ( 1 ) ;
2023-04-28 07:06:43 +00:00
let body = call . positional_nth ( 2 ) ;
2022-02-11 18:38:10 +00:00
if let ( Some ( name_expr ) , Some ( sig ) ) = ( name_expr , sig ) {
if let ( Some ( name ) , Some ( mut signature ) ) = ( & name_expr . as_string ( ) , sig . as_signature ( ) ) {
2023-01-22 19:34:15 +00:00
if let Some ( mod_name ) = module_name {
if name . as_bytes ( ) = = mod_name {
let name_expr_span = name_expr . span ;
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::NamedAsModule (
" known external " . to_string ( ) ,
name . clone ( ) ,
2023-05-06 18:39:54 +00:00
" main " . to_string ( ) ,
2023-04-07 00:35:45 +00:00
name_expr_span ,
) ) ;
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2023-01-22 19:34:15 +00:00
}
}
2022-02-21 21:05:20 +00:00
if let Some ( decl_id ) = working_set . find_predecl ( name . as_bytes ( ) ) {
2022-02-11 18:38:10 +00:00
let declaration = working_set . get_decl_mut ( decl_id ) ;
2023-01-22 19:34:15 +00:00
let external_name = if let Some ( mod_name ) = module_name {
if name . as_bytes ( ) = = b " main " {
String ::from_utf8_lossy ( mod_name ) . to_string ( )
} else {
name . clone ( )
}
} else {
name . clone ( )
} ;
signature . name = external_name . clone ( ) ;
2022-02-11 18:38:10 +00:00
signature . usage = usage . clone ( ) ;
2022-12-30 15:44:37 +00:00
signature . extra_usage = extra_usage . clone ( ) ;
2022-12-21 22:33:26 +00:00
signature . allows_unknown_args = true ;
2022-02-11 18:38:10 +00:00
2023-04-28 07:06:43 +00:00
if let Some ( block_id ) = body . and_then ( | x | x . as_block ( ) ) {
if signature . rest_positional . is_none ( ) {
working_set . error ( ParseError ::InternalError (
" Extern block must have a rest positional argument " . into ( ) ,
name_expr . span ,
) ) ;
} else {
* declaration = signature . clone ( ) . into_block_command ( block_id ) ;
2022-02-11 18:38:10 +00:00
2024-02-07 22:42:24 +00:00
working_set . get_block_mut ( block_id ) . signature = signature ;
2023-04-28 07:06:43 +00:00
}
} else {
let decl = KnownExternal {
name : external_name ,
2023-08-17 14:37:01 +00:00
usage ,
extra_usage ,
2023-04-28 07:06:43 +00:00
signature ,
} ;
* declaration = Box ::new ( decl ) ;
}
2022-02-11 18:38:10 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" Predeclaration failed to add declaration " . into ( ) ,
spans [ split_id ] ,
) ) ;
2022-02-11 18:38:10 +00:00
} ;
}
if let Some ( name ) = name_expr . as_string ( ) {
// It's OK if it returns None: The decl was already merged in previous parse pass.
working_set . merge_predecl ( name . as_bytes ( ) ) ;
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" Could not get string from string expression " . into ( ) ,
name_expr . span ,
) ) ;
2022-02-11 18:38:10 +00:00
}
}
2023-04-07 00:35:45 +00:00
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] )
2022-02-11 18:38:10 +00:00
}
2021-09-26 18:39:19 +00:00
pub fn parse_alias (
working_set : & mut StateWorkingSet ,
2022-12-30 15:44:37 +00:00
lite_command : & LiteCommand ,
2023-01-22 19:34:15 +00:00
module_name : Option < & [ u8 ] > ,
2023-04-07 00:35:45 +00:00
) -> Pipeline {
2022-12-30 15:44:37 +00:00
let spans = & lite_command . parts ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
let ( name_span , split_id ) =
if spans . len ( ) > 1 & & working_set . get_span_contents ( spans [ 0 ] ) = = b " export " {
( spans [ 1 ] , 2 )
} else {
( spans [ 0 ] , 1 )
} ;
let name = working_set . get_span_contents ( name_span ) ;
if name ! = b " alias " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" Alias statement unparsable " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage_pipeline ( spans ) ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
2023-04-07 00:35:45 +00:00
if let Some ( span ) = check_name ( working_set , spans ) {
return Pipeline ::from_vec ( vec! [ garbage ( * span ) ] ) ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
2023-07-13 19:05:03 +00:00
if let Some ( decl_id ) = working_set . find_decl ( b " alias " ) {
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
let ( command_spans , rest_spans ) = spans . split_at ( split_id ) ;
2023-04-07 00:35:45 +00:00
let original_starting_error_count = working_set . parse_errors . len ( ) ;
2023-03-10 21:20:31 +00:00
let ParsedInternalCall {
call : alias_call ,
output ,
..
2023-04-07 18:09:38 +00:00
} = parse_internal_call ( working_set , span ( command_spans ) , rest_spans , decl_id ) ;
2023-12-04 18:56:46 +00:00
2023-04-07 00:35:45 +00:00
working_set
. parse_errors
. truncate ( original_starting_error_count ) ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( has_help_flag ) = has_flag_const ( working_set , & alias_call , " help " ) else {
2024-01-11 15:19:48 +00:00
return garbage_pipeline ( spans ) ;
} ;
2023-03-10 21:20:31 +00:00
let alias_pipeline = Pipeline ::from_vec ( vec! [ Expression {
2023-04-06 21:05:09 +00:00
expr : Expr ::Call ( alias_call . clone ( ) ) ,
2023-03-10 21:20:31 +00:00
span : span ( spans ) ,
ty : output ,
custom_completion : None ,
} ] ) ;
if has_help_flag {
2023-04-07 00:35:45 +00:00
return alias_pipeline ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
2023-04-14 18:51:38 +00:00
let Some ( alias_name_expr ) = alias_call . positional_nth ( 0 ) else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" Missing positional after call check " . to_string ( ) ,
span ( spans ) ,
) ) ;
return garbage_pipeline ( spans ) ;
2023-04-06 21:05:09 +00:00
} ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
2023-04-06 21:05:09 +00:00
let alias_name = if let Some ( name ) = alias_name_expr . as_string ( ) {
if name . contains ( '#' )
| | name . contains ( '^' )
| | name . parse ::< bytesize ::ByteSize > ( ) . is_ok ( )
| | name . parse ::< f64 > ( ) . is_ok ( )
2023-03-22 21:16:06 +00:00
{
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::AliasNotValid ( alias_name_expr . span ) ) ;
return garbage_pipeline ( spans ) ;
2023-04-06 21:05:09 +00:00
} else {
name
2023-03-22 21:16:06 +00:00
}
2023-04-06 21:05:09 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::AliasNotValid ( alias_name_expr . span ) ) ;
return garbage_pipeline ( spans ) ;
2023-04-06 21:05:09 +00:00
} ;
2023-03-22 21:16:06 +00:00
2023-04-06 21:05:09 +00:00
if spans . len ( ) > = split_id + 3 {
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
if let Some ( mod_name ) = module_name {
2023-04-06 21:05:09 +00:00
if alias_name . as_bytes ( ) = = mod_name {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::NamedAsModule (
" alias " . to_string ( ) ,
alias_name ,
2023-05-06 18:39:54 +00:00
" main " . to_string ( ) ,
2023-04-07 00:35:45 +00:00
spans [ split_id ] ,
) ) ;
return alias_pipeline ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
2023-04-06 21:05:09 +00:00
if alias_name = = " main " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::ExportMainAliasNotAllowed ( spans [ split_id ] ) ) ;
return alias_pipeline ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
}
let _equals = working_set . get_span_contents ( spans [ split_id + 1 ] ) ;
let replacement_spans = & spans [ ( split_id + 2 ) .. ] ;
2023-04-06 22:12:21 +00:00
let first_bytes = working_set . get_span_contents ( replacement_spans [ 0 ] ) ;
if first_bytes ! = b " if "
& & first_bytes ! = b " match "
2023-04-07 18:09:38 +00:00
& & is_math_expression_like ( working_set , replacement_spans [ 0 ] )
2023-04-06 22:12:21 +00:00
{
2023-04-06 21:40:53 +00:00
// TODO: Maybe we need to implement a Display trait for Expression?
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
2023-04-07 18:09:38 +00:00
let expr = parse_expression ( working_set , replacement_spans , false ) ;
2023-04-07 00:35:45 +00:00
working_set . parse_errors . truncate ( starting_error_count ) ;
2023-04-06 21:40:53 +00:00
let msg = format! ( " {:?} " , expr . expr ) ;
let msg_parts : Vec < & str > = msg . split ( '(' ) . collect ( ) ;
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::CantAliasExpression (
msg_parts [ 0 ] . to_string ( ) ,
replacement_spans [ 0 ] ,
) ) ;
return alias_pipeline ;
2023-04-06 21:40:53 +00:00
}
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
2023-05-19 21:46:22 +00:00
working_set . search_predecls = false ;
2023-04-07 00:35:45 +00:00
let expr = parse_call (
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
working_set ,
replacement_spans ,
replacement_spans [ 0 ] ,
false , // TODO: Should this be set properly???
) ;
2023-05-19 21:46:22 +00:00
working_set . search_predecls = true ;
2023-04-07 00:35:45 +00:00
if starting_error_count ! = working_set . parse_errors . len ( ) {
if let Some ( e ) = working_set . parse_errors . get ( starting_error_count ) {
if let ParseError ::MissingPositional ( .. ) = e {
working_set
. parse_errors
. truncate ( original_starting_error_count ) ;
// ignore missing required positional
} else {
return garbage_pipeline ( replacement_spans ) ;
}
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
}
let ( command , wrapped_call ) = match expr {
Expression {
2023-03-10 21:20:31 +00:00
expr : Expr ::Call ( ref rhs_call ) ,
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
..
2023-03-10 21:20:31 +00:00
} = > {
let cmd = working_set . get_decl ( rhs_call . decl_id ) ;
if cmd . is_parser_keyword ( )
& & ! ALIASABLE_PARSER_KEYWORDS . contains ( & cmd . name ( ) . as_bytes ( ) )
{
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::CantAliasKeyword (
ALIASABLE_PARSER_KEYWORDS
. iter ( )
. map ( | bytes | String ::from_utf8_lossy ( bytes ) . to_string ( ) )
. collect ::< Vec < String > > ( )
. join ( " , " ) ,
rhs_call . head ,
) ) ;
return alias_pipeline ;
2023-03-10 21:20:31 +00:00
}
( Some ( cmd . clone_box ( ) ) , expr )
}
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
Expression {
expr : Expr ::ExternalCall ( .. ) ,
..
} = > ( None , expr ) ,
_ = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" Parsed call not a call " . into ( ) ,
expr . span ,
) ) ;
return alias_pipeline ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
} ;
2023-12-04 18:56:46 +00:00
// Tries to build a useful usage string
let ( usage , extra_usage ) = match lite_command . comments . is_empty ( ) {
// First from comments, if any are present
false = > working_set . build_usage ( & lite_command . comments ) ,
// Then from the command itself
true = > match alias_call . arguments . get ( 1 ) {
Some ( Argument ::Positional ( Expression {
expr : Expr ::Keyword ( .. , expr ) ,
..
} ) ) = > {
let aliased = working_set . get_span_contents ( expr . span ) ;
(
format! ( " Alias for ` {} ` " , String ::from_utf8_lossy ( aliased ) ) ,
String ::new ( ) ,
)
}
// Then with a default.
_ = > ( " User declared alias " . into ( ) , String ::new ( ) ) ,
} ,
} ;
2023-03-22 21:16:06 +00:00
let decl = Alias {
2023-04-06 21:05:09 +00:00
name : alias_name ,
2023-03-22 21:16:06 +00:00
command ,
wrapped_call ,
2023-08-17 08:58:38 +00:00
usage ,
extra_usage ,
2023-03-22 21:16:06 +00:00
} ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
2023-03-22 21:16:06 +00:00
working_set . add_decl ( Box ::new ( decl ) ) ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
2023-11-08 19:35:40 +00:00
// special case for `alias foo=bar`
if spans . len ( ) = = 2 & & working_set . get_span_contents ( spans [ 1 ] ) . contains ( & b '=' ) {
let arg = String ::from_utf8_lossy ( working_set . get_span_contents ( spans [ 1 ] ) ) ;
// split at '='. Note that the output must never be None, the
// `unwrap` is just to avoid the possibility of panic, if the
// invariant is broken.
let ( name , initial_value ) = arg . split_once ( '=' ) . unwrap_or ( ( & arg , " " ) ) ;
let name = if name . is_empty ( ) { " {name} " } else { name } ;
let initial_value = if initial_value . is_empty ( ) {
" {initial_value} "
} else {
initial_value
} ;
working_set . error ( ParseError ::IncorrectValue (
" alias argument " . into ( ) ,
spans [ 1 ] ,
format! ( " Make sure to put spaces around '=': alias {name} = {initial_value} " ) ,
) )
} else if spans . len ( ) < 4 {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::IncorrectValue (
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
" Incomplete alias " . into ( ) ,
span ( & spans [ .. split_id ] ) ,
" incomplete alias " . into ( ) ,
2023-04-07 00:35:45 +00:00
) ) ;
}
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
2023-04-07 00:35:45 +00:00
return alias_pipeline ;
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" Alias statement unparsable " . into ( ) ,
span ( spans ) ,
) ) ;
garbage_pipeline ( spans )
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
}
2022-08-22 21:19:47 +00:00
// This one will trigger if `export` appears during eval, e.g., in a script
pub fn parse_export_in_block (
working_set : & mut StateWorkingSet ,
lite_command : & LiteCommand ,
2023-04-07 00:35:45 +00:00
) -> Pipeline {
2022-08-22 21:19:47 +00:00
let call_span = span ( & lite_command . parts ) ;
let full_name = if lite_command . parts . len ( ) > 1 {
let sub = working_set . get_span_contents ( lite_command . parts [ 1 ] ) ;
match sub {
2023-11-19 15:25:09 +00:00
b " alias " | b " def " | b " extern " | b " use " | b " module " | b " const " = > {
2023-11-16 22:44:28 +00:00
[ b " export " , sub ] . concat ( )
}
2022-08-22 21:19:47 +00:00
_ = > b " export " . to_vec ( ) ,
}
} else {
b " export " . to_vec ( )
} ;
2023-07-13 19:05:03 +00:00
if let Some ( decl_id ) = working_set . find_decl ( & full_name ) {
2023-04-07 00:35:45 +00:00
let ParsedInternalCall { call , output , .. } = parse_internal_call (
2022-08-22 21:19:47 +00:00
working_set ,
if full_name = = b " export " {
lite_command . parts [ 0 ]
} else {
span ( & lite_command . parts [ 0 .. 2 ] )
} ,
if full_name = = b " export " {
& lite_command . parts [ 1 .. ]
} else {
& lite_command . parts [ 2 .. ]
} ,
decl_id ,
) ;
let decl = working_set . get_decl ( decl_id ) ;
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & decl . signature ( ) , & call ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return garbage_pipeline ( & lite_command . parts ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2023-04-07 00:35:45 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : output ,
custom_completion : None ,
} ] ) ;
2022-08-22 21:19:47 +00:00
}
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
format! (
" internal error: '{}' declaration not found " ,
String ::from_utf8_lossy ( & full_name )
) ,
span ( & lite_command . parts ) ,
) ) ;
return garbage_pipeline ( & lite_command . parts ) ;
2022-08-22 21:19:47 +00:00
} ;
if & full_name = = b " export " {
// export by itself is meaningless
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnexpectedKeyword (
" export " . into ( ) ,
lite_command . parts [ 0 ] ,
) ) ;
return garbage_pipeline ( & lite_command . parts ) ;
2022-08-22 21:19:47 +00:00
}
match full_name . as_slice ( ) {
2023-04-07 18:09:38 +00:00
b " export alias " = > parse_alias ( working_set , lite_command , None ) ,
2023-11-19 15:25:09 +00:00
b " export def " = > parse_def ( working_set , lite_command , None ) . 0 ,
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
b " export const " = > parse_const ( working_set , & lite_command . parts [ 1 .. ] ) ,
2022-08-22 21:19:47 +00:00
b " export use " = > {
2023-04-07 18:09:38 +00:00
let ( pipeline , _ ) = parse_use ( working_set , & lite_command . parts ) ;
2023-04-07 00:35:45 +00:00
pipeline
2022-08-22 21:19:47 +00:00
}
2023-05-06 20:55:10 +00:00
b " export module " = > parse_module ( working_set , lite_command , None ) . 0 ,
2023-04-07 18:09:38 +00:00
b " export extern " = > parse_extern ( working_set , lite_command , None ) ,
2023-04-07 00:35:45 +00:00
_ = > {
working_set . error ( ParseError ::UnexpectedKeyword (
2022-08-22 21:19:47 +00:00
String ::from_utf8_lossy ( & full_name ) . to_string ( ) ,
lite_command . parts [ 0 ] ,
2023-04-07 00:35:45 +00:00
) ) ;
garbage_pipeline ( & lite_command . parts )
}
2022-08-22 21:19:47 +00:00
}
}
// This one will trigger only in a module
pub fn parse_export_in_module (
2021-09-28 17:29:38 +00:00
working_set : & mut StateWorkingSet ,
2022-01-22 18:24:47 +00:00
lite_command : & LiteCommand ,
2023-01-22 19:34:15 +00:00
module_name : & [ u8 ] ,
2023-04-07 00:35:45 +00:00
) -> ( Pipeline , Vec < Exportable > ) {
2022-01-22 18:24:47 +00:00
let spans = & lite_command . parts [ .. ] ;
2021-11-15 23:16:06 +00:00
2023-11-17 15:15:55 +00:00
let export_span = if let Some ( sp ) = spans . first ( ) {
2021-11-15 23:16:06 +00:00
if working_set . get_span_contents ( * sp ) ! = b " export " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" expected export statement " . into ( ) ,
span ( spans ) ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
2021-11-15 23:16:06 +00:00
}
* sp
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" got empty input for parsing export statement " . into ( ) ,
span ( spans ) ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
2021-11-15 23:16:06 +00:00
} ;
2023-07-13 19:05:03 +00:00
let export_decl_id = if let Some ( id ) = working_set . find_decl ( b " export " ) {
2023-05-06 18:39:54 +00:00
id
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" missing export command " . into ( ) ,
export_span ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
2021-11-15 23:16:06 +00:00
} ;
2021-09-28 17:29:38 +00:00
2021-11-15 23:16:06 +00:00
let mut call = Box ::new ( Call {
head : spans [ 0 ] ,
decl_id : export_decl_id ,
2022-04-09 02:55:02 +00:00
arguments : vec ! [ ] ,
2022-02-21 22:22:21 +00:00
redirect_stdout : true ,
redirect_stderr : false ,
2023-04-05 16:56:48 +00:00
parser_info : HashMap ::new ( ) ,
2021-11-15 23:16:06 +00:00
} ) ;
2021-09-28 17:29:38 +00:00
2022-07-29 08:57:10 +00:00
let exportables = if let Some ( kw_span ) = spans . get ( 1 ) {
2021-11-15 23:16:06 +00:00
let kw_name = working_set . get_span_contents ( * kw_span ) ;
match kw_name {
2021-09-28 18:03:53 +00:00
b " def " = > {
2022-01-22 18:24:47 +00:00
let lite_command = LiteCommand {
comments : lite_command . comments . clone ( ) ,
parts : spans [ 1 .. ] . to_vec ( ) ,
} ;
2023-10-02 18:13:31 +00:00
let ( pipeline , cmd_result ) =
parse_def ( working_set , & lite_command , Some ( module_name ) ) ;
let mut result = vec! [ ] ;
if let Some ( ( decl_name , decl_id ) ) = cmd_result {
result . push ( Exportable ::Decl {
name : decl_name . to_vec ( ) ,
id : decl_id ,
} ) ;
}
2021-09-28 18:03:53 +00:00
2023-07-13 19:05:03 +00:00
let export_def_decl_id = if let Some ( id ) = working_set . find_decl ( b " export def " ) {
id
} else {
working_set . error ( ParseError ::InternalError (
" missing 'export def' command " . into ( ) ,
export_span ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
} ;
2021-09-28 18:03:53 +00:00
// Trying to warp the 'def' call into the 'export def' in a very clumsy way
2022-11-22 18:26:13 +00:00
if let Some ( PipelineElement ::Expression (
_ ,
Expression {
expr : Expr ::Call ( ref def_call ) ,
..
} ,
2023-11-17 15:15:55 +00:00
) ) = pipeline . elements . first ( )
2022-02-15 19:31:14 +00:00
{
call = def_call . clone ( ) ;
2021-11-15 23:16:06 +00:00
2022-02-15 19:31:14 +00:00
call . head = span ( & spans [ 0 ..= 1 ] ) ;
call . decl_id = export_def_decl_id ;
2021-09-28 18:03:53 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" unexpected output from parsing a definition " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
2021-09-28 18:03:53 +00:00
} ;
2022-07-29 08:57:10 +00:00
result
2021-11-15 23:16:06 +00:00
}
2023-11-16 22:44:28 +00:00
b " extern " = > {
2022-03-19 18:58:01 +00:00
let lite_command = LiteCommand {
comments : lite_command . comments . clone ( ) ,
parts : spans [ 1 .. ] . to_vec ( ) ,
} ;
2023-08-18 17:45:33 +00:00
let extern_name = [ b " export " , kw_name ] . concat ( ) ;
2023-04-07 18:09:38 +00:00
let pipeline = parse_extern ( working_set , & lite_command , Some ( module_name ) ) ;
2022-03-19 18:58:01 +00:00
2023-08-18 17:45:33 +00:00
let export_def_decl_id = if let Some ( id ) = working_set . find_decl ( & extern_name ) {
2023-07-13 19:05:03 +00:00
id
} else {
working_set . error ( ParseError ::InternalError (
2023-11-16 22:44:28 +00:00
" missing 'export extern' command " . into ( ) ,
2023-07-13 19:05:03 +00:00
export_span ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
} ;
2022-03-19 18:58:01 +00:00
// Trying to warp the 'def' call into the 'export def' in a very clumsy way
2022-11-22 18:26:13 +00:00
if let Some ( PipelineElement ::Expression (
_ ,
Expression {
expr : Expr ::Call ( ref def_call ) ,
..
} ,
2023-11-17 15:15:55 +00:00
) ) = pipeline . elements . first ( )
2022-03-19 18:58:01 +00:00
{
call = def_call . clone ( ) ;
call . head = span ( & spans [ 0 ..= 1 ] ) ;
call . decl_id = export_def_decl_id ;
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" unexpected output from parsing a definition " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
2022-03-19 18:58:01 +00:00
} ;
2022-07-29 08:57:10 +00:00
let mut result = vec! [ ] ;
2022-09-08 09:27:11 +00:00
let decl_name = match spans . get ( 2 ) {
Some ( span ) = > working_set . get_span_contents ( * span ) ,
None = > & [ ] ,
} ;
2022-07-29 08:57:10 +00:00
let decl_name = trim_quotes ( decl_name ) ;
2023-07-13 19:05:03 +00:00
if let Some ( decl_id ) = working_set . find_decl ( decl_name ) {
2022-07-29 08:57:10 +00:00
result . push ( Exportable ::Decl {
name : decl_name . to_vec ( ) ,
id : decl_id ,
} ) ;
2022-03-19 18:58:01 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" failed to find added declaration " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
2022-03-19 18:58:01 +00:00
}
2022-07-29 08:57:10 +00:00
result
2022-03-19 18:58:01 +00:00
}
b " alias " = > {
let lite_command = LiteCommand {
comments : lite_command . comments . clone ( ) ,
parts : spans [ 1 .. ] . to_vec ( ) ,
} ;
2023-04-07 18:09:38 +00:00
let pipeline = parse_alias ( working_set , & lite_command , Some ( module_name ) ) ;
2022-03-19 18:58:01 +00:00
2023-07-13 19:05:03 +00:00
let export_alias_decl_id = if let Some ( id ) = working_set . find_decl ( b " export alias " )
{
id
} else {
working_set . error ( ParseError ::InternalError (
" missing 'export alias' command " . into ( ) ,
export_span ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
} ;
2022-03-19 18:58:01 +00:00
// Trying to warp the 'alias' call into the 'export alias' in a very clumsy way
2022-11-22 18:26:13 +00:00
if let Some ( PipelineElement ::Expression (
_ ,
Expression {
expr : Expr ::Call ( ref alias_call ) ,
..
} ,
2023-11-17 15:15:55 +00:00
) ) = pipeline . elements . first ( )
2022-03-19 18:58:01 +00:00
{
call = alias_call . clone ( ) ;
call . head = span ( & spans [ 0 ..= 1 ] ) ;
call . decl_id = export_alias_decl_id ;
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" unexpected output from parsing a definition " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
2022-03-19 18:58:01 +00:00
} ;
2022-07-29 08:57:10 +00:00
let mut result = vec! [ ] ;
2022-09-08 09:27:11 +00:00
let alias_name = match spans . get ( 2 ) {
Some ( span ) = > working_set . get_span_contents ( * span ) ,
None = > & [ ] ,
} ;
2022-07-29 08:57:10 +00:00
let alias_name = trim_quotes ( alias_name ) ;
2023-07-13 19:05:03 +00:00
if let Some ( alias_id ) = working_set . find_decl ( alias_name ) {
Re-implement aliases (#8123)
# Description
This PR adds an alternative alias implementation. Old aliases still work
but you need to use `old-alias` instead of `alias`.
Instead of replacing spans in the original code and re-parsing, which
proved to be extremely error-prone and a constant source of panics, the
new implementation creates a new command that references the old
command. Consider the new alias defined as `alias ll = ls -l`. The
parser creates a new command called `ll` and remembers that it is
actually a `ls` command called with the `-l` flag. Then, when the parser
sees the `ll` command, it will translate it to `ls -l` and passes to it
any parameters that were passed to the call to `ll`. It works quite
similar to how known externals defined with `extern` are implemented.
The new alias implementation should work the same way as the old
aliases, including exporting from modules, referencing both known and
unknown externals. It seems to preserve custom completions and pipeline
metadata. It is quite robust in most cases but there are some rough
edges (see later).
Fixes https://github.com/nushell/nushell/issues/7648,
https://github.com/nushell/nushell/issues/8026,
https://github.com/nushell/nushell/issues/7512,
https://github.com/nushell/nushell/issues/5780,
https://github.com/nushell/nushell/issues/7754
No effect: https://github.com/nushell/nushell/issues/8122 (we might
revisit the completions code after this PR)
Should use custom command instead:
https://github.com/nushell/nushell/issues/6048
# User-Facing Changes
Since aliases are now basically commands, it has some new implications:
1. `alias spam = "spam"` (requires command call)
* **workaround**: use `alias spam = echo "spam"`
2. `def foo [] { 'foo' }; alias foo = ls -l` (foo defined more than
once)
* **workaround**: use different name (commands also have this
limitation)
4. `alias ls = (ls | sort-by type name -i)`
* **workaround**: Use custom command. _The common issue with this is
that it is currently not easy to pass flags through custom commands and
command referencing itself will lead to stack overflow. Both of these
issues are meant to be addressed._
5. TODO: Help messages, `which` command, `$nu.scope.aliases`, etc.
* Should we treat the aliases as commands or should they be separated
from regular commands?
6. Needs better error message and syntax highlight for recursed alias
(`alias f = f`)
7. Can't create alias with the same name as existing command (`alias ls
= ls -a`)
* Might be possible to add support for it (not 100% sure)
8. Standalone `alias` doesn't list aliases anymore
9. Can't alias parser keywords (e.g., stuff like `alias ou = overlay
use` won't work)
* TODO: Needs a better error message when attempting to do so
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-27 07:44:05 +00:00
result . push ( Exportable ::Decl {
2022-07-29 08:57:10 +00:00
name : alias_name . to_vec ( ) ,
id : alias_id ,
} ) ;
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" failed to find added alias " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
2022-07-29 08:57:10 +00:00
}
result
}
b " use " = > {
let lite_command = LiteCommand {
comments : lite_command . comments . clone ( ) ,
parts : spans [ 1 .. ] . to_vec ( ) ,
} ;
2023-04-07 18:09:38 +00:00
let ( pipeline , exportables ) = parse_use ( working_set , & lite_command . parts ) ;
2022-07-29 08:57:10 +00:00
2023-07-13 19:05:03 +00:00
let export_use_decl_id = if let Some ( id ) = working_set . find_decl ( b " export use " ) {
id
} else {
working_set . error ( ParseError ::InternalError (
" missing 'export use' command " . into ( ) ,
export_span ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
} ;
2022-07-29 08:57:10 +00:00
// Trying to warp the 'use' call into the 'export use' in a very clumsy way
2022-11-22 18:26:13 +00:00
if let Some ( PipelineElement ::Expression (
_ ,
Expression {
expr : Expr ::Call ( ref use_call ) ,
..
} ,
2023-11-17 15:15:55 +00:00
) ) = pipeline . elements . first ( )
2022-07-29 08:57:10 +00:00
{
call = use_call . clone ( ) ;
call . head = span ( & spans [ 0 ..= 1 ] ) ;
call . decl_id = export_use_decl_id ;
2022-03-19 18:58:01 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::InternalError (
" unexpected output from parsing a definition " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
2022-07-29 08:57:10 +00:00
} ;
exportables
2022-03-19 18:58:01 +00:00
}
2023-05-06 18:39:54 +00:00
b " module " = > {
2023-05-06 20:55:10 +00:00
let ( pipeline , maybe_module_id ) =
parse_module ( working_set , lite_command , Some ( module_name ) ) ;
2023-05-06 18:39:54 +00:00
let export_module_decl_id =
2023-07-13 19:05:03 +00:00
if let Some ( id ) = working_set . find_decl ( b " export module " ) {
2023-05-06 18:39:54 +00:00
id
} else {
working_set . error ( ParseError ::InternalError (
" missing 'export module' command " . into ( ) ,
export_span ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
} ;
// Trying to warp the 'module' call into the 'export module' in a very clumsy way
if let Some ( PipelineElement ::Expression (
_ ,
Expression {
expr : Expr ::Call ( ref module_call ) ,
..
} ,
2023-11-17 15:15:55 +00:00
) ) = pipeline . elements . first ( )
2023-05-06 18:39:54 +00:00
{
call = module_call . clone ( ) ;
call . head = span ( & spans [ 0 ..= 1 ] ) ;
call . decl_id = export_module_decl_id ;
} else {
working_set . error ( ParseError ::InternalError (
" unexpected output from parsing a definition " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
} ;
let mut result = vec! [ ] ;
if let Some ( module_name_span ) = spans . get ( 2 ) {
let module_name = working_set . get_span_contents ( * module_name_span ) ;
let module_name = trim_quotes ( module_name ) ;
2023-05-06 20:55:10 +00:00
if let Some ( module_id ) = maybe_module_id {
2023-05-06 18:39:54 +00:00
result . push ( Exportable ::Module {
2023-05-06 20:55:10 +00:00
name : working_set . get_module ( module_id ) . name ( ) ,
2023-05-06 18:39:54 +00:00
id : module_id ,
} ) ;
} else {
working_set . error ( ParseError ::InternalError (
2023-05-06 20:55:10 +00:00
format! (
" failed to find added module '{}' " ,
String ::from_utf8_lossy ( module_name )
) ,
2023-05-06 18:39:54 +00:00
span ( & spans [ 1 .. ] ) ,
) ) ;
}
}
result
}
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
b " const " = > {
let pipeline = parse_const ( working_set , & spans [ 1 .. ] ) ;
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
let export_const_decl_id = if let Some ( id ) = working_set . find_decl ( b " export const " )
{
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
id
} else {
working_set . error ( ParseError ::InternalError (
" missing 'export const' command " . into ( ) ,
export_span ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
} ;
// Trying to warp the 'const' call into the 'export const' in a very clumsy way
if let Some ( PipelineElement ::Expression (
_ ,
Expression {
expr : Expr ::Call ( ref def_call ) ,
..
} ,
2023-11-17 15:15:55 +00:00
) ) = pipeline . elements . first ( )
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
{
call = def_call . clone ( ) ;
call . head = span ( & spans [ 0 ..= 1 ] ) ;
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
call . decl_id = export_const_decl_id ;
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
} else {
working_set . error ( ParseError ::InternalError (
" unexpected output from parsing a definition " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
} ;
let mut result = vec! [ ] ;
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
if let Some ( var_name_span ) = spans . get ( 2 ) {
let var_name = working_set . get_span_contents ( * var_name_span ) ;
let var_name = trim_quotes ( var_name ) ;
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
if let Some ( var_id ) = working_set . find_variable ( var_name ) {
if let Err ( err ) = working_set . get_constant ( var_id ) {
working_set . error ( err ) ;
} else {
result . push ( Exportable ::VarDecl {
name : var_name . to_vec ( ) ,
id : var_id ,
} ) ;
}
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
} else {
working_set . error ( ParseError ::InternalError (
" failed to find added variable " . into ( ) ,
span ( & spans [ 1 .. ] ) ,
) ) ;
}
}
result
}
2021-11-15 23:16:06 +00:00
_ = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::Expected (
2023-11-19 15:25:09 +00:00
" def, alias, use, module, const or extern keyword " ,
2023-04-07 00:35:45 +00:00
spans [ 1 ] ,
) ) ;
2021-11-15 23:16:06 +00:00
2022-07-29 08:57:10 +00:00
vec! [ ]
2021-09-28 18:03:53 +00:00
}
2021-09-28 17:29:38 +00:00
}
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::MissingPositional (
2023-11-19 15:25:09 +00:00
" def, alias, use, module, const or extern keyword " . to_string ( ) ,
2023-04-07 00:35:45 +00:00
Span ::new ( export_span . end , export_span . end ) ,
2023-11-19 15:25:09 +00:00
" def, alias, use, module, const or extern keyword " . to_string ( ) ,
2023-04-07 00:35:45 +00:00
) ) ;
2021-11-15 23:16:06 +00:00
2022-07-29 08:57:10 +00:00
vec! [ ]
2021-11-15 23:16:06 +00:00
} ;
(
2022-02-15 19:31:14 +00:00
Pipeline ::from_vec ( vec! [ Expression {
2021-11-15 23:16:06 +00:00
expr : Expr ::Call ( call ) ,
span : span ( spans ) ,
2022-04-07 04:34:09 +00:00
ty : Type ::Any ,
2021-11-15 23:16:06 +00:00
custom_completion : None ,
2022-02-15 19:31:14 +00:00
} ] ) ,
2022-07-29 08:57:10 +00:00
exportables ,
2021-11-15 23:16:06 +00:00
)
2021-09-28 17:29:38 +00:00
}
2022-08-23 07:45:17 +00:00
pub fn parse_export_env (
working_set : & mut StateWorkingSet ,
spans : & [ Span ] ,
2023-04-07 00:35:45 +00:00
) -> ( Pipeline , Option < BlockId > ) {
2022-08-23 07:45:17 +00:00
if ! spans . is_empty ( ) & & working_set . get_span_contents ( spans [ 0 ] ) ! = b " export-env " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Wrong call name for 'export-env' command " . into ( ) ,
span ( spans ) ,
) ) ;
return ( garbage_pipeline ( spans ) , None ) ;
2022-08-23 07:45:17 +00:00
}
if spans . len ( ) < 2 {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::MissingPositional (
" block " . into ( ) ,
span ( spans ) ,
" export-env <block> " . into ( ) ,
) ) ;
return ( garbage_pipeline ( spans ) , None ) ;
2022-08-23 07:45:17 +00:00
}
2023-07-13 19:05:03 +00:00
let call = match working_set . find_decl ( b " export-env " ) {
2022-08-23 07:45:17 +00:00
Some ( decl_id ) = > {
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & [ spans [ 1 ] ] , decl_id ) ;
2022-08-23 07:45:17 +00:00
let decl = working_set . get_decl ( decl_id ) ;
let call_span = span ( spans ) ;
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & decl . signature ( ) , & call ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2022-08-23 07:45:17 +00:00
return (
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : output ,
custom_completion : None ,
} ] ) ,
2022-08-26 22:32:19 +00:00
None ,
2022-08-23 07:45:17 +00:00
) ;
}
call
}
None = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: 'export-env' declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
return ( garbage_pipeline ( spans ) , None ) ;
2022-08-23 07:45:17 +00:00
}
} ;
2022-08-26 22:32:19 +00:00
let block_id = if let Some ( block ) = call . positional_nth ( 0 ) {
if let Some ( block_id ) = block . as_block ( ) {
block_id
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: 'export-env' block is not a block " . into ( ) ,
block . span ,
) ) ;
return ( garbage_pipeline ( spans ) , None ) ;
2022-08-26 22:32:19 +00:00
}
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: 'export-env' block is missing " . into ( ) ,
span ( spans ) ,
) ) ;
return ( garbage_pipeline ( spans ) , None ) ;
2022-08-26 22:32:19 +00:00
} ;
2022-08-23 07:45:17 +00:00
let pipeline = Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2023-04-07 00:35:45 +00:00
( pipeline , Some ( block_id ) )
2022-08-23 07:45:17 +00:00
}
2022-12-30 15:44:37 +00:00
fn collect_first_comments ( tokens : & [ Token ] ) -> Vec < Span > {
let mut comments = vec! [ ] ;
let mut tokens_iter = tokens . iter ( ) . peekable ( ) ;
while let Some ( token ) = tokens_iter . next ( ) {
match token . contents {
TokenContents ::Comment = > {
comments . push ( token . span ) ;
}
TokenContents ::Eol = > {
if let Some ( Token {
contents : TokenContents ::Eol ,
..
} ) = tokens_iter . peek ( )
{
if ! comments . is_empty ( ) {
break ;
}
}
}
_ = > {
comments . clear ( ) ;
break ;
}
}
}
comments
}
2021-10-18 20:19:25 +00:00
pub fn parse_module_block (
working_set : & mut StateWorkingSet ,
2021-10-31 15:22:10 +00:00
span : Span ,
2023-01-22 19:34:15 +00:00
module_name : & [ u8 ] ,
2023-04-07 00:35:45 +00:00
) -> ( Block , Module , Vec < Span > ) {
2021-10-18 20:19:25 +00:00
working_set . enter_scope ( ) ;
2021-10-31 15:22:10 +00:00
let source = working_set . get_span_contents ( span ) ;
2021-10-18 20:19:25 +00:00
2022-01-22 18:24:47 +00:00
let ( output , err ) = lex ( source , span . start , & [ ] , & [ ] , false ) ;
2023-04-07 00:35:45 +00:00
if let Some ( err ) = err {
working_set . error ( err )
}
2021-10-18 20:19:25 +00:00
2022-12-30 15:44:37 +00:00
let module_comments = collect_first_comments ( & output ) ;
2021-10-18 20:19:25 +00:00
let ( output , err ) = lite_parse ( & output ) ;
2023-04-07 00:35:45 +00:00
if let Some ( err ) = err {
working_set . error ( err )
}
2021-10-18 20:19:25 +00:00
for pipeline in & output . block {
if pipeline . commands . len ( ) = = 1 {
2022-11-22 18:26:13 +00:00
if let LiteElement ::Command ( _ , command ) = & pipeline . commands [ 0 ] {
2023-04-07 18:09:38 +00:00
parse_def_predecl ( working_set , & command . parts ) ;
2022-11-18 21:46:48 +00:00
}
2021-10-18 20:19:25 +00:00
}
}
2023-01-22 19:34:15 +00:00
let mut module = Module ::from_span ( module_name . to_vec ( ) , span ) ;
2021-10-18 20:19:25 +00:00
2023-04-18 08:19:08 +00:00
let mut block = Block ::new_with_capacity ( output . block . len ( ) ) ;
2021-10-18 20:19:25 +00:00
2023-04-18 08:19:08 +00:00
for pipeline in output . block . iter ( ) {
if pipeline . commands . len ( ) = = 1 {
match & pipeline . commands [ 0 ] {
2024-02-08 17:30:46 +00:00
LiteElement ::Command ( _ , command )
| LiteElement ::ErrPipedCommand ( _ , command )
| LiteElement ::OutErrPipedCommand ( _ , command ) = > {
2023-04-18 08:19:08 +00:00
let name = working_set . get_span_contents ( command . parts [ 0 ] ) ;
match name {
2023-11-19 15:25:09 +00:00
b " def " = > {
2023-10-02 18:13:31 +00:00
block . pipelines . push (
parse_def (
working_set ,
command ,
None , // using commands named as the module locally is OK
)
. 0 ,
)
2023-04-18 08:19:08 +00:00
}
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
b " const " = > block
. pipelines
. push ( parse_const ( working_set , & command . parts ) ) ,
2023-11-16 22:44:28 +00:00
b " extern " = > block
. pipelines
. push ( parse_extern ( working_set , command , None ) ) ,
2023-04-18 08:19:08 +00:00
b " alias " = > {
block . pipelines . push ( parse_alias (
working_set ,
command ,
None , // using aliases named as the module locally is OK
) )
}
b " use " = > {
let ( pipeline , _ ) = parse_use ( working_set , & command . parts ) ;
block . pipelines . push ( pipeline )
}
2023-05-06 18:39:54 +00:00
b " module " = > {
2023-05-06 20:55:10 +00:00
let ( pipeline , _ ) = parse_module (
2023-05-06 18:39:54 +00:00
working_set ,
command ,
None , // using modules named as the module locally is OK
) ;
block . pipelines . push ( pipeline )
}
2023-04-18 08:19:08 +00:00
b " export " = > {
let ( pipe , exportables ) =
parse_export_in_module ( working_set , command , module_name ) ;
for exportable in exportables {
match exportable {
Exportable ::Decl { name , id } = > {
if & name = = b " main " {
2023-05-06 18:39:54 +00:00
if module . main . is_some ( ) {
let err_span = if ! pipe . elements . is_empty ( ) {
if let PipelineElement ::Expression (
_ ,
Expression {
expr : Expr ::Call ( call ) ,
..
} ,
) = & pipe . elements [ 0 ]
{
call . head
} else {
pipe . elements [ 0 ] . span ( )
}
} else {
span
} ;
working_set . error ( ParseError ::ModuleDoubleMain (
String ::from_utf8_lossy ( module_name )
. to_string ( ) ,
err_span ,
) ) ;
} else {
module . main = Some ( id ) ;
}
2023-04-18 08:19:08 +00:00
} else {
module . add_decl ( name , id ) ;
2022-11-18 21:46:48 +00:00
}
2022-07-29 08:57:10 +00:00
}
2023-05-06 18:39:54 +00:00
Exportable ::Module { name , id } = > {
if & name = = b " mod " {
let (
submodule_main ,
submodule_decls ,
submodule_submodules ,
) = {
let submodule = working_set . get_module ( id ) ;
(
submodule . main ,
submodule . decls ( ) ,
submodule . submodules ( ) ,
)
} ;
// Add submodule's decls to the parent module
for ( decl_name , decl_id ) in submodule_decls {
module . add_decl ( decl_name , decl_id ) ;
}
// Add submodule's main command to the parent module
if let Some ( main_decl_id ) = submodule_main {
if module . main . is_some ( ) {
let err_span = if ! pipe . elements . is_empty ( ) {
if let PipelineElement ::Expression (
_ ,
Expression {
expr : Expr ::Call ( call ) ,
..
} ,
) = & pipe . elements [ 0 ]
{
call . head
} else {
pipe . elements [ 0 ] . span ( )
}
} else {
span
} ;
working_set . error (
ParseError ::ModuleDoubleMain (
String ::from_utf8_lossy ( module_name )
. to_string ( ) ,
err_span ,
) ,
) ;
} else {
module . main = Some ( main_decl_id ) ;
}
}
// Add submodule's submodules to the parent module
for ( submodule_name , submodule_id ) in
submodule_submodules
{
module . add_submodule ( submodule_name , submodule_id ) ;
}
} else {
module . add_submodule ( name , id ) ;
}
}
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
Exportable ::VarDecl { name , id } = > {
module . add_variable ( name , id ) ;
}
2022-03-19 18:58:01 +00:00
}
2022-11-18 21:46:48 +00:00
}
2023-04-18 08:19:08 +00:00
block . pipelines . push ( pipe )
}
b " export-env " = > {
let ( pipe , maybe_env_block ) =
parse_export_env ( working_set , & command . parts ) ;
2022-08-26 22:32:19 +00:00
2023-04-18 08:19:08 +00:00
if let Some ( block_id ) = maybe_env_block {
module . add_env_block ( block_id ) ;
2022-11-18 21:46:48 +00:00
}
2022-08-26 22:32:19 +00:00
2023-04-18 08:19:08 +00:00
block . pipelines . push ( pipe )
}
_ = > {
working_set . error ( ParseError ::ExpectedKeyword (
2023-11-19 15:25:09 +00:00
" def, const, extern, alias, use, module, export or export-env keyword " . into ( ) ,
2023-04-18 08:19:08 +00:00
command . parts [ 0 ] ,
) ) ;
block . pipelines . push ( garbage_pipeline ( & command . parts ) )
2023-04-07 00:35:45 +00:00
}
2022-08-26 22:32:19 +00:00
}
2021-10-18 20:19:25 +00:00
}
2023-11-27 13:52:39 +00:00
LiteElement ::Redirection ( _ , _ , command , _ ) = > {
2023-04-18 08:19:08 +00:00
block . pipelines . push ( garbage_pipeline ( & command . parts ) )
}
LiteElement ::SeparateRedirection {
2023-11-27 13:52:39 +00:00
out : ( _ , command , _ ) ,
..
2023-04-18 08:19:08 +00:00
} = > block . pipelines . push ( garbage_pipeline ( & command . parts ) ) ,
Avoid blocking when `o+e>` redirects too much stderr message (#8784)
# Description
Fixes: #8565
Here is another pr #7240 tried to address the issue, but it works in a
wrong way.
After this change `o+e>` won't redirect all stdout message then stderr
message and it works more like how bash does.
# User-Facing Changes
For the given python code:
```python
# test.py
import sys
print('aa'*300, flush=True)
print('bb'*999999, file=sys.stderr, flush=True)
print('cc'*300, flush=True)
```
Running `python test.py out+err> a.txt` shoudn't hang nushell, and
`a.txt` keeps output in the same order
## About the change
The core idea is that when doing lite-parsing, introduce a new variant
`LiteElement::SameTargetRedirection` if we meet `out+err>` redirection
token(which is generated by lex function),
During converting from lite block to block,
LiteElement::SameTargetRedirection will be converted to
PipelineElement::SameTargetRedirection.
Then in the block eval process, if we get
PipelineElement::SameTargetRedirection, we'll invoke `run-external` with
`--redirect-combine` flag, then pipe the result into save command
## What happened internally?
Take the following command as example:
`^ls o+e> log.txt`
lex parsing result(`Tokens`) are not changed, but `LiteBlock` and
`Block` is changed after this pr.
### LiteBlock before
```rust
LiteBlock {
block: [
LitePipeline { commands: [
Command(None, LiteCommand { comments: [], parts: [Span { start: 39041, end: 39044 }] }),
// actually the span of first Redirection is wrong too..
Redirection(Span { start: 39058, end: 39062 }, StdoutAndStderr, LiteCommand { comments: [], parts: [Span { start: 39050, end: 39057 }] }),
]
}]
}
```
### LiteBlock after
```rust
LiteBlock {
block: [
LitePipeline {
commands: [
SameTargetRedirection {
cmd: (None, LiteCommand { comments: [], parts: [Span { start: 147945, end: 147948}]}),
redirection: (Span { start: 147949, end: 147957 }, LiteCommand { comments: [], parts: [Span { start: 147958, end: 147965 }]})
}
]
}
]
}
```
### Block before
```rust
Pipeline {
elements: [
Expression(None, Expression {
expr: ExternalCall(Expression { expr: String("ls"), span: Span { start: 39042, end: 39044 }, ty: String, custom_completion: None }, [], false),
span: Span { start: 39041, end: 39044 },
ty: Any, custom_completion: None
}),
Redirection(Span { start: 39058, end: 39062 }, StdoutAndStderr, Expression { expr: String("out.txt"), span: Span { start: 39050, end: 39057 }, ty: String, custom_completion: None })] }
```
### Block after
```rust
Pipeline {
elements: [
SameTargetRedirection {
cmd: (None, Expression {
expr: ExternalCall(Expression { expr: String("ls"), span: Span { start: 147946, end: 147948 }, ty: String, custom_completion: None}, [], false),
span: Span { start: 147945, end: 147948},
ty: Any, custom_completion: None
}),
redirection: (Span { start: 147949, end: 147957}, Expression {expr: String("log.txt"), span: Span { start: 147958, end: 147965 },ty: String,custom_completion: None}
}
]
}
```
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-utils/standard_library/tests.nu` to run the
tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-05-17 22:47:03 +00:00
LiteElement ::SameTargetRedirection {
cmd : ( _ , command ) , ..
} = > block . pipelines . push ( garbage_pipeline ( & command . parts ) ) ,
2021-10-18 20:19:25 +00:00
}
2023-04-18 08:19:08 +00:00
} else {
2023-05-24 19:53:57 +00:00
working_set . error ( ParseError ::Expected ( " not a pipeline " , span ) ) ;
2023-04-18 08:19:08 +00:00
block . pipelines . push ( garbage_pipeline ( & [ span ] ) )
}
}
2021-10-18 20:19:25 +00:00
working_set . exit_scope ( ) ;
2023-04-07 00:35:45 +00:00
( block , module , module_comments )
2021-10-18 20:19:25 +00:00
}
2023-05-06 18:39:54 +00:00
fn parse_module_file (
working_set : & mut StateWorkingSet ,
2023-05-23 20:48:50 +00:00
path : ParserPath ,
2023-05-06 18:39:54 +00:00
path_span : Span ,
name_override : Option < String > ,
) -> Option < ModuleId > {
if let Some ( i ) = working_set
. parsed_module_files
. iter ( )
2023-05-23 20:48:50 +00:00
. rposition ( | p | p = = path . path ( ) )
2023-05-06 18:39:54 +00:00
{
let mut files : Vec < String > = working_set
. parsed_module_files
. split_off ( i )
. iter ( )
. map ( | p | p . to_string_lossy ( ) . to_string ( ) )
. collect ( ) ;
2023-05-23 20:48:50 +00:00
files . push ( path . path ( ) . to_string_lossy ( ) . to_string ( ) ) ;
2023-05-06 18:39:54 +00:00
let msg = files . join ( " \n uses " ) ;
working_set . error ( ParseError ::CyclicalModuleImport ( msg , path_span ) ) ;
return None ;
}
let module_name = if let Some ( name ) = name_override {
name
} else if let Some ( stem ) = path . file_stem ( ) {
stem . to_string_lossy ( ) . to_string ( )
} else {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound (
path_span ,
path . path ( ) . to_string_lossy ( ) . to_string ( ) ,
) ) ;
2023-05-06 18:39:54 +00:00
return None ;
} ;
2023-05-23 20:48:50 +00:00
let contents = if let Some ( contents ) = path . read ( working_set ) {
2023-05-06 18:39:54 +00:00
contents
} else {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound (
path_span ,
path . path ( ) . to_string_lossy ( ) . to_string ( ) ,
) ) ;
2023-05-06 18:39:54 +00:00
return None ;
} ;
2023-05-23 20:48:50 +00:00
let file_id = working_set . add_file ( path . path ( ) . to_string_lossy ( ) . to_string ( ) , & contents ) ;
2023-05-06 18:39:54 +00:00
let new_span = working_set . get_span_for_file ( file_id ) ;
2023-05-07 11:41:40 +00:00
if let Some ( module_id ) = working_set . find_module_by_span ( new_span ) {
return Some ( module_id ) ;
}
2023-05-06 18:39:54 +00:00
// Change the currently parsed directory
let prev_currently_parsed_cwd = if let Some ( parent ) = path . parent ( ) {
2023-05-23 20:48:50 +00:00
working_set . currently_parsed_cwd . replace ( parent . into ( ) )
2023-05-06 18:39:54 +00:00
} else {
working_set . currently_parsed_cwd . clone ( )
} ;
// Add the file to the stack of parsed module files
2023-05-23 20:48:50 +00:00
working_set . parsed_module_files . push ( path . path_buf ( ) ) ;
2023-05-06 18:39:54 +00:00
// Parse the module
let ( block , module , module_comments ) =
parse_module_block ( working_set , new_span , module_name . as_bytes ( ) ) ;
// Remove the file from the stack of parsed module files
working_set . parsed_module_files . pop ( ) ;
// Restore the currently parsed directory back
working_set . currently_parsed_cwd = prev_currently_parsed_cwd ;
let _ = working_set . add_block ( block ) ;
let module_id = working_set . add_module ( & module_name , module , module_comments ) ;
Some ( module_id )
}
pub fn parse_module_file_or_dir (
working_set : & mut StateWorkingSet ,
path : & [ u8 ] ,
path_span : Span ,
name_override : Option < String > ,
) -> Option < ModuleId > {
let ( module_path_str , err ) = unescape_unquote_string ( path , path_span ) ;
if let Some ( err ) = err {
working_set . error ( err ) ;
return None ;
}
let cwd = working_set . get_cwd ( ) ;
let module_path =
if let Some ( path ) = find_in_dirs ( & module_path_str , working_set , & cwd , LIB_DIRS_VAR ) {
path
} else {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound ( path_span , module_path_str ) ) ;
2023-05-06 18:39:54 +00:00
return None ;
} ;
if module_path . is_dir ( ) {
2023-12-15 11:37:55 +00:00
if module_path . read_dir ( ) . is_none ( ) {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound (
path_span ,
module_path . path ( ) . to_string_lossy ( ) . to_string ( ) ,
) ) ;
2023-05-23 20:48:50 +00:00
return None ;
} ;
2023-05-12 22:20:33 +00:00
2023-05-23 20:48:50 +00:00
let module_name = if let Some ( stem ) = module_path . file_stem ( ) {
stem . to_string_lossy ( ) . to_string ( )
} else {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound (
path_span ,
module_path . path ( ) . to_string_lossy ( ) . to_string ( ) ,
) ) ;
2023-05-23 20:48:50 +00:00
return None ;
} ;
2023-05-12 22:20:33 +00:00
2023-05-23 20:48:50 +00:00
let mod_nu_path = module_path . clone ( ) . join ( " mod.nu " ) ;
2023-05-06 18:39:54 +00:00
2023-05-23 20:48:50 +00:00
if ! ( mod_nu_path . exists ( ) & & mod_nu_path . is_file ( ) ) {
2023-09-23 13:30:03 +00:00
working_set . error ( ParseError ::ModuleMissingModNuFile (
module_path . path ( ) . to_string_lossy ( ) . to_string ( ) ,
path_span ,
) ) ;
2023-05-23 20:48:50 +00:00
return None ;
}
2023-05-06 18:39:54 +00:00
2023-05-23 20:48:50 +00:00
if let Some ( module_id ) = parse_module_file (
working_set ,
mod_nu_path ,
path_span ,
name_override . or ( Some ( module_name ) ) ,
) {
2023-12-15 11:37:55 +00:00
let module = working_set . get_module ( module_id ) . clone ( ) ;
2023-05-07 11:41:40 +00:00
2023-05-23 20:48:50 +00:00
let module_name = String ::from_utf8_lossy ( & module . name ) . to_string ( ) ;
2023-05-07 11:41:40 +00:00
2023-05-23 20:48:50 +00:00
let module_comments = if let Some ( comments ) = working_set . get_module_comments ( module_id )
{
comments . to_vec ( )
2023-05-06 18:39:54 +00:00
} else {
2023-05-23 20:48:50 +00:00
vec! [ ]
} ;
let new_module_id = working_set . add_module ( & module_name , module , module_comments ) ;
Some ( new_module_id )
2023-05-06 18:39:54 +00:00
} else {
None
}
} else if module_path . is_file ( ) {
parse_module_file ( working_set , module_path , path_span , name_override )
} else {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound (
path_span ,
module_path . path ( ) . to_string_lossy ( ) . to_string ( ) ,
) ) ;
2023-05-06 18:39:54 +00:00
None
}
}
pub fn parse_module (
working_set : & mut StateWorkingSet ,
lite_command : & LiteCommand ,
module_name : Option < & [ u8 ] > ,
2023-05-06 20:55:10 +00:00
) -> ( Pipeline , Option < ModuleId > ) {
2021-09-26 18:39:19 +00:00
// TODO: Currently, module is closing over its parent scope (i.e., defs in the parent scope are
2021-10-10 11:31:13 +00:00
// visible and usable in this module's scope). We want to disable that for files.
2021-09-26 18:39:19 +00:00
2022-12-30 15:44:37 +00:00
let spans = & lite_command . parts ;
let mut module_comments = lite_command . comments . clone ( ) ;
2023-05-06 18:39:54 +00:00
let split_id = if spans . len ( ) > 1 & & working_set . get_span_contents ( spans [ 0 ] ) = = b " export " {
2
} else {
1
} ;
2021-09-26 18:39:19 +00:00
2023-07-13 19:05:03 +00:00
let ( call , call_span ) = match working_set . find_decl ( b " module " ) {
2023-05-06 18:39:54 +00:00
Some ( decl_id ) = > {
let ( command_spans , rest_spans ) = spans . split_at ( split_id ) ;
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , span ( command_spans ) , rest_spans , decl_id ) ;
let decl = working_set . get_decl ( decl_id ) ;
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
let call_span = span ( spans ) ;
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & decl . signature ( ) , & call ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2023-05-06 20:55:10 +00:00
return (
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : output ,
custom_completion : None ,
} ] ) ,
None ,
) ;
2023-05-06 18:39:54 +00:00
}
( call , call_span )
}
None = > {
working_set . error ( ParseError ::UnknownState (
" internal error: 'module' or 'export module' declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
2023-05-06 20:55:10 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2021-09-26 18:39:19 +00:00
}
2023-05-06 18:39:54 +00:00
} ;
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
let ( module_name_or_path , module_name_or_path_span , module_name_or_path_expr ) =
if let Some ( name ) = call . positional_nth ( 0 ) {
if let Some ( s ) = name . as_string ( ) {
if let Some ( mod_name ) = module_name {
if s . as_bytes ( ) = = mod_name {
working_set . error ( ParseError ::NamedAsModule (
" module " . to_string ( ) ,
s ,
" mod " . to_string ( ) ,
name . span ,
) ) ;
2023-05-06 20:55:10 +00:00
return (
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ,
None ,
) ;
2023-05-06 18:39:54 +00:00
}
}
( s , name . span , name . clone ( ) )
} else {
working_set . error ( ParseError ::UnknownState (
" internal error: name not a string " . into ( ) ,
span ( spans ) ,
) ) ;
2023-05-06 20:55:10 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2023-05-06 18:39:54 +00:00
}
2021-09-26 18:39:19 +00:00
} else {
2023-05-06 18:39:54 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: missing positional " . into ( ) ,
span ( spans ) ,
) ) ;
2023-05-06 20:55:10 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2023-05-06 18:39:54 +00:00
} ;
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
let pipeline = Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
if spans . len ( ) = = split_id + 1 {
2023-05-23 20:48:50 +00:00
if let Some ( module_id ) = parse_module_file_or_dir (
working_set ,
module_name_or_path . as_bytes ( ) ,
module_name_or_path_span ,
None ,
) {
return ( pipeline , Some ( module_id ) ) ;
2023-05-06 18:39:54 +00:00
} else {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound (
module_name_or_path_span ,
module_name_or_path ,
) ) ;
2023-05-06 20:55:10 +00:00
return ( pipeline , None ) ;
2023-05-06 18:39:54 +00:00
}
}
2022-12-30 15:44:37 +00:00
2023-05-06 18:39:54 +00:00
if spans . len ( ) < split_id + 2 {
working_set . error ( ParseError ::UnknownState (
" Expected structure: module <name> or module <name> <block> " . into ( ) ,
span ( spans ) ,
) ) ;
2021-09-26 18:39:19 +00:00
2023-05-06 20:55:10 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2023-05-06 18:39:54 +00:00
}
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
let module_name = module_name_or_path ;
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
let block_span = spans [ split_id + 1 ] ;
let block_bytes = working_set . get_span_contents ( block_span ) ;
let mut start = block_span . start ;
let mut end = block_span . end ;
if block_bytes . starts_with ( b " { " ) {
start + = 1 ;
2021-09-26 18:39:19 +00:00
} else {
2023-05-24 19:53:57 +00:00
working_set . error ( ParseError ::Expected ( " block " , block_span ) ) ;
2023-05-06 20:55:10 +00:00
return ( garbage_pipeline ( spans ) , None ) ;
2023-05-06 18:39:54 +00:00
}
2023-04-07 00:35:45 +00:00
2023-05-06 18:39:54 +00:00
if block_bytes . ends_with ( b " } " ) {
end - = 1 ;
} else {
working_set . error ( ParseError ::Unclosed ( " } " . into ( ) , Span ::new ( end , end ) ) ) ;
2021-09-26 18:39:19 +00:00
}
2023-05-06 18:39:54 +00:00
let block_span = Span ::new ( start , end ) ;
let ( block , module , inner_comments ) =
parse_module_block ( working_set , block_span , module_name . as_bytes ( ) ) ;
let block_id = working_set . add_block ( block ) ;
module_comments . extend ( inner_comments ) ;
2023-05-06 20:55:10 +00:00
let module_id = working_set . add_module ( & module_name , module , module_comments ) ;
2023-05-06 18:39:54 +00:00
let block_expr = Expression {
expr : Expr ::Block ( block_id ) ,
span : block_span ,
ty : Type ::Block ,
custom_completion : None ,
} ;
let module_decl_id = working_set
2023-07-13 19:05:03 +00:00
. find_decl ( b " module " )
2023-05-06 18:39:54 +00:00
. expect ( " internal error: missing module command " ) ;
let call = Box ::new ( Call {
head : span ( & spans [ .. split_id ] ) ,
decl_id : module_decl_id ,
arguments : vec ! [
Argument ::Positional ( module_name_or_path_expr ) ,
Argument ::Positional ( block_expr ) ,
] ,
redirect_stdout : true ,
redirect_stderr : false ,
parser_info : HashMap ::new ( ) ,
} ) ;
2023-05-06 20:55:10 +00:00
(
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ,
Some ( module_id ) ,
)
2021-09-26 18:39:19 +00:00
}
2023-04-07 18:09:38 +00:00
pub fn parse_use ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> ( Pipeline , Vec < Exportable > ) {
2022-08-22 21:19:47 +00:00
let ( name_span , split_id ) =
if spans . len ( ) > 1 & & working_set . get_span_contents ( spans [ 0 ] ) = = b " export " {
( spans [ 1 ] , 2 )
} else {
( spans [ 0 ] , 1 )
} ;
let use_call = working_set . get_span_contents ( name_span ) . to_vec ( ) ;
if use_call ! = b " use " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Wrong call name for 'use' command " . into ( ) ,
span ( spans ) ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
2022-08-22 21:19:47 +00:00
}
if working_set . get_span_contents ( name_span ) ! = b " use " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Wrong call name for 'use' command " . into ( ) ,
span ( spans ) ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
2022-01-10 01:39:25 +00:00
}
2021-09-26 18:39:19 +00:00
2023-07-13 19:05:03 +00:00
let ( call , call_span , args_spans ) = match working_set . find_decl ( b " use " ) {
2022-01-10 01:39:25 +00:00
Some ( decl_id ) = > {
2022-08-22 21:19:47 +00:00
let ( command_spans , rest_spans ) = spans . split_at ( split_id ) ;
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , span ( command_spans ) , rest_spans , decl_id ) ;
2022-01-10 01:39:25 +00:00
let decl = working_set . get_decl ( decl_id ) ;
let call_span = span ( spans ) ;
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & decl . signature ( ) , & call ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2022-01-10 01:39:25 +00:00
return (
2022-02-15 19:31:14 +00:00
Pipeline ::from_vec ( vec! [ Expression {
2022-01-10 01:39:25 +00:00
expr : Expr ::Call ( call ) ,
span : call_span ,
2022-06-12 19:18:00 +00:00
ty : output ,
2022-01-10 01:39:25 +00:00
custom_completion : None ,
2022-02-15 19:31:14 +00:00
} ] ) ,
2022-07-29 08:57:10 +00:00
vec! [ ] ,
2022-01-10 01:39:25 +00:00
) ;
}
2022-12-22 14:36:13 +00:00
( call , call_span , rest_spans )
2022-01-10 01:39:25 +00:00
}
None = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: 'use' declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
2021-10-26 21:30:39 +00:00
}
2022-01-10 01:39:25 +00:00
} ;
2021-09-26 18:39:19 +00:00
2023-04-07 18:09:38 +00:00
let import_pattern_expr = parse_import_pattern ( working_set , args_spans ) ;
2022-12-22 14:36:13 +00:00
let import_pattern = if let Expression {
expr : Expr ::ImportPattern ( import_pattern ) ,
..
} = & import_pattern_expr
{
import_pattern . clone ( )
2022-01-10 01:39:25 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Import pattern positional is not import pattern " . into ( ) ,
import_pattern_expr . span ,
) ) ;
return ( garbage_pipeline ( spans ) , vec! [ ] ) ;
2022-01-10 01:39:25 +00:00
} ;
2021-09-26 18:39:19 +00:00
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
let ( mut import_pattern , module , module_id ) = if let Some ( module_id ) = import_pattern . head . id {
2023-05-06 18:39:54 +00:00
let module = working_set . get_module ( module_id ) . clone ( ) ;
(
ImportPattern {
head : ImportPatternHead {
name : module . name . clone ( ) ,
id : Some ( module_id ) ,
span : import_pattern . head . span ,
} ,
members : import_pattern . members ,
hidden : HashSet ::new ( ) ,
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
constants : vec ! [ ] ,
2023-05-06 18:39:54 +00:00
} ,
module ,
module_id ,
)
} else if let Some ( module_id ) = parse_module_file_or_dir (
working_set ,
& import_pattern . head . name ,
import_pattern . head . span ,
None ,
) {
let module = working_set . get_module ( module_id ) . clone ( ) ;
(
ImportPattern {
head : ImportPatternHead {
name : module . name . clone ( ) ,
id : Some ( module_id ) ,
span : import_pattern . head . span ,
} ,
members : import_pattern . members ,
hidden : HashSet ::new ( ) ,
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
constants : vec ! [ ] ,
2023-05-06 18:39:54 +00:00
} ,
module ,
module_id ,
)
2022-12-21 22:21:03 +00:00
} else {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound (
import_pattern . head . span ,
String ::from_utf8_lossy ( & import_pattern . head . name ) . to_string ( ) ,
) ) ;
2023-05-06 18:39:54 +00:00
return (
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ,
vec! [ ] ,
) ;
2022-12-21 22:21:03 +00:00
} ;
2021-09-26 18:39:19 +00:00
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
let ( definitions , errors ) = module . resolve_import_pattern (
working_set ,
module_id ,
& import_pattern . members ,
None ,
name_span ,
) ;
2023-05-06 18:39:54 +00:00
working_set . parse_errors . extend ( errors ) ;
2021-09-26 18:39:19 +00:00
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
let mut constants = vec! [ ] ;
for ( name , const_val ) in definitions . constants {
let const_var_id =
working_set . add_variable ( name . clone ( ) , name_span , const_val . get_type ( ) , false ) ;
working_set . set_variable_const_val ( const_var_id , const_val ) ;
constants . push ( ( name , const_var_id ) ) ;
}
2023-05-06 18:39:54 +00:00
let exportables = definitions
. decls
2022-07-29 08:57:10 +00:00
. iter ( )
. map ( | ( name , decl_id ) | Exportable ::Decl {
name : name . clone ( ) ,
id : * decl_id ,
} )
2023-05-06 18:39:54 +00:00
. chain (
definitions
. modules
. iter ( )
. map ( | ( name , module_id ) | Exportable ::Module {
name : name . clone ( ) ,
id : * module_id ,
} ) ,
)
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
. chain (
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
constants
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
. iter ( )
. map ( | ( name , variable_id ) | Exportable ::VarDecl {
name : name . clone ( ) ,
id : * variable_id ,
} ) ,
)
2022-07-29 08:57:10 +00:00
. collect ( ) ;
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
import_pattern . constants = constants . iter ( ) . map ( | ( _ , id ) | * id ) . collect ( ) ;
2022-05-07 19:39:22 +00:00
// Extend the current scope with the module's exportables
2023-05-06 18:39:54 +00:00
working_set . use_decls ( definitions . decls ) ;
working_set . use_modules ( definitions . modules ) ;
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
working_set . use_variables ( constants ) ;
2021-09-26 18:39:19 +00:00
2023-05-06 18:39:54 +00:00
// Create a new Use command call to pass the import pattern as parser info
2022-01-10 01:39:25 +00:00
let import_pattern_expr = Expression {
expr : Expr ::ImportPattern ( import_pattern ) ,
2022-12-22 14:36:13 +00:00
span : span ( args_spans ) ,
ty : Type ::Any ,
2022-01-10 01:39:25 +00:00
custom_completion : None ,
} ;
2021-09-26 18:39:19 +00:00
2022-12-22 14:36:13 +00:00
let mut call = call ;
2023-04-05 16:56:48 +00:00
call . set_parser_info ( " import_pattern " . to_string ( ) , import_pattern_expr ) ;
2021-11-15 23:16:06 +00:00
2022-01-10 01:39:25 +00:00
(
2022-02-15 19:31:14 +00:00
Pipeline ::from_vec ( vec! [ Expression {
2022-01-10 01:39:25 +00:00
expr : Expr ::Call ( call ) ,
span : span ( spans ) ,
2022-04-07 04:34:09 +00:00
ty : Type ::Any ,
2022-01-10 01:39:25 +00:00
custom_completion : None ,
2022-02-15 19:31:14 +00:00
} ] ) ,
2022-07-29 08:57:10 +00:00
exportables ,
2022-01-10 01:39:25 +00:00
)
}
2021-09-26 18:39:19 +00:00
2023-04-07 18:09:38 +00:00
pub fn parse_hide ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Pipeline {
2022-01-10 01:39:25 +00:00
if working_set . get_span_contents ( spans [ 0 ] ) ! = b " hide " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Wrong call name for 'hide' command " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage_pipeline ( spans ) ;
2021-09-26 18:39:19 +00:00
}
2023-07-13 19:05:03 +00:00
let ( call , args_spans ) = match working_set . find_decl ( b " hide " ) {
2022-01-10 01:39:25 +00:00
Some ( decl_id ) = > {
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & spans [ 1 .. ] , decl_id ) ;
2022-01-10 01:39:25 +00:00
let decl = working_set . get_decl ( decl_id ) ;
let call_span = span ( spans ) ;
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & decl . signature ( ) , & call ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return garbage_pipeline ( spans ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2023-04-07 00:35:45 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : output ,
custom_completion : None ,
} ] ) ;
2022-01-10 01:39:25 +00:00
}
2022-12-22 14:36:13 +00:00
( call , & spans [ 1 .. ] )
2022-01-10 01:39:25 +00:00
}
None = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: 'hide' declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage_pipeline ( spans ) ;
2022-01-10 01:39:25 +00:00
}
} ;
2023-04-07 18:09:38 +00:00
let import_pattern_expr = parse_import_pattern ( working_set , args_spans ) ;
2022-12-22 14:36:13 +00:00
let import_pattern = if let Expression {
expr : Expr ::ImportPattern ( import_pattern ) ,
..
} = & import_pattern_expr
{
import_pattern . clone ( )
2022-01-10 01:39:25 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Import pattern positional is not import pattern " . into ( ) ,
import_pattern_expr . span ,
) ) ;
return garbage_pipeline ( spans ) ;
2022-01-10 01:39:25 +00:00
} ;
2021-09-28 20:18:48 +00:00
let bytes = working_set . get_span_contents ( spans [ 0 ] ) ;
if bytes = = b " hide " & & spans . len ( ) > = 2 {
2021-11-15 23:16:06 +00:00
for span in spans [ 1 .. ] . iter ( ) {
2023-04-07 18:09:38 +00:00
parse_string ( working_set , * span ) ;
2021-11-15 23:16:06 +00:00
}
2021-09-28 20:18:48 +00:00
2023-01-22 19:34:15 +00:00
// module used only internally, not saved anywhere
2023-04-07 18:09:38 +00:00
let ( is_module , module ) =
if let Some ( module_id ) = working_set . find_module ( & import_pattern . head . name ) {
( true , working_set . get_module ( module_id ) . clone ( ) )
} else if import_pattern . members . is_empty ( ) {
// The pattern head can be:
2023-07-13 19:05:03 +00:00
if let Some ( id ) = working_set . find_decl ( & import_pattern . head . name ) {
2023-04-07 18:09:38 +00:00
// a custom command,
let mut module = Module ::new ( b " tmp " . to_vec ( ) ) ;
module . add_decl ( import_pattern . head . name . clone ( ) , id ) ;
( false , module )
} else {
// , or it could be an env var (handled by the engine)
( false , Module ::new ( b " tmp " . to_vec ( ) ) )
}
2021-10-04 17:08:24 +00:00
} else {
Evaluate string interpolation at parse time (#11562)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
Closes #11561
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR will allow string interpolation at parse time.
Since the actual config hasn't been loaded at parse time, this uses the
`get_config()` method on `StateWorkingSet`. So file sizes and datetimes
(I think those are the only things whose string representations depend
on the config) may be formatted differently from how users have
configured things, which may come as a surprise to some. It does seem
unlikely that anyone would be formatting file sizes or date times at
parse time. Still, something to think about if/before this PR merged.
Also, I changed the `ModuleNotFound` error to include the name of the
module.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Users will be able to do stuff like:
```nu
const x = [1 2 3]
const y = $"foo($x)" // foo[1, 2, 3]
```
The main use case is `use`-ing and `source`-ing files at parse time:
```nu
const file = "foo.nu"
use $"($file)"
```
If the module isn't found, you'll see an error like this:
```
Error: nu::parser::module_not_found
× Module not found.
╭─[entry #3:1:1]
1 │ use $"($file)"
· ─────┬────
· ╰── module foo.nu not found
╰────
help: module files and their paths must be available before your script is run as parsing occurs before anything is evaluated
```
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Although there's user-facing changes, there's probably no need to change
the docs since people probably already expect string interpolation to
work at parse time.
Edit: @kubouch pointed out that we'd need to document the fact that
stuff like file sizes and datetimes won't get formatted according to
user's runtime configs, so I'll make a PR to nushell.github.io after
this one
2024-01-22 07:13:48 +00:00
working_set . error ( ParseError ::ModuleNotFound (
spans [ 1 ] ,
String ::from_utf8_lossy ( & import_pattern . head . name ) . to_string ( ) ,
) ) ;
2023-04-07 18:09:38 +00:00
return garbage_pipeline ( spans ) ;
} ;
2021-09-28 20:18:48 +00:00
2021-10-04 17:08:24 +00:00
// This kind of inverts the import pattern matching found in parse_use()
2023-04-07 18:09:38 +00:00
let decls_to_hide = if import_pattern . members . is_empty ( ) {
2021-10-31 15:38:00 +00:00
if is_module {
2023-04-07 18:09:38 +00:00
module . decl_names_with_head ( & import_pattern . head . name )
2021-10-31 15:38:00 +00:00
} else {
2023-04-07 18:09:38 +00:00
module . decl_names ( )
2021-10-31 15:38:00 +00:00
}
2021-10-04 17:08:24 +00:00
} else {
match & import_pattern . members [ 0 ] {
2023-04-07 18:09:38 +00:00
ImportPatternMember ::Glob { .. } = > module . decl_names ( ) ,
2021-10-04 17:08:24 +00:00
ImportPatternMember ::Name { name , span } = > {
2022-02-12 09:50:37 +00:00
let mut decls = vec! [ ] ;
2021-11-15 23:16:06 +00:00
2023-01-22 19:34:15 +00:00
if name = = b " main " {
if module . main . is_some ( ) {
decls . push ( import_pattern . head . name . clone ( ) ) ;
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::ExportNotFound ( * span ) ) ;
2023-01-22 19:34:15 +00:00
}
2022-02-12 09:50:37 +00:00
} else if let Some ( item ) =
2022-05-07 19:39:22 +00:00
module . decl_name_with_head ( name , & import_pattern . head . name )
2022-02-12 09:50:37 +00:00
{
decls . push ( item ) ;
2022-09-25 16:52:43 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::ExportNotFound ( * span ) ) ;
2021-10-04 17:08:24 +00:00
}
2023-04-07 18:09:38 +00:00
decls
2021-10-04 17:08:24 +00:00
}
ImportPatternMember ::List { names } = > {
2022-02-12 09:50:37 +00:00
let mut decls = vec! [ ] ;
2021-09-28 20:18:48 +00:00
2021-10-04 17:08:24 +00:00
for ( name , span ) in names {
2023-01-22 19:34:15 +00:00
if name = = b " main " {
if module . main . is_some ( ) {
decls . push ( import_pattern . head . name . clone ( ) ) ;
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::ExportNotFound ( * span ) ) ;
2023-01-22 19:34:15 +00:00
break ;
}
2022-02-12 09:50:37 +00:00
} else if let Some ( item ) =
2022-05-07 19:39:22 +00:00
module . decl_name_with_head ( name , & import_pattern . head . name )
2022-02-12 09:50:37 +00:00
{
decls . push ( item ) ;
2022-09-25 16:52:43 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::ExportNotFound ( * span ) ) ;
2021-11-15 23:16:06 +00:00
break ;
2021-10-04 17:08:24 +00:00
}
}
2023-04-07 18:09:38 +00:00
decls
2021-10-04 17:08:24 +00:00
}
}
} ;
2022-02-12 09:50:37 +00:00
let import_pattern = {
let decls : HashSet < Vec < u8 > > = decls_to_hide . iter ( ) . cloned ( ) . collect ( ) ;
2023-04-07 18:09:38 +00:00
import_pattern . with_hidden ( decls )
2022-02-12 09:50:37 +00:00
} ;
2021-11-15 23:16:06 +00:00
// TODO: `use spam; use spam foo; hide foo` will hide both `foo` and `spam foo` since
// they point to the same DeclId. Do we want to keep it that way?
working_set . hide_decls ( & decls_to_hide ) ;
2021-09-28 20:18:48 +00:00
2022-01-10 01:39:25 +00:00
// Create a new Use command call to pass the new import pattern
2021-11-15 23:16:06 +00:00
let import_pattern_expr = Expression {
expr : Expr ::ImportPattern ( import_pattern ) ,
2022-12-22 14:36:13 +00:00
span : span ( args_spans ) ,
ty : Type ::Any ,
2021-11-15 23:16:06 +00:00
custom_completion : None ,
} ;
2022-12-22 14:36:13 +00:00
let mut call = call ;
2023-04-05 16:56:48 +00:00
call . set_parser_info ( " import_pattern " . to_string ( ) , import_pattern_expr ) ;
2021-09-28 20:18:48 +00:00
2023-04-07 00:35:45 +00:00
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] )
2021-09-28 20:18:48 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" Expected structure: hide <name> " . into ( ) ,
span ( spans ) ,
) ) ;
garbage_pipeline ( spans )
2021-09-28 20:18:48 +00:00
}
}
2023-04-07 00:35:45 +00:00
pub fn parse_overlay_new ( working_set : & mut StateWorkingSet , call : Box < Call > ) -> Pipeline {
2023-03-10 21:20:31 +00:00
let call_span = call . span ( ) ;
2022-05-26 14:47:04 +00:00
let ( overlay_name , _ ) = if let Some ( expr ) = call . positional_nth ( 0 ) {
2023-01-11 22:18:06 +00:00
match eval_constant ( working_set , expr ) {
2023-12-04 19:13:47 +00:00
Ok ( val ) = > match val . as_string ( ) {
2023-01-11 22:18:06 +00:00
Ok ( s ) = > ( s , expr . span ) ,
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , call_span ) ) ;
2023-04-07 00:35:45 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
2023-01-11 22:18:06 +00:00
}
} ,
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , call_span ) ) ;
2023-04-07 00:35:45 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
2023-01-11 22:18:06 +00:00
}
2022-05-26 14:47:04 +00:00
}
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Missing required positional after call parsing " . into ( ) ,
call_span ,
) ) ;
return garbage_pipeline ( & [ call_span ] ) ;
2022-05-26 14:47:04 +00:00
} ;
let pipeline = Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
2023-03-10 21:20:31 +00:00
span : call_span ,
2022-05-26 14:47:04 +00:00
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2023-01-22 19:34:15 +00:00
let module_id = working_set . add_module (
& overlay_name ,
Module ::new ( overlay_name . as_bytes ( ) . to_vec ( ) ) ,
vec! [ ] ,
) ;
2022-05-26 14:47:04 +00:00
2023-05-06 18:39:54 +00:00
working_set . add_overlay (
overlay_name . as_bytes ( ) . to_vec ( ) ,
module_id ,
vec! [ ] ,
vec! [ ] ,
false ,
) ;
2022-05-26 14:47:04 +00:00
2023-04-07 00:35:45 +00:00
pipeline
2022-05-26 14:47:04 +00:00
}
2023-04-07 18:09:38 +00:00
pub fn parse_overlay_use ( working_set : & mut StateWorkingSet , call : Box < Call > ) -> Pipeline {
2023-03-10 21:20:31 +00:00
let call_span = call . span ( ) ;
2022-05-07 19:39:22 +00:00
let ( overlay_name , overlay_name_span ) = if let Some ( expr ) = call . positional_nth ( 0 ) {
2022-12-21 22:21:03 +00:00
match eval_constant ( working_set , expr ) {
2023-12-04 19:13:47 +00:00
Ok ( val ) = > match val . as_string ( ) {
2022-12-21 22:21:03 +00:00
Ok ( s ) = > ( s , expr . span ) ,
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , call_span ) ) ;
2023-04-07 00:35:45 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
2022-12-21 22:21:03 +00:00
}
} ,
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , call_span ) ) ;
2023-04-07 00:35:45 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
2022-12-21 22:21:03 +00:00
}
2022-05-07 19:39:22 +00:00
}
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Missing required positional after call parsing " . into ( ) ,
call_span ,
) ) ;
return garbage_pipeline ( & [ call_span ] ) ;
2022-05-07 19:39:22 +00:00
} ;
2022-08-13 14:28:18 +00:00
let new_name = if let Some ( kw_expression ) = call . positional_nth ( 1 ) {
if let Some ( new_name_expression ) = kw_expression . as_keyword ( ) {
2022-12-21 22:21:03 +00:00
match eval_constant ( working_set , new_name_expression ) {
2023-12-04 19:13:47 +00:00
Ok ( val ) = > match val . as_string ( ) {
2022-12-21 22:21:03 +00:00
Ok ( s ) = > Some ( Spanned {
item : s ,
span : new_name_expression . span ,
} ) ,
2023-04-07 00:35:45 +00:00
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , call_span ) ) ;
2023-04-07 00:35:45 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
}
2022-12-21 22:21:03 +00:00
} ,
2023-04-07 00:35:45 +00:00
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , call_span ) ) ;
2023-04-07 00:35:45 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
}
2022-08-13 14:28:18 +00:00
}
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::ExpectedKeyword (
" as keyword " . to_string ( ) ,
kw_expression . span ,
) ) ;
return garbage_pipeline ( & [ call_span ] ) ;
2022-08-13 14:28:18 +00:00
}
} else {
None
} ;
2024-02-03 11:20:40 +00:00
let Ok ( has_prefix ) = has_flag_const ( working_set , & call , " prefix " ) else {
2024-01-11 15:19:48 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
} ;
2024-02-03 11:20:40 +00:00
let Ok ( do_reload ) = has_flag_const ( working_set , & call , " reload " ) else {
2024-01-11 15:19:48 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
} ;
2022-08-12 18:06:51 +00:00
2022-05-07 19:39:22 +00:00
let pipeline = Pipeline ::from_vec ( vec! [ Expression {
2022-09-04 15:36:42 +00:00
expr : Expr ::Call ( call . clone ( ) ) ,
2023-03-10 21:20:31 +00:00
span : call_span ,
2022-05-07 19:39:22 +00:00
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2023-04-07 00:35:45 +00:00
let ( final_overlay_name , origin_module , origin_module_id , is_module_updated ) =
if let Some ( overlay_frame ) = working_set . find_overlay ( overlay_name . as_bytes ( ) ) {
// Activate existing overlay
2022-09-04 15:36:42 +00:00
2023-04-07 00:35:45 +00:00
// First, check for errors
if has_prefix & & ! overlay_frame . prefixed {
working_set . error ( ParseError ::OverlayPrefixMismatch (
2022-08-12 18:06:51 +00:00
overlay_name ,
" without " . to_string ( ) ,
overlay_name_span ,
2023-04-07 00:35:45 +00:00
) ) ;
return pipeline ;
}
2022-08-12 18:06:51 +00:00
2023-04-07 00:35:45 +00:00
if ! has_prefix & & overlay_frame . prefixed {
working_set . error ( ParseError ::OverlayPrefixMismatch (
2022-08-12 18:06:51 +00:00
overlay_name ,
" with " . to_string ( ) ,
overlay_name_span ,
2023-04-07 00:35:45 +00:00
) ) ;
return pipeline ;
}
2022-08-12 18:06:51 +00:00
2023-04-07 00:35:45 +00:00
if let Some ( new_name ) = new_name {
if new_name . item ! = overlay_name {
working_set . error ( ParseError ::CantAddOverlayHelp (
format! (
" Cannot add overlay as '{}' because it already exists under the name '{}' " ,
new_name . item , overlay_name
) ,
new_name . span ,
) ) ;
return pipeline ;
}
2022-08-13 14:28:18 +00:00
}
2023-04-07 00:35:45 +00:00
let module_id = overlay_frame . origin ;
2022-08-12 18:06:51 +00:00
2023-04-07 00:35:45 +00:00
if let Some ( new_module_id ) = working_set . find_module ( overlay_name . as_bytes ( ) ) {
if ! do_reload & & ( module_id = = new_module_id ) {
(
overlay_name ,
Module ::new ( working_set . get_module ( module_id ) . name . clone ( ) ) ,
module_id ,
false ,
)
} else {
// The origin module of an overlay changed => update it
(
overlay_name ,
working_set . get_module ( new_module_id ) . clone ( ) ,
new_module_id ,
true ,
)
}
2022-05-07 19:39:22 +00:00
} else {
2023-04-07 00:35:45 +00:00
let module_name = overlay_name . as_bytes ( ) . to_vec ( ) ;
( overlay_name , Module ::new ( module_name ) , module_id , true )
2022-05-07 19:39:22 +00:00
}
} else {
2023-05-06 18:39:54 +00:00
// Create a new overlay
2023-04-07 00:35:45 +00:00
if let Some ( module_id ) =
// the name is a module
working_set . find_module ( overlay_name . as_bytes ( ) )
2022-05-07 19:39:22 +00:00
{
2023-04-07 00:35:45 +00:00
(
new_name . map ( | spanned | spanned . item ) . unwrap_or ( overlay_name ) ,
working_set . get_module ( module_id ) . clone ( ) ,
module_id ,
true ,
)
2023-05-06 18:39:54 +00:00
} else if let Some ( module_id ) = parse_module_file_or_dir (
working_set ,
overlay_name . as_bytes ( ) ,
overlay_name_span ,
new_name . as_ref ( ) . map ( | spanned | spanned . item . clone ( ) ) ,
) {
// try file or directory
let new_module = working_set . get_module ( module_id ) . clone ( ) ;
(
new_name
. map ( | spanned | spanned . item )
2023-06-15 05:30:54 +00:00
. unwrap_or_else ( | | String ::from_utf8_lossy ( & new_module . name ) . to_string ( ) ) ,
2023-05-06 18:39:54 +00:00
new_module ,
module_id ,
true ,
)
2023-04-07 00:35:45 +00:00
} else {
2023-05-06 18:39:54 +00:00
working_set . error ( ParseError ::ModuleOrOverlayNotFound ( overlay_name_span ) ) ;
return pipeline ;
2022-05-07 19:39:22 +00:00
}
2023-04-07 00:35:45 +00:00
} ;
2022-05-07 19:39:22 +00:00
2023-05-06 18:39:54 +00:00
let ( definitions , errors ) = if is_module_updated {
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-11-24 22:45:24 +00:00
if has_prefix {
2023-05-06 18:39:54 +00:00
origin_module . resolve_import_pattern (
working_set ,
origin_module_id ,
& [ ] ,
Some ( final_overlay_name . as_bytes ( ) ) ,
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
call . head ,
2023-05-06 18:39:54 +00:00
)
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-11-24 22:45:24 +00:00
} else {
2023-05-06 18:39:54 +00:00
origin_module . resolve_import_pattern (
working_set ,
origin_module_id ,
& [ ImportPatternMember ::Glob {
span : overlay_name_span ,
} ] ,
Some ( final_overlay_name . as_bytes ( ) ) ,
Recursively export constants from modules (#10049)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-08-20 12:51:35 +00:00
call . head ,
2023-05-06 18:39:54 +00:00
)
Reorder export-env eval and allow reloading an overlay (#7231)
# Description
This PR is a response to the issues raised in
https://github.com/nushell/nushell/pull/7087. It consists of two
changes:
* `export-env`, when evaluated in `overlay use`, will see the original
environment. Previously, it would see the environment from previous
overlay activation.
* Added a new `--reload` flag that reloads the overlay. Custom
definitions will be kept but the original definitions and environment
will be reloaded.
This enables a pattern when an overlay is supposed to shadow an existing
environment variable, such as `PROMPT_COMMAND`, but `overlay use` would
keep loading the value from the first activation. You can easily test it
by defining a module
```
module prompt {
export-env {
let-env PROMPT_COMMAND = (date now | into string)
}
}
```
Calling `overlay use prompt` for the first time changes the prompt to
the current time, however, subsequent calls of `overlay use` won't
change the time. That's because overlays, once activated, store their
state so they can be hidden and restored at later time. To force-reload
the environment, use the new flag: Calling `overlay use --reload prompt`
repeatedly now updates the prompt with the current time each time.
# User-Facing Changes
* When calling `overlay use`, if the module has an `export-env` block,
the block will see the environment as it is _before_ the overlay is
activated. Previously, it was _after_.
* A new `overlay use --reload` flag.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2022-11-24 22:45:24 +00:00
}
2022-09-04 15:36:42 +00:00
} else {
Module: support defining const and use const variables inside of function (#9773)
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx
you can also mention related issues, PRs or discussions!
-->
# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.
Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
Relative: #8248
After this pr, user can define const variable inside a module.
![image](https://github.com/nushell/nushell/assets/22256154/e3e03e56-c4b5-4144-a944-d1b20bec1cbd)
And user can export const variables, the following screenshot shows how
it works (it follows
https://github.com/nushell/nushell/issues/8248#issuecomment-1637442612):
![image](https://github.com/nushell/nushell/assets/22256154/b2c14760-3f27-41cc-af77-af70a4367f2a)
## About the change
1. To make module support const, we need to change `parse_module_block`
to support `const` keyword.
2. To suport export `const`, we need to make module tracking variables,
so we add `variables` attribute to `Module`
3. During eval, the const variable may not exists in `stack`, because we
don't eval `const` when we define a module, so we need to find variables
which are already registered in `engine_state`
## One more thing to note about the const value.
Consider the following code
```
module foo { const b = 3; export def bar [] { $b } }
use foo bar
const b = 4;
bar
```
The result will be 3 (which is defined in module) rather than 4. I think
it's expected behavior.
It's something like [dynamic
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding-Tips.html)
vs [lexical
binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html)
in lisp like language, and lexical binding should be right behavior
which generates more predicable result, and it doesn't introduce really
subtle bugs in nushell code.
What if user want dynamic-binding?(For example: the example code returns
`4`)
There is no way to do this, user should consider passing the value as
argument to custom command rather than const.
## TODO
- [X] adding tests for the feature.
- [X] support export const out of module to use.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-07-31 23:09:52 +00:00
( ResolvedImportPattern ::new ( vec! [ ] , vec! [ ] , vec! [ ] ) , vec! [ ] )
2022-09-04 15:36:42 +00:00
} ;
2023-05-06 18:39:54 +00:00
if errors . is_empty ( ) {
working_set . add_overlay (
final_overlay_name . as_bytes ( ) . to_vec ( ) ,
origin_module_id ,
definitions . decls ,
definitions . modules ,
has_prefix ,
) ;
} else {
working_set . parse_errors . extend ( errors ) ;
}
2022-09-04 15:36:42 +00:00
// Change the call argument to include the Overlay expression with the module ID
let mut call = call ;
2023-04-05 16:56:48 +00:00
call . set_parser_info (
" overlay_expr " . to_string ( ) ,
Expression {
expr : Expr ::Overlay ( if is_module_updated {
Some ( origin_module_id )
} else {
None
} ) ,
span : overlay_name_span ,
ty : Type ::Any ,
custom_completion : None ,
} ,
) ;
2022-05-07 19:39:22 +00:00
2023-04-07 00:35:45 +00:00
Pipeline ::from_vec ( vec! [ Expression {
2022-09-04 15:36:42 +00:00
expr : Expr ::Call ( call ) ,
2023-03-10 21:20:31 +00:00
span : call_span ,
2022-09-04 15:36:42 +00:00
ty : Type ::Any ,
custom_completion : None ,
2023-04-07 00:35:45 +00:00
} ] )
2022-05-07 19:39:22 +00:00
}
2023-04-07 00:35:45 +00:00
pub fn parse_overlay_hide ( working_set : & mut StateWorkingSet , call : Box < Call > ) -> Pipeline {
2023-03-10 21:20:31 +00:00
let call_span = call . span ( ) ;
2022-05-07 19:39:22 +00:00
let ( overlay_name , overlay_name_span ) = if let Some ( expr ) = call . positional_nth ( 0 ) {
2023-01-11 22:18:06 +00:00
match eval_constant ( working_set , expr ) {
2023-12-04 19:13:47 +00:00
Ok ( val ) = > match val . as_string ( ) {
2023-01-11 22:18:06 +00:00
Ok ( s ) = > ( s , expr . span ) ,
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , call_span ) ) ;
2023-04-07 00:35:45 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
2023-01-11 22:18:06 +00:00
}
} ,
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , call_span ) ) ;
2023-04-07 00:35:45 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
2023-01-11 22:18:06 +00:00
}
2022-05-07 19:39:22 +00:00
}
} else {
(
String ::from_utf8_lossy ( working_set . last_overlay_name ( ) ) . to_string ( ) ,
2023-03-10 21:20:31 +00:00
call_span ,
2022-05-07 19:39:22 +00:00
)
} ;
2024-02-03 11:20:40 +00:00
let Ok ( keep_custom ) = has_flag_const ( working_set , & call , " keep-custom " ) else {
2024-01-11 15:19:48 +00:00
return garbage_pipeline ( & [ call_span ] ) ;
} ;
2022-05-24 21:22:17 +00:00
2022-05-07 19:39:22 +00:00
let pipeline = Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
2023-03-10 21:20:31 +00:00
span : call_span ,
2022-05-07 19:39:22 +00:00
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
if overlay_name = = DEFAULT_OVERLAY_NAME {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::CantHideDefaultOverlay (
overlay_name ,
overlay_name_span ,
) ) ;
return pipeline ;
2022-05-07 19:39:22 +00:00
}
if ! working_set
. unique_overlay_names ( )
2023-09-12 03:38:20 +00:00
. contains ( & overlay_name . as_bytes ( ) )
2022-05-07 19:39:22 +00:00
{
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::ActiveOverlayNotFound ( overlay_name_span ) ) ;
return pipeline ;
2022-05-07 19:39:22 +00:00
}
if working_set . num_overlays ( ) < 2 {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::CantRemoveLastOverlay ( overlay_name_span ) ) ;
return pipeline ;
2022-05-07 19:39:22 +00:00
}
2022-05-24 21:22:17 +00:00
working_set . remove_overlay ( overlay_name . as_bytes ( ) , keep_custom ) ;
2022-05-07 19:39:22 +00:00
2023-04-07 00:35:45 +00:00
pipeline
2022-05-07 19:39:22 +00:00
}
2023-07-03 05:45:10 +00:00
pub fn parse_let ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Pipeline {
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
trace! ( " parsing: let " ) ;
2021-09-26 18:39:19 +00:00
2023-07-03 05:45:10 +00:00
// JT: Disabling check_name because it doesn't work with optional types in the declaration
// if let Some(span) = check_name(working_set, spans) {
// return Pipeline::from_vec(vec![garbage(*span)]);
// }
2022-12-21 22:21:03 +00:00
2023-07-13 19:05:03 +00:00
if let Some ( decl_id ) = working_set . find_decl ( b " let " ) {
2023-07-03 05:45:10 +00:00
if spans . len ( ) > = 4 {
// This is a bit of by-hand parsing to get around the issue where we want to parse in the reverse order
// so that the var-id created by the variable isn't visible in the expression that init it
for span in spans . iter ( ) . enumerate ( ) {
let item = working_set . get_span_contents ( * span . 1 ) ;
2023-08-01 16:21:40 +00:00
// https://github.com/nushell/nushell/issues/9596, let = if $
// let x = 'f', = at least start from index 2
if item = = b " = " & & spans . len ( ) > ( span . 0 + 1 ) & & span . 0 > 1 {
2023-07-03 05:45:10 +00:00
let ( tokens , parse_error ) = lex (
working_set . get_span_contents ( nu_protocol ::span ( & spans [ ( span . 0 + 1 ) .. ] ) ) ,
2023-07-04 09:12:46 +00:00
spans [ span . 0 + 1 ] . start ,
2023-07-03 05:45:10 +00:00
& [ ] ,
& [ ] ,
true ,
) ;
2021-09-26 18:39:19 +00:00
2023-07-03 05:45:10 +00:00
if let Some ( parse_error ) = parse_error {
working_set . parse_errors . push ( parse_error )
}
2022-01-03 23:14:33 +00:00
2023-07-03 05:45:10 +00:00
let rvalue_span = nu_protocol ::span ( & spans [ ( span . 0 + 1 ) .. ] ) ;
let rvalue_block = parse_block ( working_set , & tokens , rvalue_span , false , true ) ;
2021-09-26 18:39:19 +00:00
2023-07-03 05:45:10 +00:00
let output_type = rvalue_block . output_type ( ) ;
2021-12-27 03:04:22 +00:00
2023-07-03 05:45:10 +00:00
let block_id = working_set . add_block ( rvalue_block ) ;
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
2023-07-03 05:45:10 +00:00
let rvalue = Expression {
expr : Expr ::Block ( block_id ) ,
span : rvalue_span ,
ty : output_type ,
custom_completion : None ,
} ;
let mut idx = 0 ;
let ( lvalue , explicit_type ) =
parse_var_with_opt_type ( working_set , & spans [ 1 .. ( span . 0 ) ] , & mut idx , false ) ;
let var_name =
String ::from_utf8_lossy ( working_set . get_span_contents ( lvalue . span ) )
. trim_start_matches ( '$' )
. to_string ( ) ;
2023-11-29 17:29:07 +00:00
if RESERVED_VARIABLE_NAMES . contains ( & var_name . as_str ( ) ) {
2023-07-03 05:45:10 +00:00
working_set . error ( ParseError ::NameIsBuiltinVar ( var_name , lvalue . span ) )
}
2021-12-15 22:56:12 +00:00
2023-07-03 05:45:10 +00:00
let var_id = lvalue . as_var ( ) ;
let rhs_type = rvalue . ty . clone ( ) ;
2022-06-24 21:55:25 +00:00
2023-07-03 05:45:10 +00:00
if let Some ( explicit_type ) = & explicit_type {
if ! type_compatible ( explicit_type , & rhs_type ) {
working_set . error ( ParseError ::TypeMismatch (
explicit_type . clone ( ) ,
rhs_type . clone ( ) ,
nu_protocol ::span ( & spans [ ( span . 0 + 1 ) .. ] ) ,
) ) ;
2022-06-24 21:55:25 +00:00
}
2023-07-03 05:45:10 +00:00
}
2022-06-24 21:55:25 +00:00
2023-07-03 05:45:10 +00:00
if let Some ( var_id ) = var_id {
if explicit_type . is_none ( ) {
working_set . set_variable_type ( var_id , rhs_type ) ;
}
}
2021-12-15 22:56:12 +00:00
2023-07-03 05:45:10 +00:00
let call = Box ::new ( Call {
decl_id ,
head : spans [ 0 ] ,
arguments : vec ! [ Argument ::Positional ( lvalue ) , Argument ::Positional ( rvalue ) ] ,
redirect_stdout : true ,
redirect_stderr : false ,
parser_info : HashMap ::new ( ) ,
} ) ;
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : nu_protocol ::span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
}
}
}
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & spans [ 1 .. ] , decl_id ) ;
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : nu_protocol ::span ( spans ) ,
ty : output ,
custom_completion : None ,
} ] ) ;
} else {
working_set . error ( ParseError ::UnknownState (
" internal error: let or const statements not found in core language " . into ( ) ,
span ( spans ) ,
) )
}
working_set . error ( ParseError ::UnknownState (
" internal error: let or const statement unparsable " . into ( ) ,
span ( spans ) ,
) ) ;
garbage_pipeline ( spans )
}
pub fn parse_const ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Pipeline {
trace! ( " parsing: const " ) ;
// JT: Disabling check_name because it doesn't work with optional types in the declaration
// if let Some(span) = check_name(working_set, spans) {
// return Pipeline::from_vec(vec![garbage(*span)]);
// }
2023-07-13 19:05:03 +00:00
if let Some ( decl_id ) = working_set . find_decl ( b " const " ) {
2023-07-03 05:45:10 +00:00
let cmd = working_set . get_decl ( decl_id ) ;
let call_signature = cmd . signature ( ) . call_signature ( ) ;
if spans . len ( ) > = 4 {
// This is a bit of by-hand parsing to get around the issue where we want to parse in the reverse order
// so that the var-id created by the variable isn't visible in the expression that init it
for span in spans . iter ( ) . enumerate ( ) {
let item = working_set . get_span_contents ( * span . 1 ) ;
2023-08-01 16:21:40 +00:00
// const x = 'f', = at least start from index 2
if item = = b " = " & & spans . len ( ) > ( span . 0 + 1 ) & & span . 0 > 1 {
2023-07-03 05:45:10 +00:00
let mut idx = span . 0 ;
let rvalue = parse_multispan_value (
working_set ,
spans ,
& mut idx ,
& SyntaxShape ::Keyword ( b " = " . to_vec ( ) , Box ::new ( SyntaxShape ::MathExpression ) ) ,
) ;
if idx < ( spans . len ( ) - 1 ) {
working_set
. error ( ParseError ::ExtraPositional ( call_signature , spans [ idx + 1 ] ) ) ;
}
let mut idx = 0 ;
let ( lvalue , explicit_type ) =
parse_var_with_opt_type ( working_set , & spans [ 1 .. ( span . 0 ) ] , & mut idx , false ) ;
let var_name =
String ::from_utf8_lossy ( working_set . get_span_contents ( lvalue . span ) )
. trim_start_matches ( '$' )
. to_string ( ) ;
2023-11-29 17:29:07 +00:00
if RESERVED_VARIABLE_NAMES . contains ( & var_name . as_str ( ) ) {
2023-07-03 05:45:10 +00:00
working_set . error ( ParseError ::NameIsBuiltinVar ( var_name , lvalue . span ) )
}
let var_id = lvalue . as_var ( ) ;
let rhs_type = rvalue . ty . clone ( ) ;
if let Some ( explicit_type ) = & explicit_type {
if ! type_compatible ( explicit_type , & rhs_type ) {
working_set . error ( ParseError ::TypeMismatch (
explicit_type . clone ( ) ,
rhs_type . clone ( ) ,
nu_protocol ::span ( & spans [ ( span . 0 + 1 ) .. ] ) ,
) ) ;
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
}
2023-07-03 05:45:10 +00:00
}
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
2023-07-03 05:45:10 +00:00
if let Some ( var_id ) = var_id {
if explicit_type . is_none ( ) {
working_set . set_variable_type ( var_id , rhs_type ) ;
}
2022-12-21 22:21:03 +00:00
2023-07-03 05:45:10 +00:00
match eval_constant ( working_set , & rvalue ) {
Ok ( val ) = > {
2023-07-13 21:02:05 +00:00
// In case rhs is parsed as 'any' but is evaluated to a concrete
// type:
let const_type = val . get_type ( ) ;
if let Some ( explicit_type ) = & explicit_type {
if ! type_compatible ( explicit_type , & const_type ) {
working_set . error ( ParseError ::TypeMismatch (
explicit_type . clone ( ) ,
const_type . clone ( ) ,
nu_protocol ::span ( & spans [ ( span . 0 + 1 ) .. ] ) ,
) ) ;
}
}
working_set . set_variable_type ( var_id , const_type ) ;
// Assign the constant value to the variable
working_set . set_variable_const_val ( var_id , val ) ;
2022-12-21 22:21:03 +00:00
}
2023-08-26 13:41:29 +00:00
Err ( err ) = > working_set . error ( err . wrap ( working_set , rvalue . span ) ) ,
2021-12-15 22:56:12 +00:00
}
2023-07-03 05:45:10 +00:00
}
2021-09-26 18:39:19 +00:00
2023-07-03 05:45:10 +00:00
let call = Box ::new ( Call {
decl_id ,
head : spans [ 0 ] ,
arguments : vec ! [ Argument ::Positional ( lvalue ) , Argument ::Positional ( rvalue ) ] ,
redirect_stdout : true ,
redirect_stderr : false ,
parser_info : HashMap ::new ( ) ,
} ) ;
2021-12-15 22:56:12 +00:00
2023-07-03 05:45:10 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : nu_protocol ::span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2021-11-14 19:25:57 +00:00
}
2021-09-26 18:39:19 +00:00
}
}
2023-07-03 05:45:10 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & spans [ 1 .. ] , decl_id ) ;
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : nu_protocol ::span ( spans ) ,
ty : output ,
custom_completion : None ,
} ] ) ;
} else {
working_set . error ( ParseError ::UnknownState (
" internal error: let or const statements not found in core language " . into ( ) ,
span ( spans ) ,
) )
2021-09-26 18:39:19 +00:00
}
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: let or const statement unparsable " . into ( ) ,
span ( spans ) ,
) ) ;
garbage_pipeline ( spans )
2021-09-26 18:39:19 +00:00
}
2021-10-02 02:24:43 +00:00
2023-04-07 18:09:38 +00:00
pub fn parse_mut ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Pipeline {
2023-07-11 18:36:34 +00:00
trace! ( " parsing: mut " ) ;
2022-11-11 06:51:08 +00:00
2023-07-11 18:36:34 +00:00
// JT: Disabling check_name because it doesn't work with optional types in the declaration
// if let Some(span) = check_name(working_set, spans) {
// return Pipeline::from_vec(vec![garbage(*span)]);
// }
2022-11-11 06:51:08 +00:00
2023-07-13 19:05:03 +00:00
if let Some ( decl_id ) = working_set . find_decl ( b " mut " ) {
2023-07-11 18:36:34 +00:00
if spans . len ( ) > = 4 {
// This is a bit of by-hand parsing to get around the issue where we want to parse in the reverse order
// so that the var-id created by the variable isn't visible in the expression that init it
for span in spans . iter ( ) . enumerate ( ) {
let item = working_set . get_span_contents ( * span . 1 ) ;
2023-08-01 16:21:40 +00:00
// mut x = 'f', = at least start from index 2
if item = = b " = " & & spans . len ( ) > ( span . 0 + 1 ) & & span . 0 > 1 {
2023-07-11 18:36:34 +00:00
let ( tokens , parse_error ) = lex (
working_set . get_span_contents ( nu_protocol ::span ( & spans [ ( span . 0 + 1 ) .. ] ) ) ,
spans [ span . 0 + 1 ] . start ,
& [ ] ,
& [ ] ,
true ,
) ;
2022-11-11 06:51:08 +00:00
2023-07-11 18:36:34 +00:00
if let Some ( parse_error ) = parse_error {
working_set . parse_errors . push ( parse_error )
}
let rvalue_span = nu_protocol ::span ( & spans [ ( span . 0 + 1 ) .. ] ) ;
let rvalue_block = parse_block ( working_set , & tokens , rvalue_span , false , true ) ;
2022-11-11 06:51:08 +00:00
2023-07-11 18:36:34 +00:00
let output_type = rvalue_block . output_type ( ) ;
2022-11-11 06:51:08 +00:00
2023-07-11 18:36:34 +00:00
let block_id = working_set . add_block ( rvalue_block ) ;
let rvalue = Expression {
expr : Expr ::Block ( block_id ) ,
span : rvalue_span ,
ty : output_type ,
custom_completion : None ,
} ;
2022-11-11 06:51:08 +00:00
2023-07-11 18:36:34 +00:00
let mut idx = 0 ;
2022-11-11 06:51:08 +00:00
2023-07-11 18:36:34 +00:00
let ( lvalue , explicit_type ) =
parse_var_with_opt_type ( working_set , & spans [ 1 .. ( span . 0 ) ] , & mut idx , true ) ;
let var_name =
String ::from_utf8_lossy ( working_set . get_span_contents ( lvalue . span ) )
. trim_start_matches ( '$' )
. to_string ( ) ;
2023-11-29 17:29:07 +00:00
if RESERVED_VARIABLE_NAMES . contains ( & var_name . as_str ( ) ) {
2023-07-11 18:36:34 +00:00
working_set . error ( ParseError ::NameIsBuiltinVar ( var_name , lvalue . span ) )
}
let var_id = lvalue . as_var ( ) ;
let rhs_type = rvalue . ty . clone ( ) ;
if let Some ( explicit_type ) = & explicit_type {
if ! type_compatible ( explicit_type , & rhs_type ) {
working_set . error ( ParseError ::TypeMismatch (
explicit_type . clone ( ) ,
rhs_type . clone ( ) ,
nu_protocol ::span ( & spans [ ( span . 0 + 1 ) .. ] ) ,
) ) ;
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
}
2023-07-11 18:36:34 +00:00
}
Improve type hovers (#9515)
# Description
This PR does a few things to help improve type hovers and, in the
process, fixes a few outstanding issues in the type system. Here's a
list of the changes:
* `for` now will try to infer the type of the iteration variable based
on the expression it's given. This fixes things like `for x in [1, 2, 3]
{ }` where `x` now properly gets the int type.
* Removed old input/output type fields from the signature, focuses on
the vec of signatures. Updated a bunch of dataframe commands that hadn't
moved over. This helps tie things together a bit better
* Fixed inference of types from subexpressions to use the last
expression in the block
* Fixed handling of explicit types in `let` and `mut` calls, so we now
respect that as the authoritative type
I also tried to add `def` input/output type inference, but unfortunately
we only know the predecl types universally, which means we won't have
enough information to properly know what the types of the custom
commands are.
# User-Facing Changes
Script typechecking will get tighter in some cases
Hovers should be more accurate in some cases that previously resorted to
any.
# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library
> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-06-28 17:19:48 +00:00
2023-07-11 18:36:34 +00:00
if let Some ( var_id ) = var_id {
if explicit_type . is_none ( ) {
working_set . set_variable_type ( var_id , rhs_type ) ;
2022-11-11 06:51:08 +00:00
}
2023-07-11 18:36:34 +00:00
}
2022-11-11 06:51:08 +00:00
2023-07-11 18:36:34 +00:00
let call = Box ::new ( Call {
decl_id ,
head : spans [ 0 ] ,
arguments : vec ! [ Argument ::Positional ( lvalue ) , Argument ::Positional ( rvalue ) ] ,
redirect_stdout : true ,
redirect_stderr : false ,
parser_info : HashMap ::new ( ) ,
} ) ;
2022-11-11 06:51:08 +00:00
2023-07-11 18:36:34 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : nu_protocol ::span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2022-11-11 06:51:08 +00:00
}
}
}
2023-07-11 18:36:34 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & spans [ 1 .. ] , decl_id ) ;
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : nu_protocol ::span ( spans ) ,
ty : output ,
custom_completion : None ,
} ] ) ;
} else {
working_set . error ( ParseError ::UnknownState (
" internal error: let or const statements not found in core language " . into ( ) ,
span ( spans ) ,
) )
2022-11-11 06:51:08 +00:00
}
2023-07-11 18:36:34 +00:00
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
2023-07-11 18:36:34 +00:00
" internal error: let or const statement unparsable " . into ( ) ,
2023-04-07 00:35:45 +00:00
span ( spans ) ,
) ) ;
garbage_pipeline ( spans )
2022-11-11 06:51:08 +00:00
}
2023-04-07 18:09:38 +00:00
pub fn parse_source ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Pipeline {
2021-10-02 02:24:43 +00:00
let name = working_set . get_span_contents ( spans [ 0 ] ) ;
2022-09-08 20:41:49 +00:00
if name = = b " source " | | name = = b " source-env " {
let scoped = name = = b " source-env " ;
2023-07-13 19:05:03 +00:00
if let Some ( decl_id ) = working_set . find_decl ( name ) {
2022-01-05 00:26:01 +00:00
let cwd = working_set . get_cwd ( ) ;
2022-07-27 15:36:56 +00:00
2021-10-06 02:03:18 +00:00
// Is this the right call to be using here?
// Some of the others (`parse_let`) use it, some of them (`parse_hide`) don't.
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & spans [ 1 .. ] , decl_id ) ;
2021-10-02 02:24:43 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return garbage_pipeline ( spans ) ;
} ;
if is_help {
2023-04-07 00:35:45 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : span ( spans ) ,
ty : output ,
custom_completion : None ,
} ] ) ;
2022-02-24 15:32:10 +00:00
}
2021-10-02 02:24:43 +00:00
// Command and one file name
if spans . len ( ) > = 2 {
2023-04-07 18:09:38 +00:00
let expr = parse_value ( working_set , spans [ 1 ] , & SyntaxShape ::Any ) ;
2022-12-21 22:21:03 +00:00
let val = match eval_constant ( working_set , & expr ) {
Ok ( val ) = > val ,
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , span ( & spans [ 1 .. ] ) ) ) ;
2023-04-07 00:35:45 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : span ( & spans [ 1 .. ] ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2022-12-21 22:21:03 +00:00
}
} ;
2023-12-04 19:13:47 +00:00
let filename = match val . as_string ( ) {
2022-12-21 22:21:03 +00:00
Ok ( s ) = > s ,
Err ( err ) = > {
2023-08-26 13:41:29 +00:00
working_set . error ( err . wrap ( working_set , span ( & spans [ 1 .. ] ) ) ) ;
2023-04-07 00:35:45 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : span ( & spans [ 1 .. ] ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2022-12-21 22:21:03 +00:00
}
} ;
2023-04-05 16:56:48 +00:00
if let Some ( path ) = find_in_dirs ( & filename , working_set , & cwd , LIB_DIRS_VAR ) {
2023-05-23 20:48:50 +00:00
if let Some ( contents ) = path . read ( working_set ) {
2022-12-21 22:21:03 +00:00
// Change currently parsed directory
let prev_currently_parsed_cwd = if let Some ( parent ) = path . parent ( ) {
2023-05-23 20:48:50 +00:00
working_set . currently_parsed_cwd . replace ( parent . into ( ) )
2022-12-21 22:21:03 +00:00
} else {
working_set . currently_parsed_cwd . clone ( )
} ;
// This will load the defs from the file into the
// working set, if it was a successful parse.
2023-04-07 00:35:45 +00:00
let block = parse (
2022-12-21 22:21:03 +00:00
working_set ,
2023-05-23 20:48:50 +00:00
Some ( & path . path ( ) . to_string_lossy ( ) ) ,
2022-12-21 22:21:03 +00:00
& contents ,
scoped ,
) ;
// Restore the currently parsed directory back
working_set . currently_parsed_cwd = prev_currently_parsed_cwd ;
2023-04-07 00:35:45 +00:00
// Save the block into the working set
let block_id = working_set . add_block ( block ) ;
2021-12-03 19:49:11 +00:00
2023-04-07 00:35:45 +00:00
let mut call_with_block = call ;
// FIXME: Adding this expression to the positional creates a syntax highlighting error
// after writing `source example.nu`
call_with_block . set_parser_info (
" block_id " . to_string ( ) ,
Expression {
expr : Expr ::Int ( block_id as i64 ) ,
span : spans [ 1 ] ,
ty : Type ::Any ,
custom_completion : None ,
} ,
) ;
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call_with_block ) ,
span : span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2021-10-02 02:24:43 +00:00
}
2021-10-31 15:53:53 +00:00
} else {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::SourcedFileNotFound ( filename , spans [ 1 ] ) ) ;
2021-10-02 02:24:43 +00:00
}
}
2023-04-07 00:35:45 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2021-10-02 02:24:43 +00:00
}
}
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: source statement unparsable " . into ( ) ,
span ( spans ) ,
) ) ;
garbage_pipeline ( spans )
2021-10-02 02:24:43 +00:00
}
2021-10-31 08:17:01 +00:00
2023-04-07 18:09:38 +00:00
pub fn parse_where_expr ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Expression {
2022-12-10 17:23:24 +00:00
trace! ( " parsing: where " ) ;
if ! spans . is_empty ( ) & & working_set . get_span_contents ( spans [ 0 ] ) ! = b " where " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Wrong call name for 'where' command " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage ( span ( spans ) ) ;
2022-12-10 17:23:24 +00:00
}
if spans . len ( ) < 2 {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::MissingPositional (
" row condition " . into ( ) ,
span ( spans ) ,
" where <row_condition> " . into ( ) ,
) ) ;
return garbage ( span ( spans ) ) ;
2022-12-10 17:23:24 +00:00
}
2023-07-13 19:05:03 +00:00
let call = match working_set . find_decl ( b " where " ) {
2022-12-10 17:23:24 +00:00
Some ( decl_id ) = > {
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & spans [ 1 .. ] , decl_id ) ;
2022-12-10 17:23:24 +00:00
let decl = working_set . get_decl ( decl_id ) ;
let call_span = span ( spans ) ;
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & decl . signature ( ) , & call ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return garbage ( span ( spans ) ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2023-04-07 00:35:45 +00:00
return Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : output ,
custom_completion : None ,
} ;
2022-12-10 17:23:24 +00:00
}
call
}
None = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: 'where' declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage ( span ( spans ) ) ;
2022-12-10 17:23:24 +00:00
}
} ;
2023-04-07 00:35:45 +00:00
Expression {
expr : Expr ::Call ( call ) ,
span : span ( spans ) ,
ty : Type ::Any ,
custom_completion : None ,
}
2022-12-10 17:23:24 +00:00
}
2023-04-07 18:09:38 +00:00
pub fn parse_where ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Pipeline {
let expression = parse_where_expr ( working_set , spans ) ;
2023-04-07 00:35:45 +00:00
Pipeline ::from_vec ( vec! [ expression ] )
2022-12-10 17:23:24 +00:00
}
2021-11-02 20:56:00 +00:00
#[ cfg(feature = " plugin " ) ]
2023-04-07 18:09:38 +00:00
pub fn parse_register ( working_set : & mut StateWorkingSet , spans : & [ Span ] ) -> Pipeline {
2022-09-07 14:07:42 +00:00
use nu_plugin ::{ get_signature , PluginDeclaration } ;
Make plugin commands support examples. (#7984)
# Description
As title, we can't provide examples for plugin commands, this pr would
make it possible
# User-Facing Changes
Take plugin `nu-example-1` as example:
```
❯ nu-example-1 -h
PluginSignature test 1 for plugin. Returns Value::Nothing
Usage:
> nu-example-1 {flags} <a> <b> (opt) ...(rest)
Flags:
-h, --help - Display the help message for this command
-f, --flag - a flag for the signature
-n, --named <String> - named string
Parameters:
a <int>: required integer value
b <string>: required string value
(optional) opt <int>: Optional number
...rest <string>: rest value string
Examples:
running example with an int value and string value
> nu-example-1 3 bb
```
The examples session is newly added.
## Basic idea behind these changes
when nushell query plugin signatures, plugin just returns it's signature
without any examples, so nushell have no idea about the examples of
plugin commands.
To adding the feature, we just making plugin returns it's signature with
examples.
Before:
```
1. get signature
---------------->
Nushell ------------------ Plugin
<-----------------
2. returns Vec<Signature>
```
After:
```
1. get signature
---------------->
Nushell ------------------ Plugin
<-----------------
2. returns Vec<PluginSignature>
```
When writing plugin signature to $nu.plugin-path:
Serialize `<PluginSignature>` rather than `<Signature>`, which would
enable us to serialize examples to `$nu.plugin-path`
## Shortcoming
It's a breaking changes because `Plugin::signature` is changed, and it
requires plugin authors to change their code for new signatures.
Fortunally it should be easy to change, for rust based plugin, we just
need to make a global replace from word `Signature` to word
`PluginSignature` in their plugin project.
Our content of plugin-path is really large, if one plugin have many
examples, it'd results to larger body of $nu.plugin-path, which is not
really scale. A solution would be save register information in other
binary formats rather than `json`. But I think it'd be another story.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-08 22:14:18 +00:00
use nu_protocol ::{ engine ::Stack , PluginSignature } ;
2022-08-08 12:26:49 +00:00
2022-01-05 00:26:01 +00:00
let cwd = working_set . get_cwd ( ) ;
2021-11-19 02:51:42 +00:00
2021-12-12 11:50:35 +00:00
// Checking that the function is used with the correct name
// Maybe this is not necessary but it is a sanity check
if working_set . get_span_contents ( spans [ 0 ] ) ! = b " register " {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Wrong call name for parse plugin function " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage_pipeline ( spans ) ;
2021-10-31 08:17:01 +00:00
}
2021-12-12 11:50:35 +00:00
// Parsing the spans and checking that they match the register signature
// Using a parsed call makes more sense than checking for how many spans are in the call
// Also, by creating a call, it can be checked if it matches the declaration signature
2023-07-13 19:05:03 +00:00
let ( call , call_span ) = match working_set . find_decl ( b " register " ) {
2021-12-12 11:50:35 +00:00
None = > {
2023-04-07 00:35:45 +00:00
working_set . error ( ParseError ::UnknownState (
" internal error: Register declaration not found " . into ( ) ,
span ( spans ) ,
) ) ;
return garbage_pipeline ( spans ) ;
2021-12-12 11:50:35 +00:00
}
Some ( decl_id ) = > {
2023-04-07 18:09:38 +00:00
let ParsedInternalCall { call , output } =
parse_internal_call ( working_set , spans [ 0 ] , & spans [ 1 .. ] , decl_id ) ;
2021-12-12 11:50:35 +00:00
let decl = working_set . get_decl ( decl_id ) ;
2021-12-03 14:29:55 +00:00
2021-12-18 20:10:40 +00:00
let call_span = span ( spans ) ;
2023-04-07 00:35:45 +00:00
let starting_error_count = working_set . parse_errors . len ( ) ;
check_call ( working_set , call_span , & decl . signature ( ) , & call ) ;
2024-01-11 15:19:48 +00:00
2024-02-03 11:20:40 +00:00
let Ok ( is_help ) = has_flag_const ( working_set , & call , " help " ) else {
2024-01-11 15:19:48 +00:00
return garbage_pipeline ( spans ) ;
} ;
if starting_error_count ! = working_set . parse_errors . len ( ) | | is_help {
2023-04-07 00:35:45 +00:00
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : output ,
custom_completion : None ,
} ] ) ;
2021-12-12 11:50:35 +00:00
}
2021-12-03 14:29:55 +00:00
2021-12-12 11:50:35 +00:00
( call , call_span )
}
} ;
2021-12-03 14:29:55 +00:00
2021-12-12 11:50:35 +00:00
// Extracting the required arguments from the call and keeping them together in a tuple
let arguments = call
2022-04-09 02:55:02 +00:00
. positional_nth ( 0 )
2021-12-12 11:50:35 +00:00
. map ( | expr | {
2023-08-26 13:41:29 +00:00
let val =
eval_constant ( working_set , expr ) . map_err ( | err | err . wrap ( working_set , call . head ) ) ? ;
2023-12-04 19:13:47 +00:00
let filename = val
. as_string ( )
. map_err ( | err | err . wrap ( working_set , call . head ) ) ? ;
2023-04-08 20:04:57 +00:00
let Some ( path ) = find_in_dirs ( & filename , working_set , & cwd , PLUGIN_DIRS_VAR ) else {
2023-09-04 07:42:31 +00:00
return Err ( ParseError ::RegisteredFileNotFound ( filename , expr . span ) ) ;
2023-04-07 00:35:45 +00:00
} ;
2022-05-01 18:37:20 +00:00
2023-04-08 20:04:57 +00:00
if path . exists ( ) & & path . is_file ( ) {
2023-04-07 00:35:45 +00:00
Ok ( ( path , expr . span ) )
} else {
2023-04-08 20:04:57 +00:00
Err ( ParseError ::RegisteredFileNotFound ( filename , expr . span ) )
2022-05-01 18:37:20 +00:00
}
2021-12-12 11:50:35 +00:00
} )
2022-09-07 14:07:42 +00:00
. expect ( " required positional has being checked " ) ;
2021-10-31 08:17:01 +00:00
2021-12-18 18:13:56 +00:00
// Signature is an optional value from the call and will be used to decide if
2021-12-12 11:50:35 +00:00
// the plugin is called to get the signatures or to use the given signature
2022-04-09 02:55:02 +00:00
let signature = call . positional_nth ( 1 ) . map ( | expr | {
2021-12-12 11:50:35 +00:00
let signature = working_set . get_span_contents ( expr . span ) ;
Make plugin commands support examples. (#7984)
# Description
As title, we can't provide examples for plugin commands, this pr would
make it possible
# User-Facing Changes
Take plugin `nu-example-1` as example:
```
❯ nu-example-1 -h
PluginSignature test 1 for plugin. Returns Value::Nothing
Usage:
> nu-example-1 {flags} <a> <b> (opt) ...(rest)
Flags:
-h, --help - Display the help message for this command
-f, --flag - a flag for the signature
-n, --named <String> - named string
Parameters:
a <int>: required integer value
b <string>: required string value
(optional) opt <int>: Optional number
...rest <string>: rest value string
Examples:
running example with an int value and string value
> nu-example-1 3 bb
```
The examples session is newly added.
## Basic idea behind these changes
when nushell query plugin signatures, plugin just returns it's signature
without any examples, so nushell have no idea about the examples of
plugin commands.
To adding the feature, we just making plugin returns it's signature with
examples.
Before:
```
1. get signature
---------------->
Nushell ------------------ Plugin
<-----------------
2. returns Vec<Signature>
```
After:
```
1. get signature
---------------->
Nushell ------------------ Plugin
<-----------------
2. returns Vec<PluginSignature>
```
When writing plugin signature to $nu.plugin-path:
Serialize `<PluginSignature>` rather than `<Signature>`, which would
enable us to serialize examples to `$nu.plugin-path`
## Shortcoming
It's a breaking changes because `Plugin::signature` is changed, and it
requires plugin authors to change their code for new signatures.
Fortunally it should be easy to change, for rust based plugin, we just
need to make a global replace from word `Signature` to word
`PluginSignature` in their plugin project.
Our content of plugin-path is really large, if one plugin have many
examples, it'd results to larger body of $nu.plugin-path, which is not
really scale. A solution would be save register information in other
binary formats rather than `json`. But I think it'd be another story.
# Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# After Submitting
If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
2023-02-08 22:14:18 +00:00
serde_json ::from_slice ::< PluginSignature > ( signature ) . map_err ( | e | {
2021-12-12 11:50:35 +00:00
ParseError ::LabeledError (
" Signature deserialization error " . into ( ) ,
2023-01-30 01:37:54 +00:00
format! ( " unable to deserialize signature: {e} " ) ,
2021-12-12 11:50:35 +00:00
spans [ 0 ] ,
)
} )
} ) ;
2021-10-31 08:17:01 +00:00
2021-12-18 18:13:56 +00:00
// Shell is another optional value used as base to call shell to plugins
let shell = call . get_flag_expr ( " shell " ) . map ( | expr | {
let shell_expr = working_set . get_span_contents ( expr . span ) ;
String ::from_utf8 ( shell_expr . to_vec ( ) )
. map_err ( | _ | ParseError ::NonUtf8 ( expr . span ) )
. and_then ( | name | {
2022-03-25 21:43:46 +00:00
canonicalize_with ( & name , cwd )
. map_err ( | _ | ParseError ::RegisteredFileNotFound ( name , expr . span ) )
2021-12-18 18:13:56 +00:00
} )
. and_then ( | path | {
if path . exists ( ) & path . is_file ( ) {
Ok ( path )
} else {
2022-03-25 21:43:46 +00:00
Err ( ParseError ::RegisteredFileNotFound (
2023-01-30 01:37:54 +00:00
format! ( " {path:?} " ) ,
2022-03-25 21:43:46 +00:00
expr . span ,
) )
2021-12-18 18:13:56 +00:00
}
} )
} ) ;
let shell = match shell {
None = > None ,
Some ( path ) = > match path {
Ok ( path ) = > Some ( path ) ,
Err ( err ) = > {
2023-04-07 00:35:45 +00:00
working_set . error ( err ) ;
return Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Any ,
custom_completion : None ,
} ] ) ;
2021-12-18 18:13:56 +00:00
}
} ,
} ;
2022-08-08 12:26:49 +00:00
// We need the current environment variables for `python` based plugins
// Or we'll likely have a problem when a plugin is implemented in a virtual Python environment.
let stack = Stack ::new ( ) ;
let current_envs =
nu_engine ::env ::env_to_strings ( working_set . permanent_state , & stack ) . unwrap_or_default ( ) ;
2023-08-14 13:39:23 +00:00
let error = arguments . and_then ( | ( path , path_span ) | {
let path = path . path_buf ( ) ;
// restrict plugin file name starts with `nu_plugin_`
let valid_plugin_name = path
. file_name ( )
. map ( | s | s . to_string_lossy ( ) . starts_with ( " nu_plugin_ " ) ) ;
let Some ( true ) = valid_plugin_name else {
return Err ( ParseError ::LabeledError (
" Register plugin failed " . into ( ) ,
" plugin name must start with nu_plugin_ " . into ( ) ,
path_span ,
) ) ;
} ;
let signatures = signature . map_or_else (
| | {
2023-09-12 23:00:58 +00:00
let signatures =
get_signature ( & path , shell . as_deref ( ) , & current_envs ) . map_err ( | err | {
ParseError ::LabeledError (
" Error getting signatures " . into ( ) ,
err . to_string ( ) ,
spans [ 0 ] ,
)
} ) ;
2023-08-14 13:39:23 +00:00
if signatures . is_ok ( ) {
// mark plugins file as dirty only when the user is registering plugins
// and not when we evaluate plugin.nu on shell startup
2022-09-04 23:00:20 +00:00
working_set . mark_plugins_file_dirty ( ) ;
2023-08-14 13:39:23 +00:00
}
2021-10-31 08:17:01 +00:00
2023-08-14 13:39:23 +00:00
signatures
} ,
| sig | sig . map ( | sig | vec! [ sig ] ) ,
) ? ;
for signature in signatures {
// create plugin command declaration (need struct impl Command)
// store declaration in working set
let plugin_decl = PluginDeclaration ::new ( path . clone ( ) , signature , shell . clone ( ) ) ;
working_set . add_decl ( Box ::new ( plugin_decl ) ) ;
}
Ok ( ( ) )
} ) ;
2021-12-12 11:50:35 +00:00
2023-08-14 13:39:23 +00:00
if let Err ( err ) = error {
2023-04-07 00:35:45 +00:00
working_set . error ( err ) ;
}
Pipeline ::from_vec ( vec! [ Expression {
expr : Expr ::Call ( call ) ,
span : call_span ,
ty : Type ::Nothing ,
custom_completion : None ,
} ] )
2021-10-31 08:17:01 +00:00
}
2022-03-12 20:12:15 +00:00
2023-04-05 16:56:48 +00:00
pub fn find_dirs_var ( working_set : & StateWorkingSet , var_name : & str ) -> Option < VarId > {
working_set
. find_variable ( format! ( " $ {} " , var_name ) . as_bytes ( ) )
2023-07-13 21:02:05 +00:00
. filter ( | var_id | working_set . get_variable ( * var_id ) . const_val . is_some ( ) )
2023-04-05 16:56:48 +00:00
}
2022-07-27 15:36:56 +00:00
/// This helper function is used to find files during parsing
///
2022-07-29 20:42:00 +00:00
/// First, the actual current working directory is selected as
/// a) the directory of a file currently being parsed
/// b) current working directory (PWD)
2022-07-27 15:36:56 +00:00
///
2023-04-05 16:56:48 +00:00
/// Then, if the file is not found in the actual cwd, dirs_var is checked.
/// For now, we first check for a const with the name of `dirs_var_name`,
/// and if that's not found, then we try to look for an environment variable of the same name.
/// If there is a relative path in dirs_var, it is assumed to be relative to the actual cwd
2022-07-29 20:42:00 +00:00
/// determined in the first step.
///
/// Always returns an absolute path
2022-08-31 20:32:56 +00:00
pub fn find_in_dirs (
2022-03-12 20:12:15 +00:00
filename : & str ,
working_set : & StateWorkingSet ,
cwd : & str ,
2023-04-05 16:56:48 +00:00
dirs_var_name : & str ,
2023-05-23 20:48:50 +00:00
) -> Option < ParserPath > {
2023-04-05 16:56:48 +00:00
pub fn find_in_dirs_with_id (
filename : & str ,
working_set : & StateWorkingSet ,
cwd : & str ,
dirs_var_name : & str ,
2023-05-23 20:48:50 +00:00
) -> Option < ParserPath > {
2023-04-05 16:56:48 +00:00
// Choose whether to use file-relative or PWD-relative path
let actual_cwd = if let Some ( currently_parsed_cwd ) = & working_set . currently_parsed_cwd {
currently_parsed_cwd . as_path ( )
} else {
Path ::new ( cwd )
} ;
2023-05-23 20:48:50 +00:00
// Try if we have an existing virtual path
if let Some ( virtual_path ) = working_set . find_virtual_path ( filename ) {
return Some ( ParserPath ::from_virtual_path (
working_set ,
filename ,
virtual_path ,
) ) ;
} else {
let abs_virtual_filename = actual_cwd . join ( filename ) ;
let abs_virtual_filename = abs_virtual_filename . to_string_lossy ( ) ;
if let Some ( virtual_path ) = working_set . find_virtual_path ( & abs_virtual_filename ) {
return Some ( ParserPath ::from_virtual_path (
working_set ,
& abs_virtual_filename ,
virtual_path ,
) ) ;
}
}
// Try if we have an existing physical path
2023-04-05 16:56:48 +00:00
if let Ok ( p ) = canonicalize_with ( filename , actual_cwd ) {
2023-05-23 20:48:50 +00:00
return Some ( ParserPath ::RealPath ( p ) ) ;
2023-04-05 16:56:48 +00:00
}
2022-07-29 20:42:00 +00:00
2023-05-23 20:48:50 +00:00
// Early-exit if path is non-existent absolute path
2022-03-12 20:12:15 +00:00
let path = Path ::new ( filename ) ;
2023-04-05 16:56:48 +00:00
if ! path . is_relative ( ) {
return None ;
}
2022-03-12 20:12:15 +00:00
2023-05-23 20:48:50 +00:00
// Look up relative path from NU_LIB_DIRS
2023-04-05 16:56:48 +00:00
working_set
2023-07-13 21:02:05 +00:00
. get_variable ( find_dirs_var ( working_set , dirs_var_name ) ? )
. const_val
. as_ref ( ) ?
2023-04-05 16:56:48 +00:00
. as_list ( )
. ok ( ) ?
. iter ( )
. map ( | lib_dir | -> Option < PathBuf > {
let dir = lib_dir . as_path ( ) . ok ( ) ? ;
let dir_abs = canonicalize_with ( dir , actual_cwd ) . ok ( ) ? ;
canonicalize_with ( filename , dir_abs ) . ok ( )
} )
. find ( Option ::is_some )
. flatten ( )
2023-05-23 20:48:50 +00:00
. map ( ParserPath ::RealPath )
2023-04-05 16:56:48 +00:00
}
// TODO: remove (see #8310)
2023-05-23 20:48:50 +00:00
// Same as find_in_dirs_with_id but using $env.NU_LIB_DIRS instead of constant
2023-04-05 16:56:48 +00:00
pub fn find_in_dirs_old (
filename : & str ,
working_set : & StateWorkingSet ,
cwd : & str ,
dirs_env : & str ,
) -> Option < PathBuf > {
// Choose whether to use file-relative or PWD-relative path
let actual_cwd = if let Some ( currently_parsed_cwd ) = & working_set . currently_parsed_cwd {
currently_parsed_cwd . as_path ( )
} else {
Path ::new ( cwd )
} ;
if let Ok ( p ) = canonicalize_with ( filename , actual_cwd ) {
Some ( p )
} else {
let path = Path ::new ( filename ) ;
if path . is_relative ( ) {
if let Some ( lib_dirs ) = working_set . get_env_var ( dirs_env ) {
if let Ok ( dirs ) = lib_dirs . as_list ( ) {
for lib_dir in dirs {
if let Ok ( dir ) = lib_dir . as_path ( ) {
// make sure the dir is absolute path
if let Ok ( dir_abs ) = canonicalize_with ( dir , actual_cwd ) {
if let Ok ( path ) = canonicalize_with ( filename , dir_abs ) {
return Some ( path ) ;
}
2022-03-12 20:12:15 +00:00
}
}
}
2023-04-05 16:56:48 +00:00
None
} else {
None
}
2022-03-12 20:12:15 +00:00
} else {
None
}
} else {
None
}
}
}
2023-04-05 16:56:48 +00:00
2023-05-23 20:48:50 +00:00
find_in_dirs_with_id ( filename , working_set , cwd , dirs_var_name ) . or_else ( | | {
find_in_dirs_old ( filename , working_set , cwd , dirs_var_name ) . map ( ParserPath ::RealPath )
} )
2022-03-12 20:12:15 +00:00
}
2023-05-12 14:10:40 +00:00
fn detect_params_in_name (
working_set : & StateWorkingSet ,
name_span : Span ,
decl_name : & str ,
) -> Option < ParseError > {
let name = working_set . get_span_contents ( name_span ) ;
let extract_span = | delim : u8 | {
// it is okay to unwrap because we know the slice contains the byte
let ( idx , _ ) = name
. iter ( )
. find_position ( | c | * * c = = delim )
. unwrap_or ( ( name . len ( ) , & b ' ' ) ) ;
let param_span = Span ::new ( name_span . start + idx - 1 , name_span . start + idx - 1 ) ;
let error = ParseError ::LabeledErrorWithHelp {
error : " no space between name and parameters " . into ( ) ,
label : " expected space " . into ( ) ,
help : format ! ( " consider adding a space between the `{decl_name}` command's name and its parameters " ) ,
span : param_span ,
} ;
Some ( error )
} ;
if name . contains ( & b '[' ) {
extract_span ( b '[' )
} else if name . contains ( & b '(' ) {
extract_span ( b '(' )
} else {
None
}
}
2024-01-11 15:19:48 +00:00
/// Run has_flag_const and and push possible error to working_set
2024-02-03 11:20:40 +00:00
fn has_flag_const ( working_set : & mut StateWorkingSet , call : & Call , name : & str ) -> Result < bool , ( ) > {
2024-01-11 15:19:48 +00:00
call . has_flag_const ( working_set , name ) . map_err ( | err | {
working_set . error ( err . wrap ( working_set , call . span ( ) ) ) ;
} )
}