From c6334285e3b8369ca685ca436d94ff1f64201044 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 May 2020 13:57:19 +0200 Subject: Fix visibility --- crates/ra_assists/src/assist_context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs index 3085c4330..a680f752b 100644 --- a/crates/ra_assists/src/assist_context.rs +++ b/crates/ra_assists/src/assist_context.rs @@ -49,7 +49,7 @@ use crate::{Assist, AssistId, GroupLabel, ResolvedAssist}; /// easier to just compute the edit eagerly :-) pub(crate) struct AssistContext<'a> { pub(crate) sema: Semantics<'a, RootDatabase>, - pub(super) db: &'a RootDatabase, + pub(crate) db: &'a RootDatabase, pub(crate) frange: FileRange, source_file: SourceFile, } -- cgit v1.2.3 From 231fddab5420ffe5edf7b93609ea21155653254a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 May 2020 14:40:11 +0200 Subject: More fluent indent API --- crates/ra_assists/src/handlers/add_function.rs | 13 ++++++++----- crates/ra_assists/src/handlers/add_missing_impl_members.rs | 5 ++--- crates/ra_assists/src/handlers/early_return.rs | 13 ++++++++----- crates/ra_assists/src/handlers/replace_if_let_with_match.rs | 9 ++++++--- crates/ra_assists/src/handlers/replace_let_with_if_let.rs | 3 +-- crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | 10 +++++++--- 6 files changed, 32 insertions(+), 21 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index 95faf0f4f..de016ae4e 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs @@ -1,7 +1,11 @@ use hir::HirDisplay; use ra_db::FileId; use ra_syntax::{ - ast::{self, edit::IndentLevel, ArgListOwner, AstNode, ModuleItemOwner}, + ast::{ + self, + edit::{AstNodeEdit, IndentLevel}, + ArgListOwner, AstNode, ModuleItemOwner, + }, SyntaxKind, SyntaxNode, TextSize, }; use rustc_hash::{FxHashMap, FxHashSet}; @@ -116,17 +120,16 @@ impl FunctionBuilder { let (fn_def, insert_offset) = match self.target { GeneratedFunctionTarget::BehindItem(it) => { let with_leading_blank_line = ast::make::add_leading_newlines(2, fn_def); - let indented = IndentLevel::from_node(&it).increase_indent(with_leading_blank_line); + let indented = with_leading_blank_line.indent(IndentLevel::from_node(&it)); (indented, it.text_range().end()) } GeneratedFunctionTarget::InEmptyItemList(it) => { let indent_once = IndentLevel(1); let indent = IndentLevel::from_node(it.syntax()); - let fn_def = ast::make::add_leading_newlines(1, fn_def); - let fn_def = indent_once.increase_indent(fn_def); + let fn_def = fn_def.indent(indent_once); let fn_def = ast::make::add_trailing_newlines(1, fn_def); - let fn_def = indent.increase_indent(fn_def); + let fn_def = fn_def.indent(indent); (fn_def, it.syntax().text_range().start() + TextSize::of('{')) } }; diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs index 3482a75bf..c1ce87914 100644 --- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs @@ -2,7 +2,7 @@ use hir::HasSource; use ra_syntax::{ ast::{ self, - edit::{self, IndentLevel}, + edit::{self, AstNodeEdit, IndentLevel}, make, AstNode, NameOwner, }, SmolStr, @@ -176,8 +176,7 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef { if fn_def.body().is_some() { return fn_def; } - let body = make::block_expr(None, Some(make::expr_todo())); - let body = IndentLevel(1).increase_indent(body); + let body = make::block_expr(None, Some(make::expr_todo())).indent(IndentLevel(1)); fn_def.with_body(body) } diff --git a/crates/ra_assists/src/handlers/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs index 810784ad5..0f2914950 100644 --- a/crates/ra_assists/src/handlers/early_return.rs +++ b/crates/ra_assists/src/handlers/early_return.rs @@ -2,7 +2,11 @@ use std::{iter::once, ops::RangeInclusive}; use ra_syntax::{ algo::replace_children, - ast::{self, edit::IndentLevel, make}, + ast::{ + self, + edit::{AstNodeEdit, IndentLevel}, + make, + }, AstNode, SyntaxKind::{FN_DEF, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE}, SyntaxNode, @@ -105,8 +109,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) let then_branch = make::block_expr(once(make::expr_stmt(early_expression).into()), None); let cond = invert_boolean_expression(cond_expr); - let e = make::expr_if(make::condition(cond, None), then_branch); - if_indent_level.increase_indent(e) + make::expr_if(make::condition(cond, None), then_branch).indent(if_indent_level) }; replace(new_expr.syntax(), &then_block, &parent_block, &if_expr) } @@ -140,7 +143,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) make::bind_pat(make::name(&bound_ident.syntax().to_string())).into(), Some(match_expr), ); - let let_stmt = if_indent_level.increase_indent(let_stmt); + let let_stmt = let_stmt.indent(if_indent_level); replace(let_stmt.syntax(), &then_block, &parent_block, &if_expr) } }; @@ -153,7 +156,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) parent_block: &ast::BlockExpr, if_expr: &ast::IfExpr, ) -> SyntaxNode { - let then_block_items = IndentLevel::from(1).decrease_indent(then_block.clone()); + let then_block_items = then_block.unindent(IndentLevel::from(1)); let end_of_then = then_block_items.syntax().last_child_or_token().unwrap(); let end_of_then = if end_of_then.prev_sibling_or_token().map(|n| n.kind()) == Some(WHITESPACE) { diff --git a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs index a59a06efa..65f5fc6ab 100644 --- a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs @@ -1,6 +1,10 @@ use ra_fmt::unwrap_trivial_block; use ra_syntax::{ - ast::{self, edit::IndentLevel, make}, + ast::{ + self, + edit::{AstNodeEdit, IndentLevel}, + make, + }, AstNode, }; @@ -61,10 +65,9 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) make::match_arm(vec![pattern], else_expr) }; make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])) + .indent(IndentLevel::from_node(if_expr.syntax())) }; - let match_expr = IndentLevel::from_node(if_expr.syntax()).increase_indent(match_expr); - edit.set_cursor(if_expr.syntax().text_range().start()); edit.replace_ast::(if_expr.into(), match_expr); }) diff --git a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs index d3f214591..482957dc6 100644 --- a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs @@ -53,8 +53,7 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> ) .into(), }; - let block = - IndentLevel::from_node(let_stmt.syntax()).increase_indent(make::block_expr(None, None)); + let block = make::block_expr(None, None).indent(IndentLevel::from_node(let_stmt.syntax())); let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block); let stmt = make::expr_stmt(if_); diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index a46998b8e..c4b56f6e9 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs @@ -1,7 +1,11 @@ use std::iter; use ra_syntax::{ - ast::{self, edit::IndentLevel, make}, + ast::{ + self, + edit::{AstNodeEdit, IndentLevel}, + make, + }, AstNode, }; @@ -51,8 +55,8 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); - let match_expr = make::expr_match(caller.clone(), match_arm_list); - let match_expr = IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); + let match_expr = make::expr_match(caller.clone(), match_arm_list) + .indent(IndentLevel::from_node(method_call.syntax())); edit.set_cursor(caller.syntax().text_range().start()); edit.replace_ast::(method_call.into(), match_expr); -- cgit v1.2.3