10015: internal: more declarative re-indentation API r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-08-30 08:34:49 +00:00 committed by GitHub
commit d250aa77ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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)),
}
.clone_for_update();
block.reset_indent();
block.indent(indent);
block.reindent_to(indent);
let expr_for_loop = make::expr_for_loop(param, receiver, block);
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 itertools::izip;
use syntax::{
ast::{self, edit::AstNodeEdit, ArgListOwner},
ast::{self, edit_in_place::Indent, ArgListOwner},
ted, AstNode,
};
@ -217,11 +217,11 @@ pub(crate) fn inline_(
}
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() {
Some(expr) if replacement.statements().next().is_none() => expr,
_ => ast::Expr::BlockExpr(replacement),
let replacement = match body.tail_expr() {
Some(expr) if body.statements().next().is_none() => expr,
_ => ast::Expr::BlockExpr(body),
};
builder.replace_ast(expr, replacement);
},

View file

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