Skip to content

Commit

Permalink
Reuse LineNumbers inside CursorPositionWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
AlisCode committed Dec 3, 2024
1 parent 629c20a commit d04c430
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions compiler-core/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub mod memory;

use crate::error::{Error, FileIoAction, FileKind, Result};
use crate::{
error::{Error, FileIoAction, FileKind, Result},
line_numbers::LineNumbers,
};
use async_trait::async_trait;
use debug_ignore::DebugIgnore;
use flate2::read::GzDecoder;
Expand Down Expand Up @@ -109,11 +112,14 @@ impl<'a, W: Utf8Writer + std::fmt::Write> Utf8Writer for CursorPositionWriter<'a
if str.is_empty() {
return Ok(());
}
let newline_count = str.rmatches('\n').count();
self.line += newline_count;
if newline_count > 0 {
let lastline = str.lines().last().expect("Should have at least one line");
self.column = lastline.len();
let line_numbers = LineNumbers::new(str);
if line_numbers.line_starts.len() >= 2 {
self.line += line_numbers.line_starts.len() - 1;
self.column = *line_numbers
.line_starts
.last()
.expect("Should have at least one line") as usize
- 1;
} else {
self.column += str.len();
}
Expand Down

0 comments on commit d04c430

Please sign in to comment.