on master help sign in
enable smart case regex search by default (#761)
commit4a003782a51a94259ef3b5ddfacb2a148c5056e7 authorkraem <ebrin.ronnie@protonmail.com> dateMon 20 Sep 2021, 06:45 (5 years ago) parente0e41f4
6 files changed+26−7
TODO.md−2 book/src/configuration.md+1 book/src/keymap.md+1−2 helix-term/src/commands.rs+10−2 helix-term/src/ui/mod.rs+11−1 helix-view/src/editor.rs+3
TODO.md−2
@@ -22,8 +22,6 @@ as you type completion!
2222
2323- [ ] lsp: signature help
2424
25−- [ ] search: smart case by default: insensitive unless upper detected
26−
27252
2826- [ ] macro recording
2927- [ ] extend selection (treesitter select parent node) (replaces viw, vi(, va( etc )
book/src/configuration.md+1
@@ -17,6 +17,7 @@ To override global configuration parameters, create a `config.toml` file located
1717| `scroll-lines` | Number of lines to scroll per scroll wheel step. | `3` |
1818| `shell` | Shell to use when running external commands. | Unix: `["sh", "-c"]`<br/>Windows: `["cmd", "/C"]` |
1919| `line-number` | Line number display (`absolute`, `relative`) | `absolute` |
20+| `smart-case` | Enable smart case regex searching (case insensitive unless pattern contains upper case characters) | `true` |
2021
2122## LSP
2223
book/src/keymap.md+1−2
@@ -104,8 +104,7 @@
104104
105105### Search
106106
107−> TODO: The search implementation isn't ideal yet -- we don't support searching
108−in reverse, or searching via smartcase.
107+> TODO: The search implementation isn't ideal yet -- we don't support searching in reverse.
109108
110109| Key | Description | Command |
111110| ----- | ----------- | ------- |
helix-term/src/commands.rs+10−2
@@ -5,7 +5,7 @@ use helix_core::{
55 match_brackets,
66 movement::{self, Direction},
77 object, pos_at_coords,
8− regex::{self, Regex},
8+ regex::{self, Regex, RegexBuilder},
99 register::Register,
1010 search, selection, surround, textobject, LineEnding, Position, Range, Rope, RopeGraphemes,
1111 RopeSlice, Selection, SmallVec, Tendril, Transaction,
@@ -1154,7 +1154,15 @@ fn search_next_impl(cx: &mut Context, extend: bool) {
11541154 if let Some(query) = registers.read('/') {
11551155 let query = query.last().unwrap();
11561156 let contents = doc.text().slice(..).to_string();
1157− if let Ok(regex) = Regex::new(query) {
1157+ let case_insensitive = if cx.editor.config.smart_case {
1158+ !query.chars().any(char::is_uppercase)
1159+ } else {
1160+ false
1161+ };
1162+ if let Ok(regex) = RegexBuilder::new(query)
1163+ .case_insensitive(case_insensitive)
1164+ .build()
1165+ {
11581166 search_impl(doc, view, &contents, &regex, extend);
11591167 } else {
11601168 // get around warning `mutable_borrow_reservation_conflict`
helix-term/src/ui/mod.rs+11−1
@@ -20,6 +20,7 @@ pub use spinner::{ProgressSpinners, Spinner};
2020pub use text::Text;
2121
2222use helix_core::regex::Regex;
23+use helix_core::regex::RegexBuilder;
2324use helix_view::{Document, Editor, View};
2425
2526use std::path::PathBuf;
@@ -53,7 +54,16 @@ pub fn regex_prompt(
5354 return;
5455 }
5556
56− match Regex::new(input) {
57+ let case_insensitive = if cx.editor.config.smart_case {
58+ !input.chars().any(char::is_uppercase)
59+ } else {
60+ false
61+ };
62+
63+ match RegexBuilder::new(input)
64+ .case_insensitive(case_insensitive)
65+ .build()
66+ {
5767 Ok(regex) => {
5868 let (view, doc) = current!(cx.editor);
5969
helix-view/src/editor.rs+3
@@ -39,6 +39,8 @@ pub struct Config {
3939 pub line_number: LineNumber,
4040 /// Middle click paste support. Defaults to true
4141 pub middle_click_paste: bool,
42+ /// Smart case: Case insensitive searching unless pattern contains upper case characters. Defaults to true.
43+ pub smart_case: bool,
4244}
4345
4446#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
@@ -64,6 +66,7 @@ impl Default for Config {
6466 },
6567 line_number: LineNumber::Absolute,
6668 middle_click_paste: true,
69+ smart_case: true,
6770 }
6871 }
6972}