chore: import upstream snapshot with attribution
Python CI / Python bindings (ubuntu-latest) (push) Failing after 0s
Build & Publish / Build Neovim aarch64-linux-android (push) Failing after 0s
Build & Publish / Build Neovim aarch64-unknown-linux-gnu (push) Failing after 0s
Build & Publish / Build MCP x86_64-unknown-linux-musl (push) Failing after 1s
Build & Publish / Build Python wheels aarch64 (ubuntu-latest) (push) Failing after 0s
Build & Publish / Build Python wheels x86_64 (ubuntu-latest) (push) Failing after 1s
Build & Publish / Build Python sdist (push) Failing after 1s
Rust CI / Fuzz Tests (ubuntu-latest) (push) Failing after 1s
Rust CI / Build i686-unknown-linux-gnu (push) Failing after 0s
Rust CI / cargo clippy (push) Failing after 1s
e2e Tests / e2e (ubuntu-latest) (push) Failing after 1s
Lua CI / luacheck lint (push) Failing after 1s
Build & Publish / Build Neovim aarch64-unknown-linux-musl (push) Failing after 1s
Build & Publish / Build Neovim x86_64-unknown-linux-gnu (push) Failing after 0s
Build & Publish / Build Neovim x86_64-unknown-linux-musl (push) Failing after 1s
Build & Publish / Build C FFI aarch64-linux-android (push) Failing after 0s
Build & Publish / Build C FFI aarch64-unknown-linux-gnu (push) Failing after 0s
Build & Publish / Build C FFI aarch64-unknown-linux-musl (push) Failing after 1s
Build & Publish / Build C FFI x86_64-unknown-linux-gnu (push) Failing after 0s
Build & Publish / Build C FFI x86_64-unknown-linux-musl (push) Failing after 1s
Build & Publish / Build MCP aarch64-unknown-linux-gnu (push) Failing after 1s
Build & Publish / Build MCP aarch64-unknown-linux-musl (push) Failing after 0s
Build & Publish / Build MCP x86_64-unknown-linux-gnu (push) Failing after 1s
Spelling / Spell Check with Typos (push) Failing after 0s
Rust CI / Test (ubuntu-latest) (push) Failing after 0s
Rust CI / cargo fmt (push) Failing after 0s
Stylua / Check lua files using Stylua (push) Failing after 1s
Lua CI / lua-language-server type check (push) Failing after 12m29s
e2e Tests / e2e (alpine-musl) (push) Successful in 57m31s
e2e Tests / e2e (macos-latest) (push) Has been cancelled
Build & Publish / Build C FFI aarch64-apple-darwin (push) Has been cancelled
Rust CI / Test (windows-latest) (push) Has been cancelled
e2e Tests / e2e (windows-latest) (push) Has been cancelled
Nix CI / check (push) Has been cancelled
Python CI / Python bindings (macos-latest) (push) Has been cancelled
Python CI / Python bindings (windows-latest) (push) Has been cancelled
Build & Publish / Build Neovim aarch64-apple-darwin (push) Has been cancelled
Build & Publish / Build Neovim aarch64-pc-windows-msvc (push) Has been cancelled
Build & Publish / Build Neovim x86_64-apple-darwin (push) Has been cancelled
Build & Publish / Build Neovim x86_64-pc-windows-msvc (push) Has been cancelled
Build & Publish / Build C FFI aarch64-pc-windows-msvc (push) Has been cancelled
Build & Publish / Build C FFI x86_64-apple-darwin (push) Has been cancelled
Build & Publish / Build C FFI x86_64-pc-windows-msvc (push) Has been cancelled
Build & Publish / Build MCP aarch64-apple-darwin (push) Has been cancelled
Build & Publish / Build MCP aarch64-pc-windows-msvc (push) Has been cancelled
Build & Publish / Build MCP x86_64-apple-darwin (push) Has been cancelled
Build & Publish / Build MCP x86_64-pc-windows-msvc (push) Has been cancelled
Build & Publish / Build Python wheels aarch64 (macos-latest) (push) Has been cancelled
Build & Publish / Build Python wheels x86_64 (macos-latest) (push) Has been cancelled
Build & Publish / Build Python wheels x86_64 (windows-latest) (push) Has been cancelled
Build & Publish / Release (push) Has been cancelled
Build & Publish / Publish Python wheels to PyPI (push) Has been cancelled
Build & Publish / Publish Rust crates (push) Has been cancelled
Build & Publish / Publish npm packages (push) Has been cancelled
Rust CI / Fuzz Tests (windows-latest) (push) Has been cancelled
Rust CI / Test (macos-latest) (push) Has been cancelled
Rust CI / Fuzz Tests (macos-latest) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 12:27:16 +08:00
commit d5f207424b
328 changed files with 87463 additions and 0 deletions
+248
View File
@@ -0,0 +1,248 @@
use crate::constraints::Constraint;
use crate::glob_detect::has_wildcards;
/// Check if a token looks like a filename/path for use as a `FilePath` constraint.
#[deprecated(note = "use `Constraint::is_filename_constraint_token`")]
pub fn is_filename_constraint_token(token: &str) -> bool {
Constraint::is_filename_constraint_token(token)
}
/// Parser configuration trait - allows different picker types to customize parsing
pub trait ParserConfig {
fn enable_glob(&self) -> bool {
true
}
/// Should parse extension shortcuts (e.g., *.rs)
fn enable_extension(&self) -> bool {
true
}
/// Should parse exclusion patterns (e.g., !test)
fn enable_exclude(&self) -> bool {
true
}
/// Should parse path segments (e.g., /src/)
fn enable_path_segments(&self) -> bool {
true
}
/// Should parse type constraints (e.g., type:rust)
fn enable_type_filter(&self) -> bool {
true
}
/// Should parse git status (e.g., status:modified)
fn enable_git_status(&self) -> bool {
true
}
/// Should parse location suffixes (e.g., file:12, file:12:4)
/// Disabled for grep modes where colon-number patterns like localhost:8080
/// are search text, not file locations.
fn enable_location(&self) -> bool {
true
}
/// Determine whether a token should be treated as a glob constraint.
///
/// The default implementation delegates to `zlob::has_wildcards` with
/// `RECOMMENDED` flags, which recognises `*`, `?`, `[`, `{…}` etc.
///
/// Override this in configs where some wildcard characters are common
/// in search text (e.g. grep mode where `?` and `[` appear in code).
fn is_glob_pattern(&self, token: &str) -> bool {
has_wildcards(token)
}
/// If `true`, a PathSegment constraint that is the ONLY token in the
/// query is demoted to fuzzy text to avoid over filtering
fn treat_lone_path_as_text(&self) -> bool {
true
}
/// If `true`, tokens that look like filenames (`score.rs`, `src/main.rs`)
/// become `FilePath` constraints that scope the search to matching paths.
/// Off by default: a partial filename like `vite.conf` would otherwise
/// filter out `vite.config.ts` and yield zero results.
fn enable_filename_constraint(&self) -> bool {
false
}
/// Custom constraint parsers for picker-specific needs.
///
/// Returns `None` by default. Filename-token detection is handled
/// separately by the parser via `enable_filename_constraint`.
fn parse_custom<'a>(&self, _input: &'a str) -> Option<Constraint<'a>> {
None
}
}
/// Default configuration for the file picker.
///
/// Filename-constraint detection is off (trait default), so partial filenames
/// like `vite.conf` don't silently filter out fuzzy matches. The Neovim layer
/// overrides `enable_filename_constraint` to opt in based on user config.
#[derive(Debug, Clone, Copy, Default)]
pub struct FileSearchConfig;
impl ParserConfig for FileSearchConfig {}
/// Configuration for full-text search (grep) - file constraints enabled for
/// filtering which files to search, git status disabled since it's not useful
/// when searching file contents.
///
/// Glob detection is narrowed: only patterns containing a path separator (`/`)
/// or brace expansion (`{…}`) are treated as globs. Characters like `?` and
/// `[` are extremely common in source code and must remain literal search text.
#[derive(Debug, Clone, Copy, Default)]
pub struct GrepConfig;
impl ParserConfig for GrepConfig {
fn enable_path_segments(&self) -> bool {
true
}
fn enable_git_status(&self) -> bool {
false
}
fn enable_location(&self) -> bool {
false
}
/// Only recognise globs that are clearly directory/path oriented.
///
/// Characters like `?`, `[`, and bare `*` (without `/`) are extremely
/// common in source code (`foo?`, `arr[0]`, `*ptr`) and must NOT be
/// consumed as glob constraints. We only treat a token as a glob when
/// it contains path-oriented patterns:
///
/// - Contains `/` → path glob (e.g. `src/**/*.rs`, `*/tests/*`)
/// - Contains `{…}` → brace expansion (e.g. `{src,lib}`)
fn is_glob_pattern(&self, token: &str) -> bool {
// Must contain at least one glob wildcard character
if !has_wildcards(token) {
return false;
}
let bytes = token.as_bytes();
// Contains path separator → clearly a path glob
if bytes.contains(&b'/') {
return true;
}
// Brace expansion → useful for directory alternatives.
// Require a comma between `{` and `}` AND at least one letter to
// distinguish real glob expansions like `{src,lib}` or `*.{ts,tsx}`
// from code patterns like `format!("{}")` and regex quantifiers `{2,3}`.
// Guard `open < close` so reversed braces like `}{` don't panic on slicing.
if let Some(open) = bytes.iter().position(|&b| b == b'{')
&& let Some(close) = bytes.iter().rposition(|&b| b == b'}')
&& open < close
{
let inner = &bytes[open + 1..close];
if inner.contains(&b',') && inner.iter().any(|b| b.is_ascii_alphabetic()) {
return true;
}
}
// Everything else (?, [, bare * without /) → treat as literal text
false
}
}
/// Configuration for AI-mode grep — extends `GrepConfig` behavior with
/// automatic file-path constraint detection.
///
/// Bare filenames with valid extensions (`schema.rs`) and path-prefixed
/// filenames (`libswscale/input.c`) are detected as `FilePath` constraints
/// so the search is scoped to matching files. The caller validates the
/// constraint against the index and drops it if no files match (fallback).
#[derive(Debug, Clone, Copy, Default)]
pub struct AiGrepConfig;
/// Configuration for directory and mixed search modes.
///
/// Disables path segment parsing so that trailing `/` is kept as fuzzy text
/// (e.g. `fff-core/` fuzzy-matches directory paths instead of becoming a
/// `PathSegment("fff-core")` constraint with an empty query). Extension and
/// filename constraints are also disabled since they don't apply to directories.
#[derive(Debug, Clone, Copy, Default)]
pub struct DirSearchConfig;
impl ParserConfig for AiGrepConfig {
fn enable_path_segments(&self) -> bool {
true
}
fn enable_git_status(&self) -> bool {
false
}
fn enable_location(&self) -> bool {
false
}
fn enable_filename_constraint(&self) -> bool {
true
}
fn is_glob_pattern(&self, token: &str) -> bool {
// First check GrepConfig's strict rules (path globs, brace expansion)
if GrepConfig.is_glob_pattern(token) {
return true;
}
// AI agents use `*text*` to scope file searches (e.g. `*quote* TODO`).
// Recognise tokens that start AND end with `*` with non-empty text
// between them as glob constraints. Bare `*` or `**` are excluded.
if !has_wildcards(token) {
return false;
}
let bytes = token.as_bytes();
if bytes.len() >= 3
&& bytes[0] == b'*'
&& bytes[bytes.len() - 1] == b'*'
&& bytes[1..bytes.len() - 1].iter().all(|&b| b != b'*')
{
return true;
}
false
}
}
impl ParserConfig for DirSearchConfig {
fn enable_path_segments(&self) -> bool {
false
}
fn enable_extension(&self) -> bool {
false
}
fn enable_type_filter(&self) -> bool {
false
}
fn enable_git_status(&self) -> bool {
false
}
}
/// Configuration for mixed (files + directories) search.
///
/// Like `DirSearchConfig`, disables path segment parsing so trailing `/`
/// triggers dirs-only mode instead of becoming a constraint. Keeps git
/// status and extension filters enabled since files are part of the results.
#[derive(Debug, Clone, Copy, Default)]
pub struct MixedSearchConfig;
impl ParserConfig for MixedSearchConfig {
fn enable_path_segments(&self) -> bool {
false
}
}
@@ -0,0 +1,83 @@
use crate::glob_detect::has_wildcards;
/// Constraint types that can be extracted from a query
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Constraint<'a> {
/// Match file extension: *.rs -> Extension("rs")
Extension(&'a str),
/// Glob pattern: **/*.rs -> Glob("**/*.rs")
Glob(&'a str),
/// Multiple text search parts: ["src", "name"]
/// Uses slice to avoid allocation
Parts(&'a [&'a str]),
/// Single text token (optimized case)
Text(&'a str),
/// Exclude pattern: !test -> Exclude(&["test"])
Exclude(&'a [&'a str]),
/// Path constraint: /src/ -> PathSegment("src")
PathSegment(&'a str),
/// File path constraint (AI mode): "libswscale/input.c" → FilePath("libswscale/input.c")
/// Matches files whose relative path ends with this suffix at a `/` boundary.
FilePath(&'a str),
/// File type constraint: type:rust -> FileType("rust")
FileType(&'a str),
/// Git status constraint: status:modified -> GitStatus(Modified)
GitStatus(GitStatusFilter),
/// Negation constraint: !extension:rs -> Not(Extension("rs"))
/// Negates the inner constraint
Not(Box<Constraint<'a>>),
}
impl Constraint<'_> {
#[inline(always)]
pub fn is_filename_constraint_token(token: &str) -> bool {
let bytes = token.as_bytes();
// Must NOT end with / or .
if token.is_empty() || (bytes.last() == Some(&b'/') && bytes.first() != Some(&b'.')) {
return false;
}
// Must NOT contain wildcards (those are globs)
if has_wildcards(token) {
return false;
}
// Get the filename component (after last /)
let filename = token.rsplit('/').next().unwrap_or(token);
// Extension must exist and look like a real file extension:
// starts with an ASCII letter (rejects version numbers like "v2.0"),
// followed by alphanumeric chars, max 10 chars total.
match filename.rfind('.') {
None => false,
Some(dot_idx) => {
let extension = &filename[dot_idx + 1..];
!extension.is_empty()
&& extension.len() <= 10 // just an sassumption
&& extension.bytes().all(|b| b.is_ascii_alphanumeric())
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GitStatusFilter {
Modified,
Untracked,
Staged,
Unmodified,
}
/// Buffer for text parts during query parsing.
pub(crate) type TextPartsBuffer<'a> = Vec<&'a str>;
@@ -0,0 +1,18 @@
//! Glob wildcard detection — delegates to zlob when available, pure-Rust fallback otherwise.
//!
//! All call sites use a single function: `has_wildcards(text) -> bool`.
//! When the `zlob` feature is enabled this calls `zlob::has_wildcards` with
//! `ZlobFlags::RECOMMENDED`; without it we check for the same set of wildcard
//! characters (`*`, `?`, `[`, `{`) in pure Rust.
#[cfg(feature = "zlob")]
#[inline]
pub fn has_wildcards(s: &str) -> bool {
zlob::has_wildcards(s, zlob::ZlobFlags::RECOMMENDED)
}
#[cfg(not(feature = "zlob"))]
#[inline]
pub fn has_wildcards(s: &str) -> bool {
s.bytes().any(|b| matches!(b, b'*' | b'?' | b'[' | b'{'))
}
+235
View File
@@ -0,0 +1,235 @@
//! Fast query parser for file search
//!
//! This parser takes a search query and extracts structured constraints
//! while preserving text for fuzzy matching. Designed for maximum performance:
//! - Single-pass parsing with minimal branching
//! - Stack-allocated string buffers
//!
//! # Examples
//!
//! ```
//! use fff_query_parser::{QueryParser, Constraint, FuzzyQuery};
//!
//! let parser = QueryParser::default();
//!
//! // Single-token queries return FFFQuery with Text fuzzy query and no constraints
//! let result = parser.parse("hello");
//! assert!(result.constraints.is_empty());
//! assert_eq!(result.fuzzy_query, FuzzyQuery::Text("hello"));
//!
//! // Multi-token queries are parsed
//! let result = parser.parse("name *.rs");
//! match &result.fuzzy_query {
//! FuzzyQuery::Text(text) => assert_eq!(*text, "name"),
//! _ => panic!("Expected text"),
//! }
//! assert!(matches!(result.constraints[0], Constraint::Extension("rs")));
//!
//! // Parse glob pattern with text
//! let result = parser.parse("**/*.rs foo");
//! assert!(matches!(result.constraints[0], Constraint::Glob("**/*.rs")));
//!
//! // Parse negation
//! let result = parser.parse("!*.rs foo");
//! match &result.constraints[0] {
//! Constraint::Not(inner) => {
//! assert!(matches!(inner.as_ref(), Constraint::Extension("rs")));
//! }
//! _ => panic!("Expected Not constraint"),
//! }
//! ```
mod config;
mod constraints;
pub mod glob_detect;
pub mod location;
mod parser;
#[allow(deprecated)]
pub use config::is_filename_constraint_token;
pub use config::{
AiGrepConfig, DirSearchConfig, FileSearchConfig, GrepConfig, MixedSearchConfig, ParserConfig,
};
pub use constraints::{Constraint, GitStatusFilter};
pub use location::Location;
pub use parser::{FFFQuery, FuzzyQuery, QueryParser};
pub type ConstraintVec<'a> = Vec<Constraint<'a>>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_query() {
let parser = QueryParser::default();
let result = parser.parse("");
assert!(result.constraints.is_empty());
assert_eq!(result.fuzzy_query, FuzzyQuery::Empty);
}
#[test]
fn test_whitespace_only() {
let parser = QueryParser::default();
let result = parser.parse(" ");
assert!(result.constraints.is_empty());
assert_eq!(result.fuzzy_query, FuzzyQuery::Empty);
}
#[test]
fn test_single_token() {
let parser = QueryParser::default();
let result = parser.parse("hello");
assert!(result.constraints.is_empty());
assert_eq!(result.fuzzy_query, FuzzyQuery::Text("hello"));
}
#[test]
fn test_simple_text() {
let parser = QueryParser::default();
let result = parser.parse("hello world");
match &result.fuzzy_query {
FuzzyQuery::Parts(parts) => {
assert_eq!(parts.len(), 2);
assert_eq!(parts[0], "hello");
assert_eq!(parts[1], "world");
}
_ => panic!("Expected Parts fuzzy query"),
}
assert_eq!(result.constraints.len(), 0);
}
#[test]
fn test_extension_only() {
let parser = QueryParser::default();
// Single constraint token - returns Some so constraint can be applied
let result = parser.parse("*.rs");
assert!(matches!(result.fuzzy_query, FuzzyQuery::Empty));
assert_eq!(result.constraints.len(), 1);
assert!(matches!(result.constraints[0], Constraint::Extension("rs")));
}
#[test]
fn test_glob_pattern() {
let parser = QueryParser::default();
let result = parser.parse("**/*.rs foo");
assert_eq!(result.constraints.len(), 1);
// Glob patterns with ** are treated as globs, not extensions
match &result.constraints[0] {
Constraint::Glob(pattern) => assert_eq!(*pattern, "**/*.rs"),
other => panic!("Expected Glob constraint, got {:?}", other),
}
}
#[test]
fn test_negation_pattern() {
let parser = QueryParser::default();
let result = parser.parse("!test foo");
assert_eq!(result.constraints.len(), 1);
match &result.constraints[0] {
Constraint::Not(inner) => {
assert!(matches!(**inner, Constraint::Text("test")));
}
_ => panic!("Expected Not constraint"),
}
}
#[test]
fn test_path_segment() {
let parser = QueryParser::default();
let result = parser.parse("/src/ foo");
assert_eq!(result.constraints.len(), 1);
assert!(matches!(
result.constraints[0],
Constraint::PathSegment("src")
));
}
#[test]
fn test_git_status() {
let parser = QueryParser::default();
let result = parser.parse("status:modified foo");
assert_eq!(result.constraints.len(), 1);
assert!(matches!(
result.constraints[0],
Constraint::GitStatus(GitStatusFilter::Modified)
));
}
#[test]
fn test_file_type() {
let parser = QueryParser::default();
let result = parser.parse("type:rust foo");
assert_eq!(result.constraints.len(), 1);
assert!(matches!(
result.constraints[0],
Constraint::FileType("rust")
));
}
#[test]
fn test_complex_query() {
let parser = QueryParser::default();
let result = parser.parse("src name *.rs !test /lib/ status:modified");
// Verify we have fuzzy text
match &result.fuzzy_query {
FuzzyQuery::Parts(parts) => {
assert_eq!(parts.len(), 2);
assert_eq!(parts[0], "src");
assert_eq!(parts[1], "name");
}
_ => panic!("Expected Parts fuzzy query"),
}
// Should have multiple constraints
assert!(result.constraints.len() >= 4);
// Verify specific constraints exist
let has_extension = result
.constraints
.iter()
.any(|c| matches!(c, Constraint::Extension("rs")));
let has_not = result
.constraints
.iter()
.any(|c| matches!(c, Constraint::Not(_)));
let has_path = result
.constraints
.iter()
.any(|c| matches!(c, Constraint::PathSegment("lib")));
let has_git_status = result
.constraints
.iter()
.any(|c| matches!(c, Constraint::GitStatus(_)));
assert!(has_extension, "Should have Extension constraint");
assert!(has_not, "Should have Not constraint");
assert!(has_path, "Should have PathSegment constraint");
assert!(has_git_status, "Should have GitStatus constraint");
}
#[test]
fn test_small_constraint_count() {
let parser = QueryParser::default();
let result = parser.parse("*.rs *.toml !test");
assert_eq!(result.constraints.len(), 3);
}
#[test]
fn test_many_fuzzy_parts() {
let parser = QueryParser::default();
let result = parser.parse("one two three four five six");
match &result.fuzzy_query {
FuzzyQuery::Parts(parts) => {
assert_eq!(parts.len(), 6);
assert_eq!(parts[0], "one");
assert_eq!(parts[5], "six");
}
_ => panic!("Expected Parts fuzzy query"),
}
}
}
+265
View File
@@ -0,0 +1,265 @@
//! Location parsing for file:line:col patterns
//!
//! Parses various location formats like:
//! - `file:12` - Line number
//! - `file:12:4` - Line and column
//! - `file:12-114` - Line range
//! - `file:12:4-20` - Column range on same line
//! - `file:12:4-14:20` - Position range
//! - `file(12)` - Visual Studio style line
//! - `file(12,4)` - Visual Studio style line and column
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Location {
Line(i32),
Range { start: (i32, i32), end: (i32, i32) },
Position { line: i32, col: i32 },
}
fn parse_number_pair(location: &str, split_char: char) -> Option<(i32, i32)> {
let mut iter = location.split(split_char);
let start_str = iter.next()?;
let end_str = iter.next()?;
// if there are more than 2 parts it's not the range treat as normal query
if iter.next().is_some() {
return None;
}
let start = start_str.parse::<i32>().ok()?;
let end = end_str.parse::<i32>().ok()?;
Some((start, end))
}
/// Parse "line-line" format
fn parse_simple_range(location: &str) -> Option<Location> {
let (start, end) = parse_number_pair(location, '-')?;
if end < start {
return Some(Location::Line(start));
}
Some(Location::Range {
start: (start, 0),
end: (end, 0),
})
}
/// Parse "line:col-col" format (column range on same line)
fn parse_column_range(start_part: &str, end_part: &str) -> Option<Location> {
let (line_str, start_col_str) = start_part.split_once(':')?;
let line = line_str.parse::<i32>().ok()?;
let start_col = start_col_str.parse::<i32>().ok()?;
let end_col = end_part.parse::<i32>().ok()?;
if end_col < start_col {
return Some(Location::Line(line));
}
Some(Location::Range {
start: (line, start_col),
end: (line, end_col),
})
}
/// Parse "line:col-line:col" format (position range)
fn parse_position_range(start_part: &str, end_part: &str) -> Option<Location> {
let (start_line, start_col) = parse_number_pair(start_part, ':')?;
let (end_line, end_col) = parse_number_pair(end_part, ':')?;
if end_line < start_line || (end_line == start_line && end_col < start_col) {
return Some(Location::Position {
line: start_line,
col: start_col,
});
}
Some(Location::Range {
start: (start_line, start_col),
end: (end_line, end_col),
})
}
/// Try to parse range patterns (contains '-')
fn try_parse_column_range(location: &str) -> Option<Location> {
if !location.contains('-') {
return None;
}
let (start_part, end_part) = location.split_once('-')?;
// Try position range (line:col-line:col)
if start_part.contains(':') && end_part.contains(':') {
return parse_position_range(start_part, end_part);
}
// Try column range (line:col-col)
if start_part.contains(':') {
return parse_column_range(start_part, end_part);
}
// Try simple line range (line-line)
parse_simple_range(location)
}
/// Try to parse position patterns (contains ':' but not '-')
fn try_parse_column_position(location: &str) -> Option<Location> {
if !location.contains(':') {
return None;
}
let (line_str, col_str) = location.split_once(':')?;
let line = line_str.parse::<i32>().ok()?;
let col = col_str.parse::<i32>().ok()?;
Some(Location::Position { line, col })
}
/// Parses various location formats like file:12, file:12:4, file:12-114
fn parse_column_location(query: &str) -> Option<(&str, Location)> {
let (file_path, location_part) = query.split_once(':')?;
if let Some(range_location) = try_parse_column_range(location_part) {
return Some((file_path, range_location));
}
if let Some(position_location) = try_parse_column_position(location_part) {
return Some((file_path, position_location));
}
if let Ok(line_location) = location_part.parse::<i32>() {
return Some((file_path, Location::Line(line_location)));
}
None
}
fn parse_vstudio_location(query: &str) -> Option<(&str, Location)> {
if !query.ends_with(')') {
return None;
}
let (file_path, location_with_paren) = query.rsplit_once('(')?;
let location = location_with_paren.trim_end_matches(')');
if let Ok(line) = location.parse::<i32>() {
return Some((file_path, Location::Line(line)));
}
if let Some((line, col)) = parse_number_pair(location, ',') {
return Some((file_path, Location::Position { line, col }));
}
None
}
/// Parse location from the end of a query string.
///
/// Returns the query without the location suffix, and the parsed location if found.
///
/// # Examples
/// ```
/// use fff_query_parser::location::{parse_location, Location};
///
/// let (query, loc) = parse_location("file:12");
/// assert_eq!(query, "file");
/// assert_eq!(loc, Some(Location::Line(12)));
///
/// let (query, loc) = parse_location("search term");
/// assert_eq!(query, "search term");
/// assert_eq!(loc, None);
/// ```
pub fn parse_location(query: &str) -> (&str, Option<Location>) {
// simply ignore the last semicolon even if there are no additional location info
let query = query.trim_end_matches([':', '-', '(']);
if let Some((path, location)) = parse_column_location(query) {
return (path, Some(location));
}
if let Some((path, location)) = parse_vstudio_location(query) {
return (path, Some(location));
}
(query, None)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_location_parsing() {
assert_eq!(
parse_location("new_file:12"),
("new_file", Some(Location::Line(12)))
);
assert_eq!(parse_location("new_file:12ab"), ("new_file:12ab", None));
assert_eq!(parse_location("something"), ("something", None));
assert_eq!(
parse_location("file:12:4"),
("file", Some(Location::Position { line: 12, col: 4 }))
);
assert_eq!(
parse_location("file:12-114"),
(
"file",
Some(Location::Range {
start: (12, 0),
end: (114, 0)
})
)
);
assert_eq!(
parse_location("file:12:4-20"),
(
"file",
Some(Location::Range {
start: (12, 4),
end: (12, 20)
})
)
);
assert_eq!(
parse_location("file:100:4-14:20"),
("file", Some(Location::Position { line: 100, col: 4 }))
);
assert_eq!(
parse_location("file:12:4-14:20"),
(
"file",
Some(Location::Range {
start: (12, 4),
end: (14, 20)
})
)
);
}
#[test]
fn test_vstudio_parsing() {
assert_eq!(
parse_location("file(12)"),
("file", Some(Location::Line(12)))
);
assert_eq!(
parse_location("file(12,4)"),
("file", Some(Location::Position { line: 12, col: 4 }))
);
}
#[test]
fn trimes_end_character() {
assert_eq!(
parse_location("file:12-"),
("file", Some(Location::Line(12)))
);
assert_eq!(parse_location("file:-"), ("file", None));
assert_eq!(parse_location("file("), ("file", None));
}
}
File diff suppressed because it is too large Load Diff