Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smartcase regex search #761

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ as you type completion!

- [ ] lsp: signature help

- [ ] search: smart case by default: insensitive unless upper detected

2
- [ ] macro recording
- [ ] extend selection (treesitter select parent node) (replaces viw, vi(, va( etc )
Expand Down
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ To override global configuration parameters, create a `config.toml` file located
| `scroll-lines` | Number of lines to scroll per scroll wheel step. | `3` |
| `shell` | Shell to use when running external commands. | Unix: `["sh", "-c"]`<br/>Windows: `["cmd", "/C"]` |
| `line-number` | Line number display (`absolute`, `relative`) | `absolute` |
| `smart-case` | Enable smart case regex searching (case insensitive unless pattern contains upper case characters) | `true` |

## LSP

Expand Down
3 changes: 1 addition & 2 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@

### Search

> TODO: The search implementation isn't ideal yet -- we don't support searching
in reverse, or searching via smartcase.
> TODO: The search implementation isn't ideal yet -- we don't support searching in reverse.

| Key | Description | Command |
| ----- | ----------- | ------- |
Expand Down
12 changes: 10 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use helix_core::{
match_brackets,
movement::{self, Direction},
object, pos_at_coords,
regex::{self, Regex},
regex::{self, Regex, RegexBuilder},
register::Register,
search, selection, surround, textobject, LineEnding, Position, Range, Rope, RopeGraphemes,
RopeSlice, Selection, SmallVec, Tendril, Transaction,
Expand Down Expand Up @@ -1154,7 +1154,15 @@ fn search_next_impl(cx: &mut Context, extend: bool) {
if let Some(query) = registers.read('/') {
let query = query.last().unwrap();
let contents = doc.text().slice(..).to_string();
if let Ok(regex) = Regex::new(query) {
let case_insensitive = if cx.editor.config.smart_case {
!query.chars().any(char::is_uppercase)
} else {
false
};
if let Ok(regex) = RegexBuilder::new(query)
.case_insensitive(case_insensitive)
.build()
{
search_impl(doc, view, &contents, &regex, extend);
} else {
// get around warning `mutable_borrow_reservation_conflict`
Expand Down
12 changes: 11 additions & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use spinner::{ProgressSpinners, Spinner};
pub use text::Text;

use helix_core::regex::Regex;
use helix_core::regex::RegexBuilder;
use helix_view::{Document, Editor, View};

use std::path::PathBuf;
Expand Down Expand Up @@ -53,7 +54,16 @@ pub fn regex_prompt(
return;
}

match Regex::new(input) {
let case_insensitive = if cx.editor.config.smart_case {
!input.chars().any(char::is_uppercase)
} else {
false
};

match RegexBuilder::new(input)
.case_insensitive(case_insensitive)
.build()
{
Ok(regex) => {
let (view, doc) = current!(cx.editor);

Expand Down
3 changes: 3 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct Config {
pub line_number: LineNumber,
/// Middle click paste support. Defaults to true
pub middle_click_paste: bool,
/// Smart case: Case insensitive searching unless pattern contains upper case characters. Defaults to true.
pub smart_case: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
Expand All @@ -64,6 +66,7 @@ impl Default for Config {
},
line_number: LineNumber::Absolute,
middle_click_paste: true,
smart_case: true,
}
}
}
Expand Down