Refactor formatting code out of ra_ida_api_light into ra_fmt.

This commit is contained in:
Andrea Pretto 2019-02-08 18:58:27 +01:00
parent 12e3b4c70b
commit 02dd0cfd8c
10 changed files with 33 additions and 10 deletions

11
Cargo.lock generated
View file

@ -900,8 +900,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_db 0.1.0", "ra_db 0.1.0",
"ra_fmt 0.1.0",
"ra_hir 0.1.0", "ra_hir 0.1.0",
"ra_ide_api_light 0.1.0",
"ra_syntax 0.1.0", "ra_syntax 0.1.0",
"ra_text_edit 0.1.0", "ra_text_edit 0.1.0",
"test_utils 0.1.0", "test_utils 0.1.0",
@ -932,6 +932,14 @@ dependencies = [
"test_utils 0.1.0", "test_utils 0.1.0",
] ]
[[package]]
name = "ra_fmt"
version = "0.1.0"
dependencies = [
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_syntax 0.1.0",
]
[[package]] [[package]]
name = "ra_hir" name = "ra_hir"
version = "0.1.0" version = "0.1.0"
@ -986,6 +994,7 @@ dependencies = [
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"proptest 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_fmt 0.1.0",
"ra_syntax 0.1.0", "ra_syntax 0.1.0",
"ra_text_edit 0.1.0", "ra_text_edit 0.1.0",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -7,9 +7,9 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
[dependencies] [dependencies]
join_to_string = "0.1.3" join_to_string = "0.1.3"
ra_ide_api_light = { path = "../ra_ide_api_light" }
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_text_edit = { path = "../ra_text_edit" } ra_text_edit = { path = "../ra_text_edit" }
ra_fmt = { path = "../ra_fmt" }
ra_db = { path = "../ra_db" } ra_db = { path = "../ra_db" }
hir = { path = "../ra_hir", package = "ra_hir" } hir = { path = "../ra_hir", package = "ra_hir" }

View file

@ -5,7 +5,7 @@ use ra_syntax::{
SourceFile, TextRange, AstNode, TextUnit, SyntaxNode, SourceFile, TextRange, AstNode, TextUnit, SyntaxNode,
algo::{find_leaf_at_offset, find_node_at_offset, find_covering_node, LeafAtOffset}, 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}; use crate::{AssistLabel, AssistAction};

View file

@ -1,5 +1,5 @@
use ra_syntax::{AstNode, ast}; 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 hir::db::HirDatabase;
use crate::{AssistCtx, Assist}; use crate::{AssistCtx, Assist};

11
crates/ra_fmt/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
edition = "2018"
name = "ra_fmt"
version = "0.1.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
publish = false
[dependencies]
itertools = "0.8.0"
ra_syntax = { path = "../ra_syntax" }

View file

@ -1,3 +1,5 @@
//! This crate provides some utilities for indenting rust code.
//!
use itertools::Itertools; use itertools::Itertools;
use ra_syntax::{ use ra_syntax::{
AstNode, AstNode,
@ -50,7 +52,7 @@ pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
Some(expr) Some(expr)
} }
pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { pub fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
match left.kind() { match left.kind() {
L_PAREN | L_BRACK => return "", L_PAREN | L_BRACK => return "",
L_CURLY => { L_CURLY => {

View file

@ -13,6 +13,7 @@ rustc-hash = "1.0"
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_text_edit = { path = "../ra_text_edit" } ra_text_edit = { path = "../ra_text_edit" }
ra_fmt = { path = "../ra_fmt" }
[dev-dependencies] [dev-dependencies]
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }

View file

@ -5,10 +5,11 @@ use ra_syntax::{
algo::find_covering_node, algo::find_covering_node,
ast, ast,
}; };
use ra_fmt::{
compute_ws, extract_trivial_expression
};
use crate::{ use crate::{
LocalEdit, TextEditBuilder, LocalEdit, TextEditBuilder,
formatting::{compute_ws, extract_trivial_expression},
}; };
pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit { pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {

View file

@ -3,7 +3,6 @@
//! This usually means functions which take syntax tree as an input and produce //! This usually means functions which take syntax tree as an input and produce
//! an edit or some auxiliary info. //! an edit or some auxiliary info.
pub mod formatting;
mod extend_selection; mod extend_selection;
mod folding_ranges; mod folding_ranges;
mod line_index; mod line_index;

View file

@ -4,8 +4,8 @@ use ra_syntax::{
algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset}, algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset},
ast::{self, AstToken}, ast::{self, AstToken},
}; };
use ra_fmt::leading_indent;
use crate::{LocalEdit, TextEditBuilder, formatting::leading_indent}; use crate::{LocalEdit, TextEditBuilder};
pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> { pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
let comment = let comment =