mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Refactor position
This commit is contained in:
parent
0bb21533c6
commit
fcbe73ec1c
1 changed files with 19 additions and 22 deletions
|
@ -79,36 +79,33 @@ fn position(
|
||||||
position_encoding: &PositionEncoding,
|
position_encoding: &PositionEncoding,
|
||||||
span: &DiagnosticSpan,
|
span: &DiagnosticSpan,
|
||||||
line_offset: usize,
|
line_offset: usize,
|
||||||
column_offset: usize,
|
column_offset_utf32: usize,
|
||||||
) -> lsp_types::Position {
|
) -> lsp_types::Position {
|
||||||
let line_index = line_offset - span.line_start;
|
let line_index = line_offset - span.line_start;
|
||||||
|
|
||||||
let mut true_column_offset = column_offset;
|
let column_offset_encoded = match span.text.get(line_index) {
|
||||||
if let Some(line) = span.text.get(line_index) {
|
// Fast path.
|
||||||
if line.text.chars().count() == line.text.len() {
|
Some(line) if line.text.is_ascii() => column_offset_utf32,
|
||||||
// all one byte utf-8 char
|
Some(line) => {
|
||||||
return lsp_types::Position {
|
let line_prefix_len = line
|
||||||
line: (line_offset as u32).saturating_sub(1),
|
.text
|
||||||
character: (column_offset as u32).saturating_sub(1),
|
.char_indices()
|
||||||
};
|
.take(column_offset_utf32)
|
||||||
}
|
.last()
|
||||||
let mut char_offset = 0;
|
.map(|(pos, c)| pos + c.len_utf8())
|
||||||
for c in line.text.chars() {
|
.unwrap_or(0);
|
||||||
char_offset += 1;
|
let line_prefix = &line.text[..line_prefix_len];
|
||||||
if char_offset > column_offset {
|
match position_encoding {
|
||||||
break;
|
PositionEncoding::Utf8 => line_prefix.len(),
|
||||||
|
PositionEncoding::Wide(enc) => enc.measure(line_prefix),
|
||||||
}
|
}
|
||||||
let len = match position_encoding {
|
|
||||||
PositionEncoding::Utf8 => c.len_utf8(),
|
|
||||||
PositionEncoding::Wide(w) => w.measure(&c.to_string()),
|
|
||||||
};
|
|
||||||
true_column_offset += len - 1;
|
|
||||||
}
|
}
|
||||||
}
|
None => column_offset_utf32,
|
||||||
|
};
|
||||||
|
|
||||||
lsp_types::Position {
|
lsp_types::Position {
|
||||||
line: (line_offset as u32).saturating_sub(1),
|
line: (line_offset as u32).saturating_sub(1),
|
||||||
character: (true_column_offset as u32).saturating_sub(1),
|
character: (column_offset_encoded as u32).saturating_sub(1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue