-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TAB key in TextEdit, fill the space with different sizes #3879
base: master
Are you sure you want to change the base?
Changes from 15 commits
175cd45
634aa80
3f8e70a
569be33
bcfb054
a367c89
f7ccce4
2a7de1b
76da7b7
296c040
b0aba92
b4ff232
cea7ebb
d0ff012
cf1b3dc
829f6c3
f63537f
8a16102
12a9180
d042b92
919d984
b6014c3
3d51de9
7487725
01642f5
db071f0
f07170c
2b4a271
66e11b6
cc55f8f
60f2917
bc39148
85c6202
82ae3ac
4ad5579
fec401b
24a1e93
e38f7ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,18 +302,108 @@ pub fn find_line_start(text: &str, current_index: CCursor) -> CCursor { | |
// number of multi byte chars. | ||
// We need to know the char index to be able to correctly set the cursor | ||
// later. | ||
let chars_count = text.chars().count(); | ||
let mut char_line_indexes: Vec<usize> = create_char_line_indexes(text); | ||
let line_start_index = get_line_start_index(&mut char_line_indexes, current_index.index); | ||
|
||
let position = text | ||
.chars() | ||
.rev() | ||
.skip(chars_count - current_index.index) | ||
.position(|x| x == '\n'); | ||
CCursor::new(line_start_index) | ||
} | ||
|
||
pub fn create_char_line_indexes(text: &str) -> Vec<usize> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a good name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not from an English-speaking country, so it's hard for me to choose even a single word. |
||
let mut line_indexes = vec![0]; | ||
|
||
let mut count = 0; | ||
for c in text.chars() { | ||
count += 1; | ||
if c == '\n' { | ||
line_indexes.push(count); | ||
} | ||
} | ||
line_indexes.push(count); | ||
|
||
line_indexes | ||
} | ||
|
||
pub fn get_line_start_index(line_indexes: &mut [usize], index: usize) -> usize { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a lot of naked There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this, we can easily find out the current Colume and line within TextEdit. I'm using this, and I'm sure others might want to use it too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it return a byte index or a char index? The name doesn't tell me, neither does the return type. What about the arguments, are they byte indexes or char indices? Try to put yourself into the shoes of someone who is just reading the function header |
||
let (_current_line, line_start) = get_line_and_start_index(line_indexes, index); | ||
line_start | ||
} | ||
|
||
pub fn get_line_and_start_index(line_indexes: &mut [usize], index: usize) -> (usize, usize) { | ||
let mut current_line = 1; | ||
for (i, line_start) in line_indexes.iter().enumerate() { | ||
current_line = i; | ||
if *line_start > index { | ||
break; | ||
} | ||
} | ||
|
||
(current_line, line_indexes[current_line - 1]) | ||
} | ||
|
||
match position { | ||
Some(pos) => CCursor::new(current_index.index - pos), | ||
None => CCursor::new(0), | ||
pub fn get_current_column(text: &str, line_indexes: &mut [usize], index: usize) -> usize { | ||
let (current_column, _total_column) = | ||
get_current_column_total_column(text, line_indexes, index); | ||
current_column | ||
} | ||
|
||
pub fn get_current_column_total_column( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you shouldn't be using https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not from an English-speaking country, so it's hard for me to choose even a single word. |
||
text: &str, | ||
line_indexes: &mut [usize], | ||
index: usize, | ||
) -> (usize, usize) { | ||
let (current_column, total_column, _total_line) = | ||
get_current_column_total_column_line(text, line_indexes, index); | ||
(current_column, total_column) | ||
} | ||
|
||
pub fn get_current_column_total_column_line( | ||
text: &str, | ||
line_indexes: &mut [usize], | ||
index: usize, | ||
) -> (usize, usize, usize) { | ||
let (current_column, _current_line, total_columns, total_lines) = | ||
get_current_column_line_total_column_line(text, line_indexes, index); | ||
|
||
(current_column, total_columns, total_lines) | ||
} | ||
|
||
pub fn get_current_column_line_total_column_line( | ||
text: &str, | ||
line_indexes: &mut [usize], | ||
index: usize, | ||
) -> (usize, usize, usize, usize) { | ||
let (current_line, line_start_index) = get_line_and_start_index(line_indexes, index); | ||
|
||
let total_lines = line_indexes.len() - 1; | ||
|
||
let mut i: usize = line_start_index; | ||
let mut current_column: usize = 1; | ||
let mut total_columns: usize = 0; | ||
while let Some(c) = text.chars().nth(i) { | ||
if c == '\n' { | ||
break; | ||
} else if c == '\t' { | ||
total_columns += text::TAB_SIZE; | ||
} else { | ||
// The simple( not exact ) width : | ||
// let width = match c.len_utf8() > 2 { | ||
// true => 2, | ||
// false => 1, | ||
// }; | ||
|
||
// The exact width : | ||
let width = unicode_width::UnicodeWidthChar::width_cjk(c).unwrap_or(2); | ||
|
||
total_columns += width; | ||
} | ||
|
||
if i < index { | ||
current_column = total_columns + 1; | ||
} | ||
i += 1; | ||
} | ||
|
||
(current_column, current_line, total_columns, total_lines) | ||
} | ||
|
||
pub fn byte_index_from_char_index(s: &str, char_index: usize) -> usize { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid adding this dependency? It's not a huge one, but I wonder how much larger the .wasm becomes with these tables: https://github.com/unicode-rs/unicode-width/blob/master/src/tables.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this dependency can be avoided. These are codes so they will not be very large and are essential for countries that use UTF8 characters. All pictograms are also affected by this.