change comments to English

This commit is contained in:
2026-03-24 13:40:29 +00:00
parent 66f34eeb58
commit 912b9b0075
8 changed files with 61 additions and 62 deletions
+20 -21
View File
@@ -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));
}
}