Skip to content

Commit 07cea5e

Browse files
Merge #8791
8791: feat: auto-indent use tree lists r=jonas-schievink a=jonas-schievink ![Peek 2021-05-10 15-20](https://user-images.githubusercontent.com/1786438/117665627-53e16c80-b1a3-11eb-8906-1b88b394367b.gif) bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 1703875 + e718c6b commit 07cea5e

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

crates/ide/src/typing/on_enter.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Text
5454
cov_mark::hit!(indent_block_contents);
5555
return Some(edit);
5656
}
57+
58+
// Typing enter after the `{` of a use tree list.
59+
if let Some(edit) = find_node_at_offset(file.syntax(), position.offset - TextSize::of('{'))
60+
.and_then(|list| on_enter_in_use_tree_list(list, position))
61+
{
62+
cov_mark::hit!(indent_block_contents);
63+
return Some(edit);
64+
}
5765
}
5866

5967
None
@@ -111,6 +119,21 @@ fn on_enter_in_block(block: ast::BlockExpr, position: FilePosition) -> Option<Te
111119
Some(edit)
112120
}
113121

122+
fn on_enter_in_use_tree_list(list: ast::UseTreeList, position: FilePosition) -> Option<TextEdit> {
123+
if list.syntax().text().contains_char('\n') {
124+
return None;
125+
}
126+
127+
let indent = IndentLevel::from_node(list.syntax());
128+
let mut edit = TextEdit::insert(position.offset, format!("\n{}$0", indent + 1));
129+
edit.union(TextEdit::insert(
130+
list.r_curly_token()?.text_range().start(),
131+
format!("\n{}", indent),
132+
))
133+
.ok()?;
134+
Some(edit)
135+
}
136+
114137
fn block_contents(block: &ast::BlockExpr) -> Option<SyntaxNode> {
115138
let mut node = block.tail_expr().map(|e| e.syntax().clone());
116139

@@ -484,4 +507,96 @@ fn f() {$0
484507
"#,
485508
);
486509
}
510+
511+
#[test]
512+
fn indents_use_tree_list() {
513+
do_check(
514+
r#"
515+
use crate::{$0};
516+
"#,
517+
r#"
518+
use crate::{
519+
$0
520+
};
521+
"#,
522+
);
523+
do_check(
524+
r#"
525+
use crate::{$0Object, path::to::OtherThing};
526+
"#,
527+
r#"
528+
use crate::{
529+
$0Object, path::to::OtherThing
530+
};
531+
"#,
532+
);
533+
do_check(
534+
r#"
535+
use {crate::{$0Object, path::to::OtherThing}};
536+
"#,
537+
r#"
538+
use {crate::{
539+
$0Object, path::to::OtherThing
540+
}};
541+
"#,
542+
);
543+
do_check(
544+
r#"
545+
use {
546+
crate::{$0Object, path::to::OtherThing}
547+
};
548+
"#,
549+
r#"
550+
use {
551+
crate::{
552+
$0Object, path::to::OtherThing
553+
}
554+
};
555+
"#,
556+
);
557+
}
558+
559+
#[test]
560+
fn does_not_indent_use_tree_list_when_not_at_curly_brace() {
561+
do_check_noop(
562+
r#"
563+
use path::{Thing$0};
564+
"#,
565+
);
566+
}
567+
568+
#[test]
569+
fn does_not_indent_use_tree_list_without_curly_braces() {
570+
do_check_noop(
571+
r#"
572+
use path::Thing$0;
573+
"#,
574+
);
575+
do_check_noop(
576+
r#"
577+
use path::$0Thing;
578+
"#,
579+
);
580+
do_check_noop(
581+
r#"
582+
use path::Thing$0};
583+
"#,
584+
);
585+
do_check_noop(
586+
r#"
587+
use path::{$0Thing;
588+
"#,
589+
);
590+
}
591+
592+
#[test]
593+
fn does_not_indent_multiline_use_tree_list() {
594+
do_check_noop(
595+
r#"
596+
use path::{$0
597+
Thing
598+
};
599+
"#,
600+
);
601+
}
487602
}

0 commit comments

Comments
 (0)