Cleanup find_first_non_whitespace_char funcs

pull/179/head
Wojciech Kępka 3 years ago committed by Blaž Hrastnik
parent 48df05b16d
commit c978d811d9

@ -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 {

@ -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

@ -18,24 +18,9 @@ pub mod syntax;
mod transaction;
pub mod words;
pub fn find_first_non_whitespace_char2(line: RopeSlice) -> Option<usize> {
pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> {
line.chars().position(|ch| !ch.is_whitespace())
}
pub(crate) fn find_first_non_whitespace_char(text: RopeSlice, line_num: usize) -> Option<usize> {
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<std::path::PathBuf> {
let current_dir = std::env::current_dir().expect("unable to determine current directory");

@ -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 {

Loading…
Cancel
Save