Skip to content

Commit

Permalink
use line-index
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunchen committed Sep 5, 2024
1 parent 71e6e30 commit 5dc9b86
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions crates/moonbuild/src/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use anyhow::Context;
use colored::Colorize;
use moonutil::common::line_col_to_byte_idx;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::path::PathBuf;
Expand Down Expand Up @@ -217,14 +218,6 @@ impl Replace {
}
}

fn line_col_to_byte_idx(line_index: &line_index::LineIndex, line: u32, col: u32) -> Option<usize> {
let offset = line_index.offset(line_index.to_utf8(
line_index::WideEncoding::Utf32,
line_index::WideLineCol { line, col },
)?)?;
Some(usize::from(offset))
}

fn parse_expect_failed_message(msg: &str) -> anyhow::Result<Replace> {
let j: ExpectFailedRaw = serde_json_lenient::from_str(msg)
.context(format!("parse expect test result failed: {}", msg))?;
Expand Down
1 change: 1 addition & 0 deletions crates/moonutil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ env_logger.workspace = true
log.workspace = true
thiserror.workspace = true
schemars.workspace = true
line-index.workspace = true

[dev-dependencies]
expect-test.workspace = true
Expand Down
12 changes: 12 additions & 0 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,15 @@ impl MooncGenTestInfo {
result
}
}

pub fn line_col_to_byte_idx(
line_index: &line_index::LineIndex,
line: u32,
col: u32,
) -> Option<usize> {
let offset = line_index.offset(line_index.to_utf8(
line_index::WideEncoding::Utf32,
line_index::WideLineCol { line, col },
)?)?;
Some(usize::from(offset))
}
28 changes: 11 additions & 17 deletions crates/moonutil/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use ariadne::Fmt;
use serde::{Deserialize, Serialize};

use crate::common::line_col_to_byte_idx;

#[derive(Debug, Serialize, Deserialize)]
pub struct MooncDiagnostic {
pub level: String,
Expand All @@ -43,24 +45,16 @@ pub struct Position {

impl Position {
pub fn calculate_offset(&self, content: &str) -> usize {
// line and col count from 1
let mut current_line = 1;
let mut current_col = 1;

for (char_idx, (_, c)) in content.char_indices().enumerate() {
if current_line == self.line && current_col == self.col {
return char_idx;
}

if c == '\n' {
current_line += 1;
current_col = 1;
} else {
current_col += 1;
}
}
let line_index = line_index::LineIndex::new(content);
let byte_based_index =
line_col_to_byte_idx(&line_index, self.line as u32 - 1, self.col as u32 - 1).unwrap();

content.chars().count()
content
.char_indices()
.enumerate()
.find(|(_, (byte_offset, _))| *byte_offset == byte_based_index)
.map(|(i, _)| i)
.unwrap_or(usize::from(line_index.len()))
}
}

Expand Down

0 comments on commit 5dc9b86

Please sign in to comment.