use regex::Regex;
use crate::models::SymbolKind;
#[derive(Debug, Clone)]
pub struct QueryFilter {
pub language: Option<crate::models::Language>,
pub kind: Option<SymbolKind>,
pub use_ast: bool,
pub use_regex: bool,
pub limit: Option<usize>,
pub symbols_mode: bool,
pub expand: bool,
pub file_pattern: Option<String>,
pub exact: bool,
pub use_contains: bool,
pub ignore_case: bool,
#[doc(hidden)]
pub rewritten_from: Option<String>,
pub exclude_text: bool,
pub include_locks: bool,
pub include_generated: bool,
pub count_only: bool,
pub timeout_secs: u64,
pub glob_patterns: Vec<String>,
pub exclude_patterns: Vec<String>,
pub paths_only: bool,
pub offset: Option<usize>,
pub force: bool,
pub suppress_output: bool,
pub include_dependencies: bool,
pub context_lines: usize,
pub collect_timings: bool,
pub require_exact_total: bool,
#[doc(hidden)]
pub test_large_index_threshold: Option<usize>,
#[doc(hidden)]
pub test_short_pattern_threshold: Option<usize>,
}
impl Default for QueryFilter {
fn default() -> Self {
Self {
language: None,
kind: None,
use_ast: false,
use_regex: false,
limit: Some(100), symbols_mode: false,
expand: false,
file_pattern: None,
exact: false,
use_contains: false, ignore_case: false, rewritten_from: None,
exclude_text: false, include_locks: false, include_generated: false, count_only: false, timeout_secs: 30, glob_patterns: Vec::new(),
exclude_patterns: Vec::new(),
paths_only: false,
offset: None,
force: false, suppress_output: false, include_dependencies: false, context_lines: 0, collect_timings: false, require_exact_total: false, test_large_index_threshold: None, test_short_pattern_threshold: None, }
}
}
impl QueryFilter {
pub fn count_only_line_search(&self) -> bool {
self.count_only
&& self.limit.is_none()
&& !self.symbols_mode
&& !self.use_ast
&& self.kind.is_none()
}
}
pub fn tier_admits(
lang: crate::models::Language,
want: Option<crate::models::Language>,
exclude_text: bool,
include_locks: bool,
include_generated: bool,
) -> bool {
use crate::models::Language;
if let Some(want) = want {
return lang == want;
}
match lang {
Language::Text => !exclude_text,
Language::Lock => include_locks,
Language::Generated => include_generated,
_ => true,
}
}
pub fn excluded_by_default(
lang: crate::models::Language,
want: Option<crate::models::Language>,
include_locks: bool,
include_generated: bool,
) -> bool {
use crate::models::Language;
want.is_none()
&& match lang {
Language::Lock => !include_locks,
Language::Generated => !include_generated,
_ => false,
}
}
pub fn excluded_by_default_hint_text(files: usize) -> String {
format!(
"{} candidate file(s) were lock or generated files, which every search leaves \
out by default — pass include_locks:true / include_generated:true \
(--include-locks / --include-generated on the CLI), or lang:\"lock\" / \
lang:\"generated\", to search them.",
files
)
}
pub fn keyword_to_kind(keyword: &str) -> Option<SymbolKind> {
match keyword {
"class" => Some(SymbolKind::Class),
"struct" => Some(SymbolKind::Struct),
"enum" => Some(SymbolKind::Enum),
"interface" => Some(SymbolKind::Interface),
"trait" => Some(SymbolKind::Trait),
"type" => Some(SymbolKind::Type),
"record" => Some(SymbolKind::Struct), "function" | "fn" | "def" | "func" => Some(SymbolKind::Function),
"const" | "static" => Some(SymbolKind::Constant),
"var" | "let" => Some(SymbolKind::Variable),
"mod" | "module" | "namespace" => Some(SymbolKind::Module),
"impl" | "async" => None,
_ => None,
}
}
pub fn has_word_boundary_match(line: &str, pattern: &str) -> bool {
let escaped_pattern = regex::escape(pattern);
let pattern_with_boundaries = format!(r"\b{}\b", escaped_pattern);
if let Ok(re) = Regex::new(&pattern_with_boundaries) {
re.is_match(line)
} else {
log::debug!(
"Word boundary regex failed for pattern '{}', falling back to substring",
pattern
);
line.contains(pattern)
}
}
pub const REGEX_ONLY_CHARS: &[char] = &['(', ')', '[', ']', '{', '}', '<', '>'];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LiteralPattern {
pub effective: String,
pub use_regex: bool,
pub warning: Option<String>,
}
pub fn prepare_literal_pattern(pattern: &str, filter: &QueryFilter) -> LiteralPattern {
let not_a_line_search = filter.use_ast || filter.symbols_mode || filter.kind.is_some();
let not_a_literal_line_search = filter.use_regex || filter.use_contains || not_a_line_search;
let mut out = if not_a_literal_line_search || !pattern.contains(REGEX_ONLY_CHARS) {
LiteralPattern {
effective: pattern.to_string(),
use_regex: filter.use_regex,
warning: None,
}
} else {
LiteralPattern {
effective: regex::escape(pattern),
use_regex: true,
warning: Some(format!(
"Pattern {:?} contains brackets, which a whole-identifier search can never \
match. Searched it as an escaped regex instead (substring semantics). For \
explicit control use a regex search (search_code with regex:true / --regex) \
or substring mode (contains:true / --contains).",
pattern
)),
}
};
if filter.ignore_case && !not_a_line_search {
let body = if filter.use_regex {
pattern.to_string()
} else if filter.use_contains || out.use_regex {
regex::escape(pattern)
} else {
format!(r"\b{}\b", regex::escape(pattern))
};
out.effective = format!("(?i){}", body);
out.use_regex = true;
}
out
}
pub fn substring_hint_text(count: usize, pattern: &str) -> String {
format!(
"0 whole-identifier matches; {} substring matches — pass contains:true \
(--contains on the CLI) to see them. Reflex matches whole identifiers by \
default, so {:?} does not match longer names that merely contain it.",
count, pattern
)
}
pub fn no_symbol_parser_warning(filter: &QueryFilter) -> Option<String> {
let language = filter.language?;
let symbol_search = filter.symbols_mode || filter.kind.is_some();
(symbol_search
&& language.is_code()
&& !crate::parsers::ParserFactory::has_symbol_parser(language))
.then(|| {
format!(
"{0:?} has no symbol parser, so a symbol search finds no {0:?} definitions. \
Full-text search (without symbols / kind) still finds them.",
language
)
})
}
pub fn regex_syntax_warning(pattern: &str, filter: &QueryFilter) -> Option<String> {
if filter.use_regex || filter.use_ast || pattern.contains(REGEX_ONLY_CHARS) {
return None;
}
let escape = pattern
.as_bytes()
.windows(2)
.any(|w| w[0] == b'\\' && b"wWsSdDbB.()[]{}|+*?^$".contains(&w[1]));
(escape || pattern.contains(".*") || pattern.contains(".+")).then(|| {
format!(
"Pattern {:?} looks like a regular expression, but it was searched as literal \
text. Use search_code with regex:true (--regex on the CLI) to search it as a \
regex.",
pattern
)
})
}
#[cfg(test)]
mod literal_pattern_tests {
use super::*;
fn plain() -> QueryFilter {
QueryFilter::default()
}
#[test]
fn regex_syntax_in_a_literal_search_warns() {
for p in [r"impl\s+\w+\s+for", r"foo\.bar", r"get_.*_id", r"^\d+"] {
let w = regex_syntax_warning(p, &plain()).unwrap_or_else(|| panic!("{p}"));
assert!(w.contains("search_code with regex:true"), "{w}");
}
let contains = QueryFilter {
use_contains: true,
..plain()
};
assert!(regex_syntax_warning(r"\w+_id", &contains).is_some());
let regex = QueryFilter {
use_regex: true,
..plain()
};
assert!(regex_syntax_warning(r"impl\s+\w+", ®ex).is_none());
for p in [
r"fn (get|set)_\w+",
"verify_csrf",
r#"\"name\""#,
r"C:\\temp",
"a.b",
r"\n",
] {
assert!(regex_syntax_warning(p, &plain()).is_none(), "{p}");
}
}
#[test]
fn bracket_patterns_are_escaped_and_routed_to_regex() {
for p in [
"unwrap()",
"#[derive(",
"-> Result<",
"RealmId::nil()",
"vec![]",
] {
let got = prepare_literal_pattern(p, &plain());
assert!(got.use_regex, "{p} must run as a regex");
assert_eq!(got.effective, regex::escape(p));
let w = got.warning.expect("rewrite must be reported");
assert!(w.contains("bracket"), "{w}");
assert!(w.contains(p), "warning names the original pattern: {w}");
}
}
#[test]
fn plain_identifiers_are_untouched() {
let got = prepare_literal_pattern("verify_csrf", &plain());
assert_eq!(
got,
LiteralPattern {
effective: "verify_csrf".into(),
use_regex: false,
warning: None
}
);
}
#[test]
fn ignore_case_becomes_a_case_insensitive_regex() {
let ic = QueryFilter {
ignore_case: true,
..plain()
};
let got = prepare_literal_pattern("realm.id", &ic);
assert_eq!(got.effective, r"(?i)\brealm\.id\b");
assert!(got.use_regex);
assert!(got.warning.is_none());
let got = prepare_literal_pattern(
"realm.id",
&QueryFilter {
use_contains: true,
..ic.clone()
},
);
assert_eq!(got.effective, r"(?i)realm\.id");
assert!(got.warning.is_none());
let got = prepare_literal_pattern(
r"realm_?id\b",
&QueryFilter {
use_regex: true,
..ic.clone()
},
);
assert_eq!(got.effective, r"(?i)realm_?id\b");
let got = prepare_literal_pattern("unwrap()", &ic);
assert_eq!(got.effective, r"(?i)unwrap\(\)");
assert!(got.warning.is_some());
for f in [
QueryFilter {
symbols_mode: true,
..ic.clone()
},
QueryFilter {
kind: Some(SymbolKind::Function),
..ic.clone()
},
QueryFilter {
use_ast: true,
..ic.clone()
},
] {
let got = prepare_literal_pattern("RealmId", &f);
assert_eq!(got.effective, "RealmId");
assert!(!got.use_regex);
}
}
#[test]
fn non_literal_modes_are_never_rewritten() {
let cases: Vec<(&str, QueryFilter)> = vec![
(
"contains",
QueryFilter {
use_contains: true,
..plain()
},
),
(
"regex",
QueryFilter {
use_regex: true,
..plain()
},
),
(
"symbols",
QueryFilter {
symbols_mode: true,
..plain()
},
),
(
"kind",
QueryFilter {
kind: Some(SymbolKind::Function),
..plain()
},
),
(
"ast",
QueryFilter {
use_ast: true,
..plain()
},
),
];
for (name, f) in cases {
let got = prepare_literal_pattern("unwrap()", &f);
assert_eq!(
got.effective, "unwrap()",
"{name}: pattern must be verbatim"
);
assert_eq!(
got.use_regex, f.use_regex,
"{name}: regex flag passes through"
);
assert!(got.warning.is_none(), "{name}: no warning");
}
}
#[test]
fn hint_names_the_count_and_both_switches() {
let h = substring_hint_text(89, "verify_csrf");
assert!(h.contains("89 substring match"));
assert!(h.contains("contains:true"));
assert!(h.contains("--contains"));
assert!(h.contains("verify_csrf"));
}
}