From 4753409f86bf21b8d0ba7bd83918aa951921c97c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 20 Aug 2019 19:05:44 +0300 Subject: refactor TryConvWith similar to ConvWith --- crates/ra_assists/src/introduce_variable.rs | 5 +-- crates/ra_lsp_server/src/conv.rs | 68 +++++++++++------------------ 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index 5eb708310..95c18d0e3 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs @@ -56,10 +56,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option // but we do not want to duplicate possible // extra newlines in the indent block let text = indent.text(); - if text.starts_with("\r\n") { - buf.push_str("\r\n"); - buf.push_str(text.trim_start_matches("\r\n")); - } else if text.starts_with('\n') { + if text.starts_with('\n') { buf.push_str("\n"); buf.push_str(text.trim_start_matches('\n')); } else { diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index bd1ffd8f5..1a70ec3a2 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -25,10 +25,9 @@ pub trait ConvWith { fn conv_with(self, ctx: CTX) -> Self::Output; } -pub trait TryConvWith { - type Ctx; +pub trait TryConvWith { type Output; - fn try_conv_with(self, ctx: &Self::Ctx) -> Result; + fn try_conv_with(self, ctx: CTX) -> Result; } impl Conv for SyntaxKind { @@ -235,48 +234,42 @@ impl, CTX> ConvWith for Option { } } -impl<'a> TryConvWith for &'a Url { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for &'_ Url { type Output = FileId; fn try_conv_with(self, world: &WorldSnapshot) -> Result { world.uri_to_file_id(self) } } -impl TryConvWith for FileId { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for FileId { type Output = Url; fn try_conv_with(self, world: &WorldSnapshot) -> Result { world.file_id_to_uri(self) } } -impl<'a> TryConvWith for &'a TextDocumentItem { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentItem { type Output = FileId; fn try_conv_with(self, world: &WorldSnapshot) -> Result { self.uri.try_conv_with(world) } } -impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for &'_ VersionedTextDocumentIdentifier { type Output = FileId; fn try_conv_with(self, world: &WorldSnapshot) -> Result { self.uri.try_conv_with(world) } } -impl<'a> TryConvWith for &'a TextDocumentIdentifier { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentIdentifier { type Output = FileId; fn try_conv_with(self, world: &WorldSnapshot) -> Result { world.uri_to_file_id(&self.uri) } } -impl<'a> TryConvWith for &'a TextDocumentPositionParams { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentPositionParams { type Output = FilePosition; fn try_conv_with(self, world: &WorldSnapshot) -> Result { let file_id = self.text_document.try_conv_with(world)?; @@ -286,8 +279,7 @@ impl<'a> TryConvWith for &'a TextDocumentPositionParams { } } -impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for (&'_ TextDocumentIdentifier, Range) { type Output = FileRange; fn try_conv_with(self, world: &WorldSnapshot) -> Result { let file_id = self.0.try_conv_with(world)?; @@ -297,10 +289,9 @@ impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) { } } -impl TryConvWith for Vec { - type Ctx = ::Ctx; - type Output = Vec<::Output>; - fn try_conv_with(self, ctx: &Self::Ctx) -> Result { +impl, CTX: Copy> TryConvWith for Vec { + type Output = Vec<>::Output>; + fn try_conv_with(self, ctx: CTX) -> Result { let mut res = Vec::with_capacity(self.len()); for item in self { res.push(item.try_conv_with(ctx)?); @@ -309,8 +300,7 @@ impl TryConvWith for Vec { } } -impl TryConvWith for SourceChange { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for SourceChange { type Output = req::SourceChange; fn try_conv_with(self, world: &WorldSnapshot) -> Result { let cursor_position = match self.cursor_position { @@ -349,8 +339,7 @@ impl TryConvWith for SourceChange { } } -impl TryConvWith for SourceFileEdit { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for SourceFileEdit { type Output = TextDocumentEdit; fn try_conv_with(self, world: &WorldSnapshot) -> Result { let text_document = VersionedTextDocumentIdentifier { @@ -365,8 +354,7 @@ impl TryConvWith for SourceFileEdit { } } -impl TryConvWith for FileSystemEdit { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for FileSystemEdit { type Output = ResourceOp; fn try_conv_with(self, world: &WorldSnapshot) -> Result { let res = match self { @@ -384,8 +372,7 @@ impl TryConvWith for FileSystemEdit { } } -impl TryConvWith for &NavigationTarget { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for &NavigationTarget { type Output = Location; fn try_conv_with(self, world: &WorldSnapshot) -> Result { let line_index = world.analysis().file_line_index(self.file_id())?; @@ -394,8 +381,7 @@ impl TryConvWith for &NavigationTarget { } } -impl TryConvWith for (FileId, RangeInfo) { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for (FileId, RangeInfo) { type Output = LocationLink; fn try_conv_with(self, world: &WorldSnapshot) -> Result { let (src_file_id, target) = self; @@ -422,8 +408,7 @@ impl TryConvWith for (FileId, RangeInfo) { } } -impl TryConvWith for (FileId, RangeInfo>) { - type Ctx = WorldSnapshot; +impl TryConvWith<&'_ WorldSnapshot> for (FileId, RangeInfo>) { type Output = req::GotoDefinitionResponse; fn try_conv_with(self, world: &WorldSnapshot) -> Result { let (file_id, RangeInfo { range, info: navs }) = self; @@ -488,22 +473,21 @@ where } } -pub trait TryConvWithToVec<'a>: Sized + 'a { - type Ctx; +pub trait TryConvWithToVec: Sized { type Output; - fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result>; + fn try_conv_with_to_vec(self, ctx: CTX) -> Result>; } -impl<'a, I> TryConvWithToVec<'a> for I +impl TryConvWithToVec for I where - I: Iterator + 'a, - I::Item: TryConvWith, + I: Iterator, + I::Item: TryConvWith, + CTX: Copy, { - type Ctx = ::Ctx; - type Output = ::Output; + type Output = >::Output; - fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result> { + fn try_conv_with_to_vec(self, ctx: CTX) -> Result> { self.map(|it| it.try_conv_with(ctx)).collect() } } -- cgit v1.2.3