-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.rs
145 lines (122 loc) · 3.49 KB
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#![allow(dead_code)]
use rhai_rowan::syntax::{SyntaxKind::*, SyntaxNode, SyntaxToken};
use rowan::Direction;
use crate::algorithm::{self, BeginToken, BreakToken, Breaks, Formatter};
use std::{
io::{self, Write},
mem,
};
impl<W: Write> Formatter<W> {
pub(crate) fn ibox(&mut self, indent: isize) {
self.scan_begin(BeginToken {
offset: indent,
breaks: Breaks::Inconsistent,
});
}
pub(crate) fn cbox(&mut self, indent: isize) {
self.scan_begin(BeginToken {
offset: indent,
breaks: Breaks::Consistent,
});
}
pub(crate) fn end(&mut self) {
self.scan_end();
}
pub(crate) fn word(&mut self, wrd: &'static str) -> io::Result<()> {
self.scan_string(wrd)
}
fn spaces(&mut self, n: usize) {
self.scan_break(BreakToken {
blank_space: n,
..BreakToken::default()
});
}
pub(crate) fn zerobreak(&mut self) {
self.spaces(0);
}
pub(crate) fn space(&mut self) {
self.spaces(1);
}
pub(crate) fn nbsp(&mut self) -> io::Result<()> {
self.word(" ")
}
pub(crate) fn hardbreak(&mut self) {
self.spaces(algorithm::SIZE_INFINITY as usize);
}
pub(crate) fn hardbreaks(&mut self, count: u64) {
for _ in 0..count.min(self.options.max_empty_lines + 1) {
self.spaces(algorithm::SIZE_INFINITY as usize);
}
}
pub(crate) fn space_if_nonempty(&mut self) {
self.scan_break(BreakToken {
blank_space: 1,
if_nonempty: true,
..BreakToken::default()
});
}
pub(crate) fn hardbreak_if_nonempty(&mut self) {
self.scan_break(BreakToken {
blank_space: algorithm::SIZE_INFINITY as usize,
if_nonempty: true,
..BreakToken::default()
});
}
pub(crate) fn trailing_comma(&mut self, is_last: bool) -> io::Result<()> {
if is_last {
self.scan_break(BreakToken {
pre_break: Some(','),
..BreakToken::default()
});
} else {
self.word(",")?;
self.space();
}
Ok(())
}
pub(crate) fn trailing_comma_or_space(&mut self, is_last: bool) -> io::Result<()> {
if is_last {
self.scan_break(BreakToken {
blank_space: 1,
pre_break: Some(','),
..BreakToken::default()
});
} else {
self.word(",")?;
self.space();
}
Ok(())
}
pub(crate) fn neverbreak(&mut self) {
self.scan_break(BreakToken {
never_break: true,
..BreakToken::default()
});
}
}
pub(crate) trait ScopedStatic {
fn static_text(&self) -> &'static str;
}
impl ScopedStatic for SyntaxToken {
fn static_text(&self) -> &'static str {
// SAFETY: we guarantee that the syntax token
// outlives the formatting process.
unsafe { mem::transmute(self.text()) }
}
}
pub(crate) fn breaks_before(node: &SyntaxNode) -> u64 {
if let Some(elem) = node.siblings_with_tokens(Direction::Prev).nth(1) {
if let Some(t) = elem.into_token() {
if t.kind() == WHITESPACE {
return break_count(&t);
}
}
}
0
}
pub(crate) fn break_count(t: &SyntaxToken) -> u64 {
if t.kind() != WHITESPACE {
return 0;
}
t.text().chars().filter(|c| *c == '\n').count() as u64
}