mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 12:14:43 +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 metadata = metadata(uri.to_file_path().unwrap()).unwrap();
|
||||||
let mut code = entry.code.clone();
|
let mut code = entry.code.clone();
|
||||||
let start = util::pos_to_index(&code, old.start);
|
let start = util::pos_to_byte_index(&code, old.start);
|
||||||
let end = util::pos_to_index(&code, old.end);
|
let end = util::pos_to_byte_index(&code, old.end);
|
||||||
code.replace_range(start..end, new_code);
|
code.replace_range(start..end, new_code);
|
||||||
let token_stream = Lexer::from_str(code.clone()).lex().ok();
|
let token_stream = Lexer::from_str(code.clone()).lex().ok();
|
||||||
entry.code = code;
|
entry.code = code;
|
||||||
|
@ -193,8 +193,8 @@ impl FileCache {
|
||||||
let mut code = entry.code.clone();
|
let mut code = entry.code.clone();
|
||||||
for change in params.content_changes {
|
for change in params.content_changes {
|
||||||
let range = change.range.unwrap();
|
let range = change.range.unwrap();
|
||||||
let start = util::pos_to_index(&code, range.start);
|
let start = util::pos_to_byte_index(&code, range.start);
|
||||||
let end = util::pos_to_index(&code, range.end);
|
let end = util::pos_to_byte_index(&code, range.end);
|
||||||
code.replace_range(start..end, &change.text);
|
code.replace_range(start..end, &change.text);
|
||||||
}
|
}
|
||||||
let token_stream = Lexer::from_str(code.clone()).lex().ok();
|
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 {
|
pub fn pos_to_byte_index(src: &str, pos: Position) -> usize {
|
||||||
let mut index = 0;
|
|
||||||
let mut line = 0;
|
let mut line = 0;
|
||||||
let mut col = 0;
|
let mut col = 0;
|
||||||
for c in src.chars() {
|
for (index, c) in src.char_indices() {
|
||||||
if line == pos.line && col == pos.character {
|
if line == pos.line && col == pos.character {
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
if c == '\n' {
|
if c == '\n' {
|
||||||
line += 1;
|
line += 1;
|
||||||
col = 0;
|
col = 0;
|
||||||
index += 1;
|
|
||||||
} else {
|
} else {
|
||||||
col += 1;
|
col += 1;
|
||||||
index += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
index
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_token_stream(uri: Url) -> ELSResult<TokenStream> {
|
pub fn get_token_stream(uri: Url) -> ELSResult<TokenStream> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue