Skip to content

Commit 08ec37f

Browse files
committed
fix(analysis): avoid redundant startup workspace reindex
`reload_workspace_files` was introduced to support full workspace reloads, but startup now also routes through that helper. Because the helper always called `reindex()` after `update_files_by_path(...)`, bootstrapping an empty workspace analyzed the same files twice. Only run the final reindex when non-stdlib local workspace files already exist. That preserves reload behavior for existing workspaces while skipping the extra startup pass.
1 parent 1e919e0 commit 08ec37f

1 file changed

Lines changed: 63 additions & 5 deletions

File tree

  • crates/emmylua_code_analysis/src

crates/emmylua_code_analysis/src/lib.rs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub struct EmmyLuaAnalysis {
5353
pub compilation: LuaCompilation,
5454
pub diagnostic: LuaDiagnostic,
5555
pub emmyrc: Arc<Emmyrc>,
56+
#[cfg(test)]
57+
reindex_count: usize,
5658
}
5759

5860
impl EmmyLuaAnalysis {
@@ -62,6 +64,8 @@ impl EmmyLuaAnalysis {
6264
compilation: LuaCompilation::new(emmyrc.clone()),
6365
diagnostic: LuaDiagnostic::new(),
6466
emmyrc,
67+
#[cfg(test)]
68+
reindex_count: 0,
6569
}
6670
}
6771

@@ -239,17 +243,24 @@ impl EmmyLuaAnalysis {
239243
let mut kept_paths = open_paths.clone();
240244
kept_paths.extend(files.iter().map(|(path, _)| path.clone()));
241245

242-
let stale_uris = {
246+
let (had_existing_non_std_local_files, stale_uris) = {
243247
let db = self.compilation.get_db();
244248
let vfs = db.get_vfs();
245249
let module_index = db.get_module_index();
246-
vfs.get_all_local_file_ids()
250+
let mut had_existing_non_std_local_files = false;
251+
let stale_uris = vfs
252+
.get_all_local_file_ids()
247253
.into_iter()
248-
.filter(|file_id| !module_index.is_std(file_id))
254+
.filter(|file_id| {
255+
let is_non_std = !module_index.is_std(file_id);
256+
had_existing_non_std_local_files |= is_non_std;
257+
is_non_std
258+
})
249259
.filter_map(|file_id| vfs.get_file_path(&file_id).cloned())
250260
.filter(|path| !kept_paths.contains(path))
251261
.filter_map(|path| file_path_to_uri(&path))
252-
.collect::<Vec<_>>()
262+
.collect::<Vec<_>>();
263+
(had_existing_non_std_local_files, stale_uris)
253264
};
254265
for uri in &stale_uris {
255266
self.remove_file_by_uri(uri);
@@ -267,7 +278,9 @@ impl EmmyLuaAnalysis {
267278
.map(|(uri, text)| (uri, Some(text)))
268279
.collect(),
269280
);
270-
self.reindex();
281+
if had_existing_non_std_local_files {
282+
self.reindex();
283+
}
271284
stale_uris
272285
}
273286

@@ -291,6 +304,10 @@ impl EmmyLuaAnalysis {
291304
}
292305

293306
pub fn reindex(&mut self) {
307+
#[cfg(test)]
308+
{
309+
self.reindex_count += 1;
310+
}
294311
let file_ids = self.compilation.get_db().get_vfs().get_all_file_ids();
295312
self.compilation.clear_index();
296313
self.compilation.update_index(file_ids);
@@ -417,3 +434,44 @@ impl Default for EmmyLuaAnalysis {
417434

418435
unsafe impl Send for EmmyLuaAnalysis {}
419436
unsafe impl Sync for EmmyLuaAnalysis {}
437+
438+
#[cfg(test)]
439+
mod tests {
440+
use super::*;
441+
442+
#[test]
443+
fn reload_workspace_files_skips_reindex_when_bootstrapping_workspace() {
444+
let mut analysis = EmmyLuaAnalysis::new();
445+
let workspace_root = std::env::current_dir().unwrap();
446+
let file_path = workspace_root.join("__reload_workspace_startup_test.lua");
447+
analysis.add_main_workspace(workspace_root);
448+
449+
analysis.reload_workspace_files(
450+
vec![(file_path.clone(), Some("return true\n".to_string()))],
451+
Vec::new(),
452+
);
453+
454+
assert_eq!(analysis.reindex_count, 0);
455+
assert!(
456+
analysis
457+
.get_file_id(&file_path_to_uri(&file_path).unwrap())
458+
.is_some()
459+
);
460+
}
461+
462+
#[test]
463+
fn reload_workspace_files_reindexes_existing_workspace_files() {
464+
let mut analysis = EmmyLuaAnalysis::new();
465+
let workspace_root = std::env::current_dir().unwrap();
466+
let file_path = workspace_root.join("__reload_workspace_existing_test.lua");
467+
analysis.add_main_workspace(workspace_root);
468+
analysis.update_files_by_path(vec![(file_path.clone(), Some("return true\n".to_string()))]);
469+
470+
analysis.reload_workspace_files(
471+
vec![(file_path, Some("return false\n".to_string()))],
472+
Vec::new(),
473+
);
474+
475+
assert_eq!(analysis.reindex_count, 1);
476+
}
477+
}

0 commit comments

Comments
 (0)