ncp_matcher/config.rs
1use crate::chars::CharClass;
2use crate::score::BONUS_BOUNDARY;
3
4/// Configuration data that controls matcher behaviour.
5#[non_exhaustive]
6#[derive(PartialEq, Eq, Debug, Clone)]
7pub struct Config {
8 /// Characters that act as delimiters and provide bonus
9 /// for matching the following char
10 pub(crate) delimiter_chars: &'static [u8],
11 /// Extra bonus for word boundary after whitespace character or beginning of the string
12 pub(crate) bonus_boundary_white: u16,
13 /// Extra bonus for word boundary after slash, colon, semi-colon, and comma
14 pub(crate) bonus_boundary_delimiter: u16,
15 pub(crate) initial_char_class: CharClass,
16
17 /// Whether to normalize latin script characters to ASCII (enabled by default).
18 pub normalize: bool,
19 /// Whether to ignore casing.
20 pub ignore_case: bool,
21 /// Whether to provide a bonus to matches by their distance from the start
22 /// of the haystack. The bonus is fairly small compared to the normal gap
23 /// penalty to avoid messing with the normal score heuristic. This setting
24 /// is not turned on by default and only recommended for autocompletion
25 /// usecases where the expectation is that the user is typing the entire
26 /// match. For a full `fzf`-like fuzzy matcher/picker word segmentation and
27 /// explicit prefix literals should be used instead.
28 pub prefer_prefix: bool,
29}
30
31impl Config {
32 /// The default configuration for nucleo, implemented as a constant since
33 /// [`Default::default`] cannot be called in a `const` context.
34 pub const DEFAULT: Self = {
35 Config {
36 delimiter_chars: b"/,:;|",
37 bonus_boundary_white: BONUS_BOUNDARY + 2,
38 bonus_boundary_delimiter: BONUS_BOUNDARY + 1,
39 initial_char_class: CharClass::Whitespace,
40 normalize: true,
41 ignore_case: true,
42 prefer_prefix: false,
43 }
44 };
45}
46
47impl Config {
48 /// Configures the matcher with bonuses appropriate for matching file paths.
49 pub fn set_match_paths(&mut self) {
50 if cfg!(windows) {
51 self.delimiter_chars = b"/:\\";
52 } else {
53 self.delimiter_chars = b"/:";
54 }
55 self.bonus_boundary_white = BONUS_BOUNDARY;
56 self.initial_char_class = CharClass::Delimiter;
57 }
58
59 /// Configures the matcher with bonuses appropriate for matching file paths.
60 pub const fn match_paths(mut self) -> Self {
61 if cfg!(windows) {
62 self.delimiter_chars = b"/\\";
63 } else {
64 self.delimiter_chars = b"/";
65 }
66 self.bonus_boundary_white = BONUS_BOUNDARY;
67 self.initial_char_class = CharClass::Delimiter;
68 self
69 }
70}