internal: more declarative re-indentation API

This commit is contained in:
Aleksey Kladov 2021-08-24 18:18:43 +03:00
parent 6287d388c0
commit d1cd81f387
3 changed files with 14 additions and 14 deletions

View file

@ -61,8 +61,7 @@ pub(crate) fn convert_iter_for_each_to_for(acc: &mut Assists, ctx: &AssistContex
_ => make::block_expr(Vec::new(), Some(body)), _ => make::block_expr(Vec::new(), Some(body)),
} }
.clone_for_update(); .clone_for_update();
block.reset_indent(); block.reindent_to(indent);
block.indent(indent);
let expr_for_loop = make::expr_for_loop(param, receiver, block); let expr_for_loop = make::expr_for_loop(param, receiver, block);
builder.replace(range, expr_for_loop.to_string()) builder.replace(range, expr_for_loop.to_string())

View file

@ -3,7 +3,7 @@ use hir::{HasSource, PathResolution, TypeInfo};
use ide_db::{defs::Definition, path_transform::PathTransform, search::FileReference}; use ide_db::{defs::Definition, path_transform::PathTransform, search::FileReference};
use itertools::izip; use itertools::izip;
use syntax::{ use syntax::{
ast::{self, edit::AstNodeEdit, ArgListOwner}, ast::{self, edit_in_place::Indent, ArgListOwner},
ted, AstNode, ted, AstNode,
}; };
@ -217,11 +217,11 @@ pub(crate) fn inline_(
} }
let original_indentation = expr.indent_level(); let original_indentation = expr.indent_level();
let replacement = body.reset_indent().indent(original_indentation); body.reindent_to(original_indentation);
let replacement = match replacement.tail_expr() { let replacement = match body.tail_expr() {
Some(expr) if replacement.statements().next().is_none() => expr, Some(expr) if body.statements().next().is_none() => expr,
_ => ast::Expr::BlockExpr(replacement), _ => ast::Expr::BlockExpr(body),
}; };
builder.replace_ast(expr, replacement); builder.replace_ast(expr, replacement);
}, },

View file

@ -487,15 +487,16 @@ pub trait Indent: AstNode + Clone + Sized {
fn indent_level(&self) -> IndentLevel { fn indent_level(&self) -> IndentLevel {
IndentLevel::from_node(self.syntax()) IndentLevel::from_node(self.syntax())
} }
fn indent(&self, level: IndentLevel) { fn indent(&self, by: IndentLevel) {
level.increase_indent(self.syntax()); by.increase_indent(self.syntax());
} }
fn dedent(&self, level: IndentLevel) { fn dedent(&self, by: IndentLevel) {
level.decrease_indent(self.syntax()); by.decrease_indent(self.syntax());
} }
fn reset_indent(&self) { fn reindent_to(&self, target_level: IndentLevel) {
let level = IndentLevel::from_node(self.syntax()); let current_level = IndentLevel::from_node(self.syntax());
self.dedent(level); self.dedent(current_level);
self.indent(target_level)
} }
} }