Notify language server when saving a buffer

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-10-26 11:45:59 -07:00
parent 0674e76864
commit de8218314c

View file

@ -13,6 +13,7 @@ use clock::ReplicaId;
use futures::FutureExt as _; use futures::FutureExt as _;
use gpui::{AppContext, Entity, ModelContext, MutableAppContext, Task}; use gpui::{AppContext, Entity, ModelContext, MutableAppContext, Task};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use lsp::LanguageServer;
use parking_lot::Mutex; use parking_lot::Mutex;
use postage::{prelude::Stream, sink::Sink, watch}; use postage::{prelude::Stream, sink::Sink, watch};
use rpc::proto; use rpc::proto;
@ -73,6 +74,7 @@ pub struct Snapshot {
} }
struct LanguageServerState { struct LanguageServerState {
server: Arc<LanguageServer>,
latest_snapshot: watch::Sender<Option<LanguageServerSnapshot>>, latest_snapshot: watch::Sender<Option<LanguageServerSnapshot>>,
pending_snapshots: BTreeMap<usize, LanguageServerSnapshot>, pending_snapshots: BTreeMap<usize, LanguageServerSnapshot>,
next_version: usize, next_version: usize,
@ -316,6 +318,7 @@ impl Buffer {
latest_snapshot: latest_snapshot_tx, latest_snapshot: latest_snapshot_tx,
pending_snapshots: Default::default(), pending_snapshots: Default::default(),
next_version: 0, next_version: 0,
server: server.clone(),
_maintain_server: cx.background().spawn( _maintain_server: cx.background().spawn(
async move { async move {
let mut prev_snapshot: Option<LanguageServerSnapshot> = None; let mut prev_snapshot: Option<LanguageServerSnapshot> = None;
@ -401,6 +404,25 @@ impl Buffer {
if let Some(new_file) = new_file { if let Some(new_file) = new_file {
self.file = Some(new_file); self.file = Some(new_file);
} }
if let Some(state) = &self.language_server {
cx.background()
.spawn(
state
.server
.notify::<lsp::notification::DidSaveTextDocument>(
lsp::DidSaveTextDocumentParams {
text_document: lsp::TextDocumentIdentifier {
uri: lsp::Url::from_file_path(
self.file.as_ref().unwrap().abs_path(cx).unwrap(),
)
.unwrap(),
},
text: None,
},
),
)
.detach()
}
cx.emit(Event::Saved); cx.emit(Event::Saved);
} }