on master help sign in
Cleanup find_first_non_whitespace_char funcs
commitc978d811d96d03acf04ddbe5af66fff57c17e287 authorWojciech Kępka <wojtek.kepka@protonmail.com> dateTue 8 Jun 2021, 09:11 (5 years ago) parent48df05b
4 files changed+14−26
helix-core/src/comment.rs+2−2 helix-core/src/indent.rs+8−5 helix-core/src/lib.rs+1−16 helix-term/src/commands.rs+3−3
helix-core/src/comment.rs+2−2
@@ -1,5 +1,5 @@
11use crate::{
2− find_first_non_whitespace_char2, Change, Rope, RopeSlice, Selection, Tendril, Transaction,
2+ find_first_non_whitespace_char, Change, Rope, RopeSlice, Selection, Tendril, Transaction,
33};
44use core::ops::Range;
55use std::borrow::Cow;
@@ -14,7 +14,7 @@ fn find_line_comment(
1414 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
1515 for line in lines {
1616 let line_slice = text.line(line);
17− if let Some(pos) = find_first_non_whitespace_char2(line_slice) {
17+ if let Some(pos) = find_first_non_whitespace_char(line_slice) {
1818 let len = line_slice.len_chars();
1919
2020 if pos < min {
helix-core/src/indent.rs+8−5
@@ -105,11 +105,14 @@ fn suggested_indent_for_line(
105105 line_num: usize,
106106 tab_width: usize,
107107) -> usize {
108− let line = text.line(line_num);
109− let current = indent_level_for_line(line, tab_width);
110−
111− if let Some(start) = find_first_non_whitespace_char(text, line_num) {
112− return suggested_indent_for_pos(Some(language_config), syntax, text, start, false);
108+ if let Some(start) = find_first_non_whitespace_char(text.line(line_num)) {
109+ return suggested_indent_for_pos(
110+ Some(language_config),
111+ syntax,
112+ text,
113+ start + text.line_to_char(line_num),
114+ false,
115+ );
113116 };
114117
115118 // if the line is blank, indent should be zero
helix-core/src/lib.rs+1−16
@@ -18,24 +18,9 @@ pub mod syntax;
1818mod transaction;
1919pub mod words;
2020
21−pub fn find_first_non_whitespace_char2(line: RopeSlice) -> Option<usize> {
21+pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> {
2222 line.chars().position(|ch| !ch.is_whitespace())
2323}
24−pub(crate) fn find_first_non_whitespace_char(text: RopeSlice, line_num: usize) -> Option<usize> {
25− let line = text.line(line_num);
26− let mut start = text.line_to_char(line_num);
27−
28− // find first non-whitespace char
29− for ch in line.chars() {
30− // TODO: could use memchr with chunks?
31− if ch != ' ' && ch != '\t' && ch != '\n' {
32− return Some(start);
33− }
34− start += 1;
35− }
36−
37− None
38−}
3924
4025pub fn find_root(root: Option<&str>) -> Option<std::path::PathBuf> {
4126 let current_dir = std::env::current_dir().expect("unable to determine current directory");
helix-term/src/commands.rs+3−3
@@ -1,5 +1,5 @@
11use helix_core::{
2− comment, coords_at_pos, find_first_non_whitespace_char2, find_root, graphemes, indent,
2+ comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent,
33 match_brackets,
44 movement::{self, Direction},
55 object, pos_at_coords,
@@ -224,7 +224,7 @@ pub fn move_first_nonwhitespace(cx: &mut Context) {
224224 let text = doc.text();
225225 let line_idx = text.char_to_line(range.head);
226226
227− if let Some(pos) = find_first_non_whitespace_char2(text.line(line_idx)) {
227+ if let Some(pos) = find_first_non_whitespace_char(text.line(line_idx)) {
228228 let pos = pos + text.line_to_char(line_idx);
229229 Range::new(pos, pos)
230230 } else {
@@ -447,7 +447,7 @@ pub fn extend_first_nonwhitespace(cx: &mut Context) {
447447 let text = doc.text();
448448 let line_idx = text.char_to_line(range.head);
449449
450− if let Some(pos) = find_first_non_whitespace_char2(text.line(line_idx)) {
450+ if let Some(pos) = find_first_non_whitespace_char(text.line(line_idx)) {
451451 let pos = pos + text.line_to_char(line_idx);
452452 Range::new(range.anchor, pos)
453453 } else {