Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependencies in the background evaluator #1948

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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