aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assist_context.rs2
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs13
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs5
-rw-r--r--crates/ra_assists/src/handlers/early_return.rs13
-rw-r--r--crates/ra_assists/src/handlers/replace_if_let_with_match.rs9
-rw-r--r--crates/ra_assists/src/handlers/replace_let_with_if_let.rs3
-rw-r--r--crates/ra_assists/src/handlers/replace_unwrap_with_match.rs10
-rw-r--r--crates/ra_syntax/src/ast/edit.rs26
8 files changed, 46 insertions, 35 deletions
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};
49/// easier to just compute the edit eagerly :-) 49/// easier to just compute the edit eagerly :-)
50pub(crate) struct AssistContext<'a> { 50pub(crate) struct AssistContext<'a> {
51 pub(crate) sema: Semantics<'a, RootDatabase>, 51 pub(crate) sema: Semantics<'a, RootDatabase>,
52 pub(super) db: &'a RootDatabase, 52 pub(crate) db: &'a RootDatabase,
53 pub(crate) frange: FileRange, 53 pub(crate) frange: FileRange,
54 source_file: SourceFile, 54 source_file: SourceFile,
55} 55}
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 @@
1use hir::HirDisplay; 1use hir::HirDisplay;
2use ra_db::FileId; 2use ra_db::FileId;
3use ra_syntax::{ 3use ra_syntax::{
4 ast::{self, edit::IndentLevel, ArgListOwner, AstNode, ModuleItemOwner}, 4 ast::{
5 self,
6 edit::{AstNodeEdit, IndentLevel},
7 ArgListOwner, AstNode, ModuleItemOwner,
8 },
5 SyntaxKind, SyntaxNode, TextSize, 9 SyntaxKind, SyntaxNode, TextSize,
6}; 10};
7use rustc_hash::{FxHashMap, FxHashSet}; 11use rustc_hash::{FxHashMap, FxHashSet};
@@ -116,17 +120,16 @@ impl FunctionBuilder {
116 let (fn_def, insert_offset) = match self.target { 120 let (fn_def, insert_offset) = match self.target {
117 GeneratedFunctionTarget::BehindItem(it) => { 121 GeneratedFunctionTarget::BehindItem(it) => {
118 let with_leading_blank_line = ast::make::add_leading_newlines(2, fn_def); 122 let with_leading_blank_line = ast::make::add_leading_newlines(2, fn_def);
119 let indented = IndentLevel::from_node(&it).increase_indent(with_leading_blank_line); 123 let indented = with_leading_blank_line.indent(IndentLevel::from_node(&it));
120 (indented, it.text_range().end()) 124 (indented, it.text_range().end())
121 } 125 }
122 GeneratedFunctionTarget::InEmptyItemList(it) => { 126 GeneratedFunctionTarget::InEmptyItemList(it) => {
123 let indent_once = IndentLevel(1); 127 let indent_once = IndentLevel(1);
124 let indent = IndentLevel::from_node(it.syntax()); 128 let indent = IndentLevel::from_node(it.syntax());
125
126 let fn_def = ast::make::add_leading_newlines(1, fn_def); 129 let fn_def = ast::make::add_leading_newlines(1, fn_def);
127 let fn_def = indent_once.increase_indent(fn_def); 130 let fn_def = fn_def.indent(indent_once);
128 let fn_def = ast::make::add_trailing_newlines(1, fn_def); 131 let fn_def = ast::make::add_trailing_newlines(1, fn_def);
129 let fn_def = indent.increase_indent(fn_def); 132 let fn_def = fn_def.indent(indent);
130 (fn_def, it.syntax().text_range().start() + TextSize::of('{')) 133 (fn_def, it.syntax().text_range().start() + TextSize::of('{'))
131 } 134 }
132 }; 135 };
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;
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{ 3 ast::{
4 self, 4 self,
5 edit::{self, IndentLevel}, 5 edit::{self, AstNodeEdit, IndentLevel},
6 make, AstNode, NameOwner, 6 make, AstNode, NameOwner,
7 }, 7 },
8 SmolStr, 8 SmolStr,
@@ -176,8 +176,7 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
176 if fn_def.body().is_some() { 176 if fn_def.body().is_some() {
177 return fn_def; 177 return fn_def;
178 } 178 }
179 let body = make::block_expr(None, Some(make::expr_todo())); 179 let body = make::block_expr(None, Some(make::expr_todo())).indent(IndentLevel(1));
180 let body = IndentLevel(1).increase_indent(body);
181 fn_def.with_body(body) 180 fn_def.with_body(body)
182} 181}
183 182
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};
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 algo::replace_children, 4 algo::replace_children,
5 ast::{self, edit::IndentLevel, make}, 5 ast::{
6 self,
7 edit::{AstNodeEdit, IndentLevel},
8 make,
9 },
6 AstNode, 10 AstNode,
7 SyntaxKind::{FN_DEF, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE}, 11 SyntaxKind::{FN_DEF, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE},
8 SyntaxNode, 12 SyntaxNode,
@@ -105,8 +109,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
105 let then_branch = 109 let then_branch =
106 make::block_expr(once(make::expr_stmt(early_expression).into()), None); 110 make::block_expr(once(make::expr_stmt(early_expression).into()), None);
107 let cond = invert_boolean_expression(cond_expr); 111 let cond = invert_boolean_expression(cond_expr);
108 let e = make::expr_if(make::condition(cond, None), then_branch); 112 make::expr_if(make::condition(cond, None), then_branch).indent(if_indent_level)
109 if_indent_level.increase_indent(e)
110 }; 113 };
111 replace(new_expr.syntax(), &then_block, &parent_block, &if_expr) 114 replace(new_expr.syntax(), &then_block, &parent_block, &if_expr)
112 } 115 }
@@ -140,7 +143,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
140 make::bind_pat(make::name(&bound_ident.syntax().to_string())).into(), 143 make::bind_pat(make::name(&bound_ident.syntax().to_string())).into(),
141 Some(match_expr), 144 Some(match_expr),
142 ); 145 );
143 let let_stmt = if_indent_level.increase_indent(let_stmt); 146 let let_stmt = let_stmt.indent(if_indent_level);
144 replace(let_stmt.syntax(), &then_block, &parent_block, &if_expr) 147 replace(let_stmt.syntax(), &then_block, &parent_block, &if_expr)
145 } 148 }
146 }; 149 };
@@ -153,7 +156,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
153 parent_block: &ast::BlockExpr, 156 parent_block: &ast::BlockExpr,
154 if_expr: &ast::IfExpr, 157 if_expr: &ast::IfExpr,
155 ) -> SyntaxNode { 158 ) -> SyntaxNode {
156 let then_block_items = IndentLevel::from(1).decrease_indent(then_block.clone()); 159 let then_block_items = then_block.unindent(IndentLevel::from(1));
157 let end_of_then = then_block_items.syntax().last_child_or_token().unwrap(); 160 let end_of_then = then_block_items.syntax().last_child_or_token().unwrap();
158 let end_of_then = 161 let end_of_then =
159 if end_of_then.prev_sibling_or_token().map(|n| n.kind()) == Some(WHITESPACE) { 162 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 @@
1use ra_fmt::unwrap_trivial_block; 1use ra_fmt::unwrap_trivial_block;
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, edit::IndentLevel, make}, 3 ast::{
4 self,
5 edit::{AstNodeEdit, IndentLevel},
6 make,
7 },
4 AstNode, 8 AstNode,
5}; 9};
6 10
@@ -61,10 +65,9 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
61 make::match_arm(vec![pattern], else_expr) 65 make::match_arm(vec![pattern], else_expr)
62 }; 66 };
63 make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])) 67 make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm]))
68 .indent(IndentLevel::from_node(if_expr.syntax()))
64 }; 69 };
65 70
66 let match_expr = IndentLevel::from_node(if_expr.syntax()).increase_indent(match_expr);
67
68 edit.set_cursor(if_expr.syntax().text_range().start()); 71 edit.set_cursor(if_expr.syntax().text_range().start());
69 edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); 72 edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr);
70 }) 73 })
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) ->
53 ) 53 )
54 .into(), 54 .into(),
55 }; 55 };
56 let block = 56 let block = make::block_expr(None, None).indent(IndentLevel::from_node(let_stmt.syntax()));
57 IndentLevel::from_node(let_stmt.syntax()).increase_indent(make::block_expr(None, None));
58 let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block); 57 let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block);
59 let stmt = make::expr_stmt(if_); 58 let stmt = make::expr_stmt(if_);
60 59
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 @@
1use std::iter; 1use std::iter;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 ast::{self, edit::IndentLevel, make}, 4 ast::{
5 self,
6 edit::{AstNodeEdit, IndentLevel},
7 make,
8 },
5 AstNode, 9 AstNode,
6}; 10};
7 11
@@ -51,8 +55,8 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
51 let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); 55 let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call);
52 56
53 let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); 57 let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]);
54 let match_expr = make::expr_match(caller.clone(), match_arm_list); 58 let match_expr = make::expr_match(caller.clone(), match_arm_list)
55 let match_expr = IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr); 59 .indent(IndentLevel::from_node(method_call.syntax()));
56 60
57 edit.set_cursor(caller.syntax().text_range().start()); 61 edit.set_cursor(caller.syntax().text_range().start());
58 edit.replace_ast::<ast::Expr>(method_call.into(), match_expr); 62 edit.replace_ast::<ast::Expr>(method_call.into(), match_expr);
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index 3e6dd6061..94dfb1a13 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -453,11 +453,7 @@ impl IndentLevel {
453 IndentLevel(0) 453 IndentLevel(0)
454 } 454 }
455 455
456 pub fn increase_indent<N: AstNode>(self, node: N) -> N { 456 fn increase_indent(self, node: SyntaxNode) -> SyntaxNode {
457 N::cast(self._increase_indent(node.syntax().clone())).unwrap()
458 }
459
460 fn _increase_indent(self, node: SyntaxNode) -> SyntaxNode {
461 let mut rewriter = SyntaxRewriter::default(); 457 let mut rewriter = SyntaxRewriter::default();
462 node.descendants_with_tokens() 458 node.descendants_with_tokens()
463 .filter_map(|el| el.into_token()) 459 .filter_map(|el| el.into_token())
@@ -478,11 +474,7 @@ impl IndentLevel {
478 rewriter.rewrite(&node) 474 rewriter.rewrite(&node)
479 } 475 }
480 476
481 pub fn decrease_indent<N: AstNode>(self, node: N) -> N { 477 fn decrease_indent(self, node: SyntaxNode) -> SyntaxNode {
482 N::cast(self._decrease_indent(node.syntax().clone())).unwrap()
483 }
484
485 fn _decrease_indent(self, node: SyntaxNode) -> SyntaxNode {
486 let mut rewriter = SyntaxRewriter::default(); 478 let mut rewriter = SyntaxRewriter::default();
487 node.descendants_with_tokens() 479 node.descendants_with_tokens()
488 .filter_map(|el| el.into_token()) 480 .filter_map(|el| el.into_token())
@@ -521,7 +513,7 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
521 iter::successors(Some(token), |token| token.prev_token()) 513 iter::successors(Some(token), |token| token.prev_token())
522} 514}
523 515
524pub trait AstNodeEdit: AstNode + Sized { 516pub trait AstNodeEdit: AstNode + Clone + Sized {
525 #[must_use] 517 #[must_use]
526 fn insert_children( 518 fn insert_children(
527 &self, 519 &self,
@@ -558,9 +550,17 @@ pub trait AstNodeEdit: AstNode + Sized {
558 } 550 }
559 rewriter.rewrite_ast(self) 551 rewriter.rewrite_ast(self)
560 } 552 }
553 #[must_use]
554 fn indent(&self, indent: IndentLevel) -> Self {
555 Self::cast(indent.increase_indent(self.syntax().clone())).unwrap()
556 }
557 #[must_use]
558 fn unindent(&self, indent: IndentLevel) -> Self {
559 Self::cast(indent.decrease_indent(self.syntax().clone())).unwrap()
560 }
561} 561}
562 562
563impl<N: AstNode> AstNodeEdit for N {} 563impl<N: AstNode + Clone> AstNodeEdit for N {}
564 564
565fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> { 565fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> {
566 let element = element.into(); 566 let element = element.into();
@@ -580,7 +580,7 @@ fn test_increase_indent() {
580 _ => (), 580 _ => (),
581}" 581}"
582 ); 582 );
583 let indented = IndentLevel(2).increase_indent(arm_list); 583 let indented = arm_list.indent(IndentLevel(2));
584 assert_eq!( 584 assert_eq!(
585 indented.syntax().to_string(), 585 indented.syntax().to_string(),
586 "{ 586 "{