-
Couldn't load subscription status.
- Fork 24
Use biome_line_index to avoid biome_lsp_converters
#354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| authors.workspace = true | ||
| categories.workspace = true | ||
| description = "Biome's tools for converting between byte offsets and line / column positions" | ||
| edition.workspace = true | ||
| homepage.workspace = true | ||
| keywords.workspace = true | ||
| license.workspace = true | ||
| name = "biome_line_index" | ||
| repository.workspace = true | ||
| version = "0.1.0" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
|
||
| [dependencies] | ||
| biome_text_size = { workspace = true } | ||
| rustc-hash = { workspace = true } | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| //! The crate contains tools for converting between byte offsets and line / column positions. | ||
|
|
||
| #![deny(clippy::use_self)] | ||
|
|
||
| use biome_text_size::TextSize; | ||
|
|
||
| mod line_index; | ||
|
|
||
| pub use line_index::LineIndex; | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
| pub enum WideEncoding { | ||
| Utf16, | ||
| Utf32, | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
| pub struct LineCol { | ||
| /// Zero-based | ||
| pub line: u32, | ||
| /// Zero-based utf8 offset | ||
| pub col: u32, | ||
| } | ||
|
|
||
| /// Deliberately not a generic type and different from `LineCol`. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
| pub struct WideLineCol { | ||
| /// Zero-based | ||
| pub line: u32, | ||
| /// Zero-based | ||
| pub col: u32, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Hash, PartialEq, Eq)] | ||
| pub struct WideChar { | ||
| /// Start offset of a character inside a line, zero-based | ||
| pub start: TextSize, | ||
| /// End offset of a character inside a line, zero-based | ||
| pub end: TextSize, | ||
| } | ||
|
|
||
| impl WideChar { | ||
| /// Returns the length in 8-bit UTF-8 code units. | ||
| fn len(&self) -> TextSize { | ||
| self.end - self.start | ||
| } | ||
|
|
||
| /// Returns the length in UTF-16 or UTF-32 code units. | ||
| fn wide_len(&self, enc: WideEncoding) -> usize { | ||
| match enc { | ||
| WideEncoding::Utf16 => { | ||
| if self.len() == TextSize::from(4) { | ||
| 2 | ||
| } else { | ||
| 1 | ||
| } | ||
| } | ||
|
|
||
| WideEncoding::Utf32 => 1, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::WideEncoding::{Utf16, Utf32}; | ||
| use crate::WideLineCol; | ||
| use crate::line_index::LineIndex; | ||
| use crate::{LineCol, WideEncoding}; | ||
| use biome_text_size::TextSize; | ||
|
|
||
| macro_rules! check_conversion { | ||
| ($line_index:ident : $wide_line_col:expr => $text_size:expr ) => { | ||
| let encoding = WideEncoding::Utf16; | ||
|
|
||
| let line_col = $line_index.to_utf8(encoding, $wide_line_col); | ||
| let offset = $line_index.offset(line_col); | ||
| assert_eq!(offset, Some($text_size)); | ||
|
|
||
| let line_col = $line_index.line_col(offset.unwrap()); | ||
| let wide_line_col = $line_index.to_wide(encoding, line_col.unwrap()); | ||
| assert_eq!(wide_line_col, Some($wide_line_col)); | ||
| }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_string() { | ||
| let line_index = LineIndex::new(""); | ||
| check_conversion!(line_index: WideLineCol { line: 0, col: 0 } => TextSize::from(0)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_line() { | ||
| let line_index = LineIndex::new("\n\n"); | ||
| check_conversion!(line_index: WideLineCol { line: 1, col: 0 } => TextSize::from(1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn line_end() { | ||
| let line_index = LineIndex::new("abc\ndef\nghi"); | ||
| check_conversion!(line_index: WideLineCol { line: 1, col: 3 } => TextSize::from(7)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn out_of_bounds_line() { | ||
| let line_index = LineIndex::new("abcde\nfghij\n"); | ||
|
|
||
| let offset = line_index.offset(LineCol { line: 5, col: 0 }); | ||
| assert!(offset.is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unicode() { | ||
| let line_index = LineIndex::new("'Jan 1, 2018 – Jan 1, 2019'"); | ||
|
|
||
| check_conversion!(line_index: WideLineCol { line: 0, col: 0 } => TextSize::from(0)); | ||
| check_conversion!(line_index: WideLineCol { line: 0, col: 1 } => TextSize::from(1)); | ||
| check_conversion!(line_index: WideLineCol { line: 0, col: 12 } => TextSize::from(12)); | ||
| check_conversion!(line_index: WideLineCol { line: 0, col: 13 } => TextSize::from(15)); | ||
| check_conversion!(line_index: WideLineCol { line: 0, col: 14 } => TextSize::from(18)); | ||
| check_conversion!(line_index: WideLineCol { line: 0, col: 15 } => TextSize::from(21)); | ||
| check_conversion!(line_index: WideLineCol { line: 0, col: 26 } => TextSize::from(32)); | ||
| check_conversion!(line_index: WideLineCol { line: 0, col: 27 } => TextSize::from(33)); | ||
| } | ||
|
|
||
| #[ignore] | ||
| #[test] | ||
| fn test_every_chars() { | ||
| let text: String = { | ||
| let mut chars: Vec<char> = ((0 as char)..char::MAX).collect(); | ||
| chars.extend("\n".repeat(chars.len() / 16).chars()); | ||
| chars.into_iter().collect() | ||
| }; | ||
|
|
||
| let line_index = LineIndex::new(&text); | ||
|
|
||
| let mut lin_col = LineCol { line: 0, col: 0 }; | ||
| let mut col_utf16 = 0; | ||
| let mut col_utf32 = 0; | ||
| for (offset, char) in text.char_indices() { | ||
| let got_offset = line_index.offset(lin_col).unwrap(); | ||
| assert_eq!(usize::from(got_offset), offset); | ||
|
|
||
| let got_lin_col = line_index.line_col(got_offset).unwrap(); | ||
| assert_eq!(got_lin_col, lin_col); | ||
|
|
||
| for enc in [Utf16, Utf32] { | ||
| let wide_lin_col = line_index.to_wide(enc, lin_col).unwrap(); | ||
| let got_lin_col = line_index.to_utf8(enc, wide_lin_col); | ||
| assert_eq!(got_lin_col, lin_col); | ||
|
|
||
| let want_col = match enc { | ||
| Utf16 => col_utf16, | ||
| Utf32 => col_utf32, | ||
| }; | ||
| assert_eq!(wide_lin_col.col, want_col) | ||
| } | ||
|
|
||
| if char == '\n' { | ||
| lin_col.line += 1; | ||
| lin_col.col = 0; | ||
| col_utf16 = 0; | ||
| col_utf32 = 0; | ||
| } else { | ||
| lin_col.col += char.len_utf8() as u32; | ||
| col_utf16 += char.len_utf16() as u32; | ||
| col_utf32 += 1; | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore, this is Biome code