diff --git a/src/command.rs b/src/command.rs index 51781b51e..6f71f2e45 100644 --- a/src/command.rs +++ b/src/command.rs @@ -37,9 +37,8 @@ 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" - | "--include-path" | "-I" | "--ide-goto-def" | "--ide-hover" | "--ide-complete" => { - args.next() - } + | "--include-path" | "-I" | "--ide-goto-def" | "--ide-hover" | "--ide-complete" + | "--ide-check" => args.next(), _ => None, }; @@ -92,12 +91,6 @@ pub(crate) fn parse_commandline_args( )) = pipeline.elements.get(0) { let redirect_stdin = call.get_named_arg("stdin"); - let ide_goto_def: Option = - call.get_flag(engine_state, &mut stack, "ide-goto-def")?; - let ide_hover: Option = call.get_flag(engine_state, &mut stack, "ide-hover")?; - let ide_complete: Option = - call.get_flag(engine_state, &mut stack, "ide-complete")?; - let ide_check = call.get_named_arg("ide-check"); let login_shell = call.get_named_arg("login"); let interactive_shell = call.get_named_arg("interactive"); let commands: Option = call.get_flag_expr("commands"); @@ -115,6 +108,13 @@ pub(crate) fn parse_commandline_args( let table_mode: Option = call.get_flag(engine_state, &mut stack, "table-mode")?; + let ide_goto_def: Option = + call.get_flag(engine_state, &mut stack, "ide-goto-def")?; + let ide_hover: Option = call.get_flag(engine_state, &mut stack, "ide-hover")?; + let ide_complete: Option = + call.get_flag(engine_state, &mut stack, "ide-complete")?; + let ide_check: Option = call.get_flag(engine_state, &mut stack, "ide-check")?; + fn extract_contents( expression: Option, ) -> Result>, ShellError> { @@ -229,7 +229,7 @@ pub(crate) struct NushellCliArgs { pub(crate) ide_goto_def: Option, pub(crate) ide_hover: Option, pub(crate) ide_complete: Option, - pub(crate) ide_check: Option>, + pub(crate) ide_check: Option, } #[derive(Clone)] @@ -312,8 +312,9 @@ impl Command for Nu { "list completions for the item at the given position", None, ) - .switch( + .named( "ide-check", + SyntaxShape::Int, "run a diagnostic check on the given source", None, ); diff --git a/src/ide.rs b/src/ide.rs index cb73df739..b470a6324 100644 --- a/src/ide.rs +++ b/src/ide.rs @@ -74,18 +74,28 @@ fn read_in_file<'a>( (file, working_set) } -pub fn check(engine_state: &mut EngineState, file_path: &String) { +pub fn check(engine_state: &mut EngineState, file_path: &String, max_errors: &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 mut working_set = StateWorkingSet::new(engine_state); let file = std::fs::read(file_path); + let max_errors = if let Ok(max_errors) = max_errors.as_i64() { + max_errors as usize + } else { + 100 + }; + if let Ok(contents) = file { let offset = working_set.next_span_start(); let block = parse(&mut working_set, Some(file_path), &contents, false); - for err in &working_set.parse_errors { + for (idx, err) in working_set.parse_errors.iter().enumerate() { + if idx >= max_errors { + // eprintln!("Too many errors, stopping here. idx: {idx} max_errors: {max_errors}"); + break; + } let mut span = err.span(); span.start -= offset; span.end -= offset; diff --git a/src/main.rs b/src/main.rs index cbc78714d..d0e077753 100644 --- a/src/main.rs +++ b/src/main.rs @@ -167,8 +167,8 @@ fn main() -> Result<()> { ide::complete(Arc::new(engine_state), &script_name, &ide_complete); return Ok(()); - } else if parsed_nu_cli_args.ide_check.is_some() { - ide::check(&mut engine_state, &script_name); + } else if let Some(max_errors) = parsed_nu_cli_args.ide_check { + ide::check(&mut engine_state, &script_name, &max_errors); return Ok(()); } diff --git a/src/tests/test_ide.rs b/src/tests/test_ide.rs index 49c2a648b..b5e40cfe8 100644 --- a/src/tests/test_ide.rs +++ b/src/tests/test_ide.rs @@ -4,7 +4,7 @@ use crate::tests::{test_ide_contains, TestResult}; fn parser_recovers() -> TestResult { test_ide_contains( "3 + \"bob\"\nlet x = \"fred\"\n", - &["--ide-check"], + &["--ide-check 5"], "\"typename\":\"string\"", ) }