Finalize the command

This commit is contained in:
Kirill Bulatov 2023-12-11 12:01:04 +02:00
parent e3fc810b3d
commit 3694265b6b
9 changed files with 126 additions and 53 deletions

View file

@ -1,11 +1,13 @@
use std::{path::Path, sync::Arc};
use anyhow::Context;
use async_trait::async_trait;
use gpui::{AppContext, AsyncAppContext, Model};
use language::Buffer;
use language::{point_to_lsp, proto::deserialize_anchor, Buffer};
use lsp::{LanguageServer, LanguageServerId};
use rpc::proto::{self, PeerId};
use serde::{Deserialize, Serialize};
use text::{PointUtf16, ToPointUtf16};
use crate::{lsp_command::LspCommand, Project};
@ -31,9 +33,16 @@ pub struct ExpandedMacro {
pub expansion: String,
}
pub struct ExpandMacro {}
impl ExpandedMacro {
pub fn is_empty(&self) -> bool {
self.name.is_empty() && self.expansion.is_empty()
}
}
pub struct ExpandMacro {
pub position: PointUtf16,
}
// TODO kb
#[async_trait(?Send)]
impl LspCommand for ExpandMacro {
type Response = ExpandedMacro;
@ -43,55 +52,83 @@ impl LspCommand for ExpandMacro {
fn to_lsp(
&self,
path: &Path,
buffer: &Buffer,
language_server: &Arc<LanguageServer>,
cx: &AppContext,
_: &Buffer,
_: &Arc<LanguageServer>,
_: &AppContext,
) -> ExpandMacroParams {
todo!()
ExpandMacroParams {
text_document: lsp::TextDocumentIdentifier {
uri: lsp::Url::from_file_path(path).unwrap(),
},
position: point_to_lsp(self.position),
}
}
async fn response_from_lsp(
self,
message: Option<ExpandedMacro>,
project: Model<Project>,
buffer: Model<Buffer>,
server_id: LanguageServerId,
cx: AsyncAppContext,
_: Model<Project>,
_: Model<Buffer>,
_: LanguageServerId,
_: AsyncAppContext,
) -> anyhow::Result<ExpandedMacro> {
anyhow::bail!("TODO kb")
Ok(message
.map(|message| ExpandedMacro {
name: message.name,
expansion: message.expansion,
})
.unwrap_or_default())
}
fn to_proto(&self, project_id: u64, buffer: &Buffer) -> proto::LspExtExpandMacro {
todo!()
proto::LspExtExpandMacro {
project_id,
buffer_id: buffer.remote_id(),
position: Some(language::proto::serialize_anchor(
&buffer.anchor_before(self.position),
)),
}
}
async fn from_proto(
message: Self::ProtoRequest,
project: Model<Project>,
_: Model<Project>,
buffer: Model<Buffer>,
cx: AsyncAppContext,
mut cx: AsyncAppContext,
) -> anyhow::Result<Self> {
todo!()
let position = message
.position
.and_then(deserialize_anchor)
.context("invalid position")?;
Ok(Self {
position: buffer.update(&mut cx, |buffer, _| position.to_point_utf16(buffer))?,
})
}
fn response_to_proto(
response: ExpandedMacro,
project: &mut Project,
peer_id: PeerId,
buffer_version: &clock::Global,
cx: &mut AppContext,
_: &mut Project,
_: PeerId,
_: &clock::Global,
_: &mut AppContext,
) -> proto::LspExtExpandMacroResponse {
todo!()
proto::LspExtExpandMacroResponse {
name: response.name,
expansion: response.expansion,
}
}
async fn response_from_proto(
self,
message: proto::LspExtExpandMacroResponse,
project: Model<Project>,
buffer: Model<Buffer>,
cx: AsyncAppContext,
_: Model<Project>,
_: Model<Buffer>,
_: AsyncAppContext,
) -> anyhow::Result<ExpandedMacro> {
todo!()
Ok(ExpandedMacro {
name: message.name,
expansion: message.expansion,
})
}
fn buffer_id_from_proto(message: &proto::LspExtExpandMacro) -> u64 {