Invoke remote Prettier commands

This commit is contained in:
Kirill Bulatov 2023-09-26 18:32:55 +03:00
parent faf1d38a6d
commit 2ec2036c2f
3 changed files with 172 additions and 135 deletions

View File

@ -9,6 +9,7 @@ use fs::Fs;
use gpui::{AsyncAppContext, ModelHandle};
use language::language_settings::language_settings;
use language::{Buffer, BundledFormatter, Diff};
use lsp::request::Request;
use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId};
use node_runtime::NodeRuntime;
use serde::{Deserialize, Serialize};
@ -216,12 +217,35 @@ impl Prettier {
}))
}
pub async fn invoke(
&self,
buffer: &ModelHandle<Buffer>,
buffer_path: Option<PathBuf>,
method: &str,
cx: &AsyncAppContext,
) -> anyhow::Result<Option<Diff>> {
match method {
Format::METHOD => self
.format(buffer, buffer_path, cx)
.await
.context("invoke method")
.map(Some),
ClearCache::METHOD => {
self.clear_cache().await.context("invoke method")?;
Ok(None)
}
unknown => anyhow::bail!("Unknown method {unknown}"),
}
}
pub async fn format(
&self,
buffer: &ModelHandle<Buffer>,
buffer_path: Option<PathBuf>,
cx: &AsyncAppContext,
) -> anyhow::Result<Diff> {
match self {
Self::Local(local) => {
let params = buffer.read_with(cx, |buffer, cx| {
let buffer_language = buffer.language();
let parsers_with_plugins = buffer_language
@ -342,22 +366,27 @@ impl Prettier {
},
})
}).context("prettier params calculation")?;
let response = self
.server()
.expect("TODO kb split into local and remote")
let response = local
.server
.request::<Format>(params)
.await
.context("prettier format request")?;
let diff_task = buffer.read_with(cx, |buffer, cx| buffer.diff(response.text, cx));
Ok(diff_task.await)
}
Self::Remote(remote) => todo!("TODO kb"),
}
}
pub async fn clear_cache(&self) -> anyhow::Result<()> {
self.server()
.expect("TODO kb split into local and remote")
match self {
Self::Local(local) => local
.server
.request::<ClearCache>(())
.await
.context("prettier clear cache")
.context("prettier clear cache"),
Self::Remote(remote) => todo!("TODO kb"),
}
}
pub fn server(&self) -> Option<&Arc<LanguageServer>> {

View File

@ -8353,10 +8353,13 @@ impl Project {
});
let diff = prettier
.format(&buffer, buffer_path, &cx)
.invoke(&buffer, buffer_path, &envelope.payload.method, &cx)
.await
.context("handle buffer formatting")?;
todo!("TODO kb serialize diff")
.with_context(|| format!("prettier invoke method {}", &envelope.payload.method))?;
Ok(proto::InvokePrettierForBufferResponse {
diff: todo!("TODO kb serialize diff"),
})
}
fn prettier_instance_for_buffer(

View File

@ -1578,9 +1578,14 @@ message InvokePrettierForBuffer {
uint64 buffer_id = 3;
optional uint64 worktree_id = 4;
string method = 5;
optional string command_parameters = 6;
}
message InvokePrettierForBufferResponse {
optional string diff = 1;
optional Diff diff = 1;
}
message Diff {
VectorClockEntry version = 1;
string line_ending = 2;
string edits = 3;
}