This repository was archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathutils.rs
More file actions
153 lines (139 loc) · 4.3 KB
/
utils.rs
File metadata and controls
153 lines (139 loc) · 4.3 KB
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
146
147
148
149
150
151
152
153
use super::*;
use unicode_width::UnicodeWidthStr as _;
/// like next sibling but doesn't skip trivia.
pub(crate) fn next_sibling_or_trivia<'a>(node: &LinkedNode<'a>) -> Option<LinkedNode<'a>> {
node.parent()?.children().nth(node.index() + 1)
}
/// like next sibling but doesn't skip trivia.
pub(crate) fn prev_sibling_or_trivia<'a>(node: &LinkedNode<'a>) -> Option<LinkedNode<'a>> {
if node.index() == 0 {
return None;
}
node.parent()?.children().nth(node.index() - 1)
}
/// find any child recursively that fits predicate
#[instrument(ret, skip_all)]
pub(crate) fn find_child<'a>(
node: &LinkedNode<'a>,
predicate: &impl Fn(&LinkedNode) -> bool,
) -> Option<LinkedNode<'a>> {
debug!("::find_child of {:?}", node.kind());
debug!(
"on children: {:?}",
node.children().map(|x| x.kind()).collect_vec()
);
for child in node.children() {
debug!("try for {:?}", child.kind());
if predicate(&child) {
debug!("predicate accepted");
return Some(child.clone());
} else if let Some(f) = find_child(&child, predicate) {
debug!("predicate accepted for inner of {:?}", child.kind());
return Some(f);
}
}
None
}
#[derive(Debug, Default, PartialEq)]
pub(crate) enum Btype {
#[default]
Markup,
Math,
#[allow(dead_code)]
Code,
}
#[instrument(ret, skip_all)]
pub(crate) fn block_type(node: &LinkedNode) -> Btype {
let mut node = node;
loop {
return match node.kind() {
Markup | ContentBlock => Btype::Markup,
Math => Btype::Math,
_ => {
node = node.parent().unwrap();
continue;
}
};
}
}
/// find all children recursively that fits predicate
// pub(crate) fn find_children<'a>(
// res: &mut Vec<LinkedNode<'a>>,
// node: &LinkedNode<'a>,
// predicate: &impl Fn(&LinkedNode) -> bool,
// ) {
// for child in node.children() {
// if predicate(&child) {
// debug!("predicate accepted");
// res.push(child);
// } else {
// find_children(res, &child, predicate);
// }
// }
// }
#[instrument(ret, skip_all)]
pub(crate) fn find_next<'a>(
node: &LinkedNode<'a>,
predicate: &impl Fn(&LinkedNode) -> bool,
) -> Option<LinkedNode<'a>> {
let mut next = next_sibling_or_trivia(node);
while let Some(next_inner) = next {
if predicate(&next_inner) {
return Some(next_inner);
}
next = next_sibling_or_trivia(&next_inner);
}
None
}
#[instrument(ret, skip_all)]
pub(crate) fn get_next_ignoring<'a>(
node: &'a LinkedNode<'a>,
ignoring: &[SyntaxKind],
) -> Option<LinkedNode<'a>> {
let mut next = next_sibling_or_trivia(node);
while let Some(next_inner) = &next {
let kind = next_inner.kind();
if ignoring.contains(&kind) {
next = next_sibling_or_trivia(&next_inner.clone());
continue;
}
return Some(next_inner.clone());
}
None
}
pub(crate) fn get_prev_ignoring<'a>(
node: &'a LinkedNode<'a>,
ignoring: &[SyntaxKind],
) -> Option<LinkedNode<'a>> {
let mut prev = prev_sibling_or_trivia(node);
while let Some(prev_inner) = &prev {
let kind = prev_inner.kind();
if ignoring.contains(&kind) {
prev = prev_sibling_or_trivia(&prev_inner.clone());
continue;
}
return Some(prev_inner.clone());
}
None
}
#[instrument(ret, skip_all)]
pub(crate) fn next_is_ignoring(node: &LinkedNode, is: SyntaxKind, ignoring: &[SyntaxKind]) -> bool {
let n = get_next_ignoring(node, ignoring);
debug!("next is: {:?}", n.as_ref().map(|x| x.kind()));
n.is_some_and(|n| is == n.kind())
}
#[instrument(ret, skip_all)]
pub(crate) fn prev_is_ignoring(node: &LinkedNode, is: SyntaxKind, ignoring: &[SyntaxKind]) -> bool {
let n = get_prev_ignoring(node, ignoring);
debug!("next is: {:?}", n.as_ref().map(|x| x.kind()));
n.is_some_and(|n| is == n.kind())
}
pub(crate) fn max_line_length(s: &str) -> usize {
s.lines().map(|l| l.width()).max().unwrap_or(0)
}
pub(crate) fn last_line_length(s: &str) -> usize {
s.split('\n').last().unwrap_or("").width()
}
pub(crate) fn first_line_length(s: &str) -> usize {
s.split('\n').next().unwrap_or("").width()
}