From 02dd0cfd8c7bf50cfb26c3c5178be5af4f3fdd25 Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Fri, 8 Feb 2019 18:58:27 +0100 Subject: Refactor formatting code out of ra_ida_api_light into ra_fmt. --- crates/ra_assists/Cargo.toml | 2 +- crates/ra_assists/src/assist_ctx.rs | 2 +- crates/ra_assists/src/replace_if_let_with_match.rs | 2 +- crates/ra_fmt/Cargo.toml | 11 ++++ crates/ra_fmt/src/lib.rs | 76 ++++++++++++++++++++++ crates/ra_ide_api_light/Cargo.toml | 1 + crates/ra_ide_api_light/src/formatting.rs | 74 --------------------- crates/ra_ide_api_light/src/join_lines.rs | 5 +- crates/ra_ide_api_light/src/lib.rs | 1 - crates/ra_ide_api_light/src/typing.rs | 4 +- 10 files changed, 96 insertions(+), 82 deletions(-) create mode 100644 crates/ra_fmt/Cargo.toml create mode 100644 crates/ra_fmt/src/lib.rs delete mode 100644 crates/ra_ide_api_light/src/formatting.rs (limited to 'crates') diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml index 20bc253e3..71ab2dcd2 100644 --- a/crates/ra_assists/Cargo.toml +++ b/crates/ra_assists/Cargo.toml @@ -7,9 +7,9 @@ authors = ["Aleksey Kladov "] [dependencies] join_to_string = "0.1.3" -ra_ide_api_light = { path = "../ra_ide_api_light" } ra_syntax = { path = "../ra_syntax" } ra_text_edit = { path = "../ra_text_edit" } +ra_fmt = { path = "../ra_fmt" } ra_db = { path = "../ra_db" } hir = { path = "../ra_hir", package = "ra_hir" } diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 0bf640241..7fb11d5f5 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -5,7 +5,7 @@ use ra_syntax::{ SourceFile, TextRange, AstNode, TextUnit, SyntaxNode, algo::{find_leaf_at_offset, find_node_at_offset, find_covering_node, LeafAtOffset}, }; -use ra_ide_api_light::formatting::{leading_indent, reindent}; +use ra_fmt::{leading_indent, reindent}; use crate::{AssistLabel, AssistAction}; diff --git a/crates/ra_assists/src/replace_if_let_with_match.rs b/crates/ra_assists/src/replace_if_let_with_match.rs index 683f0d119..bdac381c2 100644 --- a/crates/ra_assists/src/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/replace_if_let_with_match.rs @@ -1,5 +1,5 @@ use ra_syntax::{AstNode, ast}; -use ra_ide_api_light::formatting::extract_trivial_expression; +use ra_fmt::extract_trivial_expression; use hir::db::HirDatabase; use crate::{AssistCtx, Assist}; diff --git a/crates/ra_fmt/Cargo.toml b/crates/ra_fmt/Cargo.toml new file mode 100644 index 000000000..1cf1b520f --- /dev/null +++ b/crates/ra_fmt/Cargo.toml @@ -0,0 +1,11 @@ +[package] +edition = "2018" +name = "ra_fmt" +version = "0.1.0" +authors = ["Aleksey Kladov "] +publish = false + +[dependencies] +itertools = "0.8.0" + +ra_syntax = { path = "../ra_syntax" } diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs new file mode 100644 index 000000000..62e6fb9c1 --- /dev/null +++ b/crates/ra_fmt/src/lib.rs @@ -0,0 +1,76 @@ +//! This crate provides some utilities for indenting rust code. +//! +use itertools::Itertools; +use ra_syntax::{ + AstNode, + SyntaxNode, SyntaxKind::*, + ast::{self, AstToken}, + algo::generate, +}; + +pub fn reindent(text: &str, indent: &str) -> String { + let indent = format!("\n{}", indent); + text.lines().intersperse(&indent).collect() +} + +/// If the node is on the beginning of the line, calculate indent. +pub fn leading_indent(node: &SyntaxNode) -> Option<&str> { + for leaf in prev_leaves(node) { + if let Some(ws) = ast::Whitespace::cast(leaf) { + let ws_text = ws.text(); + if let Some(pos) = ws_text.rfind('\n') { + return Some(&ws_text[pos + 1..]); + } + } + if leaf.leaf_text().unwrap().contains('\n') { + break; + } + } + None +} + +fn prev_leaves(node: &SyntaxNode) -> impl Iterator { + generate(prev_leaf(node), |&node| prev_leaf(node)) +} + +fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> { + generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| it.last_child()).last() +} + +pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { + let expr = block.expr()?; + if expr.syntax().text().contains('\n') { + return None; + } + let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { + WHITESPACE | L_CURLY | R_CURLY => false, + _ => it != &expr.syntax(), + }); + if non_trivial_children.count() > 0 { + return None; + } + Some(expr) +} + +pub fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { + match left.kind() { + L_PAREN | L_BRACK => return "", + L_CURLY => { + if let USE_TREE = right.kind() { + return ""; + } + } + _ => (), + } + match right.kind() { + R_PAREN | R_BRACK => return "", + R_CURLY => { + if let USE_TREE = left.kind() { + return ""; + } + } + DOT => return "", + _ => (), + } + " " +} diff --git a/crates/ra_ide_api_light/Cargo.toml b/crates/ra_ide_api_light/Cargo.toml index c1e4314ce..86311e677 100644 --- a/crates/ra_ide_api_light/Cargo.toml +++ b/crates/ra_ide_api_light/Cargo.toml @@ -13,6 +13,7 @@ rustc-hash = "1.0" ra_syntax = { path = "../ra_syntax" } ra_text_edit = { path = "../ra_text_edit" } +ra_fmt = { path = "../ra_fmt" } [dev-dependencies] test_utils = { path = "../test_utils" } diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs deleted file mode 100644 index 8bc03f974..000000000 --- a/crates/ra_ide_api_light/src/formatting.rs +++ /dev/null @@ -1,74 +0,0 @@ -use itertools::Itertools; -use ra_syntax::{ - AstNode, - SyntaxNode, SyntaxKind::*, - ast::{self, AstToken}, - algo::generate, -}; - -pub fn reindent(text: &str, indent: &str) -> String { - let indent = format!("\n{}", indent); - text.lines().intersperse(&indent).collect() -} - -/// If the node is on the beginning of the line, calculate indent. -pub fn leading_indent(node: &SyntaxNode) -> Option<&str> { - for leaf in prev_leaves(node) { - if let Some(ws) = ast::Whitespace::cast(leaf) { - let ws_text = ws.text(); - if let Some(pos) = ws_text.rfind('\n') { - return Some(&ws_text[pos + 1..]); - } - } - if leaf.leaf_text().unwrap().contains('\n') { - break; - } - } - None -} - -fn prev_leaves(node: &SyntaxNode) -> impl Iterator { - generate(prev_leaf(node), |&node| prev_leaf(node)) -} - -fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> { - generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| it.last_child()).last() -} - -pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { - let expr = block.expr()?; - if expr.syntax().text().contains('\n') { - return None; - } - let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { - WHITESPACE | L_CURLY | R_CURLY => false, - _ => it != &expr.syntax(), - }); - if non_trivial_children.count() > 0 { - return None; - } - Some(expr) -} - -pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { - match left.kind() { - L_PAREN | L_BRACK => return "", - L_CURLY => { - if let USE_TREE = right.kind() { - return ""; - } - } - _ => (), - } - match right.kind() { - R_PAREN | R_BRACK => return "", - R_CURLY => { - if let USE_TREE = left.kind() { - return ""; - } - } - DOT => return "", - _ => (), - } - " " -} diff --git a/crates/ra_ide_api_light/src/join_lines.rs b/crates/ra_ide_api_light/src/join_lines.rs index 03770c52e..970afd327 100644 --- a/crates/ra_ide_api_light/src/join_lines.rs +++ b/crates/ra_ide_api_light/src/join_lines.rs @@ -5,10 +5,11 @@ use ra_syntax::{ algo::find_covering_node, ast, }; - +use ra_fmt::{ + compute_ws, extract_trivial_expression +}; use crate::{ LocalEdit, TextEditBuilder, - formatting::{compute_ws, extract_trivial_expression}, }; pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit { diff --git a/crates/ra_ide_api_light/src/lib.rs b/crates/ra_ide_api_light/src/lib.rs index f3078f51e..6d1ce8dbf 100644 --- a/crates/ra_ide_api_light/src/lib.rs +++ b/crates/ra_ide_api_light/src/lib.rs @@ -3,7 +3,6 @@ //! This usually means functions which take syntax tree as an input and produce //! an edit or some auxiliary info. -pub mod formatting; mod extend_selection; mod folding_ranges; mod line_index; diff --git a/crates/ra_ide_api_light/src/typing.rs b/crates/ra_ide_api_light/src/typing.rs index a08a5a8c5..9dd9f1c1d 100644 --- a/crates/ra_ide_api_light/src/typing.rs +++ b/crates/ra_ide_api_light/src/typing.rs @@ -4,8 +4,8 @@ use ra_syntax::{ algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset}, ast::{self, AstToken}, }; - -use crate::{LocalEdit, TextEditBuilder, formatting::leading_indent}; +use ra_fmt::leading_indent; +use crate::{LocalEdit, TextEditBuilder}; pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option { let comment = -- cgit v1.2.3