diff --git a/tooling/lsp/src/lib.rs b/tooling/lsp/src/lib.rs index 366b158ad89..c2885543844 100644 --- a/tooling/lsp/src/lib.rs +++ b/tooling/lsp/src/lib.rs @@ -387,15 +387,18 @@ pub fn insert_all_files_for_workspace_into_file_manager( workspace: &Workspace, file_manager: &mut FileManager, ) { - // First add files we cached: these have the source code of files that are modified - // but not saved to disk yet, and we want to make sure all LSP features work well - // according to these unsaved buffers, not what's saved on disk. + // Source code for files we cached override those that are read from disk. + let mut overrides: HashMap<&Path, &str> = HashMap::new(); for (path, source) in &state.input_files { let path = path.strip_prefix("file://").unwrap(); - file_manager.add_file_with_source_canonical_path(Path::new(path), source.clone()); + overrides.insert(Path::new(path), source); } - nargo::insert_all_files_for_workspace_into_file_manager(workspace, file_manager); + nargo::insert_all_files_for_workspace_into_file_manager_with_overrides( + workspace, + file_manager, + &overrides, + ); } #[test] diff --git a/tooling/nargo/src/lib.rs b/tooling/nargo/src/lib.rs index c0c7602d14d..0118e83d2a3 100644 --- a/tooling/nargo/src/lib.rs +++ b/tooling/nargo/src/lib.rs @@ -13,7 +13,7 @@ pub mod ops; pub mod package; pub mod workspace; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap}; use fm::{FileManager, FILE_EXTENSION}; use noirc_driver::{add_dep, prepare_crate, prepare_dependency}; @@ -45,9 +45,21 @@ pub fn prepare_dependencies( pub fn insert_all_files_for_workspace_into_file_manager( workspace: &workspace::Workspace, file_manager: &mut FileManager, +) { + insert_all_files_for_workspace_into_file_manager_with_overrides( + workspace, + file_manager, + &HashMap::new(), + ); +} + +pub fn insert_all_files_for_workspace_into_file_manager_with_overrides( + workspace: &workspace::Workspace, + file_manager: &mut FileManager, + overrides: &HashMap<&std::path::Path, &str>, ) { for package in workspace.clone().into_iter() { - insert_all_files_for_package_into_file_manager(package, file_manager); + insert_all_files_for_package_into_file_manager(package, file_manager, overrides); } } // We will pre-populate the file manager with all the files in the package @@ -59,6 +71,7 @@ pub fn insert_all_files_for_workspace_into_file_manager( fn insert_all_files_for_package_into_file_manager( package: &Package, file_manager: &mut FileManager, + overrides: &HashMap<&std::path::Path, &str>, ) { // Start off at the entry path and read all files in the parent directory. let entry_path_parent = package @@ -70,8 +83,12 @@ fn insert_all_files_for_package_into_file_manager( let paths = get_all_noir_source_in_dir(entry_path_parent) .expect("could not get all paths in the package"); for path in paths { - let source = std::fs::read_to_string(path.as_path()) - .unwrap_or_else(|_| panic!("could not read file {:?} into string", path)); + let source = if let Some(src) = overrides.get(path.as_path()) { + src.to_string() + } else { + std::fs::read_to_string(path.as_path()) + .unwrap_or_else(|_| panic!("could not read file {:?} into string", path)) + }; file_manager.add_file_with_source(path.as_path(), source); } @@ -87,7 +104,11 @@ fn insert_all_files_for_packages_dependencies_into_file_manager( for (_, dep) in package.dependencies.iter() { match dep { Dependency::Local { package } | Dependency::Remote { package } => { - insert_all_files_for_package_into_file_manager(package, file_manager); + insert_all_files_for_package_into_file_manager( + package, + file_manager, + &HashMap::new(), + ); insert_all_files_for_packages_dependencies_into_file_manager(package, file_manager); } }