mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-02 18:02:59 +00:00
fix(els): pos_to_index
(renamed to pos_to_byte_index
)
Fixed a crash when handling non-ascii characters
This commit is contained in:
parent
66ece61af2
commit
0c579fa6fb
2 changed files with 7 additions and 10 deletions
|
@ -175,8 +175,8 @@ impl FileCache {
|
|||
};
|
||||
let metadata = metadata(uri.to_file_path().unwrap()).unwrap();
|
||||
let mut code = entry.code.clone();
|
||||
let start = util::pos_to_index(&code, old.start);
|
||||
let end = util::pos_to_index(&code, old.end);
|
||||
let start = util::pos_to_byte_index(&code, old.start);
|
||||
let end = util::pos_to_byte_index(&code, old.end);
|
||||
code.replace_range(start..end, new_code);
|
||||
let token_stream = Lexer::from_str(code.clone()).lex().ok();
|
||||
entry.code = code;
|
||||
|
@ -193,8 +193,8 @@ impl FileCache {
|
|||
let mut code = entry.code.clone();
|
||||
for change in params.content_changes {
|
||||
let range = change.range.unwrap();
|
||||
let start = util::pos_to_index(&code, range.start);
|
||||
let end = util::pos_to_index(&code, range.end);
|
||||
let start = util::pos_to_byte_index(&code, range.start);
|
||||
let end = util::pos_to_byte_index(&code, range.end);
|
||||
code.replace_range(start..end, &change.text);
|
||||
}
|
||||
let token_stream = Lexer::from_str(code.clone()).lex().ok();
|
||||
|
|
|
@ -38,24 +38,21 @@ pub fn pos_in_loc<L: Locational>(loc: &L, pos: Position) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn pos_to_index(src: &str, pos: Position) -> usize {
|
||||
let mut index = 0;
|
||||
pub fn pos_to_byte_index(src: &str, pos: Position) -> usize {
|
||||
let mut line = 0;
|
||||
let mut col = 0;
|
||||
for c in src.chars() {
|
||||
for (index, c) in src.char_indices() {
|
||||
if line == pos.line && col == pos.character {
|
||||
return index;
|
||||
}
|
||||
if c == '\n' {
|
||||
line += 1;
|
||||
col = 0;
|
||||
index += 1;
|
||||
} else {
|
||||
col += 1;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
index
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
pub fn get_token_stream(uri: Url) -> ELSResult<TokenStream> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue