change comments to English
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
/// TextIndex fornece conversões entre offsets em bytes (usados no core)
|
||||
/// e posições LSP (linha, coluna em unidades UTF-16).
|
||||
/// TextIndex provides conversions between byte offsets (used in the core)
|
||||
/// and LSP positions (line, column in UTF-16 units).
|
||||
///
|
||||
/// Observações:
|
||||
/// - `line_starts` guarda os offsets em bytes do início de cada linha.
|
||||
/// - Mantemos uma cópia do texto para permitir conversões sem dependências externas.
|
||||
/// - A coluna LSP é contada em unidades UTF-16, excluindo o `\n` do fim de linha.
|
||||
/// Notes:
|
||||
/// - `line_starts` stores byte offsets for the start of each line.
|
||||
/// - We keep a copy of the text to allow conversions without external dependencies.
|
||||
/// - The LSP column is counted in UTF-16 units, excluding the end-of-line `\n`.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TextIndex {
|
||||
text: String,
|
||||
@@ -12,13 +12,13 @@ pub struct TextIndex {
|
||||
}
|
||||
|
||||
impl TextIndex {
|
||||
/// Constrói o índice a partir do conteúdo textual atual do arquivo.
|
||||
/// Builds the index from the file's current textual content.
|
||||
pub fn new(text: &str) -> Self {
|
||||
let mut line_starts = Vec::with_capacity(128);
|
||||
line_starts.push(0);
|
||||
for (byte, ch) in text.char_indices() {
|
||||
if ch == '\n' {
|
||||
// início da próxima linha é o byte após o '\n'
|
||||
// the start of the next line is the byte after the '\n'
|
||||
line_starts.push((byte + 1) as u32);
|
||||
}
|
||||
}
|
||||
@@ -28,15 +28,15 @@ impl TextIndex {
|
||||
}
|
||||
}
|
||||
|
||||
/// Número de linhas (0-based; linhas vazias contam).
|
||||
/// Number of lines (0-based; empty lines count).
|
||||
#[inline]
|
||||
pub fn line_count(&self) -> u32 {
|
||||
self.line_starts.len() as u32
|
||||
}
|
||||
|
||||
/// Converte um offset em bytes (dentro do arquivo) para (linha, coluna UTF-16) no padrão LSP.
|
||||
/// Converts a byte offset (within the file) to (line, UTF-16 column) in LSP format.
|
||||
///
|
||||
/// Para offsets exatamente no fim da linha, a coluna será o comprimento UTF-16 da linha.
|
||||
/// For offsets exactly at end-of-line, the column will be the line's UTF-16 length.
|
||||
pub fn byte_to_lsp(&self, byte: u32) -> (u32, u32) {
|
||||
let byte = byte.min(self.text.len() as u32);
|
||||
let line = match self.line_starts.binary_search(&byte) {
|
||||
@@ -56,10 +56,10 @@ impl TextIndex {
|
||||
(line, utf16_col)
|
||||
}
|
||||
|
||||
/// Converte (linha, coluna UTF-16) em offset em bytes.
|
||||
/// Converts (line, UTF-16 column) to a byte offset.
|
||||
///
|
||||
/// - Linhas fora do intervalo são clampadas para [0, last].
|
||||
/// - Colunas maiores que o tamanho UTF-16 da linha retornam o fim da linha.
|
||||
/// - Lines outside the range are clamped to [0, last].
|
||||
/// - Columns larger than the line's UTF-16 length return end-of-line.
|
||||
pub fn lsp_to_byte(&self, line: u32, utf16_col: u32) -> u32 {
|
||||
let line = line.min(self.line_count().saturating_sub(1));
|
||||
let (line_start, line_end) = self.line_bounds(line);
|
||||
@@ -72,7 +72,7 @@ impl TextIndex {
|
||||
}
|
||||
acc += ch.len_utf16() as u32;
|
||||
}
|
||||
// Se a coluna alvo é após o último caractere, retorne o fim da linha.
|
||||
// If the target column is after the last character, return end-of-line.
|
||||
line_end as u32
|
||||
}
|
||||
|
||||
@@ -83,9 +83,8 @@ impl TextIndex {
|
||||
.get(line as usize)
|
||||
.unwrap_or(self.line_starts.last().unwrap());
|
||||
let next = self.line_starts.get(line as usize + 1).copied();
|
||||
// Se existe próxima linha, `next` aponta para o byte após o '\n' da linha atual
|
||||
// então o conteúdo termina em `next - 1`. Caso contrário (última linha),
|
||||
// o conteúdo termina em `text.len()`.
|
||||
// If there is a next line, `next` points to the byte after the current line's '\n',
|
||||
// so the content ends at `next - 1`. Otherwise (last line), the content ends at `text.len()`.
|
||||
let end = match next {
|
||||
Some(next_start) => next_start.saturating_sub(1),
|
||||
None => self.text.len() as u32,
|
||||
@@ -103,11 +102,11 @@ mod tests_internal {
|
||||
let s = "ab\ncd\n";
|
||||
let idx = TextIndex::new(s);
|
||||
assert_eq!(idx.line_count(), 3);
|
||||
// linha 0: "ab"
|
||||
// line 0: "ab"
|
||||
assert_eq!(idx.line_bounds(0), (0, 2));
|
||||
// linha 1: "cd"
|
||||
// line 1: "cd"
|
||||
assert_eq!(idx.line_bounds(1), (3, 5));
|
||||
// linha 2: linha vazia final
|
||||
// line 2: final empty line
|
||||
assert_eq!(idx.line_bounds(2), (6, 6));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,22 +5,22 @@ fn text_index_ascii_roundtrip() {
|
||||
let text = "hello\nworld\nthis is ascii";
|
||||
let idx = TextIndex::new(text);
|
||||
|
||||
// Verifica round-trip em todas as fronteiras de char
|
||||
// Verify round-trip on all character boundaries
|
||||
let mut boundaries: Vec<usize> = text.char_indices().map(|(i, _)| i).collect();
|
||||
boundaries.push(text.len());
|
||||
|
||||
for &b in &boundaries {
|
||||
let (line, col16) = idx.byte_to_lsp(b as u32);
|
||||
let b2 = idx.lsp_to_byte(line, col16);
|
||||
assert_eq!(b2, b as u32, "roundtrip falhou para byte {} -> (l={},c16={})", b, line, col16);
|
||||
assert_eq!(b2, b as u32, "roundtrip failed for byte {} -> (l={},c16={})", b, line, col16);
|
||||
}
|
||||
|
||||
// Alguns checks diretos
|
||||
// início: (0,0)
|
||||
// Some direct checks
|
||||
// start: (0,0)
|
||||
assert_eq!(idx.byte_to_lsp(0), (0, 0));
|
||||
// após "hello" (5), antes do '\n': linha 0, col=5
|
||||
// after "hello" (5), before '\n': line 0, col=5
|
||||
assert_eq!(idx.byte_to_lsp(5), (0, 5));
|
||||
// após '\n' (6): linha 1, col=0
|
||||
// after '\n' (6): line 1, col=0
|
||||
assert_eq!(idx.byte_to_lsp(6), (1, 0));
|
||||
}
|
||||
|
||||
@@ -30,25 +30,25 @@ fn text_index_unicode_roundtrip_utf16() {
|
||||
let text = "aé🙂b";
|
||||
let idx = TextIndex::new(text);
|
||||
|
||||
// fronteiras de char + fim
|
||||
// character boundaries + end
|
||||
let mut boundaries: Vec<usize> = text.char_indices().map(|(i, _)| i).collect();
|
||||
boundaries.push(text.len());
|
||||
|
||||
for &b in &boundaries {
|
||||
let (line, col16) = idx.byte_to_lsp(b as u32);
|
||||
let b2 = idx.lsp_to_byte(line, col16);
|
||||
assert_eq!(b2, b as u32, "roundtrip unicode falhou para byte {} -> (l={},c16={})", b, line, col16);
|
||||
assert_eq!(b2, b as u32, "unicode roundtrip failed for byte {} -> (l={},c16={})", b, line, col16);
|
||||
}
|
||||
|
||||
// Checagens de colunas esperadas na linha 0
|
||||
// Expected columns on line 0
|
||||
// bytes: [0:'a'][1..2:'é'][3..6:'🙂'][7:'b'][8:end]
|
||||
assert_eq!(idx.byte_to_lsp(0), (0, 0)); // antes de 'a'
|
||||
assert_eq!(idx.byte_to_lsp(1), (0, 1)); // após 'a'
|
||||
assert_eq!(idx.byte_to_lsp(3), (0, 2)); // após 'a' + 'é' (1+1 utf16)
|
||||
assert_eq!(idx.byte_to_lsp(7), (0, 4)); // após '🙂' (2 utf16) => 1+1+2=4
|
||||
assert_eq!(idx.byte_to_lsp(8), (0, 5)); // após 'b'
|
||||
assert_eq!(idx.byte_to_lsp(0), (0, 0)); // before 'a'
|
||||
assert_eq!(idx.byte_to_lsp(1), (0, 1)); // after 'a'
|
||||
assert_eq!(idx.byte_to_lsp(3), (0, 2)); // after 'a' + 'é' (1+1 utf16)
|
||||
assert_eq!(idx.byte_to_lsp(7), (0, 4)); // after '🙂' (2 utf16) => 1+1+2=4
|
||||
assert_eq!(idx.byte_to_lsp(8), (0, 5)); // after 'b'
|
||||
|
||||
// e inverso, colunas específicas
|
||||
// and inverse, specific columns
|
||||
assert_eq!(idx.lsp_to_byte(0, 0), 0);
|
||||
assert_eq!(idx.lsp_to_byte(0, 1), 1);
|
||||
assert_eq!(idx.lsp_to_byte(0, 2), 3);
|
||||
|
||||
Reference in New Issue
Block a user