aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/test_data/parser/fuzz-failures/0001.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-12 17:26:51 +0100
committerAleksey Kladov <[email protected]>2020-08-12 17:30:53 +0100
commita1c187eef3ba08076aedb5154929f7eda8d1b424 (patch)
tree9d898eb9600b0c36a74e4f95238f679c683fa566 /crates/ra_syntax/test_data/parser/fuzz-failures/0001.rs
parent3d6889cba72a9d02199f7adaa2ecc69bc30af834 (diff)
Rename ra_syntax -> syntax
Diffstat (limited to 'crates/ra_syntax/test_data/parser/fuzz-failures/0001.rs')
-rw-r--r--crates/ra_syntax/test_data/parser/fuzz-failures/0001.rs106
1 files changed, 0 insertions, 106 deletions
diff --git a/crates/ra_syntax/test_data/parser/fuzz-failures/0001.rs b/crates/ra_syntax/test_data/parser/fuzz-failures/0001.rs
deleted file mode 100644
index 099cc5f84..000000000
--- a/crates/ra_syntax/test_data/parser/fuzz-failures/0001.rs
+++ /dev/null
@@ -1,106 +0,0 @@
1use ra_syntax::{
2 File, TextRange, SyntaxNodeRef, TextUnit,
3 SyntaxKind::*,
4 algo::{find_leaf_at_offset, LeafAtOffset, find_covering_node, ancestors, Direction, siblings},
5};
6
7pub fn extend_selection(file: &File, range: TextRange) -> Option<TextRange> {
8 let syntax = file.syntax();
9 extend(syntax.borrowed(), range)
10}
11
12pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> {
13 if range.is_empty() {
14 let offset = range.start();
15 let mut leaves = find_leaf_at_offset(root, offset);
16 if leaves.clone().all(|it| it.kind() == WHITESPACE) {
17 return Some(extend_ws(root, leaves.next()?, offset));
18 }
19 let leaf = match leaves {
20 LeafAtOffset::None => return None,
21 LeafAtOffset::Single(l) => l,
22 LeafAtOffset::Between(l, r) => pick_best(l, r),
23 };
24 return Some(leaf.range());
25 };
26 let node = find_covering_node(root, range);
27 if node.kind() == COMMENT && range == node.range() {
28 if let Some(range) = extend_comments(node) {
29 return Some(range);
30 }
31 }
32
33 match ancestors(node).skip_while(|n| n.range() == range).next() {
34 None => None,
35 Some(parent) => Some(parent.range()),
36 }
37}
38
39fn extend_ws(root: SyntaxNodeRef, ws: SyntaxNodeRef, offset: TextUnit) -> TextRange {
40 let ws_text = ws.leaf_text().unwrap();
41 let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start();
42 let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start();
43 let ws_suffix = &ws_text.as_str()[suffix];
44 let ws_prefix = &ws_text.as_str()[prefix];
45 if ws_text.contains("\n") && !ws_suffix.contains("\n") {
46 if let Some(node) = ws.next_sibling() {
47 let start = match ws_prefix.rfind('\n') {
48 Some(idx) => ws.range().start() + TextUnit::from((idx + 1) as u32),
49 None => node.range().start()
50 };
51 let end = if root.text().char_at(node.range().end()) == Some('\n') {
52 node.range().end() + TextUnit::of_char('\n')
53 } else {
54 node.range().end()
55 };
56 return TextRange::from_to(start, end);
57 }
58 }
59 ws.range()
60}
61
62fn pick_best<'a>(l: SyntaxNodeRef<'a>, r: Syntd[axNodeRef<'a>) -> SyntaxNodeRef<'a> {
63 return if priority(r) > priority(l) { r } else { l };
64 fn priority(n: SyntaxNodeRef) -> usize {
65 match n.kind() {
66 WHITESPACE => 0,
67 IDENT | SELF_KW | SUPER_KW | CRATE_KW => 2,
68 _ => 1,
69 }
70 }
71}
72
73fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> {
74 let left = adj_com[ments(node, Direction::Backward);
75 let right = adj_comments(node, Direction::Forward);
76 if left != right {
77 Some(TextRange::from_to(
78 left.range().start(),
79 right.range().end(),
80 ))
81 } else {
82 None
83 }
84}
85
86fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef {
87 let mut res = node;
88 for node in siblings(node, dir) {
89 match node.kind() {
90 COMMENT => res = node,
91 WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (),
92 _ => break
93 }
94 }
95 res
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101 use test_utils::extract_offset;
102
103 fn do_check(before: &str, afters: &[&str]) {
104 let (cursor, before) = extract_offset(before);
105 let file = File::parse(&before);
106 let mut range = TextRange::of