mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 18:58:36 +00:00
compiler: Allow mapping from line/column to offset
Add a method to the SourceFile so that we can map back from line/column values to offsets.
This commit is contained in:
parent
2b45f9448e
commit
bb3109443a
1 changed files with 12 additions and 0 deletions
|
@ -106,6 +106,18 @@ impl SourceFileInner {
|
|||
)
|
||||
}
|
||||
|
||||
/// Returns the offset that corresponds to the line/column
|
||||
pub fn offset(&self, line: usize, column: usize) -> usize {
|
||||
let col_offset = column.saturating_sub(1);
|
||||
if line <= 1 {
|
||||
// line == 0 is actually invalid!
|
||||
return col_offset;
|
||||
}
|
||||
let offsets = self.line_offsets();
|
||||
let index = std::cmp::min(line.saturating_sub(1), offsets.len());
|
||||
offsets.get(index.saturating_sub(1)).unwrap_or(&0).saturating_add(col_offset)
|
||||
}
|
||||
|
||||
fn line_offsets(&self) -> &[usize] {
|
||||
self.line_offsets.get_or_init(|| {
|
||||
self.source
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue