Skip to content

Commit

Permalink
feat(napi/parer): add get_line_column_number API
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 10, 2024
1 parent 52a4180 commit bd36325
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 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.

1 change: 1 addition & 0 deletions napi/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ doctest = false
[dependencies]
oxc = { workspace = true }
oxc_ast = { workspace = true, features = ["serialize"] } # enable feature only
oxc_data_structures = { workspace = true }
oxc_napi = { workspace = true }

rustc-hash = { workspace = true }
Expand Down
16 changes: 16 additions & 0 deletions napi/parser/src/magic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// use std::sync::Arc;

use napi_derive::napi;
use oxc_data_structures::rope::{get_line_column, Rope};

// use oxc_sourcemap::napi::SourceMap;
use self_cell::self_cell;
Expand All @@ -10,6 +11,7 @@ use string_wizard::MagicString as MS;
#[napi]
pub struct MagicString {
cell: MagicStringImpl,
rope: Option<Rope>,
}

self_cell!(
Expand All @@ -26,6 +28,12 @@ impl MagicString {
}
}

#[napi(object)]
pub struct LineColumn {
pub line: u32,
pub column: u32,
}

#[napi(object)]
pub struct OverwriteOptions {
pub content_only: bool,
Expand All @@ -45,6 +53,14 @@ impl MagicString {
&self.cell.borrow_owner()[start as usize..end as usize]
}

#[napi]
pub fn get_line_column(&self, offest: u32) -> LineColumn {

Check warning on line 57 in napi/parser/src/magic_string.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"offest" should be "offset".
let source_text = self.cell.borrow_owner();
let rope = self.rope.get_or_insert_with(|| Rope::from_str(source_text));
let (line, column) = get_line_column(rope, offset, source_text);
LineColumn { line, column }
}

#[napi]
pub fn length(&self) -> u32 {
self.cell.borrow_dependent().len() as u32
Expand Down

0 comments on commit bd36325

Please sign in to comment.