Skip to content

Commit 9540f3c

Browse files
committed
Send workspace/didChangeWatchedFiles.
Close #347.
1 parent 7cfd530 commit 9540f3c

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

src/languageclient.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,6 @@ impl State {
16961696
serde_json::from_value(r.register_options.clone().unwrap_or_default())?;
16971697
if let Some(ref mut watcher) = self.watcher {
16981698
for w in opt.watchers {
1699-
warn!("Start watching {}", w.glob_pattern);
17001699
watcher.watch(w.glob_pattern, notify::RecursiveMode::NonRecursive)?;
17011700
}
17021701
}
@@ -2425,22 +2424,54 @@ impl State {
24252424
Ok(())
24262425
}
24272426

2428-
pub fn check_fs_notify(&mut self) -> () {
2427+
pub fn check_fs_notify(&mut self) -> Result<()> {
24292428
if self.watcher.is_some() {
2429+
let mut events = vec![];
24302430
loop {
24312431
let result = self.watcher_rx.try_recv();
24322432
let event = match result {
24332433
Ok(event) => event,
24342434
Err(err) => {
24352435
if let TryRecvError::Disconnected = err {
2436-
warn!("File system notification channel disconnected!");
2436+
bail!("File system notification channel disconnected!");
24372437
}
24382438
break;
24392439
}
24402440
};
2441+
events.push(event);
2442+
}
2443+
2444+
if events.is_empty() {
2445+
return Ok(());
2446+
}
24412447

2442-
warn!("File system event: {:?}", event);
2448+
let mut changes = vec![];
2449+
for e in events {
2450+
if let Ok(c) = e.to_lsp() {
2451+
changes.extend(c);
2452+
}
24432453
}
2454+
2455+
use DidChangeWatchedFilesParams as P;
2456+
self.workspace_didChangeWatchedFiles(&P { changes }.to_params()?)?;
24442457
}
2458+
2459+
Ok(())
2460+
}
2461+
2462+
pub fn workspace_didChangeWatchedFiles(&mut self, params: &Option<Params>) -> Result<()> {
2463+
info!("Begin {}", lsp::notification::DidChangeWatchedFiles::METHOD);
2464+
let (languageId,): (String,) = self.gather_args([VimVar::LanguageId].as_ref(), params)?;
2465+
2466+
let params: DidChangeWatchedFilesParams =
2467+
serde_json::from_value(params.clone().to_value())?;
2468+
self.notify(
2469+
Some(&languageId),
2470+
lsp::notification::DidChangeWatchedFiles::METHOD,
2471+
params,
2472+
)?;
2473+
2474+
info!("End {}", lsp::notification::DidChangeWatchedFiles::METHOD);
2475+
Ok(())
24452476
}
24462477
}

src/types.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,48 @@ impl<T: Deref> OptionDeref<T> for Option<T> {
861861
self.as_ref().map(Deref::deref)
862862
}
863863
}
864+
865+
pub trait ToLSP<T> {
866+
fn to_lsp(&self) -> Result<T>;
867+
}
868+
869+
impl ToLSP<Vec<FileEvent>> for notify::DebouncedEvent {
870+
fn to_lsp(&self) -> Result<Vec<FileEvent>> {
871+
match *self {
872+
notify::DebouncedEvent::Create(ref p) => Ok(vec![
873+
FileEvent {
874+
uri: p.to_url()?,
875+
typ: FileChangeType::Created,
876+
},
877+
]),
878+
notify::DebouncedEvent::NoticeWrite(ref p) | notify::DebouncedEvent::Write(ref p) => {
879+
Ok(vec![
880+
FileEvent {
881+
uri: p.to_url()?,
882+
typ: FileChangeType::Changed,
883+
},
884+
])
885+
}
886+
notify::DebouncedEvent::NoticeRemove(ref p) | notify::DebouncedEvent::Remove(ref p) => {
887+
Ok(vec![
888+
FileEvent {
889+
uri: p.to_url()?,
890+
typ: FileChangeType::Deleted,
891+
},
892+
])
893+
}
894+
notify::DebouncedEvent::Rename(ref p1, ref p2) => Ok(vec![
895+
FileEvent {
896+
uri: p1.to_url()?,
897+
typ: FileChangeType::Deleted,
898+
},
899+
FileEvent {
900+
uri: p2.to_url()?,
901+
typ: FileChangeType::Created,
902+
},
903+
]),
904+
notify::DebouncedEvent::Chmod(_) | notify::DebouncedEvent::Rescan => Ok(vec![]),
905+
ref e @ notify::DebouncedEvent::Error(_, _) => Err(format_err!("{:?}", e)),
906+
}
907+
}
908+
}

src/vim.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ impl State {
7979
}
8080
}
8181

82-
self.check_fs_notify();
82+
if let Err(err) = self.check_fs_notify() {
83+
warn!("{:?}", err);
84+
}
8385
}
8486
}
8587

0 commit comments

Comments
 (0)