diff --git a/src/server.rs b/src/server.rs index e0d64f68..2521faba 100644 --- a/src/server.rs +++ b/src/server.rs @@ -20,6 +20,7 @@ use lsp_types::{ *, }; use notification::DidCloseTextDocument; +use notify::RecursiveMode; use request::{ Completion, DocumentHighlightRequest, DocumentSymbolRequest, HoverRequest, ResolveCompletionItem, WorkspaceSymbol, @@ -180,7 +181,7 @@ impl Server { let workspace = Arc::clone(&self.workspace); self.pool.execute(move || { register_config_capability(&req_queue, &sender, &context.client_capabilities); - pull_and_reparse(req_queue, sender, context, workspace); + pull_and_reparse_all(req_queue, sender, context, workspace); }); Ok(()) @@ -230,10 +231,18 @@ impl Server { let context = Arc::clone(&self.context); let workspace = Arc::clone(&self.workspace); self.pool.execute(move || { - pull_and_reparse(req_queue, sender, context, workspace); + pull_and_reparse_all(req_queue, sender, context, workspace); }); } else { push_config(&self.context.options, params.settings); + if let Some(path) = { self.context.options.read().unwrap().aux_directory.clone() } { + let _ = self.workspace.watch(path, RecursiveMode::NonRecursive); + } + + let workspace = Arc::clone(&self.workspace); + self.pool.execute(move || { + reparse_all(workspace.as_ref()); + }); } Ok(()) @@ -846,7 +855,7 @@ impl Server { } } -fn pull_and_reparse( +fn pull_and_reparse_all( req_queue: Arc>>, sender: Sender, context: Arc, @@ -854,6 +863,14 @@ fn pull_and_reparse( ) { let client_capabilities = { context.client_capabilities.lock().unwrap().clone() }; pull_config(&req_queue, &sender, &context.options, &client_capabilities); + if let Some(path) = { context.options.read().unwrap().aux_directory.clone() } { + let _ = workspace.watch(path, RecursiveMode::NonRecursive); + } + + reparse_all(workspace.as_ref()); +} + +fn reparse_all(workspace: &dyn Workspace) { for document in workspace.documents() { workspace.open( Arc::clone(&document.uri), diff --git a/src/workspace/api.rs b/src/workspace/api.rs index 1ccd19d4..6a3c3d02 100644 --- a/src/workspace/api.rs +++ b/src/workspace/api.rs @@ -1,6 +1,7 @@ use std::{fs, path::PathBuf, sync::Arc}; use anyhow::Result; +use notify::RecursiveMode; use crate::{DocumentLanguage, Uri}; @@ -37,9 +38,9 @@ pub trait Workspace: Send + Sync { return Ok(self.get(&uri)); } - let data = fs::read(&path)?; - let text = String::from_utf8_lossy(&data).into_owned(); if let Some(language) = DocumentLanguage::by_path(&path) { + let data = fs::read(&path)?; + let text = String::from_utf8_lossy(&data).into_owned(); Ok(Some(self.open( uri, text, @@ -83,4 +84,6 @@ pub trait Workspace: Send + Sync { fn is_open(&self, uri: &Uri) -> bool; fn subset(&self, uri: Arc) -> Option; + + fn watch(&self, path: PathBuf, mode: RecursiveMode) -> Result<()>; } diff --git a/src/workspace/children_expand.rs b/src/workspace/children_expand.rs index 006e7b17..8fecf2d5 100644 --- a/src/workspace/children_expand.rs +++ b/src/workspace/children_expand.rs @@ -1,5 +1,7 @@ -use std::sync::Arc; +use std::{path::PathBuf, sync::Arc}; +use anyhow::Result; +use notify::RecursiveMode; use rayon::iter::{IntoParallelIterator, ParallelIterator}; use crate::{ @@ -89,4 +91,8 @@ impl Workspace for ChildrenExpander { fn subset(&self, uri: Arc) -> Option { self.workspace.subset(uri) } + + fn watch(&self, path: PathBuf, mode: RecursiveMode) -> Result<()> { + self.workspace.watch(path, mode) + } } diff --git a/src/workspace/parent_expand.rs b/src/workspace/parent_expand.rs index 33b0ca3c..0eef06ce 100644 --- a/src/workspace/parent_expand.rs +++ b/src/workspace/parent_expand.rs @@ -1,5 +1,7 @@ -use std::{fs, sync::Arc}; +use std::{fs, path::PathBuf, sync::Arc}; +use anyhow::Result; +use notify::RecursiveMode; use rayon::iter::{IntoParallelIterator, ParallelIterator}; use rustc_hash::FxHashSet; @@ -96,6 +98,10 @@ where fn subset(&self, uri: Arc) -> Option { self.workspace.subset(uri) } + + fn watch(&self, path: PathBuf, mode: RecursiveMode) -> Result<()> { + self.workspace.watch(path, mode) + } } impl ParentExpander diff --git a/src/workspace/storage.rs b/src/workspace/storage.rs index 964535a7..5f067a16 100644 --- a/src/workspace/storage.rs +++ b/src/workspace/storage.rs @@ -1,5 +1,10 @@ -use std::sync::{Arc, Mutex}; +use std::{ + path::PathBuf, + sync::{Arc, Mutex}, +}; +use anyhow::Result; +use notify::RecursiveMode; use petgraph::{graphmap::UnGraphMap, visit::Dfs}; use rustc_hash::{FxHashMap, FxHashSet}; @@ -133,4 +138,8 @@ impl Workspace for Storage { Some(WorkspaceSubset { documents }) } + + fn watch(&self, _path: PathBuf, _mode: RecursiveMode) -> Result<()> { + Ok(()) + } } diff --git a/src/workspace/watch.rs b/src/workspace/watch.rs index 2d506eb2..4cc18346 100644 --- a/src/workspace/watch.rs +++ b/src/workspace/watch.rs @@ -57,20 +57,11 @@ impl Workspace for DocumentWatcher { if document.uri.scheme() == "file" { if let Ok(mut path) = document.uri.to_file_path() { path.pop(); - let mut watched_paths = self.watched_paths.lock().unwrap(); - if !watched_paths.contains(&path) { - if let Err(why) = self - .watcher - .lock() - .unwrap() - .watch(&path, RecursiveMode::NonRecursive) - { - warn!( - "Failed to watch folder of document \"{}\": {}", - document.uri, why - ); - } - watched_paths.insert(path); + if let Err(why) = self.watch(path, RecursiveMode::NonRecursive) { + warn!( + "Failed to watch folder of document \"{}\": {}", + document.uri, why + ); } } } @@ -104,4 +95,14 @@ impl Workspace for DocumentWatcher { fn subset(&self, uri: Arc) -> Option { self.workspace.subset(uri) } + + fn watch(&self, path: PathBuf, mode: RecursiveMode) -> Result<()> { + let mut watched_paths = self.watched_paths.lock().unwrap(); + if !watched_paths.contains(&path) { + self.watcher.lock().unwrap().watch(&path, mode)?; + watched_paths.insert(path); + } + + Ok(()) + } }