aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src
diff options
context:
space:
mode:
authorAndrea Pretto <[email protected]>2019-02-08 17:58:27 +0000
committerAndrea Pretto <[email protected]>2019-02-08 17:58:27 +0000
commit02dd0cfd8c7bf50cfb26c3c5178be5af4f3fdd25 (patch)
tree7fe910ed99307268a06e5668bb4401b6c6916fdb /crates/ra_ide_api_light/src
parent12e3b4c70b5ef23b2fdfc197296d483680e125f9 (diff)
Refactor formatting code out of ra_ida_api_light into ra_fmt.
Diffstat (limited to 'crates/ra_ide_api_light/src')
-rw-r--r--crates/ra_ide_api_light/src/formatting.rs74
-rw-r--r--crates/ra_ide_api_light/src/join_lines.rs5
-rw-r--r--crates/ra_ide_api_light/src/lib.rs1
-rw-r--r--crates/ra_ide_api_light/src/typing.rs4
4 files changed, 5 insertions, 79 deletions
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 @@
1use itertools::Itertools;
2use ra_syntax::{
3 AstNode,
4 SyntaxNode, SyntaxKind::*,
5 ast::{self, AstToken},
6 algo::generate,
7};
8
9pub fn reindent(text: &str, indent: &str) -> String {
10 let indent = format!("\n{}", indent);
11 text.lines().intersperse(&indent).collect()
12}
13
14/// If the node is on the beginning of the line, calculate indent.
15pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
16 for leaf in prev_leaves(node) {
17 if let Some(ws) = ast::Whitespace::cast(leaf) {
18 let ws_text = ws.text();
19 if let Some(pos) = ws_text.rfind('\n') {
20 return Some(&ws_text[pos + 1..]);
21 }
22 }
23 if leaf.leaf_text().unwrap().contains('\n') {
24 break;
25 }
26 }
27 None
28}
29
30fn prev_leaves(node: &SyntaxNode) -> impl Iterator<Item = &SyntaxNode> {
31 generate(prev_leaf(node), |&node| prev_leaf(node))
32}
33
34fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
35 generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| it.last_child()).last()
36}
37
38pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
39 let expr = block.expr()?;
40 if expr.syntax().text().contains('\n') {
41 return None;
42 }
43 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
44 WHITESPACE | L_CURLY | R_CURLY => false,
45 _ => it != &expr.syntax(),
46 });
47 if non_trivial_children.count() > 0 {
48 return None;
49 }
50 Some(expr)
51}
52
53pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
54 match left.kind() {
55 L_PAREN | L_BRACK => return "",
56 L_CURLY => {
57 if let USE_TREE = right.kind() {
58 return "";
59 }
60 }
61 _ => (),
62 }
63 match right.kind() {
64 R_PAREN | R_BRACK => return "",
65 R_CURLY => {
66 if let USE_TREE = left.kind() {
67 return "";
68 }
69 }
70 DOT => return "",
71 _ => (),
72 }
73 " "
74}
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::{
5 algo::find_covering_node, 5 algo::find_covering_node,
6 ast, 6 ast,
7}; 7};
8 8use ra_fmt::{
9 compute_ws, extract_trivial_expression
10};
9use crate::{ 11use crate::{
10 LocalEdit, TextEditBuilder, 12 LocalEdit, TextEditBuilder,
11 formatting::{compute_ws, extract_trivial_expression},
12}; 13};
13 14
14pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit { 15pub 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 @@
3//! This usually means functions which take syntax tree as an input and produce 3//! This usually means functions which take syntax tree as an input and produce
4//! an edit or some auxiliary info. 4//! an edit or some auxiliary info.
5 5
6pub mod formatting;
7mod extend_selection; 6mod extend_selection;
8mod folding_ranges; 7mod folding_ranges;
9mod line_index; 8mod 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::{
4 algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset}, 4 algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset},
5 ast::{self, AstToken}, 5 ast::{self, AstToken},
6}; 6};
7 7use ra_fmt::leading_indent;
8use crate::{LocalEdit, TextEditBuilder, formatting::leading_indent}; 8use crate::{LocalEdit, TextEditBuilder};
9 9
10pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> { 10pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> {
11 let comment = 11 let comment =