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
154
155
156
157
use core::SearchType::{self, ExactMatch, StartsWith};
use core::Session;
use std;
use std::cmp;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
pub fn getline(filepath: &Path, linenum: usize, session: &Session) -> String {
let reader = BufReader::new(session.open_file(filepath).unwrap());
reader.lines().nth(linenum - 1).unwrap_or(Ok("not found".into())).unwrap()
}
pub fn is_pattern_char(c: char) -> bool {
c.is_alphanumeric() || c.is_whitespace() || (c == '_') || (c == ':') || (c == '.')
}
pub fn is_search_expr_char(c: char) -> bool {
c.is_alphanumeric() || (c == '_') || (c == ':') || (c == '.')
}
pub fn is_ident_char(c: char) -> bool {
c.is_alphanumeric() || (c == '_')
}
pub fn txt_matches(stype: SearchType, needle: &str, haystack: &str) -> bool {
match stype {
ExactMatch => {
let nlen = needle.len();
let hlen = haystack.len();
if nlen == 0 {
return true;
}
let mut n=0;
while let Some(n1) = haystack[n..].find(needle) {
n += n1;
if (n == 0 || !is_ident_char(char_at(haystack, n-1))) &&
(n+nlen == hlen || !is_ident_char(char_at(haystack, n+nlen))) {
return true;
}
n += 1;
}
false
},
StartsWith => {
if needle.is_empty() {
return true;
}
let mut n=0;
while let Some(n1) = haystack[n..].find(needle) {
n += n1;
if n == 0 || !is_ident_char(char_at(haystack, n-1)) {
return true;
}
n += 1;
}
false
}
}
}
pub fn symbol_matches(stype: SearchType, searchstr: &str, candidate: &str) -> bool {
match stype {
ExactMatch => searchstr == candidate,
StartsWith => candidate.starts_with(searchstr)
}
}
pub fn is_double_dot(msrc: &str, i: usize) -> bool {
(i > 1) && &msrc[i-1..i+1] == ".."
}
#[test]
fn txt_matches_matches_stuff() {
assert_eq!(true, txt_matches(ExactMatch, "Vec","Vec"));
assert_eq!(true, txt_matches(StartsWith, "Vec","Vector"));
assert_eq!(false, txt_matches(ExactMatch, "Vec","use Vector"));
assert_eq!(true, txt_matches(StartsWith, "Vec","use Vector"));
assert_eq!(false, txt_matches(StartsWith, "Vec","use aVector"));
assert_eq!(true, txt_matches(ExactMatch, "Vec","use Vec"));
}
pub fn expand_ident(s: &str, pos: usize) -> (usize, usize) {
let pos = cmp::min(s.len(), pos);
let sb = &s[..pos];
let mut start = pos;
for (i, c) in sb.char_indices().rev() {
if !is_ident_char(c) {
break;
}
start = i;
}
(start, pos)
}
pub fn find_ident_end(s: &str, pos: usize) -> usize {
let sa = &s[pos..];
for (i, c) in sa.char_indices() {
if !is_ident_char(c) {
return pos + i;
}
}
s.len()
}
#[test]
fn find_ident_end_ascii() {
assert_eq!(5, find_ident_end("ident", 0));
assert_eq!(6, find_ident_end("(ident)", 1));
assert_eq!(17, find_ident_end("let an_identifier = 100;", 4));
}
#[test]
fn find_ident_end_unicode() {
assert_eq!(7, find_ident_end("num_µs", 0));
assert_eq!(10, find_ident_end("ends_in_µ", 0));
}
pub fn char_at(src: &str, i: usize) -> char {
src[i..].chars().next().unwrap()
}
pub fn path_exists<P: AsRef<Path>>(path: P) -> bool {
is_dir(&path) || File::open(path).is_ok()
}
pub fn is_dir<P: AsRef<Path>>(path: P) -> bool {
std::fs::metadata(path).map(|info| info.is_dir()).unwrap_or(false)
}