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 anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use client::{proto, PeerId};
|
||||
use futures::{future::LocalBoxFuture, FutureExt};
|
||||
use gpui::{AppContext, AsyncAppContext, ModelContext, ModelHandle};
|
||||
use language::{
|
||||
proto::deserialize_anchor, range_from_lsp, Anchor, Bias, Buffer, PointUtf16, ToLspPosition,
|
||||
|
@ -9,6 +9,7 @@ use language::{
|
|||
};
|
||||
use std::{ops::Range, path::Path};
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub(crate) trait LspCommand: 'static + Sized {
|
||||
type Response: 'static + Default + Send;
|
||||
type LspRequest: 'static + Send + lsp::request::Request;
|
||||
|
@ -19,13 +20,13 @@ pub(crate) trait LspCommand: 'static + Sized {
|
|||
path: &Path,
|
||||
cx: &AppContext,
|
||||
) -> <Self::LspRequest as lsp::request::Request>::Params;
|
||||
fn response_from_lsp(
|
||||
async fn response_from_lsp(
|
||||
self,
|
||||
message: <Self::LspRequest as lsp::request::Request>::Result,
|
||||
project: ModelHandle<Project>,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
cx: AsyncAppContext,
|
||||
) -> LocalBoxFuture<'static, Result<Self::Response>>;
|
||||
) -> Result<Self::Response>;
|
||||
|
||||
fn to_proto(
|
||||
&self,
|
||||
|
@ -48,26 +49,26 @@ pub(crate) trait LspCommand: 'static + Sized {
|
|||
buffer_version: &clock::Global,
|
||||
cx: &mut ModelContext<Project>,
|
||||
) -> <Self::ProtoRequest as proto::RequestMessage>::Response;
|
||||
fn response_from_proto(
|
||||
async fn response_from_proto(
|
||||
self,
|
||||
message: <Self::ProtoRequest as proto::RequestMessage>::Response,
|
||||
project: ModelHandle<Project>,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
cx: AsyncAppContext,
|
||||
) -> LocalBoxFuture<'static, Result<Self::Response>>;
|
||||
) -> Result<Self::Response>;
|
||||
}
|
||||
|
||||
pub(crate) struct PrepareRename {
|
||||
pub position: PointUtf16,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct PerformRename {
|
||||
pub position: PointUtf16,
|
||||
pub new_name: String,
|
||||
pub push_to_history: bool,
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl LspCommand for PrepareRename {
|
||||
type Response = Option<Range<Anchor>>;
|
||||
type LspRequest = lsp::request::PrepareRenameRequest;
|
||||
|
@ -82,31 +83,28 @@ impl LspCommand for PrepareRename {
|
|||
}
|
||||
}
|
||||
|
||||
fn response_from_lsp(
|
||||
async fn response_from_lsp(
|
||||
self,
|
||||
message: Option<lsp::PrepareRenameResponse>,
|
||||
_: ModelHandle<Project>,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
cx: AsyncAppContext,
|
||||
) -> LocalBoxFuture<'static, Result<Option<Range<Anchor>>>> {
|
||||
async move {
|
||||
Ok(message.and_then(|result| match result {
|
||||
) -> Result<Option<Range<Anchor>>> {
|
||||
buffer.read_with(&cx, |buffer, _| {
|
||||
if let Some(
|
||||
lsp::PrepareRenameResponse::Range(range)
|
||||
| lsp::PrepareRenameResponse::RangeWithPlaceholder { range, .. } => buffer
|
||||
.read_with(&cx, |buffer, _| {
|
||||
let range = range_from_lsp(range);
|
||||
if buffer.clip_point_utf16(range.start, Bias::Left) == range.start
|
||||
&& buffer.clip_point_utf16(range.end, Bias::Left) == range.end
|
||||
| lsp::PrepareRenameResponse::RangeWithPlaceholder { range, .. },
|
||||
) = message
|
||||
{
|
||||
Some(buffer.anchor_after(range.start)..buffer.anchor_before(range.end))
|
||||
} else {
|
||||
None
|
||||
let Range { start, end } = range_from_lsp(range);
|
||||
if buffer.clip_point_utf16(start, Bias::Left) == start
|
||||
&& buffer.clip_point_utf16(end, Bias::Left) == end
|
||||
{
|
||||
return Ok(Some(buffer.anchor_after(start)..buffer.anchor_before(end)));
|
||||
}
|
||||
}),
|
||||
_ => None,
|
||||
}))
|
||||
}
|
||||
.boxed_local()
|
||||
Ok(None)
|
||||
})
|
||||
}
|
||||
|
||||
fn to_proto(
|
||||
|
@ -166,14 +164,13 @@ impl LspCommand for PrepareRename {
|
|||
}
|
||||
}
|
||||
|
||||
fn response_from_proto(
|
||||
async fn response_from_proto(
|
||||
self,
|
||||
message: proto::PrepareRenameResponse,
|
||||
_: ModelHandle<Project>,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
mut cx: AsyncAppContext,
|
||||
) -> LocalBoxFuture<'static, Result<Option<Range<Anchor>>>> {
|
||||
async move {
|
||||
) -> Result<Option<Range<Anchor>>> {
|
||||
if message.can_rename {
|
||||
buffer
|
||||
.update(&mut cx, |buffer, _| {
|
||||
|
@ -187,10 +184,9 @@ impl LspCommand for PrepareRename {
|
|||
Ok(None)
|
||||
}
|
||||
}
|
||||
.boxed_local()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl LspCommand for PerformRename {
|
||||
type Response = ProjectTransaction;
|
||||
type LspRequest = lsp::request::Rename;
|
||||
|
@ -209,14 +205,13 @@ impl LspCommand for PerformRename {
|
|||
}
|
||||
}
|
||||
|
||||
fn response_from_lsp(
|
||||
async fn response_from_lsp(
|
||||
self,
|
||||
message: Option<lsp::WorkspaceEdit>,
|
||||
project: ModelHandle<Project>,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
mut cx: AsyncAppContext,
|
||||
) -> LocalBoxFuture<'static, Result<ProjectTransaction>> {
|
||||
async move {
|
||||
) -> Result<ProjectTransaction> {
|
||||
if let Some(edit) = message {
|
||||
let (language_name, language_server) = buffer.read_with(&cx, |buffer, _| {
|
||||
let language = buffer
|
||||
|
@ -241,8 +236,6 @@ impl LspCommand for PerformRename {
|
|||
Ok(ProjectTransaction::default())
|
||||
}
|
||||
}
|
||||
.boxed_local()
|
||||
}
|
||||
|
||||
fn to_proto(
|
||||
&self,
|
||||
|
@ -300,14 +293,13 @@ impl LspCommand for PerformRename {
|
|||
}
|
||||
}
|
||||
|
||||
fn response_from_proto(
|
||||
async fn response_from_proto(
|
||||
self,
|
||||
message: proto::PerformRenameResponse,
|
||||
project: ModelHandle<Project>,
|
||||
_: ModelHandle<Buffer>,
|
||||
mut cx: AsyncAppContext,
|
||||
) -> LocalBoxFuture<'static, Result<ProjectTransaction>> {
|
||||
async move {
|
||||
) -> Result<ProjectTransaction> {
|
||||
let message = message
|
||||
.transaction
|
||||
.ok_or_else(|| anyhow!("missing transaction"))?;
|
||||
|
@ -317,6 +309,4 @@ impl LspCommand for PerformRename {
|
|||
})
|
||||
.await
|
||||
}
|
||||
.boxed_local()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue