From e718c6b3e89bfca3ea0805a603ff1d4f60517316 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 10 May 2021 15:10:56 +0200 Subject: feat: auto-indent use tree lists --- crates/ide/src/typing/on_enter.rs | 115 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) (limited to 'crates/ide/src/typing') diff --git a/crates/ide/src/typing/on_enter.rs b/crates/ide/src/typing/on_enter.rs index 7d2db201a..81c4d95b1 100644 --- a/crates/ide/src/typing/on_enter.rs +++ b/crates/ide/src/typing/on_enter.rs @@ -54,6 +54,14 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option Option Option { + if list.syntax().text().contains_char('\n') { + return None; + } + + let indent = IndentLevel::from_node(list.syntax()); + let mut edit = TextEdit::insert(position.offset, format!("\n{}$0", indent + 1)); + edit.union(TextEdit::insert( + list.r_curly_token()?.text_range().start(), + format!("\n{}", indent), + )) + .ok()?; + Some(edit) +} + fn block_contents(block: &ast::BlockExpr) -> Option { let mut node = block.tail_expr().map(|e| e.syntax().clone()); @@ -484,4 +507,96 @@ fn f() {$0 "#, ); } + + #[test] + fn indents_use_tree_list() { + do_check( + r#" +use crate::{$0}; + "#, + r#" +use crate::{ + $0 +}; + "#, + ); + do_check( + r#" +use crate::{$0Object, path::to::OtherThing}; + "#, + r#" +use crate::{ + $0Object, path::to::OtherThing +}; + "#, + ); + do_check( + r#" +use {crate::{$0Object, path::to::OtherThing}}; + "#, + r#" +use {crate::{ + $0Object, path::to::OtherThing +}}; + "#, + ); + do_check( + r#" +use { + crate::{$0Object, path::to::OtherThing} +}; + "#, + r#" +use { + crate::{ + $0Object, path::to::OtherThing + } +}; + "#, + ); + } + + #[test] + fn does_not_indent_use_tree_list_when_not_at_curly_brace() { + do_check_noop( + r#" +use path::{Thing$0}; + "#, + ); + } + + #[test] + fn does_not_indent_use_tree_list_without_curly_braces() { + do_check_noop( + r#" +use path::Thing$0; + "#, + ); + do_check_noop( + r#" +use path::$0Thing; + "#, + ); + do_check_noop( + r#" +use path::Thing$0}; + "#, + ); + do_check_noop( + r#" +use path::{$0Thing; + "#, + ); + } + + #[test] + fn does_not_indent_multiline_use_tree_list() { + do_check_noop( + r#" +use path::{$0 + Thing +}; + "#, + ); + } } -- cgit v1.2.3