on master help sign in
Cleanup find_first_non_whitespace_char funcs
c978d81Wojciech Kępka
helix-core/src/comment.rs+2−2
@@ -1,5 +1,5 @@
1use crate::{
1use 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,
3};
3};
4use core::ops::Range;
4use core::ops::Range;
5use std::borrow::Cow;
5use std::borrow::Cow;
@@ -14,7 +14,7 @@ fn find_line_comment(
14 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
14 let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
15 for line in lines {
15 for line in lines {
16 let line_slice = text.line(line);
16 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) {
18 let len = line_slice.len_chars();
18 let len = line_slice.len_chars();
19
19
20 if pos < min {
20 if pos < min {
helix-core/src/indent.rs+8−5
@@ -105,11 +105,14 @@ fn suggested_indent_for_line(
105 line_num: usize,
105 line_num: usize,
106 tab_width: usize,
106 tab_width: usize,
107) -> usize {
107) -> 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) {
108+ if let Some(start) = find_first_non_whitespace_char(text.line(line_num)) {
112− return suggested_indent_for_pos(Some(language_config), syntax, text, start, false);
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+ );
113 };
116 };
114
117
115 // if the line is blank, indent should be zero
118 // if the line is blank, indent should be zero
helix-core/src/lib.rs+1−16
@@ -18,24 +18,9 @@ pub mod syntax;
18mod transaction;
18mod transaction;
19pub mod words;
19pub mod words;
20
20
21−pub fn find_first_non_whitespace_char2(line: RopeSlice) -> Option<usize> {
21+pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> {
22 line.chars().position(|ch| !ch.is_whitespace())
22 line.chars().position(|ch| !ch.is_whitespace())
23}
23}
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−}
39
24
40pub fn find_root(root: Option<&str>) -> Option<std::path::PathBuf> {
25pub fn find_root(root: Option<&str>) -> Option<std::path::PathBuf> {
41 let current_dir = std::env::current_dir().expect("unable to determine current directory");
26 let current_dir = std::env::current_dir().expect("unable to determine current directory");
helix-term/src/commands.rs+3−3
@@ -1,5 +1,5 @@
1use helix_core::{
1use 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,
3 match_brackets,
3 match_brackets,
4 movement::{self, Direction},
4 movement::{self, Direction},
5 object, pos_at_coords,
5 object, pos_at_coords,
@@ -224,7 +224,7 @@ pub fn move_first_nonwhitespace(cx: &mut Context) {
224 let text = doc.text();
224 let text = doc.text();
225 let line_idx = text.char_to_line(range.head);
225 let line_idx = text.char_to_line(range.head);
226
226
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)) {
228 let pos = pos + text.line_to_char(line_idx);
228 let pos = pos + text.line_to_char(line_idx);
229 Range::new(pos, pos)
229 Range::new(pos, pos)
230 } else {
230 } else {
@@ -447,7 +447,7 @@ pub fn extend_first_nonwhitespace(cx: &mut Context) {
447 let text = doc.text();
447 let text = doc.text();
448 let line_idx = text.char_to_line(range.head);
448 let line_idx = text.char_to_line(range.head);
449
449
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)) {
451 let pos = pos + text.line_to_char(line_idx);
451 let pos = pos + text.line_to_char(line_idx);
452 Range::new(range.anchor, pos)
452 Range::new(range.anchor, pos)
453 } else {
453 } else {