aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/typing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src/typing.rs')
-rw-r--r--crates/libeditor/src/typing.rs69
1 files changed, 30 insertions, 39 deletions
diff --git a/crates/libeditor/src/typing.rs b/crates/libeditor/src/typing.rs
index f888f3240..826b16181 100644
--- a/crates/libeditor/src/typing.rs
+++ b/crates/libeditor/src/typing.rs
@@ -45,10 +45,11 @@ pub fn join_lines(file: &File, range: TextRange) -> LocalEdit {
45 for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') { 45 for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') {
46 let pos: TextUnit = (pos as u32).into(); 46 let pos: TextUnit = (pos as u32).into();
47 let off = node.range().start() + range.start() + pos; 47 let off = node.range().start() + range.start() + pos;
48 remove_newline(&mut edit, node, text.as_str(), off); 48 if !edit.invalidates_offset(off) {
49 remove_newline(&mut edit, node, text.as_str(), off);
50 }
49 } 51 }
50 } 52 }
51 eprintln!("{:?}", edit);
52 53
53 LocalEdit { 54 LocalEdit {
54 edit: edit.finish(), 55 edit: edit.finish(),
@@ -239,17 +240,17 @@ fn foo() {
239}"); 240}");
240 } 241 }
241 242
242 #[test] 243 fn check_join_lines_sel(before: &str, after: &str) {
243 fn test_join_lines_selection() { 244 let (sel, before) = extract_range(before);
244 fn do_check(before: &str, after: &str) { 245 let file = File::parse(&before);
245 let (sel, before) = extract_range(before); 246 let result = join_lines(&file, sel);
246 let file = File::parse(&before); 247 let actual = result.edit.apply(&before);
247 let result = join_lines(&file, sel); 248 assert_eq_text!(after, &actual);
248 let actual = result.edit.apply(&before); 249 }
249 assert_eq_text!(after, &actual);
250 }
251 250
252 do_check(r" 251 #[test]
252 fn test_join_lines_selection_fn_args() {
253 check_join_lines_sel(r"
253fn foo() { 254fn foo() {
254 <|>foo(1, 255 <|>foo(1,
255 2, 256 2,
@@ -261,15 +262,22 @@ fn foo() {
261 foo(1, 2, 3) 262 foo(1, 2, 3)
262} 263}
263 "); 264 ");
265 }
264 266
265 do_check(r" 267 #[test]
268 fn test_join_lines_selection_struct() {
269 check_join_lines_sel(r"
266struct Foo <|>{ 270struct Foo <|>{
267 f: u32, 271 f: u32,
268}<|> 272}<|>
269 ", r" 273 ", r"
270struct Foo { f: u32 } 274struct Foo { f: u32 }
271 "); 275 ");
272 do_check(r" 276 }
277
278 #[test]
279 fn test_join_lines_selection_dot_chain() {
280 check_join_lines_sel(r"
273fn foo() { 281fn foo() {
274 join(<|>type_params.type_params() 282 join(<|>type_params.type_params()
275 .filter_map(|it| it.name()) 283 .filter_map(|it| it.name())
@@ -278,39 +286,22 @@ fn foo() {
278fn foo() { 286fn foo() {
279 join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text())) 287 join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()))
280}"); 288}");
289 }
281 290
282 do_check(r" 291 #[test]
283pub fn handle_find_matching_brace( 292 fn test_join_lines_selection_lambda_block_body() {
284 world: ServerWorld, 293 check_join_lines_sel(r"
285 params: req::FindMatchingBraceParams, 294pub fn handle_find_matching_brace() {
286) -> Result<Vec<Position>> { 295 params.offsets
287 let file_id = params.text_document.try_conv_with(&world)?;
288 let file = world.analysis().file_syntax(file_id);
289 let line_index = world.analysis().file_line_index(file_id);
290 let res = params.offsets
291 .into_iter()
292 .map_conv_with(&line_index)
293 .map(|offset| <|>{ 296 .map(|offset| <|>{
294 world.analysis().matching_brace(&file, offset).unwrap_or(offset) 297 world.analysis().matching_brace(&file, offset).unwrap_or(offset)
295 }<|>) 298 }<|>)
296 .map_conv_with(&line_index)
297 .collect(); 299 .collect();
298 Ok(res)
299}", r" 300}", r"
300pub fn handle_find_matching_brace( 301pub fn handle_find_matching_brace() {
301 world: ServerWorld, 302 params.offsets
302 params: req::FindMatchingBraceParams,
303) -> Result<Vec<Position>> {
304 let file_id = params.text_document.try_conv_with(&world)?;
305 let file = world.analysis().file_syntax(file_id);
306 let line_index = world.analysis().file_line_index(file_id);
307 let res = params.offsets
308 .into_iter()
309 .map_conv_with(&line_index)
310 .map(|offset| world.analysis().matching_brace(&file, offset).unwrap_or(offset)) 303 .map(|offset| world.analysis().matching_brace(&file, offset).unwrap_or(offset))
311 .map_conv_with(&line_index)
312 .collect(); 304 .collect();
313 Ok(res)
314}"); 305}");
315 } 306 }
316 307