Skip to content

Commit

Permalink
Update dependencies in the background evaluator (#1948)
Browse files Browse the repository at this point in the history
The background evaluator needs to track dependencies separately, because
it doesn't share file ids with the main cache. This updates the
background dependencies when the foreground ones get updated.
  • Loading branch information
jneem authored Jun 10, 2024
1 parent c767d09 commit 9fe8b98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
33 changes: 27 additions & 6 deletions lsp/nls/src/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use anyhow::anyhow;
use codespan::FileId;
use crossbeam::channel::{bounded, Receiver, RecvTimeoutError, Sender};
use log::warn;
use lsp_types::Url;
Expand All @@ -28,6 +29,10 @@ enum Command {
text: String,
deps: Vec<Url>,
},
UpdateDeps {
uri: Url,
deps: Vec<Url>,
},
EvalFile {
uri: Url,
},
Expand Down Expand Up @@ -218,6 +223,9 @@ impl SupervisorState {
self.contents.insert(uri.clone(), text);
self.deps.insert(uri, deps);
}
Command::UpdateDeps { uri, deps } => {
self.deps.insert(uri, deps);
}
Command::EvalFile { uri } => {
if !self.banned_files.contains(&uri) {
// If we re-request an evaluation, remove the old one. (This is quadratic in the
Expand Down Expand Up @@ -292,17 +300,30 @@ impl BackgroundJobs {
}
}

pub fn update_file(&mut self, uri: Url, text: String, world: &World) {
let Ok(Some(file_id)) = world.cache.file_id(&uri) else {
return;
};
let deps = world
fn deps(&self, file_id: FileId, world: &World) -> Vec<Url> {
world
.cache
.get_imports(file_id)
.filter_map(|dep_id| world.file_uris.get(&dep_id))
.cloned()
.collect();
.collect()
}

pub fn update_file_deps(&mut self, uri: Url, world: &World) {
let Ok(Some(file_id)) = world.cache.file_id(&uri) else {
return;
};
let deps = self.deps(file_id, world);
// Ignore errors here, because if we've failed to set up a background worker
// then we just skip doing background evaluation.
let _ = self.sender.send(Command::UpdateDeps { uri, deps });
}

pub fn update_file(&mut self, uri: Url, text: String, world: &World) {
let Ok(Some(file_id)) = world.cache.file_id(&uri) else {
return;
};
let deps = self.deps(file_id, world);
// Ignore errors here, because if we've failed to set up a background worker
// then we just skip doing background evaluation.
let _ = self.sender.send(Command::UpdateFile { uri, text, deps });
Expand Down
4 changes: 4 additions & 0 deletions lsp/nls/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ impl Server {
.update_file(uri.clone(), contents, &self.world);
self.background_jobs.eval_file(uri);
for uri in invalid {
self.background_jobs
.update_file_deps(uri.clone(), &self.world);
self.background_jobs.eval_file(uri);
}
Ok(())
Expand All @@ -203,6 +205,8 @@ impl Server {
.update_file(uri.clone(), contents, &self.world);
self.background_jobs.eval_file(uri);
for uri in invalid {
self.background_jobs
.update_file_deps(uri.clone(), &self.world);
self.background_jobs.eval_file(uri);
}
Ok(())
Expand Down

0 comments on commit 9fe8b98

Please sign in to comment.