diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 986ccee9b..4fc218227 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -1719,7 +1719,8 @@ pub fn parse_use(working_set: &mut StateWorkingSet, spans: &[Span]) -> (Pipeline }; if let Ok(contents) = std::fs::read(&module_path) { - let file_id = working_set.add_file(module_filename, &contents); + let file_id = + working_set.add_file(module_path.to_string_lossy().to_string(), &contents); let new_span = working_set.get_span_for_file(file_id); // Change the currently parsed directory diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index b72fd066d..bc82f6f72 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -5813,7 +5813,12 @@ pub fn parse( scoped: bool, ) -> Block { let name = match fname { - Some(fname) => fname.to_string(), + Some(fname) => { + // use the canonical name for this filename + nu_path::expand_to_real_path(fname) + .to_string_lossy() + .to_string() + } None => "source".to_string(), }; diff --git a/src/command.rs b/src/command.rs index 6a2958b59..f1cb5411f 100644 --- a/src/command.rs +++ b/src/command.rs @@ -37,7 +37,9 @@ pub(crate) fn gather_commandline_args() -> (Vec, String, Vec) { #[cfg(feature = "plugin")] "--plugin-config" => args.next().map(|a| escape_quote_string(&a)), "--log-level" | "--log-target" | "--testbin" | "--threads" | "-t" - | "--ide-goto-def" | "--ide-hover" | "--ide-complete" => args.next(), + | "--include-path" | "-I" | "--ide-goto-def" | "--ide-hover" | "--ide-complete" => { + args.next() + } _ => None, }; @@ -109,6 +111,7 @@ pub(crate) fn parse_commandline_args( let log_level: Option = call.get_flag_expr("log-level"); let log_target: Option = call.get_flag_expr("log-target"); let execute: Option = call.get_flag_expr("execute"); + let include_path: Option = call.get_flag_expr("include-path"); let table_mode: Option = call.get_flag(engine_state, &mut stack, "table-mode")?; @@ -142,6 +145,7 @@ pub(crate) fn parse_commandline_args( let log_level = extract_contents(log_level)?; let log_target = extract_contents(log_target)?; let execute = extract_contents(execute)?; + let include_path = extract_contents(include_path)?; let help = call.has_flag("help"); @@ -183,6 +187,7 @@ pub(crate) fn parse_commandline_args( log_level, log_target, execute, + include_path, ide_goto_def, ide_hover, ide_complete, @@ -220,6 +225,7 @@ pub(crate) struct NushellCliArgs { pub(crate) log_target: Option>, pub(crate) execute: Option>, pub(crate) table_mode: Option, + pub(crate) include_path: Option>, pub(crate) ide_goto_def: Option, pub(crate) ide_hover: Option, pub(crate) ide_complete: Option, @@ -249,6 +255,12 @@ impl Command for Nu { "run the given commands and then enter an interactive shell", Some('e'), ) + .named( + "include-path", + SyntaxShape::String, + "set the NU_LIB_DIRS for the given script", + Some('I'), + ) .switch("interactive", "start as an interactive shell", Some('i')) .switch("login", "start as a login shell", Some('l')) .named( diff --git a/src/ide.rs b/src/ide.rs index 18e249a90..cb73df739 100644 --- a/src/ide.rs +++ b/src/ide.rs @@ -10,6 +10,7 @@ use reedline::Completer; use serde_json::json; use std::sync::Arc; +#[derive(Debug)] enum Id { Variable(VarId), Declaration(DeclId), @@ -74,6 +75,9 @@ fn read_in_file<'a>( } pub fn check(engine_state: &mut EngineState, file_path: &String) { + let cwd = std::env::current_dir().expect("Could not get current working directory."); + engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy())); + let mut working_set = StateWorkingSet::new(engine_state); let file = std::fs::read(file_path); @@ -124,10 +128,13 @@ pub fn check(engine_state: &mut EngineState, file_path: &String) { } pub fn goto_def(engine_state: &mut EngineState, file_path: &String, location: &Value) { + let cwd = std::env::current_dir().expect("Could not get current working directory."); + engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy())); + let (file, mut working_set) = read_in_file(engine_state, file_path); match find_id(&mut working_set, file_path, &file, location) { - Some((Id::Declaration(decl_id), offset, _)) => { + Some((Id::Declaration(decl_id), ..)) => { let result = working_set.get_decl(decl_id); if let Some(block_id) = result.get_block_id() { let block = working_set.get_block(block_id); @@ -139,8 +146,8 @@ pub fn goto_def(engine_state: &mut EngineState, file_path: &String, location: &V json!( { "file": file.0, - "start": span.start - offset, - "end": span.end - offset + "start": span.start - file.1, + "end": span.end - file.1 } ) ); @@ -150,7 +157,7 @@ pub fn goto_def(engine_state: &mut EngineState, file_path: &String, location: &V } } } - Some((Id::Variable(var_id), offset, _)) => { + Some((Id::Variable(var_id), ..)) => { let var = working_set.get_variable(var_id); for file in working_set.files() { if var.declaration_span.start >= file.1 && var.declaration_span.start < file.2 { @@ -159,8 +166,8 @@ pub fn goto_def(engine_state: &mut EngineState, file_path: &String, location: &V json!( { "file": file.0, - "start": var.declaration_span.start - offset, - "end": var.declaration_span.end - offset + "start": var.declaration_span.start - file.1, + "end": var.declaration_span.end - file.1 } ) ); @@ -175,6 +182,9 @@ pub fn goto_def(engine_state: &mut EngineState, file_path: &String, location: &V } pub fn hover(engine_state: &mut EngineState, file_path: &String, location: &Value) { + let cwd = std::env::current_dir().expect("Could not get current working directory."); + engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy())); + let (file, mut working_set) = read_in_file(engine_state, file_path); match find_id(&mut working_set, file_path, &file, location) { diff --git a/src/main.rs b/src/main.rs index b1d1f8fb9..bd74cee1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use log::Level; use miette::Result; use nu_cli::gather_parent_env_vars; use nu_command::{create_default_context, get_init_cwd}; -use nu_protocol::report_error_new; +use nu_protocol::{report_error_new, Span, Value}; use nu_protocol::{util::BufferedReader, PipelineData, RawStream}; use nu_utils::utils::perf; use run::{run_commands, run_file, run_repl}; @@ -137,6 +137,16 @@ fn main() -> Result<()> { use_color, ); + if let Some(include_path) = &parsed_nu_cli_args.include_path { + engine_state.add_env_var( + "NU_LIB_DIRS".into(), + Value::List { + vals: vec![Value::test_string(&include_path.item)], + span: Span::test_data(), + }, + ); + } + // IDE commands if let Some(ide_goto_def) = parsed_nu_cli_args.ide_goto_def { ide::goto_def(&mut engine_state, &script_name, &ide_goto_def); @@ -147,6 +157,9 @@ fn main() -> Result<()> { return Ok(()); } else if let Some(ide_complete) = parsed_nu_cli_args.ide_complete { + let cwd = std::env::current_dir().expect("Could not get current working directory."); + engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy())); + ide::complete(Arc::new(engine_state), &script_name, &ide_complete); return Ok(());