aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/join_lines.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-04-24 22:40:41 +0100
committerAleksey Kladov <[email protected]>2020-04-25 10:59:18 +0100
commitb1d5817dd18b7b5fc102a63b084b1ee7ff4f9996 (patch)
treee5d136c5ba4a6ba96aeeb423e6e3f64ca7cea3f9 /crates/ra_ide/src/join_lines.rs
parent27a7718880d93f55f905da606d108d3b3c682ab4 (diff)
Convert code to text-size
Diffstat (limited to 'crates/ra_ide/src/join_lines.rs')
-rw-r--r--crates/ra_ide/src/join_lines.rs38
1 files changed, 19 insertions, 19 deletions
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs
index 7d70dab9c..040846ec3 100644
--- a/crates/ra_ide/src/join_lines.rs
+++ b/crates/ra_ide/src/join_lines.rs
@@ -7,7 +7,7 @@ use ra_syntax::{
7 ast::{self, AstNode, AstToken}, 7 ast::{self, AstNode, AstToken},
8 Direction, NodeOrToken, SourceFile, 8 Direction, NodeOrToken, SourceFile,
9 SyntaxKind::{self, WHITESPACE}, 9 SyntaxKind::{self, WHITESPACE},
10 SyntaxNode, SyntaxToken, TextRange, TextUnit, T, 10 SyntaxNode, SyntaxToken, TextRange, TextSize, T,
11}; 11};
12use ra_text_edit::{TextEdit, TextEditBuilder}; 12use ra_text_edit::{TextEdit, TextEditBuilder};
13 13
@@ -19,7 +19,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
19 None => return TextEditBuilder::default().finish(), 19 None => return TextEditBuilder::default().finish(),
20 Some(pos) => pos, 20 Some(pos) => pos,
21 }; 21 };
22 TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n')) 22 TextRange::at(range.start() + pos, TextSize::of('\n'))
23 } else { 23 } else {
24 range 24 range
25 }; 25 };
@@ -30,13 +30,13 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
30 }; 30 };
31 let mut edit = TextEditBuilder::default(); 31 let mut edit = TextEditBuilder::default();
32 for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) { 32 for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) {
33 let range = match range.intersection(&token.text_range()) { 33 let range = match range.intersect(token.text_range()) {
34 Some(range) => range, 34 Some(range) => range,
35 None => continue, 35 None => continue,
36 } - token.text_range().start(); 36 } - token.text_range().start();
37 let text = token.text(); 37 let text = token.text();
38 for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') { 38 for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') {
39 let pos: TextUnit = (pos as u32).into(); 39 let pos: TextSize = (pos as u32).into();
40 let off = token.text_range().start() + range.start() + pos; 40 let off = token.text_range().start() + range.start() + pos;
41 if !edit.invalidates_offset(off) { 41 if !edit.invalidates_offset(off) {
42 remove_newline(&mut edit, &token, off); 42 remove_newline(&mut edit, &token, off);
@@ -47,16 +47,16 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
47 edit.finish() 47 edit.finish()
48} 48}
49 49
50fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextUnit) { 50fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextSize) {
51 if token.kind() != WHITESPACE || token.text().bytes().filter(|&b| b == b'\n').count() != 1 { 51 if token.kind() != WHITESPACE || token.text().bytes().filter(|&b| b == b'\n').count() != 1 {
52 // The node is either the first or the last in the file 52 // The node is either the first or the last in the file
53 let suff = &token.text()[TextRange::from_to( 53 let suff = &token.text()[TextRange::new(
54 offset - token.text_range().start() + TextUnit::of_char('\n'), 54 offset - token.text_range().start() + TextSize::of('\n'),
55 TextUnit::of_str(token.text()), 55 TextSize::of(token.text().as_str()),
56 )]; 56 )];
57 let spaces = suff.bytes().take_while(|&b| b == b' ').count(); 57 let spaces = suff.bytes().take_while(|&b| b == b' ').count();
58 58
59 edit.replace(TextRange::offset_len(offset, ((spaces + 1) as u32).into()), " ".to_string()); 59 edit.replace(TextRange::at(offset, ((spaces + 1) as u32).into()), " ".to_string());
60 return; 60 return;
61 } 61 }
62 62
@@ -65,7 +65,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
65 let next = token.next_sibling_or_token().unwrap(); 65 let next = token.next_sibling_or_token().unwrap();
66 if is_trailing_comma(prev.kind(), next.kind()) { 66 if is_trailing_comma(prev.kind(), next.kind()) {
67 // Removes: trailing comma, newline (incl. surrounding whitespace) 67 // Removes: trailing comma, newline (incl. surrounding whitespace)
68 edit.delete(TextRange::from_to(prev.text_range().start(), token.text_range().end())); 68 edit.delete(TextRange::new(prev.text_range().start(), token.text_range().end()));
69 return; 69 return;
70 } 70 }
71 if prev.kind() == T![,] && next.kind() == T!['}'] { 71 if prev.kind() == T![,] && next.kind() == T!['}'] {
@@ -76,7 +76,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
76 " " 76 " "
77 }; 77 };
78 edit.replace( 78 edit.replace(
79 TextRange::from_to(prev.text_range().start(), token.text_range().end()), 79 TextRange::new(prev.text_range().start(), token.text_range().end()),
80 space.to_string(), 80 space.to_string(),
81 ); 81 );
82 return; 82 return;
@@ -87,9 +87,9 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
87 next.as_token().cloned().and_then(ast::Comment::cast), 87 next.as_token().cloned().and_then(ast::Comment::cast),
88 ) { 88 ) {
89 // Removes: newline (incl. surrounding whitespace), start of the next comment 89 // Removes: newline (incl. surrounding whitespace), start of the next comment
90 edit.delete(TextRange::from_to( 90 edit.delete(TextRange::new(
91 token.text_range().start(), 91 token.text_range().start(),
92 next.syntax().text_range().start() + TextUnit::of_str(next.prefix()), 92 next.syntax().text_range().start() + TextSize::of(next.prefix()),
93 )); 93 ));
94 return; 94 return;
95 } 95 }
@@ -420,10 +420,10 @@ fn foo() {
420 check_join_lines( 420 check_join_lines(
421 r" 421 r"
422<|>use ra_syntax::{ 422<|>use ra_syntax::{
423 TextUnit, TextRange, 423 TextSize, TextRange,
424};", 424};",
425 r" 425 r"
426<|>use ra_syntax::{TextUnit, TextRange, 426<|>use ra_syntax::{TextSize, TextRange,
427};", 427};",
428 ); 428 );
429 } 429 }
@@ -434,11 +434,11 @@ fn foo() {
434 check_join_lines( 434 check_join_lines(
435 r" 435 r"
436use ra_syntax::{ 436use ra_syntax::{
437<|> TextUnit, TextRange 437<|> TextSize, TextRange
438};", 438};",
439 r" 439 r"
440use ra_syntax::{ 440use ra_syntax::{
441<|> TextUnit, TextRange};", 441<|> TextSize, TextRange};",
442 ); 442 );
443 } 443 }
444 444
@@ -448,11 +448,11 @@ use ra_syntax::{
448 check_join_lines( 448 check_join_lines(
449 r" 449 r"
450use ra_syntax::{ 450use ra_syntax::{
451<|> TextUnit, TextRange, 451<|> TextSize, TextRange,
452};", 452};",
453 r" 453 r"
454use ra_syntax::{ 454use ra_syntax::{
455<|> TextUnit, TextRange};", 455<|> TextSize, TextRange};",
456 ); 456 );
457 } 457 }
458 458