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

line_search: search lines in current buffer #3462

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ This layer is a kludge of mappings, mostly pickers.
| `Y` | Yank main selection to clipboard | `yank_main_selection_to_clipboard` |
| `R` | Replace selections by clipboard contents | `replace_selections_with_clipboard` |
| `/` | Global search in workspace folder | `global_search` |
| `l` | Search lines in current buffer | `line_search` |
| `?` | Open command palette | `command_palette` |

> TIP: Global search displays results in a fuzzy picker, use `space + '` to bring it back up after opening a file.
Expand Down
87 changes: 87 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl MappableCommand {
extend_search_prev, "Add previous search match to selection",
search_selection, "Use current selection as search pattern",
global_search, "Global search in workspace folder",
line_search, "Search lines in current buffer",
extend_line, "Select current line, if already selected, extend to next line",
extend_line_above, "Select current line, if already selected, extend to previous line",
extend_to_line_bounds, "Extend selection to line bounds",
Expand Down Expand Up @@ -1938,6 +1939,92 @@ fn global_search(cx: &mut Context) {
cx.jobs.callback(show_picker);
}

fn line_search(cx: &mut Context) {
#[derive(Debug)]
struct LineResult {
/// 0 indexed lines
line_num: usize,
text: String,
}

impl ui::menu::Item for LineResult {
type Data = ();

fn label(&self, _: &Self::Data) -> Spans {
self.text.as_str().into()
}
}

let (all_matches_sx, all_matches_rx) = tokio::sync::mpsc::unbounded_channel::<LineResult>();

let reg = cx.register.unwrap_or('/');

let completions = search_completions(cx, Some(reg));
ui::regex_prompt(
cx,
"line-search:".into(),
Some(reg),
move |_editor: &Editor, input: &str| {
completions
.iter()
.filter(|comp| comp.starts_with(input))
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
.collect()
},
move |_view, doc, regex, event| {
if event != PromptEvent::Validate {
return;
}

for (num, line) in doc.text().lines().enumerate() {
if line.as_str().map_or(false, |l| regex.is_match(l)) {
all_matches_sx
.send(LineResult {
line_num: num,
text: line.as_str().map_or_else(String::new, |s| s.to_string()),
})
.unwrap();
}
}
},
);

let show_picker = async move {
let all_matches: Vec<LineResult> =
UnboundedReceiverStream::new(all_matches_rx).collect().await;
let call: job::Callback =
Box::new(move |editor: &mut Editor, compositor: &mut Compositor| {
if all_matches.is_empty() {
editor.set_status("No matches found");
return;
}

let picker = FilePicker::new(
all_matches,
(),
move |cx, LineResult { line_num, text: _ }, _action| {
let line_num = *line_num;
let (view, doc) = current!(cx.editor);
let text = doc.text();
let start = text.line_to_char(line_num);
let end = text.line_to_char((line_num + 1).min(text.len_lines()));

doc.set_selection(view.id, Selection::single(start, end));
align_view(doc, view, Align::Center);
},
|editor, LineResult { line_num, text: _ }| {
let current_path = doc!(editor).path().cloned();

current_path.map(|p| (p, Some((*line_num, *line_num))))
},
);
compositor.push(Box::new(overlayed(picker)));
});
Ok(call)
};
cx.jobs.callback(show_picker);
}

enum Extend {
Above,
Below,
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"P" => paste_clipboard_before,
"R" => replace_selections_with_clipboard,
"/" => global_search,
"l" => line_search,
"k" => hover,
"r" => rename_symbol,
"h" => select_references_to_symbol_under_cursor,
Expand Down