-
-
Notifications
You must be signed in to change notification settings - Fork 934
LSP: cleanup when a client has closed all files in a project #5381
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
10e14ce
9ec4e0f
0c7a705
010ce04
13033e3
1143530
6cbf694
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,9 +90,13 @@ impl Request { | |
| #[derive(Debug)] | ||
| pub enum Notification { | ||
| /// A Gleam file has been modified in memory, and the new text is provided. | ||
| SourceFileChangedInMemory { path: Utf8PathBuf, text: String }, | ||
| SourceFileChangedInMemory { | ||
| path: Utf8PathBuf, | ||
| opened: bool, | ||
|
||
| text: String, | ||
| }, | ||
| /// A Gleam file has been saved or closed in the editor. | ||
| SourceFileMatchesDisc { path: Utf8PathBuf }, | ||
| SourceFileMatchesDisc { path: Utf8PathBuf, closed: bool }, | ||
TrippleCCC marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// gleam.toml has changed. | ||
| ConfigFileChanged { path: Utf8PathBuf }, | ||
| /// It's time to compile all open projects. | ||
|
|
@@ -107,6 +111,7 @@ impl Notification { | |
| let notification = Notification::SourceFileChangedInMemory { | ||
| path: super::path(¶ms.text_document.uri), | ||
| text: params.text_document.text, | ||
| opened: true, | ||
| }; | ||
| Some(Message::Notification(notification)) | ||
| } | ||
|
|
@@ -115,6 +120,7 @@ impl Notification { | |
| let notification = Notification::SourceFileChangedInMemory { | ||
| path: super::path(¶ms.text_document.uri), | ||
| text: params.content_changes.into_iter().next_back()?.text, | ||
| opened: false, | ||
|
||
| }; | ||
| Some(Message::Notification(notification)) | ||
| } | ||
|
|
@@ -123,13 +129,15 @@ impl Notification { | |
| let params = cast_notification::<DidSaveTextDocument>(notification); | ||
| let notification = Notification::SourceFileMatchesDisc { | ||
| path: super::path(¶ms.text_document.uri), | ||
| closed: false, | ||
| }; | ||
| Some(Message::Notification(notification)) | ||
| } | ||
| "textDocument/didClose" => { | ||
| let params = cast_notification::<DidCloseTextDocument>(notification); | ||
| let notification = Notification::SourceFileMatchesDisc { | ||
| path: super::path(¶ms.text_document.uri), | ||
| closed: true, | ||
| }; | ||
| Some(Message::Notification(notification)) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ pub struct LanguageServer<'a, IO> { | |
| outside_of_project_feedback: FeedbackBookKeeper, | ||
| router: Router<IO, ConnectionProgressReporter<'a>>, | ||
| changed_projects: HashSet<Utf8PathBuf>, | ||
| open_files: HashSet<Utf8PathBuf>, | ||
TrippleCCC marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| io: FileSystemProxy<IO>, | ||
| } | ||
|
|
||
|
|
@@ -63,6 +64,7 @@ where | |
| connection: connection.into(), | ||
| initialise_params, | ||
| changed_projects: HashSet::new(), | ||
| open_files: HashSet::new(), | ||
| outside_of_project_feedback: FeedbackBookKeeper::default(), | ||
| router, | ||
| io, | ||
|
|
@@ -131,12 +133,35 @@ where | |
| .expect("channel send LSP response") | ||
| } | ||
|
|
||
| fn cleanup_engine(&mut self, path: &Utf8PathBuf) { | ||
| _ = self.open_files.remove(path); | ||
| if let Some(project_path) = self.router.project_path(path) { | ||
| if self | ||
| .open_files | ||
| .iter() | ||
| .any(|path| path.starts_with(&project_path)) | ||
| { | ||
| self.router.delete_engine_for_path(&project_path); | ||
| } | ||
| } | ||
TrippleCCC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| fn handle_notification(&mut self, notification: Notification) { | ||
| let feedback = match notification { | ||
| Notification::CompilePlease => self.compile_please(), | ||
| Notification::SourceFileMatchesDisc { path } => self.discard_in_memory_cache(path), | ||
| Notification::SourceFileChangedInMemory { path, text } => { | ||
| self.cache_file_in_memory(path, text) | ||
| Notification::SourceFileMatchesDisc { path, closed } => { | ||
| let feedback = self.discard_in_memory_cache(&path); | ||
| if closed { | ||
| self.cleanup_engine(&path); | ||
| } | ||
| feedback | ||
| } | ||
| Notification::SourceFileChangedInMemory { path, opened, text } => { | ||
| let feedback = self.cache_file_in_memory(&path, text); | ||
| if opened { | ||
| _ = self.open_files.insert(path); | ||
| } | ||
|
||
| feedback | ||
| } | ||
| Notification::ConfigFileChanged { path } => self.watched_files_changed(path), | ||
| }; | ||
|
|
@@ -428,17 +453,17 @@ where | |
| self.respond_with_engine(path, |engine| engine.find_references(params)) | ||
| } | ||
|
|
||
| fn cache_file_in_memory(&mut self, path: Utf8PathBuf, text: String) -> Feedback { | ||
| self.project_changed(&path); | ||
| if let Err(error) = self.io.write_mem_cache(&path, &text) { | ||
| fn cache_file_in_memory(&mut self, path: &Utf8PathBuf, text: String) -> Feedback { | ||
| self.project_changed(path); | ||
| if let Err(error) = self.io.write_mem_cache(path, &text) { | ||
| return self.outside_of_project_feedback.error(error); | ||
| } | ||
| Feedback::none() | ||
| } | ||
|
|
||
| fn discard_in_memory_cache(&mut self, path: Utf8PathBuf) -> Feedback { | ||
| self.project_changed(&path); | ||
| if let Err(error) = self.io.delete_mem_cache(&path) { | ||
| fn discard_in_memory_cache(&mut self, path: &Utf8PathBuf) -> Feedback { | ||
| self.project_changed(path); | ||
| if let Err(error) = self.io.delete_mem_cache(path) { | ||
| return self.outside_of_project_feedback.error(error); | ||
| } | ||
| Feedback::none() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.