From c978d811d96d03acf04ddbe5af66fff57c17e287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20K=C4=99pka?= Date: Tue, 8 Jun 2021 09:11:45 +0200 Subject: [PATCH] Cleanup find_first_non_whitespace_char funcs --- helix-core/src/comment.rs | 4 ++-- helix-core/src/indent.rs | 13 ++++++++----- helix-core/src/lib.rs | 17 +---------------- helix-term/src/commands.rs | 6 +++--- 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index fd9d9058b..dff99babd 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -1,5 +1,5 @@ use crate::{ - find_first_non_whitespace_char2, Change, Rope, RopeSlice, Selection, Tendril, Transaction, + find_first_non_whitespace_char, Change, Rope, RopeSlice, Selection, Tendril, Transaction, }; use core::ops::Range; use std::borrow::Cow; @@ -14,7 +14,7 @@ fn find_line_comment( let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char for line in lines { let line_slice = text.line(line); - if let Some(pos) = find_first_non_whitespace_char2(line_slice) { + if let Some(pos) = find_first_non_whitespace_char(line_slice) { let len = line_slice.len_chars(); if pos < min { diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 7ab810fd2..9032d660e 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -105,11 +105,14 @@ fn suggested_indent_for_line( line_num: usize, tab_width: usize, ) -> usize { - let line = text.line(line_num); - let current = indent_level_for_line(line, tab_width); - - if let Some(start) = find_first_non_whitespace_char(text, line_num) { - return suggested_indent_for_pos(Some(language_config), syntax, text, start, false); + if let Some(start) = find_first_non_whitespace_char(text.line(line_num)) { + return suggested_indent_for_pos( + Some(language_config), + syntax, + text, + start + text.line_to_char(line_num), + false, + ); }; // if the line is blank, indent should be zero diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index da48ba7ec..f5e7de520 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -18,24 +18,9 @@ pub mod syntax; mod transaction; pub mod words; -pub fn find_first_non_whitespace_char2(line: RopeSlice) -> Option { +pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option { line.chars().position(|ch| !ch.is_whitespace()) } -pub(crate) fn find_first_non_whitespace_char(text: RopeSlice, line_num: usize) -> Option { - let line = text.line(line_num); - let mut start = text.line_to_char(line_num); - - // find first non-whitespace char - for ch in line.chars() { - // TODO: could use memchr with chunks? - if ch != ' ' && ch != '\t' && ch != '\n' { - return Some(start); - } - start += 1; - } - - None -} pub fn find_root(root: Option<&str>) -> Option { let current_dir = std::env::current_dir().expect("unable to determine current directory"); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index fc1b363c9..d8e377c87 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,5 +1,5 @@ use helix_core::{ - comment, coords_at_pos, find_first_non_whitespace_char2, find_root, graphemes, indent, + comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent, match_brackets, movement::{self, Direction}, object, pos_at_coords, @@ -224,7 +224,7 @@ pub fn move_first_nonwhitespace(cx: &mut Context) { let text = doc.text(); let line_idx = text.char_to_line(range.head); - if let Some(pos) = find_first_non_whitespace_char2(text.line(line_idx)) { + if let Some(pos) = find_first_non_whitespace_char(text.line(line_idx)) { let pos = pos + text.line_to_char(line_idx); Range::new(pos, pos) } else { @@ -447,7 +447,7 @@ pub fn extend_first_nonwhitespace(cx: &mut Context) { let text = doc.text(); let line_idx = text.char_to_line(range.head); - if let Some(pos) = find_first_non_whitespace_char2(text.line(line_idx)) { + if let Some(pos) = find_first_non_whitespace_char(text.line(line_idx)) { let pos = pos + text.line_to_char(line_idx); Range::new(range.anchor, pos) } else {