mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2026-02-02 09:25:11 +01:00
refactor: rename Match -> Span
This commit is contained in:
parent
4fc7a75046
commit
9fbfff70f3
16 changed files with 316 additions and 330 deletions
|
|
@ -139,35 +139,35 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn simple_matches() {
|
||||
fn simple_hints() {
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let hints = alphabet.make_hints(3);
|
||||
assert_eq!(hints, ["a", "b", "c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composed_matches() {
|
||||
fn composed_hints() {
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let hints = alphabet.make_hints(6);
|
||||
assert_eq!(hints, ["a", "b", "c", "da", "db", "dc"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composed_matches_multiple() {
|
||||
fn composed_hints_multiple() {
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let hints = alphabet.make_hints(8);
|
||||
assert_eq!(hints, ["a", "b", "ca", "cb", "da", "db", "dc", "dd"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composed_matches_max_2() {
|
||||
fn composed_hints_max_2() {
|
||||
let alphabet = Alphabet("ab".to_string());
|
||||
let hints = alphabet.make_hints(4);
|
||||
assert_eq!(hints, ["aa", "ab", "ba", "bb"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composed_matches_max_4() {
|
||||
fn composed_hints_max_4() {
|
||||
let alphabet = Alphabet("abcd".to_string());
|
||||
let hints = alphabet.make_hints(13);
|
||||
assert_eq!(
|
||||
|
|
@ -177,7 +177,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn matches_with_longest_alphabet() {
|
||||
fn hints_with_longest_alphabet() {
|
||||
let alphabet = Alphabet("ab".to_string());
|
||||
let hints = alphabet.make_hints(2500);
|
||||
assert_eq!(hints.len(), 2500);
|
||||
|
|
@ -186,7 +186,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn matches_exceed_longest_alphabet() {
|
||||
fn hints_exceed_longest_alphabet() {
|
||||
let alphabet = Alphabet("ab".to_string());
|
||||
let hints = alphabet.make_hints(10000);
|
||||
// 2500 unique hints are produced from the longest alphabet
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
/// Represents matched text, its location on screen, the pattern that created
|
||||
/// it, and the associated hint.
|
||||
#[derive(Debug)]
|
||||
pub struct Match<'a> {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub pattern: &'a str,
|
||||
pub text: &'a str,
|
||||
pub hint: String,
|
||||
}
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
pub(crate) mod alphabet;
|
||||
mod matches;
|
||||
mod model;
|
||||
mod raw_match;
|
||||
mod raw_span;
|
||||
pub(crate) mod regexes;
|
||||
mod span;
|
||||
|
||||
pub use matches::Match;
|
||||
pub use model::Model;
|
||||
pub use span::Span;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
|
@ -22,7 +22,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -31,11 +31,11 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.first().unwrap().hint, "a");
|
||||
assert_eq!(results.last().unwrap().hint, "c");
|
||||
assert_eq!(spans.len(), 3);
|
||||
assert_eq!(spans.first().unwrap().hint, "a");
|
||||
assert_eq!(spans.last().unwrap().hint, "c");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -48,7 +48,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = true;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -57,11 +57,11 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.first().unwrap().hint, "a");
|
||||
assert_eq!(results.last().unwrap().hint, "a");
|
||||
assert_eq!(spans.len(), 3);
|
||||
assert_eq!(spans.first().unwrap().hint, "a");
|
||||
assert_eq!(spans.last().unwrap().hint, "a");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -74,7 +74,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -83,11 +83,11 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(spans.len(), 1);
|
||||
assert_eq!(
|
||||
results.get(0).unwrap().text,
|
||||
spans.get(0).unwrap().text,
|
||||
"30557a29d5abc51e5f1d5b472e79b7e296f595abcf19fe6b9199dbbc809c6ff4"
|
||||
);
|
||||
}
|
||||
|
|
@ -103,7 +103,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = true;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -112,12 +112,12 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "/var/log/nginx.log");
|
||||
assert_eq!(results.get(1).unwrap().text, "test/log/nginx-2.log");
|
||||
assert_eq!(results.get(2).unwrap().text, "folder/.nginx@4df2.log");
|
||||
assert_eq!(spans.len(), 3);
|
||||
assert_eq!(spans.get(0).unwrap().text, "/var/log/nginx.log");
|
||||
assert_eq!(spans.get(1).unwrap().text, "test/log/nginx-2.log");
|
||||
assert_eq!(spans.get(2).unwrap().text, "folder/.nginx@4df2.log");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -131,7 +131,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -140,12 +140,12 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().text, "/tmp/foo/bar_lol");
|
||||
assert_eq!(results.get(1).unwrap().text, "/var/log/boot-strap.log");
|
||||
assert_eq!(results.get(2).unwrap().text, "../log/kern.log");
|
||||
assert_eq!(spans.len(), 3);
|
||||
assert_eq!(spans.get(0).unwrap().text, "/tmp/foo/bar_lol");
|
||||
assert_eq!(spans.get(1).unwrap().text, "/var/log/boot-strap.log");
|
||||
assert_eq!(spans.get(2).unwrap().text, "../log/kern.log");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -158,7 +158,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -167,10 +167,10 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().text, "~/.gnu/.config.txt");
|
||||
assert_eq!(spans.len(), 1);
|
||||
assert_eq!(spans.get(0).unwrap().text, "~/.gnu/.config.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -183,7 +183,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -192,9 +192,9 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(spans.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -207,7 +207,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -216,14 +216,14 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "fd70b5695");
|
||||
assert_eq!(results.get(1).unwrap().text, "5246ddf");
|
||||
assert_eq!(results.get(2).unwrap().text, "f924213");
|
||||
assert_eq!(spans.len(), 4);
|
||||
assert_eq!(spans.get(0).unwrap().text, "fd70b5695");
|
||||
assert_eq!(spans.get(1).unwrap().text, "5246ddf");
|
||||
assert_eq!(spans.get(2).unwrap().text, "f924213");
|
||||
assert_eq!(
|
||||
results.get(3).unwrap().text,
|
||||
spans.get(3).unwrap().text,
|
||||
"973113963b491874ab2e372ee60d4b4cb75f717c"
|
||||
);
|
||||
}
|
||||
|
|
@ -238,7 +238,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -247,15 +247,15 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "ipv4");
|
||||
assert_eq!(results.get(0).unwrap().text, "127.0.0.1");
|
||||
assert_eq!(results.get(1).unwrap().pattern, "ipv4");
|
||||
assert_eq!(results.get(1).unwrap().text, "255.255.10.255");
|
||||
assert_eq!(results.get(2).unwrap().pattern, "ipv4");
|
||||
assert_eq!(results.get(2).unwrap().text, "127.0.0.1");
|
||||
assert_eq!(spans.len(), 3);
|
||||
assert_eq!(spans.get(0).unwrap().pattern, "ipv4");
|
||||
assert_eq!(spans.get(0).unwrap().text, "127.0.0.1");
|
||||
assert_eq!(spans.get(1).unwrap().pattern, "ipv4");
|
||||
assert_eq!(spans.get(1).unwrap().text, "255.255.10.255");
|
||||
assert_eq!(spans.get(2).unwrap().pattern, "ipv4");
|
||||
assert_eq!(spans.get(2).unwrap().text, "127.0.0.1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -268,7 +268,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -277,16 +277,16 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "fe80::2:202:fe4");
|
||||
assert_eq!(spans.len(), 4);
|
||||
assert_eq!(spans.get(0).unwrap().text, "fe80::2:202:fe4");
|
||||
assert_eq!(
|
||||
results.get(1).unwrap().text,
|
||||
spans.get(1).unwrap().text,
|
||||
"2001:67c:670:202:7ba8:5e41:1591:d723"
|
||||
);
|
||||
assert_eq!(results.get(2).unwrap().text, "fe80::2:1");
|
||||
assert_eq!(results.get(3).unwrap().text, "fe80:22:312:fe::1%eth0");
|
||||
assert_eq!(spans.get(2).unwrap().text, "fe80::2:1");
|
||||
assert_eq!(spans.get(3).unwrap().text, "fe80:22:312:fe::1%eth0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -300,7 +300,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -309,13 +309,13 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 2);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "markdown-url");
|
||||
assert_eq!(results.get(0).unwrap().text, "https://github.io?foo=bar");
|
||||
assert_eq!(results.get(1).unwrap().pattern, "markdown-url");
|
||||
assert_eq!(results.get(1).unwrap().text, "http://cdn.com/img.jpg");
|
||||
assert_eq!(spans.len(), 2);
|
||||
assert_eq!(spans.get(0).unwrap().pattern, "markdown-url");
|
||||
assert_eq!(spans.get(0).unwrap().text, "https://github.io?foo=bar");
|
||||
assert_eq!(spans.get(1).unwrap().pattern, "markdown-url");
|
||||
assert_eq!(spans.get(1).unwrap().text, "http://cdn.com/img.jpg");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -328,7 +328,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -337,20 +337,20 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(spans.len(), 4);
|
||||
assert_eq!(
|
||||
results.get(0).unwrap().text,
|
||||
spans.get(0).unwrap().text,
|
||||
"https://www.rust-lang.org/tools"
|
||||
);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "url");
|
||||
assert_eq!(results.get(1).unwrap().text, "https://crates.io");
|
||||
assert_eq!(results.get(1).unwrap().pattern, "url");
|
||||
assert_eq!(results.get(2).unwrap().text, "https://github.io?foo=bar");
|
||||
assert_eq!(results.get(2).unwrap().pattern, "url");
|
||||
assert_eq!(results.get(3).unwrap().text, "ssh://github.io");
|
||||
assert_eq!(results.get(3).unwrap().pattern, "url");
|
||||
assert_eq!(spans.get(0).unwrap().pattern, "url");
|
||||
assert_eq!(spans.get(1).unwrap().text, "https://crates.io");
|
||||
assert_eq!(spans.get(1).unwrap().pattern, "url");
|
||||
assert_eq!(spans.get(2).unwrap().text, "https://github.io?foo=bar");
|
||||
assert_eq!(spans.get(2).unwrap().pattern, "url");
|
||||
assert_eq!(spans.get(3).unwrap().text, "ssh://github.io");
|
||||
assert_eq!(spans.get(3).unwrap().pattern, "url");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -364,7 +364,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -373,17 +373,14 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 2);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "email");
|
||||
assert_eq!(spans.len(), 2);
|
||||
assert_eq!(spans.get(0).unwrap().pattern, "email");
|
||||
assert_eq!(spans.get(0).unwrap().text, "first.last+social@example.com");
|
||||
assert_eq!(spans.get(1).unwrap().pattern, "email");
|
||||
assert_eq!(
|
||||
results.get(0).unwrap().text,
|
||||
"first.last+social@example.com"
|
||||
);
|
||||
assert_eq!(results.get(1).unwrap().pattern, "email");
|
||||
assert_eq!(
|
||||
results.get(1).unwrap().text,
|
||||
spans.get(1).unwrap().text,
|
||||
"john@server.department.company.com"
|
||||
);
|
||||
}
|
||||
|
|
@ -398,7 +395,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -407,15 +404,15 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 3);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "mem-address");
|
||||
assert_eq!(results.get(0).unwrap().text, "0xfd70b5695");
|
||||
assert_eq!(results.get(1).unwrap().pattern, "mem-address");
|
||||
assert_eq!(results.get(1).unwrap().text, "0x5246ddf");
|
||||
assert_eq!(results.get(2).unwrap().pattern, "mem-address");
|
||||
assert_eq!(results.get(2).unwrap().text, "0x973113");
|
||||
assert_eq!(spans.len(), 3);
|
||||
assert_eq!(spans.get(0).unwrap().pattern, "mem-address");
|
||||
assert_eq!(spans.get(0).unwrap().text, "0xfd70b5695");
|
||||
assert_eq!(spans.get(1).unwrap().pattern, "mem-address");
|
||||
assert_eq!(spans.get(1).unwrap().text, "0x5246ddf");
|
||||
assert_eq!(spans.get(2).unwrap().pattern, "mem-address");
|
||||
assert_eq!(spans.get(2).unwrap().text, "0x973113");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -428,7 +425,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -437,13 +434,13 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 4);
|
||||
assert_eq!(results.get(0).unwrap().text, "#fd7b56");
|
||||
assert_eq!(results.get(1).unwrap().text, "#FF00FF");
|
||||
assert_eq!(results.get(2).unwrap().text, "#00fF05");
|
||||
assert_eq!(results.get(3).unwrap().text, "#abcd00");
|
||||
assert_eq!(spans.len(), 4);
|
||||
assert_eq!(spans.get(0).unwrap().text, "#fd7b56");
|
||||
assert_eq!(spans.get(1).unwrap().text, "#FF00FF");
|
||||
assert_eq!(spans.get(2).unwrap().text, "#00fF05");
|
||||
assert_eq!(spans.get(3).unwrap().text, "#abcd00");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -456,7 +453,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -465,11 +462,11 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(spans.len(), 1);
|
||||
assert_eq!(
|
||||
results.get(0).unwrap().text,
|
||||
spans.get(0).unwrap().text,
|
||||
"QmRdbNSxDJBXmssAc9fvTtux4duptMvfSGiGuq6yHAQVKQ"
|
||||
);
|
||||
}
|
||||
|
|
@ -484,7 +481,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -493,9 +490,9 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 8);
|
||||
assert_eq!(spans.len(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -508,7 +505,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -517,11 +514,11 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "diff-a");
|
||||
assert_eq!(results.get(0).unwrap().text, "src/main.rs");
|
||||
assert_eq!(spans.len(), 1);
|
||||
assert_eq!(spans.get(0).unwrap().pattern, "diff-a");
|
||||
assert_eq!(spans.get(0).unwrap().text, "src/main.rs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -534,7 +531,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -543,11 +540,11 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 1);
|
||||
assert_eq!(results.get(0).unwrap().pattern, "diff-b");
|
||||
assert_eq!(results.get(0).unwrap().text, "src/main.rs");
|
||||
assert_eq!(spans.len(), 1);
|
||||
assert_eq!(spans.get(0).unwrap().pattern, "diff-b");
|
||||
assert_eq!(spans.get(0).unwrap().text, "src/main.rs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -563,7 +560,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -572,22 +569,22 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 9);
|
||||
assert_eq!(results.get(0).unwrap().text, "http://foo.bar");
|
||||
assert_eq!(results.get(1).unwrap().text, "CUSTOM-52463");
|
||||
assert_eq!(results.get(2).unwrap().text, "ISSUE-123");
|
||||
assert_eq!(results.get(3).unwrap().text, "/var/fd70b569/9999.log");
|
||||
assert_eq!(results.get(4).unwrap().text, "52463");
|
||||
assert_eq!(results.get(5).unwrap().text, "973113");
|
||||
assert_eq!(spans.len(), 9);
|
||||
assert_eq!(spans.get(0).unwrap().text, "http://foo.bar");
|
||||
assert_eq!(spans.get(1).unwrap().text, "CUSTOM-52463");
|
||||
assert_eq!(spans.get(2).unwrap().text, "ISSUE-123");
|
||||
assert_eq!(spans.get(3).unwrap().text, "/var/fd70b569/9999.log");
|
||||
assert_eq!(spans.get(4).unwrap().text, "52463");
|
||||
assert_eq!(spans.get(5).unwrap().text, "973113");
|
||||
assert_eq!(
|
||||
results.get(6).unwrap().text,
|
||||
spans.get(6).unwrap().text,
|
||||
"123e4567-e89b-12d3-a456-426655440000"
|
||||
);
|
||||
assert_eq!(results.get(7).unwrap().text, "8888");
|
||||
assert_eq!(spans.get(7).unwrap().text, "8888");
|
||||
assert_eq!(
|
||||
results.get(8).unwrap().text,
|
||||
spans.get(8).unwrap().text,
|
||||
"https://crates.io/23456/fd70b569"
|
||||
);
|
||||
}
|
||||
|
|
@ -605,7 +602,7 @@ mod tests {
|
|||
let alphabet = Alphabet("abcd".to_string());
|
||||
let reverse = false;
|
||||
let unique_hint = false;
|
||||
let results = Model::new(
|
||||
let spans = Model::new(
|
||||
&lines,
|
||||
&alphabet,
|
||||
use_all_patterns,
|
||||
|
|
@ -614,12 +611,12 @@ mod tests {
|
|||
reverse,
|
||||
unique_hint,
|
||||
)
|
||||
.matches;
|
||||
.spans;
|
||||
|
||||
assert_eq!(results.len(), 2);
|
||||
assert_eq!(results.get(0).unwrap().text, "http://foo.bar");
|
||||
assert_eq!(spans.len(), 2);
|
||||
assert_eq!(spans.get(0).unwrap().text, "http://foo.bar");
|
||||
assert_eq!(
|
||||
results.get(1).unwrap().text,
|
||||
spans.get(1).unwrap().text,
|
||||
"https://crates.io/23456/fd70b569"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,22 +4,20 @@ use regex::Regex;
|
|||
use sequence_trie::SequenceTrie;
|
||||
|
||||
use super::alphabet::Alphabet;
|
||||
use super::matches::Match;
|
||||
use super::raw_match::RawMatch;
|
||||
use super::raw_span::RawSpan;
|
||||
use super::regexes::{NamedPattern, EXCLUDE_PATTERNS, PATTERNS};
|
||||
use super::span::Span;
|
||||
|
||||
/// Holds data for the `Ui`.
|
||||
pub struct Model<'a> {
|
||||
// buffer: &'a str,
|
||||
pub lines: &'a [&'a str],
|
||||
pub reverse: bool,
|
||||
pub matches: Vec<Match<'a>>,
|
||||
pub spans: Vec<Span<'a>>,
|
||||
pub lookup_trie: SequenceTrie<char, usize>,
|
||||
}
|
||||
|
||||
impl<'a> Model<'a> {
|
||||
pub fn new(
|
||||
// buffer: &'a str,
|
||||
lines: &'a [&'a str],
|
||||
alphabet: &'a Alphabet,
|
||||
use_all_patterns: bool,
|
||||
|
|
@ -28,36 +26,34 @@ impl<'a> Model<'a> {
|
|||
reverse: bool,
|
||||
unique_hint: bool,
|
||||
) -> Model<'a> {
|
||||
// let lines = buffer.split('\n').collect::<Vec<_>>();
|
||||
|
||||
let mut raw_matches =
|
||||
raw_matches(&lines, named_patterns, custom_patterns, use_all_patterns);
|
||||
let mut raw_spans =
|
||||
find_raw_spans(&lines, named_patterns, custom_patterns, use_all_patterns);
|
||||
|
||||
if reverse {
|
||||
raw_matches.reverse();
|
||||
raw_spans.reverse();
|
||||
}
|
||||
|
||||
let mut matches = associate_hints(&raw_matches, alphabet, unique_hint);
|
||||
let mut spans = associate_hints(&raw_spans, alphabet, unique_hint);
|
||||
|
||||
if reverse {
|
||||
matches.reverse();
|
||||
spans.reverse();
|
||||
}
|
||||
|
||||
let lookup_trie = build_lookup_trie(&matches);
|
||||
let lookup_trie = build_lookup_trie(&spans);
|
||||
|
||||
Model {
|
||||
// buffer,
|
||||
lines,
|
||||
reverse,
|
||||
matches,
|
||||
spans,
|
||||
lookup_trie,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal function that searches the model's lines for pattern matches.
|
||||
/// Returns a vector of `RawMatch`es (text, location, pattern id) without
|
||||
/// an associated hint. The hint is attached to `Match`, not to `RawMatch`.
|
||||
/// Returns a vector of `RawSpan` (text, location, pattern id) without
|
||||
/// an associated hint. The hint is attached to `Span`, not to `RawSpan`.
|
||||
///
|
||||
/// # Notes
|
||||
///
|
||||
|
|
@ -65,12 +61,12 @@ impl<'a> Model<'a> {
|
|||
///
|
||||
/// If no named patterns were specified, it will search for all available
|
||||
/// patterns from the `PATTERNS` catalog.
|
||||
fn raw_matches<'a>(
|
||||
fn find_raw_spans<'a>(
|
||||
lines: &'a [&'a str],
|
||||
named_patterns: &'a [NamedPattern],
|
||||
custom_patterns: &'a [String],
|
||||
use_all_patterns: bool,
|
||||
) -> Vec<RawMatch<'a>> {
|
||||
) -> Vec<RawSpan<'a>> {
|
||||
let exclude_regexes = EXCLUDE_PATTERNS
|
||||
.iter()
|
||||
.map(|&(name, pattern)| (name, Regex::new(pattern).unwrap()))
|
||||
|
|
@ -100,7 +96,7 @@ fn raw_matches<'a>(
|
|||
|
||||
let all_regexes = [exclude_regexes, custom_regexes, regexes].concat();
|
||||
|
||||
let mut raw_matches = Vec::new();
|
||||
let mut raw_spans = Vec::new();
|
||||
|
||||
for (index, line) in lines.iter().enumerate() {
|
||||
// Chunk is the remainder of the line to be searched for matches.
|
||||
|
|
@ -110,7 +106,7 @@ fn raw_matches<'a>(
|
|||
|
||||
// Use all avail regexes to match the chunk and select the match
|
||||
// occuring the earliest on the chunk. Save its matched text and
|
||||
// position in a `RawMatch` struct.
|
||||
// position in a `RawSpan` struct.
|
||||
loop {
|
||||
// For each avalable regex, use the `find_iter` iterator to
|
||||
// get the first non-overlapping match in the chunk, returning
|
||||
|
|
@ -149,7 +145,7 @@ fn raw_matches<'a>(
|
|||
None => (text, 0),
|
||||
};
|
||||
|
||||
raw_matches.push(RawMatch {
|
||||
raw_spans.push(RawSpan {
|
||||
x: offset + reg_match.start() as i32 + substart as i32,
|
||||
y: index as i32,
|
||||
pattern: pat_name,
|
||||
|
|
@ -164,54 +160,54 @@ fn raw_matches<'a>(
|
|||
}
|
||||
}
|
||||
|
||||
raw_matches
|
||||
raw_spans
|
||||
}
|
||||
|
||||
/// Associate a hint to each `RawMatch`, returning a vector of `Match`es.
|
||||
/// Associate a hint to each `RawSpan`, returning a vector of `Span`.
|
||||
///
|
||||
/// If `unique` is `true`, all duplicate matches will have the same hint.
|
||||
/// For copying matched text, this seems easier and more natural.
|
||||
/// If `unique` is `false`, duplicate matches will have their own hint.
|
||||
/// If `unique` is `true`, all duplicate spans will have the same hint.
|
||||
/// For copying text spans, this seems easier and more natural.
|
||||
/// If `unique` is `false`, duplicate spans will have their own hint.
|
||||
fn associate_hints<'a>(
|
||||
raw_matches: &[RawMatch<'a>],
|
||||
raw_spans: &[RawSpan<'a>],
|
||||
alphabet: &'a Alphabet,
|
||||
unique: bool,
|
||||
) -> Vec<Match<'a>> {
|
||||
let hints = alphabet.make_hints(raw_matches.len());
|
||||
) -> Vec<Span<'a>> {
|
||||
let hints = alphabet.make_hints(raw_spans.len());
|
||||
let mut hints_iter = hints.iter();
|
||||
|
||||
let mut result: Vec<Match<'a>> = vec![];
|
||||
let mut result: Vec<Span<'a>> = vec![];
|
||||
|
||||
if unique {
|
||||
// Map (text, hint)
|
||||
let mut known: collections::HashMap<&str, &str> = collections::HashMap::new();
|
||||
|
||||
for raw_mat in raw_matches {
|
||||
let hint: &str = known.entry(raw_mat.text).or_insert_with(|| {
|
||||
for raw_span in raw_spans {
|
||||
let hint: &str = known.entry(raw_span.text).or_insert_with(|| {
|
||||
hints_iter
|
||||
.next()
|
||||
.expect("We should have as many hints as necessary, even invisible ones.")
|
||||
});
|
||||
|
||||
result.push(Match {
|
||||
x: raw_mat.x,
|
||||
y: raw_mat.y,
|
||||
pattern: raw_mat.pattern,
|
||||
text: raw_mat.text,
|
||||
result.push(Span {
|
||||
x: raw_span.x,
|
||||
y: raw_span.y,
|
||||
pattern: raw_span.pattern,
|
||||
text: raw_span.text,
|
||||
hint: hint.to_string(),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
for raw_mat in raw_matches {
|
||||
for raw_span in raw_spans {
|
||||
let hint = hints_iter
|
||||
.next()
|
||||
.expect("We should have as many hints as necessary, even invisible ones.");
|
||||
|
||||
result.push(Match {
|
||||
x: raw_mat.x,
|
||||
y: raw_mat.y,
|
||||
pattern: raw_mat.pattern,
|
||||
text: raw_mat.text,
|
||||
result.push(Span {
|
||||
x: raw_span.x,
|
||||
y: raw_span.y,
|
||||
pattern: raw_span.pattern,
|
||||
text: raw_span.text,
|
||||
hint: hint.to_string(),
|
||||
});
|
||||
}
|
||||
|
|
@ -222,12 +218,12 @@ fn associate_hints<'a>(
|
|||
|
||||
/// Builds a `SequenceTrie` that helps determine if a sequence of keys
|
||||
/// entered by the user corresponds to a match. This kind of lookup
|
||||
/// directly returns a reference to the corresponding `Match` if any.
|
||||
fn build_lookup_trie<'a>(matches: &'a [Match<'a>]) -> SequenceTrie<char, usize> {
|
||||
/// directly returns a reference to the corresponding `Span` if any.
|
||||
fn build_lookup_trie<'a>(spans: &'a [Span<'a>]) -> SequenceTrie<char, usize> {
|
||||
let mut trie = SequenceTrie::new();
|
||||
|
||||
for (index, mat) in matches.iter().enumerate() {
|
||||
let hint_chars = mat.hint.chars().collect::<Vec<char>>();
|
||||
for (index, span) in spans.iter().enumerate() {
|
||||
let hint_chars = span.hint.chars().collect::<Vec<char>>();
|
||||
|
||||
// no need to insert twice the same hint
|
||||
if trie.get(&hint_chars).is_none() {
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
/// Internal surrogate for `Match`, before a Hint has been associated.
|
||||
#[derive(Debug)]
|
||||
pub(super) struct RawMatch<'a> {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub pattern: &'a str,
|
||||
pub text: &'a str,
|
||||
}
|
||||
8
src/textbuf/raw_span.rs
Normal file
8
src/textbuf/raw_span.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
/// Internal surrogate for `Span`, before a Hint has been associated.
|
||||
#[derive(Debug)]
|
||||
pub(super) struct RawSpan<'a> {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub pattern: &'a str,
|
||||
pub text: &'a str,
|
||||
}
|
||||
10
src/textbuf/span.rs
Normal file
10
src/textbuf/span.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/// Represents some span of text, its location on screen, the pattern that
|
||||
/// created it, and the associated hint.
|
||||
#[derive(Debug)]
|
||||
pub struct Span<'a> {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub pattern: &'a str,
|
||||
pub text: &'a str,
|
||||
pub hint: String,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue