Use async_trait for LspCommand
This commit is contained in:
parent
80bca57bfa
commit
de87fa58f6
1 changed files with 71 additions and 81 deletions
|
@ -1,7 +1,7 @@
|
||||||
use crate::{Project, ProjectTransaction};
|
use crate::{Project, ProjectTransaction};
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
|
use async_trait::async_trait;
|
||||||
use client::{proto, PeerId};
|
use client::{proto, PeerId};
|
||||||
use futures::{future::LocalBoxFuture, FutureExt};
|
|
||||||
use gpui::{AppContext, AsyncAppContext, ModelContext, ModelHandle};
|
use gpui::{AppContext, AsyncAppContext, ModelContext, ModelHandle};
|
||||||
use language::{
|
use language::{
|
||||||
proto::deserialize_anchor, range_from_lsp, Anchor, Bias, Buffer, PointUtf16, ToLspPosition,
|
proto::deserialize_anchor, range_from_lsp, Anchor, Bias, Buffer, PointUtf16, ToLspPosition,
|
||||||
|
@ -9,6 +9,7 @@ use language::{
|
||||||
};
|
};
|
||||||
use std::{ops::Range, path::Path};
|
use std::{ops::Range, path::Path};
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
pub(crate) trait LspCommand: 'static + Sized {
|
pub(crate) trait LspCommand: 'static + Sized {
|
||||||
type Response: 'static + Default + Send;
|
type Response: 'static + Default + Send;
|
||||||
type LspRequest: 'static + Send + lsp::request::Request;
|
type LspRequest: 'static + Send + lsp::request::Request;
|
||||||
|
@ -19,13 +20,13 @@ pub(crate) trait LspCommand: 'static + Sized {
|
||||||
path: &Path,
|
path: &Path,
|
||||||
cx: &AppContext,
|
cx: &AppContext,
|
||||||
) -> <Self::LspRequest as lsp::request::Request>::Params;
|
) -> <Self::LspRequest as lsp::request::Request>::Params;
|
||||||
fn response_from_lsp(
|
async fn response_from_lsp(
|
||||||
self,
|
self,
|
||||||
message: <Self::LspRequest as lsp::request::Request>::Result,
|
message: <Self::LspRequest as lsp::request::Request>::Result,
|
||||||
project: ModelHandle<Project>,
|
project: ModelHandle<Project>,
|
||||||
buffer: ModelHandle<Buffer>,
|
buffer: ModelHandle<Buffer>,
|
||||||
cx: AsyncAppContext,
|
cx: AsyncAppContext,
|
||||||
) -> LocalBoxFuture<'static, Result<Self::Response>>;
|
) -> Result<Self::Response>;
|
||||||
|
|
||||||
fn to_proto(
|
fn to_proto(
|
||||||
&self,
|
&self,
|
||||||
|
@ -48,26 +49,26 @@ pub(crate) trait LspCommand: 'static + Sized {
|
||||||
buffer_version: &clock::Global,
|
buffer_version: &clock::Global,
|
||||||
cx: &mut ModelContext<Project>,
|
cx: &mut ModelContext<Project>,
|
||||||
) -> <Self::ProtoRequest as proto::RequestMessage>::Response;
|
) -> <Self::ProtoRequest as proto::RequestMessage>::Response;
|
||||||
fn response_from_proto(
|
async fn response_from_proto(
|
||||||
self,
|
self,
|
||||||
message: <Self::ProtoRequest as proto::RequestMessage>::Response,
|
message: <Self::ProtoRequest as proto::RequestMessage>::Response,
|
||||||
project: ModelHandle<Project>,
|
project: ModelHandle<Project>,
|
||||||
buffer: ModelHandle<Buffer>,
|
buffer: ModelHandle<Buffer>,
|
||||||
cx: AsyncAppContext,
|
cx: AsyncAppContext,
|
||||||
) -> LocalBoxFuture<'static, Result<Self::Response>>;
|
) -> Result<Self::Response>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct PrepareRename {
|
pub(crate) struct PrepareRename {
|
||||||
pub position: PointUtf16,
|
pub position: PointUtf16,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct PerformRename {
|
pub(crate) struct PerformRename {
|
||||||
pub position: PointUtf16,
|
pub position: PointUtf16,
|
||||||
pub new_name: String,
|
pub new_name: String,
|
||||||
pub push_to_history: bool,
|
pub push_to_history: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
impl LspCommand for PrepareRename {
|
impl LspCommand for PrepareRename {
|
||||||
type Response = Option<Range<Anchor>>;
|
type Response = Option<Range<Anchor>>;
|
||||||
type LspRequest = lsp::request::PrepareRenameRequest;
|
type LspRequest = lsp::request::PrepareRenameRequest;
|
||||||
|
@ -82,31 +83,28 @@ impl LspCommand for PrepareRename {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn response_from_lsp(
|
async fn response_from_lsp(
|
||||||
self,
|
self,
|
||||||
message: Option<lsp::PrepareRenameResponse>,
|
message: Option<lsp::PrepareRenameResponse>,
|
||||||
_: ModelHandle<Project>,
|
_: ModelHandle<Project>,
|
||||||
buffer: ModelHandle<Buffer>,
|
buffer: ModelHandle<Buffer>,
|
||||||
cx: AsyncAppContext,
|
cx: AsyncAppContext,
|
||||||
) -> LocalBoxFuture<'static, Result<Option<Range<Anchor>>>> {
|
) -> Result<Option<Range<Anchor>>> {
|
||||||
async move {
|
buffer.read_with(&cx, |buffer, _| {
|
||||||
Ok(message.and_then(|result| match result {
|
if let Some(
|
||||||
lsp::PrepareRenameResponse::Range(range)
|
lsp::PrepareRenameResponse::Range(range)
|
||||||
| lsp::PrepareRenameResponse::RangeWithPlaceholder { range, .. } => buffer
|
| lsp::PrepareRenameResponse::RangeWithPlaceholder { range, .. },
|
||||||
.read_with(&cx, |buffer, _| {
|
) = message
|
||||||
let range = range_from_lsp(range);
|
{
|
||||||
if buffer.clip_point_utf16(range.start, Bias::Left) == range.start
|
let Range { start, end } = range_from_lsp(range);
|
||||||
&& buffer.clip_point_utf16(range.end, Bias::Left) == range.end
|
if buffer.clip_point_utf16(start, Bias::Left) == start
|
||||||
{
|
&& buffer.clip_point_utf16(end, Bias::Left) == end
|
||||||
Some(buffer.anchor_after(range.start)..buffer.anchor_before(range.end))
|
{
|
||||||
} else {
|
return Ok(Some(buffer.anchor_after(start)..buffer.anchor_before(end)));
|
||||||
None
|
}
|
||||||
}
|
}
|
||||||
}),
|
Ok(None)
|
||||||
_ => None,
|
})
|
||||||
}))
|
|
||||||
}
|
|
||||||
.boxed_local()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_proto(
|
fn to_proto(
|
||||||
|
@ -166,31 +164,29 @@ impl LspCommand for PrepareRename {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn response_from_proto(
|
async fn response_from_proto(
|
||||||
self,
|
self,
|
||||||
message: proto::PrepareRenameResponse,
|
message: proto::PrepareRenameResponse,
|
||||||
_: ModelHandle<Project>,
|
_: ModelHandle<Project>,
|
||||||
buffer: ModelHandle<Buffer>,
|
buffer: ModelHandle<Buffer>,
|
||||||
mut cx: AsyncAppContext,
|
mut cx: AsyncAppContext,
|
||||||
) -> LocalBoxFuture<'static, Result<Option<Range<Anchor>>>> {
|
) -> Result<Option<Range<Anchor>>> {
|
||||||
async move {
|
if message.can_rename {
|
||||||
if message.can_rename {
|
buffer
|
||||||
buffer
|
.update(&mut cx, |buffer, _| {
|
||||||
.update(&mut cx, |buffer, _| {
|
buffer.wait_for_version(message.version.into())
|
||||||
buffer.wait_for_version(message.version.into())
|
})
|
||||||
})
|
.await;
|
||||||
.await;
|
let start = message.start.and_then(deserialize_anchor);
|
||||||
let start = message.start.and_then(deserialize_anchor);
|
let end = message.end.and_then(deserialize_anchor);
|
||||||
let end = message.end.and_then(deserialize_anchor);
|
Ok(start.zip(end).map(|(start, end)| start..end))
|
||||||
Ok(start.zip(end).map(|(start, end)| start..end))
|
} else {
|
||||||
} else {
|
Ok(None)
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.boxed_local()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait(?Send)]
|
||||||
impl LspCommand for PerformRename {
|
impl LspCommand for PerformRename {
|
||||||
type Response = ProjectTransaction;
|
type Response = ProjectTransaction;
|
||||||
type LspRequest = lsp::request::Rename;
|
type LspRequest = lsp::request::Rename;
|
||||||
|
@ -209,39 +205,36 @@ impl LspCommand for PerformRename {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn response_from_lsp(
|
async fn response_from_lsp(
|
||||||
self,
|
self,
|
||||||
message: Option<lsp::WorkspaceEdit>,
|
message: Option<lsp::WorkspaceEdit>,
|
||||||
project: ModelHandle<Project>,
|
project: ModelHandle<Project>,
|
||||||
buffer: ModelHandle<Buffer>,
|
buffer: ModelHandle<Buffer>,
|
||||||
mut cx: AsyncAppContext,
|
mut cx: AsyncAppContext,
|
||||||
) -> LocalBoxFuture<'static, Result<ProjectTransaction>> {
|
) -> Result<ProjectTransaction> {
|
||||||
async move {
|
if let Some(edit) = message {
|
||||||
if let Some(edit) = message {
|
let (language_name, language_server) = buffer.read_with(&cx, |buffer, _| {
|
||||||
let (language_name, language_server) = buffer.read_with(&cx, |buffer, _| {
|
let language = buffer
|
||||||
let language = buffer
|
.language()
|
||||||
.language()
|
.ok_or_else(|| anyhow!("buffer's language was removed"))?;
|
||||||
.ok_or_else(|| anyhow!("buffer's language was removed"))?;
|
let language_server = buffer
|
||||||
let language_server = buffer
|
.language_server()
|
||||||
.language_server()
|
.cloned()
|
||||||
.cloned()
|
.ok_or_else(|| anyhow!("buffer's language server was removed"))?;
|
||||||
.ok_or_else(|| anyhow!("buffer's language server was removed"))?;
|
Ok::<_, anyhow::Error>((language.name().to_string(), language_server))
|
||||||
Ok::<_, anyhow::Error>((language.name().to_string(), language_server))
|
})?;
|
||||||
})?;
|
Project::deserialize_workspace_edit(
|
||||||
Project::deserialize_workspace_edit(
|
project,
|
||||||
project,
|
edit,
|
||||||
edit,
|
self.push_to_history,
|
||||||
self.push_to_history,
|
language_name,
|
||||||
language_name,
|
language_server,
|
||||||
language_server,
|
&mut cx,
|
||||||
&mut cx,
|
)
|
||||||
)
|
.await
|
||||||
.await
|
} else {
|
||||||
} else {
|
Ok(ProjectTransaction::default())
|
||||||
Ok(ProjectTransaction::default())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.boxed_local()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_proto(
|
fn to_proto(
|
||||||
|
@ -300,23 +293,20 @@ impl LspCommand for PerformRename {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn response_from_proto(
|
async fn response_from_proto(
|
||||||
self,
|
self,
|
||||||
message: proto::PerformRenameResponse,
|
message: proto::PerformRenameResponse,
|
||||||
project: ModelHandle<Project>,
|
project: ModelHandle<Project>,
|
||||||
_: ModelHandle<Buffer>,
|
_: ModelHandle<Buffer>,
|
||||||
mut cx: AsyncAppContext,
|
mut cx: AsyncAppContext,
|
||||||
) -> LocalBoxFuture<'static, Result<ProjectTransaction>> {
|
) -> Result<ProjectTransaction> {
|
||||||
async move {
|
let message = message
|
||||||
let message = message
|
.transaction
|
||||||
.transaction
|
.ok_or_else(|| anyhow!("missing transaction"))?;
|
||||||
.ok_or_else(|| anyhow!("missing transaction"))?;
|
project
|
||||||
project
|
.update(&mut cx, |project, cx| {
|
||||||
.update(&mut cx, |project, cx| {
|
project.deserialize_project_transaction(message, self.push_to_history, cx)
|
||||||
project.deserialize_project_transaction(message, self.push_to_history, cx)
|
})
|
||||||
})
|
.await
|
||||||
.await
|
|
||||||
}
|
|
||||||
.boxed_local()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue