Skip to content

Commit

Permalink
Select fields by substring in the picker query
Browse files Browse the repository at this point in the history
So you can select a field by a substring of its name, like
`%p:syntax.rs` to specify the `path` field as "syntax.rs".

Co-authored-by: ItsEthra <107059409+ItsEthra@users.noreply.github.com>
  • Loading branch information
the-mikedavis and ItsEthra committed Jan 23, 2024
1 parent d5d7320 commit 3a8ce21
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,10 +1126,14 @@ fn parse_query(
'%' => in_field = true,
' ' => (),
':' if in_field => {
// Go over all columns and their indices, find all that starts with field key,
// select a column that fits key the most.
field = column_names
.iter()
.position(|name| name == &text)
.map(|idx| column_names[idx]);
.filter(|col| col.starts_with(&text))
// select "fittest" column
.min_by_key(|col| col.len())
.copied();
text.clear();
in_field = false;
}
Expand Down Expand Up @@ -1224,7 +1228,7 @@ mod test {

#[test]
fn parse_query_test() {
let columns = &["primary", "field1", "field2"];
let columns = &["primary", "field1", "field2", "another", "anode"];
let primary_column = 0;

// Basic field splitting
Expand Down Expand Up @@ -1291,5 +1295,35 @@ mod test {
"field1" => "a\"b".to_string(),
)
);

// Prefix
assert_eq!(
parse_query(columns, primary_column, "hello %anot:abc"),
hashmap!(
"primary" => "hello".to_string(),
"another" => "abc".to_string(),
)
);
assert_eq!(
parse_query(columns, primary_column, "hello %ano:abc"),
hashmap!(
"primary" => "hello".to_string(),
"anode" => "abc".to_string()
)
);
assert_eq!(
parse_query(columns, primary_column, "hello %field1:xyz %fie:abc"),
hashmap!(
"primary" => "hello".to_string(),
"field1" => "xyz abc".to_string()
)
);
assert_eq!(
parse_query(columns, primary_column, "hello %fie:abc"),
hashmap!(
"primary" => "hello".to_string(),
"field1" => "abc".to_string()
)
);
}
}

0 comments on commit 3a8ce21

Please sign in to comment.