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 add a small bonus based on the first selected index in the
22 /// haystack, after scoring the alignment and its internal gaps. Internal
23 /// gap penalties cannot consume this bonus.
24 ///
25 /// The bonus is fairly small compared to the normal gap
26 /// penalty to avoid messing with the normal score heuristic. This setting
27 /// is not turned on by default and only recommended for autocompletion
28 /// usecases where the expectation is that the user is typing the entire
29 /// match. For a full `fzf`-like fuzzy matcher/picker word segmentation and
30 /// explicit prefix literals should be used instead.
31 pub prefer_prefix: bool,
32}
33
34impl Config {
35 /// The default configuration for nucleo, implemented as a constant since
36 /// [`Default::default`] cannot be called in a `const` context.
37 pub const DEFAULT: Self = {
38 Config {
39 delimiter_chars: b"/,:;|",
40 bonus_boundary_white: BONUS_BOUNDARY + 2,
41 bonus_boundary_delimiter: BONUS_BOUNDARY + 1,
42 initial_char_class: CharClass::Whitespace,
43 normalize: true,
44 ignore_case: true,
45 prefer_prefix: false,
46 }
47 };
48}
49
50impl Config {
51 /// Configures the matcher with bonuses appropriate for matching file paths.
52 pub const fn set_match_paths(&mut self) {
53 self.delimiter_chars = if cfg!(windows) { b"/:\\" } else { b"/:" };
54 self.bonus_boundary_white = BONUS_BOUNDARY;
55 self.initial_char_class = CharClass::Delimiter;
56 }
57
58 /// Configures the matcher with bonuses appropriate for matching file paths.
59 pub const fn match_paths(mut self) -> Self {
60 self.set_match_paths();
61 self
62 }
63}