-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpath_search.js
More file actions
84 lines (72 loc) · 2.9 KB
/
path_search.js
File metadata and controls
84 lines (72 loc) · 2.9 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
// This file is part of PeakRDL-html <https://github.com/SystemRDL/PeakRDL-html>.
// and can be redistributed under the terms of GNU LGPLv3 <https://www.gnu.org/licenses/>.
class PathSearch {
static #MAX_RESULTS = 100;
static #SEARCH_CHOMP_SIZE = 100;
static async start(query, abortSignal){
if(query.length < 2) return;
// Sanitize query
var sp_query = query.split(" ");
var keywords = [];
for(var i=0; i<sp_query.length; i++){
if(sp_query[i] == "") continue;
keywords.push(sp_query[i]);
}
var match_count = 0;
for(var id=0; id<RAL.number_of_ids(); id++){
var path = RAL.get_path(id, null, false);
if(id % this.#SEARCH_CHOMP_SIZE == (this.#SEARCH_CHOMP_SIZE-1)){
// Occasionally insert break to allow other events to continue
await take_a_break();
if(abortSignal.aborted) return;
}
// Search regular path
var text_segments = this.#test_path(path, keywords);
if(text_segments != null){
// direct node path matched
add_search_result(text_segments, id);
match_count++;
} else {
// No match yet. If this node has fields, add them to the path and try that
if(RAL.is_register(id)){
var node = RAL.get_node(id);
for(var i=0; i<node.fields.length; i++){
var path_with_field = path + "." + node.fields[i].name;
text_segments = this.#test_path(path_with_field, keywords);
if(text_segments != null){
add_search_result(text_segments, id, null, node.fields[i].name);
match_count++;
}
}
}
}
if(match_count >= this.#MAX_RESULTS) {
break;
}
}
}
static #test_path(path, keywords){
// If match, returns text_segments.
// Otherwise, null
var path_lc = path.toLowerCase();
var text_segments = [];
var start = 0;
// Scan path to see if all keywords match
for(var i=0; i<keywords.length; i++){
var result = path_lc.indexOf(keywords[i], start);
if(result < 0){
// Did not match
return(null);
}
// matched!
// extract non-highlighted text before
text_segments.push(path.slice(start, result));
// highlighted text
text_segments.push(path.slice(result, result + keywords[i].length));
// move search start for next keyword
start = result + keywords[i].length;
}
text_segments.push(path.slice(start, path.length));
return(text_segments);
}
}