Skip to content

Commit

Permalink
Watch aux directory for changes by default
Browse files Browse the repository at this point in the history
See #563.
  • Loading branch information
pfoerster committed Feb 18, 2022
1 parent 4dd2518 commit 1090831
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 22 deletions.
23 changes: 20 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use lsp_types::{
*,
};
use notification::DidCloseTextDocument;
use notify::RecursiveMode;
use request::{
Completion, DocumentHighlightRequest, DocumentSymbolRequest, HoverRequest,
ResolveCompletionItem, WorkspaceSymbol,
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -846,14 +855,22 @@ impl Server {
}
}

fn pull_and_reparse(
fn pull_and_reparse_all(
req_queue: Arc<Mutex<lsp_server::ReqQueue<IncomingData, req_queue::OutgoingData>>>,
sender: Sender<Message>,
context: Arc<ServerContext>,
workspace: Arc<dyn Workspace>,
) {
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),
Expand Down
7 changes: 5 additions & 2 deletions src/workspace/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fs, path::PathBuf, sync::Arc};

use anyhow::Result;
use notify::RecursiveMode;

use crate::{DocumentLanguage, Uri};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -83,4 +84,6 @@ pub trait Workspace: Send + Sync {
fn is_open(&self, uri: &Uri) -> bool;

fn subset(&self, uri: Arc<Uri>) -> Option<WorkspaceSubset>;

fn watch(&self, path: PathBuf, mode: RecursiveMode) -> Result<()>;
}
8 changes: 7 additions & 1 deletion src/workspace/children_expand.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -89,4 +91,8 @@ impl<W: Workspace> Workspace for ChildrenExpander<W> {
fn subset(&self, uri: Arc<Uri>) -> Option<WorkspaceSubset> {
self.workspace.subset(uri)
}

fn watch(&self, path: PathBuf, mode: RecursiveMode) -> Result<()> {
self.workspace.watch(path, mode)
}
}
8 changes: 7 additions & 1 deletion src/workspace/parent_expand.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -96,6 +98,10 @@ where
fn subset(&self, uri: Arc<Uri>) -> Option<WorkspaceSubset> {
self.workspace.subset(uri)
}

fn watch(&self, path: PathBuf, mode: RecursiveMode) -> Result<()> {
self.workspace.watch(path, mode)
}
}

impl<W> ParentExpander<W>
Expand Down
11 changes: 10 additions & 1 deletion src/workspace/storage.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -133,4 +138,8 @@ impl Workspace for Storage {

Some(WorkspaceSubset { documents })
}

fn watch(&self, _path: PathBuf, _mode: RecursiveMode) -> Result<()> {
Ok(())
}
}
29 changes: 15 additions & 14 deletions src/workspace/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,11 @@ impl<W: Workspace> Workspace for DocumentWatcher<W> {
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
);
}
}
}
Expand Down Expand Up @@ -104,4 +95,14 @@ impl<W: Workspace> Workspace for DocumentWatcher<W> {
fn subset(&self, uri: Arc<Uri>) -> Option<WorkspaceSubset> {
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(())
}
}

0 comments on commit 1090831

Please sign in to comment.