aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-08-20 17:05:44 +0100
committerAleksey Kladov <[email protected]>2019-08-20 17:53:05 +0100
commit4753409f86bf21b8d0ba7bd83918aa951921c97c (patch)
tree2932f72edeb02e053828447c408d940cdf1d0d3a /crates
parent6ea4184fd107e5cc155b95a3cf058200c38d544d (diff)
refactor TryConvWith similar to ConvWith
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/introduce_variable.rs5
-rw-r--r--crates/ra_lsp_server/src/conv.rs68
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<impl HirDatabase>) -> Option
56 // but we do not want to duplicate possible 56 // but we do not want to duplicate possible
57 // extra newlines in the indent block 57 // extra newlines in the indent block
58 let text = indent.text(); 58 let text = indent.text();
59 if text.starts_with("\r\n") { 59 if text.starts_with('\n') {
60 buf.push_str("\r\n");
61 buf.push_str(text.trim_start_matches("\r\n"));
62 } else if text.starts_with('\n') {
63 buf.push_str("\n"); 60 buf.push_str("\n");
64 buf.push_str(text.trim_start_matches('\n')); 61 buf.push_str(text.trim_start_matches('\n'));
65 } else { 62 } 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<CTX> {
25 fn conv_with(self, ctx: CTX) -> Self::Output; 25 fn conv_with(self, ctx: CTX) -> Self::Output;
26} 26}
27 27
28pub trait TryConvWith { 28pub trait TryConvWith<CTX> {
29 type Ctx;
30 type Output; 29 type Output;
31 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output>; 30 fn try_conv_with(self, ctx: CTX) -> Result<Self::Output>;
32} 31}
33 32
34impl Conv for SyntaxKind { 33impl Conv for SyntaxKind {
@@ -235,48 +234,42 @@ impl<T: ConvWith<CTX>, CTX> ConvWith<CTX> for Option<T> {
235 } 234 }
236} 235}
237 236
238impl<'a> TryConvWith for &'a Url { 237impl TryConvWith<&'_ WorldSnapshot> for &'_ Url {
239 type Ctx = WorldSnapshot;
240 type Output = FileId; 238 type Output = FileId;
241 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> { 239 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> {
242 world.uri_to_file_id(self) 240 world.uri_to_file_id(self)
243 } 241 }
244} 242}
245 243
246impl TryConvWith for FileId { 244impl TryConvWith<&'_ WorldSnapshot> for FileId {
247 type Ctx = WorldSnapshot;
248 type Output = Url; 245 type Output = Url;
249 fn try_conv_with(self, world: &WorldSnapshot) -> Result<Url> { 246 fn try_conv_with(self, world: &WorldSnapshot) -> Result<Url> {
250 world.file_id_to_uri(self) 247 world.file_id_to_uri(self)
251 } 248 }
252} 249}
253 250
254impl<'a> TryConvWith for &'a TextDocumentItem { 251impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentItem {
255 type Ctx = WorldSnapshot;
256 type Output = FileId; 252 type Output = FileId;
257 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> { 253 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> {
258 self.uri.try_conv_with(world) 254 self.uri.try_conv_with(world)
259 } 255 }
260} 256}
261 257
262impl<'a> TryConvWith for &'a VersionedTextDocumentIdentifier { 258impl TryConvWith<&'_ WorldSnapshot> for &'_ VersionedTextDocumentIdentifier {
263 type Ctx = WorldSnapshot;
264 type Output = FileId; 259 type Output = FileId;
265 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> { 260 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> {
266 self.uri.try_conv_with(world) 261 self.uri.try_conv_with(world)
267 } 262 }
268} 263}
269 264
270impl<'a> TryConvWith for &'a TextDocumentIdentifier { 265impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentIdentifier {
271 type Ctx = WorldSnapshot;
272 type Output = FileId; 266 type Output = FileId;
273 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> { 267 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileId> {
274 world.uri_to_file_id(&self.uri) 268 world.uri_to_file_id(&self.uri)
275 } 269 }
276} 270}
277 271
278impl<'a> TryConvWith for &'a TextDocumentPositionParams { 272impl TryConvWith<&'_ WorldSnapshot> for &'_ TextDocumentPositionParams {
279 type Ctx = WorldSnapshot;
280 type Output = FilePosition; 273 type Output = FilePosition;
281 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FilePosition> { 274 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FilePosition> {
282 let file_id = self.text_document.try_conv_with(world)?; 275 let file_id = self.text_document.try_conv_with(world)?;
@@ -286,8 +279,7 @@ impl<'a> TryConvWith for &'a TextDocumentPositionParams {
286 } 279 }
287} 280}
288 281
289impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) { 282impl TryConvWith<&'_ WorldSnapshot> for (&'_ TextDocumentIdentifier, Range) {
290 type Ctx = WorldSnapshot;
291 type Output = FileRange; 283 type Output = FileRange;
292 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileRange> { 284 fn try_conv_with(self, world: &WorldSnapshot) -> Result<FileRange> {
293 let file_id = self.0.try_conv_with(world)?; 285 let file_id = self.0.try_conv_with(world)?;
@@ -297,10 +289,9 @@ impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) {
297 } 289 }
298} 290}
299 291
300impl<T: TryConvWith> TryConvWith for Vec<T> { 292impl<T: TryConvWith<CTX>, CTX: Copy> TryConvWith<CTX> for Vec<T> {
301 type Ctx = <T as TryConvWith>::Ctx; 293 type Output = Vec<<T as TryConvWith<CTX>>::Output>;
302 type Output = Vec<<T as TryConvWith>::Output>; 294 fn try_conv_with(self, ctx: CTX) -> Result<Self::Output> {
303 fn try_conv_with(self, ctx: &Self::Ctx) -> Result<Self::Output> {
304 let mut res = Vec::with_capacity(self.len()); 295 let mut res = Vec::with_capacity(self.len());
305 for item in self { 296 for item in self {
306 res.push(item.try_conv_with(ctx)?); 297 res.push(item.try_conv_with(ctx)?);
@@ -309,8 +300,7 @@ impl<T: TryConvWith> TryConvWith for Vec<T> {
309 } 300 }
310} 301}
311 302
312impl TryConvWith for SourceChange { 303impl TryConvWith<&'_ WorldSnapshot> for SourceChange {
313 type Ctx = WorldSnapshot;
314 type Output = req::SourceChange; 304 type Output = req::SourceChange;
315 fn try_conv_with(self, world: &WorldSnapshot) -> Result<req::SourceChange> { 305 fn try_conv_with(self, world: &WorldSnapshot) -> Result<req::SourceChange> {
316 let cursor_position = match self.cursor_position { 306 let cursor_position = match self.cursor_position {
@@ -349,8 +339,7 @@ impl TryConvWith for SourceChange {
349 } 339 }
350} 340}
351 341
352impl TryConvWith for SourceFileEdit { 342impl TryConvWith<&'_ WorldSnapshot> for SourceFileEdit {
353 type Ctx = WorldSnapshot;
354 type Output = TextDocumentEdit; 343 type Output = TextDocumentEdit;
355 fn try_conv_with(self, world: &WorldSnapshot) -> Result<TextDocumentEdit> { 344 fn try_conv_with(self, world: &WorldSnapshot) -> Result<TextDocumentEdit> {
356 let text_document = VersionedTextDocumentIdentifier { 345 let text_document = VersionedTextDocumentIdentifier {
@@ -365,8 +354,7 @@ impl TryConvWith for SourceFileEdit {
365 } 354 }
366} 355}
367 356
368impl TryConvWith for FileSystemEdit { 357impl TryConvWith<&'_ WorldSnapshot> for FileSystemEdit {
369 type Ctx = WorldSnapshot;
370 type Output = ResourceOp; 358 type Output = ResourceOp;
371 fn try_conv_with(self, world: &WorldSnapshot) -> Result<ResourceOp> { 359 fn try_conv_with(self, world: &WorldSnapshot) -> Result<ResourceOp> {
372 let res = match self { 360 let res = match self {
@@ -384,8 +372,7 @@ impl TryConvWith for FileSystemEdit {
384 } 372 }
385} 373}
386 374
387impl TryConvWith for &NavigationTarget { 375impl TryConvWith<&'_ WorldSnapshot> for &NavigationTarget {
388 type Ctx = WorldSnapshot;
389 type Output = Location; 376 type Output = Location;
390 fn try_conv_with(self, world: &WorldSnapshot) -> Result<Location> { 377 fn try_conv_with(self, world: &WorldSnapshot) -> Result<Location> {
391 let line_index = world.analysis().file_line_index(self.file_id())?; 378 let line_index = world.analysis().file_line_index(self.file_id())?;
@@ -394,8 +381,7 @@ impl TryConvWith for &NavigationTarget {
394 } 381 }
395} 382}
396 383
397impl TryConvWith for (FileId, RangeInfo<NavigationTarget>) { 384impl TryConvWith<&'_ WorldSnapshot> for (FileId, RangeInfo<NavigationTarget>) {
398 type Ctx = WorldSnapshot;
399 type Output = LocationLink; 385 type Output = LocationLink;
400 fn try_conv_with(self, world: &WorldSnapshot) -> Result<LocationLink> { 386 fn try_conv_with(self, world: &WorldSnapshot) -> Result<LocationLink> {
401 let (src_file_id, target) = self; 387 let (src_file_id, target) = self;
@@ -422,8 +408,7 @@ impl TryConvWith for (FileId, RangeInfo<NavigationTarget>) {
422 } 408 }
423} 409}
424 410
425impl TryConvWith for (FileId, RangeInfo<Vec<NavigationTarget>>) { 411impl TryConvWith<&'_ WorldSnapshot> for (FileId, RangeInfo<Vec<NavigationTarget>>) {
426 type Ctx = WorldSnapshot;
427 type Output = req::GotoDefinitionResponse; 412 type Output = req::GotoDefinitionResponse;
428 fn try_conv_with(self, world: &WorldSnapshot) -> Result<req::GotoTypeDefinitionResponse> { 413 fn try_conv_with(self, world: &WorldSnapshot) -> Result<req::GotoTypeDefinitionResponse> {
429 let (file_id, RangeInfo { range, info: navs }) = self; 414 let (file_id, RangeInfo { range, info: navs }) = self;
@@ -488,22 +473,21 @@ where
488 } 473 }
489} 474}
490 475
491pub trait TryConvWithToVec<'a>: Sized + 'a { 476pub trait TryConvWithToVec<CTX>: Sized {
492 type Ctx;
493 type Output; 477 type Output;
494 478
495 fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result<Vec<Self::Output>>; 479 fn try_conv_with_to_vec(self, ctx: CTX) -> Result<Vec<Self::Output>>;
496} 480}
497 481
498impl<'a, I> TryConvWithToVec<'a> for I 482impl<I, CTX> TryConvWithToVec<CTX> for I
499where 483where
500 I: Iterator + 'a, 484 I: Iterator,
501 I::Item: TryConvWith, 485 I::Item: TryConvWith<CTX>,
486 CTX: Copy,
502{ 487{
503 type Ctx = <I::Item as TryConvWith>::Ctx; 488 type Output = <I::Item as TryConvWith<CTX>>::Output;
504 type Output = <I::Item as TryConvWith>::Output;
505 489
506 fn try_conv_with_to_vec(self, ctx: &'a Self::Ctx) -> Result<Vec<Self::Output>> { 490 fn try_conv_with_to_vec(self, ctx: CTX) -> Result<Vec<Self::Output>> {
507 self.map(|it| it.try_conv_with(ctx)).collect() 491 self.map(|it| it.try_conv_with(ctx)).collect()
508 } 492 }
509} 493}