766: Formatting code into ra_fmt r=matklad a=eulerdisk

As discussed https://github.com/rust-analyzer/rust-analyzer/pull/762#discussion_r254905885

I did only move the code without other improvements.

Co-authored-by: Andrea Pretto <eulerdisk@gmail.com>
This commit is contained in:
bors[bot] 2019-02-09 09:56:54 +00:00
commit 34398a8756
10 changed files with 33 additions and 10 deletions

11
Cargo.lock generated
View file

@ -902,8 +902,8 @@ version = "0.1.0"
dependencies = [
"join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_db 0.1.0",
"ra_fmt 0.1.0",
"ra_hir 0.1.0",
"ra_ide_api_light 0.1.0",
"ra_syntax 0.1.0",
"ra_text_edit 0.1.0",
"test_utils 0.1.0",
@ -934,6 +934,14 @@ dependencies = [
"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]]
name = "ra_hir"
version = "0.1.0"
@ -988,6 +996,7 @@ dependencies = [
"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)",
"proptest 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_fmt 0.1.0",
"ra_syntax 0.1.0",
"ra_text_edit 0.1.0",
"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]
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" }

View file

@ -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};

View file

@ -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};

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 ra_syntax::{
AstNode,
@ -50,7 +52,7 @@ pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::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() {
L_PAREN | L_BRACK => return "",
L_CURLY => {

View file

@ -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" }

View file

@ -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 {

View file

@ -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;

View file

@ -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<LocalEdit> {
let comment =