mirror of https://github.com/helix-editor/helix
Cleanup find_first_non_whitespace_char funcs
parent
48df05b16d
commit
c978d811d9
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
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 core::ops::Range;
|
||||||
use std::borrow::Cow;
|
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
|
let mut min = usize::MAX; // minimum col for find_first_non_whitespace_char
|
||||||
for line in lines {
|
for line in lines {
|
||||||
let line_slice = text.line(line);
|
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();
|
let len = line_slice.len_chars();
|
||||||
|
|
||||||
if pos < min {
|
if pos < min {
|
||||||
|
|
|
@ -105,11 +105,14 @@ fn suggested_indent_for_line(
|
||||||
line_num: usize,
|
line_num: usize,
|
||||||
tab_width: usize,
|
tab_width: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
let line = text.line(line_num);
|
if let Some(start) = find_first_non_whitespace_char(text.line(line_num)) {
|
||||||
let current = indent_level_for_line(line, tab_width);
|
return suggested_indent_for_pos(
|
||||||
|
Some(language_config),
|
||||||
if let Some(start) = find_first_non_whitespace_char(text, line_num) {
|
syntax,
|
||||||
return suggested_indent_for_pos(Some(language_config), syntax, text, start, false);
|
text,
|
||||||
|
start + text.line_to_char(line_num),
|
||||||
|
false,
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// if the line is blank, indent should be zero
|
// if the line is blank, indent should be zero
|
||||||
|
|
|
@ -18,24 +18,9 @@ pub mod syntax;
|
||||||
mod transaction;
|
mod transaction;
|
||||||
pub mod words;
|
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())
|
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> {
|
pub fn find_root(root: Option<&str>) -> Option<std::path::PathBuf> {
|
||||||
let current_dir = std::env::current_dir().expect("unable to determine current directory");
|
let current_dir = std::env::current_dir().expect("unable to determine current directory");
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use helix_core::{
|
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,
|
match_brackets,
|
||||||
movement::{self, Direction},
|
movement::{self, Direction},
|
||||||
object, pos_at_coords,
|
object, pos_at_coords,
|
||||||
|
@ -224,7 +224,7 @@ pub fn move_first_nonwhitespace(cx: &mut Context) {
|
||||||
let text = doc.text();
|
let text = doc.text();
|
||||||
let line_idx = text.char_to_line(range.head);
|
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);
|
let pos = pos + text.line_to_char(line_idx);
|
||||||
Range::new(pos, pos)
|
Range::new(pos, pos)
|
||||||
} else {
|
} else {
|
||||||
|
@ -447,7 +447,7 @@ pub fn extend_first_nonwhitespace(cx: &mut Context) {
|
||||||
let text = doc.text();
|
let text = doc.text();
|
||||||
let line_idx = text.char_to_line(range.head);
|
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);
|
let pos = pos + text.line_to_char(line_idx);
|
||||||
Range::new(range.anchor, pos)
|
Range::new(range.anchor, pos)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue