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
|
|
||||||
&& buffer.clip_point_utf16(range.end, Bias::Left) == range.end
|
|
||||||
{
|
{
|
||||||
Some(buffer.anchor_after(range.start)..buffer.anchor_before(range.end))
|
let Range { start, end } = range_from_lsp(range);
|
||||||
} else {
|
if buffer.clip_point_utf16(start, Bias::Left) == start
|
||||||
None
|
&& 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(
|
fn to_proto(
|
||||||
|
@ -166,14 +164,13 @@ 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, _| {
|
||||||
|
@ -187,10 +184,9 @@ impl LspCommand for PrepareRename {
|
||||||
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,14 +205,13 @@ 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
|
||||||
|
@ -241,8 +236,6 @@ impl LspCommand for PerformRename {
|
||||||
Ok(ProjectTransaction::default())
|
Ok(ProjectTransaction::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.boxed_local()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_proto(
|
fn to_proto(
|
||||||
&self,
|
&self,
|
||||||
|
@ -300,14 +293,13 @@ 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"))?;
|
||||||
|
@ -317,6 +309,4 @@ impl LspCommand for PerformRename {
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
.boxed_local()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue