Skip to content

Commit

Permalink
load all the submodules (amtoine#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Apr 9, 2023
1 parent f44a4f8 commit c3df973
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
12 changes: 6 additions & 6 deletions crates/nu-std/lib/mod.nu
@@ -1,13 +1,13 @@
# std.nu, `used` to load all standard library components

export use crates/nu-std/lib/assert.nu *
export use crates/nu-std/lib/dirs.nu *
export use assert *
export use dirs *
export-env {
use crates/nu-std/lib/dirs.nu *
use dirs *
}
export use crates/nu-std/lib/help.nu *
export use crates/nu-std/lib/log.nu *
export use crates/nu-std/lib/xml.nu *
export use help *
export use log *
export use xml *

# Add the given paths to the PATH.
#
Expand Down
64 changes: 41 additions & 23 deletions crates/nu-std/src/lib.rs
Expand Up @@ -2,8 +2,29 @@ use nu_parser::{parse, parse_module_block};
use nu_protocol::report_error;
use nu_protocol::{engine::StateWorkingSet, Module, ShellError, Span};

fn get_standard_library() -> &'static str {
include_str!("../lib/mod.nu")
fn add_file(
working_set: &mut StateWorkingSet,
name: &String,
content: &[u8],
) -> (Module, Vec<Span>) {
let start = working_set.next_span_start();
working_set.add_file(name.clone(), content);
let end = working_set.next_span_start();

let (_, module, comments) =
parse_module_block(working_set, Span::new(start, end), name.as_bytes());

if let Some(err) = working_set.parse_errors.first() {
report_error(working_set, err);
}

parse(working_set, Some(name), content, true);

if let Some(err) = working_set.parse_errors.first() {
report_error(working_set, err);
}

(module, comments)
}

fn load_prelude(working_set: &mut StateWorkingSet, prelude: Vec<(&str, &str)>, module: &Module) {
Expand Down Expand Up @@ -45,26 +66,15 @@ pub fn load_standard_library(
) -> Result<(), miette::ErrReport> {
let delta = {
let name = "std".to_string();
let content = get_standard_library().as_bytes();

let mut working_set = StateWorkingSet::new(engine_state);

let start = working_set.next_span_start();
working_set.add_file(name.clone(), content);
let end = working_set.next_span_start();

let (_, module, comments) =
parse_module_block(&mut working_set, Span::new(start, end), name.as_bytes());

if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err);
}

parse(&mut working_set, Some(&name), content, true);

if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err);
}
let content = include_str!("../lib/mod.nu");

let submodules = vec![
("assert", include_str!("../lib/assert.nu")),
("dirs", include_str!("../lib/dirs.nu")),
("help", include_str!("../lib/help.nu")),
("log", include_str!("../lib/log.nu")),
("xml", include_str!("../lib/xml.nu")),
];

let prelude = vec![
("std help", "help"),
Expand All @@ -75,8 +85,16 @@ pub fn load_standard_library(
("std help operators", "help operators"),
];

load_prelude(&mut working_set, prelude, &module);
let mut working_set = StateWorkingSet::new(engine_state);

for (name, content) in submodules {
let (module, comments) =
add_file(&mut working_set, &name.to_string(), content.as_bytes());
working_set.add_module(name, module, comments);
}

let (module, comments) = add_file(&mut working_set, &name, content.as_bytes());
load_prelude(&mut working_set, prelude, &module);
working_set.add_module(&name, module, comments);

working_set.render()
Expand Down

0 comments on commit c3df973

Please sign in to comment.