refactor TryConvWith similar to ConvWith

This commit is contained in:
Aleksey Kladov 2019-08-20 19:05:44 +03:00
parent 6ea4184fd1
commit 4753409f86
2 changed files with 27 additions and 46 deletions

View file

@ -56,10 +56,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
// but we do not want to duplicate possible // but we do not want to duplicate possible
// extra newlines in the indent block // extra newlines in the indent block
let text = indent.text(); let text = indent.text();
if text.starts_with("\r\n") { if text.starts_with('\n') {
buf.push_str("\r\n");
buf.push_str(text.trim_start_matches("\r\n"));
} else if text.starts_with('\n') {
buf.push_str("\n"); buf.push_str("\n");
buf.push_str(text.trim_start_matches('\n')); buf.push_str(text.trim_start_matches('\n'));
} else { } else {

View file

@ -25,10 +25,9 @@ pub trait ConvWith<CTX> {
fn conv_with(self, ctx: CTX) -> Self::Output; fn conv_with(self, ctx: CTX) -> Self::Output;
} }
pub trait TryConvWith { pub trait TryConvWith<CTX> {
type Ctx;
type Output; type Output;
fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>; fn try_conv_with(self, ctx: CTX) -> Result<Self::Output>;
} }
impl Conv for SyntaxKind { impl Conv for SyntaxKind {
@ -235,48 +234,42 @@ impl<T: ConvWith<CTX>, CTX> ConvWith<CTX> for Option<T> {
} }
} }
impl<'a> TryConvWith for &'a Url { impl TryConvWith<&'_ WorldSnapshot> for &'_ Url {
type Ctx = WorldSnapshot;
type Output = FileId; type Output = FileId;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> {
world.uri_to_file_id(self) world.uri_to_file_id(self)
} }
} }
impl TryConvWith for FileId { impl TryConvWith<&'_ WorldSnapshot> for FileId {
type Ctx = WorldSnapshot;
type Output = Url; type Output = Url;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<Url> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<Url> {
world.file_id_to_uri(self) world.file_id_to_uri(self)
} }
} }
impl<'a> TryConvWith for &'a TextDocumentItem { impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentItem {
type Ctx = WorldSnapshot;
type Output = FileId; type Output = FileId;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> {
self.uri.try_conv_with(world) self.uri.try_conv_with(world)
} }
} }
impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier { impl TryConvWith<&'_ WorldSnapshot> for &'_ VersionedTextDocumentIdentifier {
type Ctx = WorldSnapshot;
type Output = FileId; type Output = FileId;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> {
self.uri.try_conv_with(world) self.uri.try_conv_with(world)
} }
} }
impl<'a> TryConvWith for &'a TextDocumentIdentifier { impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentIdentifier {
type Ctx = WorldSnapshot;
type Output = FileId; type Output = FileId;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> {
world.uri_to_file_id(&self.uri) world.uri_to_file_id(&self.uri)
} }
} }
impl<'a> TryConvWith for &'a TextDocumentPositionParams { impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentPositionParams {
type Ctx = WorldSnapshot;
type Output = FilePosition; type Output = FilePosition;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<FilePosition> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<FilePosition> {
let file_id = self.text_document.try_conv_with(world)?; 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) { impl TryConvWith<&'_ WorldSnapshot> for (&'_ TextDocumentIdentifier, Range) {
type Ctx = WorldSnapshot;
type Output = FileRange; type Output = FileRange;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileRange> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileRange> {
let file_id = self.0.try_conv_with(world)?; let file_id = self.0.try_conv_with(world)?;
@ -297,10 +289,9 @@ impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) {
} }
} }
impl<T: TryConvWith> TryConvWith for Vec<T> { impl<T: TryConvWith<CTX>, CTX: Copy> TryConvWith<CTX> for Vec<T> {
type Ctx = <T as TryConvWith>::Ctx; type Output = Vec<<T as TryConvWith<CTX>>::Output>;
type Output = Vec<<T as TryConvWith>::Output>; fn try_conv_with(self, ctx: CTX) -> Result<Self::Output> {
fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
let mut res = Vec::with_capacity(self.len()); let mut res = Vec::with_capacity(self.len());
for item in self { for item in self {
res.push(item.try_conv_with(ctx)?); res.push(item.try_conv_with(ctx)?);
@ -309,8 +300,7 @@ impl<T: TryConvWith> TryConvWith for Vec<T> {
} }
} }
impl TryConvWith for SourceChange { impl TryConvWith<&'_ WorldSnapshot> for SourceChange {
type Ctx = WorldSnapshot;
type Output = req::SourceChange; type Output = req::SourceChange;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<req::SourceChange> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<req::SourceChange> {
let cursor_position = match self.cursor_position { let cursor_position = match self.cursor_position {
@ -349,8 +339,7 @@ impl TryConvWith for SourceChange {
} }
} }
impl TryConvWith for SourceFileEdit { impl TryConvWith<&'_ WorldSnapshot> for SourceFileEdit {
type Ctx = WorldSnapshot;
type Output = TextDocumentEdit; type Output = TextDocumentEdit;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<TextDocumentEdit> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<TextDocumentEdit> {
let text_document = VersionedTextDocumentIdentifier { let text_document = VersionedTextDocumentIdentifier {
@ -365,8 +354,7 @@ impl TryConvWith for SourceFileEdit {
} }
} }
impl TryConvWith for FileSystemEdit { impl TryConvWith<&'_ WorldSnapshot> for FileSystemEdit {
type Ctx = WorldSnapshot;
type Output = ResourceOp; type Output = ResourceOp;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<ResourceOp> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<ResourceOp> {
let res = match self { let res = match self {
@ -384,8 +372,7 @@ impl TryConvWith for FileSystemEdit {
} }
} }
impl TryConvWith for &NavigationTarget { impl TryConvWith<&'_ WorldSnapshot> for &NavigationTarget {
type Ctx = WorldSnapshot;
type Output = Location; type Output = Location;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<Location> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<Location> {
let line_index = world.analysis().file_line_index(self.file_id())?; let line_index = world.analysis().file_line_index(self.file_id())?;
@ -394,8 +381,7 @@ impl TryConvWith for &NavigationTarget {
} }
} }
impl TryConvWith for (FileId, RangeInfo<NavigationTarget>) { impl TryConvWith<&'_ WorldSnapshot> for (FileId, RangeInfo<NavigationTarget>) {
type Ctx = WorldSnapshot;
type Output = LocationLink; type Output = LocationLink;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<LocationLink> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<LocationLink> {
let (src_file_id, target) = self; let (src_file_id, target) = self;
@ -422,8 +408,7 @@ impl TryConvWith for (FileId, RangeInfo<NavigationTarget>) {
} }
} }
impl TryConvWith for (FileId, RangeInfo<Vec<NavigationTarget>>) { impl TryConvWith<&'_ WorldSnapshot> for (FileId, RangeInfo<Vec<NavigationTarget>>) {
type Ctx = WorldSnapshot;
type Output = req::GotoDefinitionResponse; type Output = req::GotoDefinitionResponse;
fn try_conv_with(self, world: &WorldSnapshot) -> Result<req::GotoTypeDefinitionResponse> { fn try_conv_with(self, world: &WorldSnapshot) -> Result<req::GotoTypeDefinitionResponse> {
let (file_id, RangeInfo { range, info: navs }) = self; let (file_id, RangeInfo { range, info: navs }) = self;
@ -488,22 +473,21 @@ where
} }
} }
pub trait TryConvWithToVec<'a>: Sized + 'a { pub trait TryConvWithToVec<CTX>: Sized {
type Ctx;
type Output; type Output;
fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result<Vec<Self::Output>>; fn try_conv_with_to_vec(self, ctx: CTX) -> Result<Vec<Self::Output>>;
} }
impl<'a, I> TryConvWithToVec<'a> for I impl<I, CTX> TryConvWithToVec<CTX> for I
where where
I: Iterator + 'a, I: Iterator,
I::Item: TryConvWith, I::Item: TryConvWith<CTX>,
CTX: Copy,
{ {
type Ctx = <I::Item as TryConvWith>::Ctx; type Output = <I::Item as TryConvWith<CTX>>::Output;
type Output = <I::Item as TryConvWith>::Output;
fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result<Vec<Self::Output>> { fn try_conv_with_to_vec(self, ctx: CTX) -> Result<Vec<Self::Output>> {
self.map(|it| it.try_conv_with(ctx)).collect() self.map(|it| it.try_conv_with(ctx)).collect()
} }
} }