pr 01
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
use std::collections::HashMap;
|
||||
use crate::ids::FileId;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FileDB {
|
||||
files: Vec<FileData>,
|
||||
uri_to_id: HashMap<String, FileId>,
|
||||
}
|
||||
|
||||
struct FileData {
|
||||
uri: String,
|
||||
text: String,
|
||||
line_index: LineIndex,
|
||||
}
|
||||
|
||||
pub struct LineIndex {
|
||||
line_starts: Vec<u32>,
|
||||
total_len: u32,
|
||||
}
|
||||
|
||||
impl FileDB {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
files: Vec::new(),
|
||||
uri_to_id: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn upsert(&mut self, uri: &str, text: String) -> FileId {
|
||||
if let Some(&id) = self.uri_to_id.get(uri) {
|
||||
let line_index = LineIndex::new(&text);
|
||||
self.files[id.0 as usize] = FileData {
|
||||
uri: uri.to_string(),
|
||||
text,
|
||||
line_index,
|
||||
};
|
||||
id
|
||||
} else {
|
||||
let id = FileId(self.files.len() as u32);
|
||||
let line_index = LineIndex::new(&text);
|
||||
self.files.push(FileData {
|
||||
uri: uri.to_string(),
|
||||
text,
|
||||
line_index,
|
||||
});
|
||||
self.uri_to_id.insert(uri.to_string(), id);
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_id(&self, uri: &str) -> Option<FileId> {
|
||||
self.uri_to_id.get(uri).copied()
|
||||
}
|
||||
|
||||
pub fn uri(&self, id: FileId) -> &str {
|
||||
&self.files[id.0 as usize].uri
|
||||
}
|
||||
|
||||
pub fn text(&self, id: FileId) -> &str {
|
||||
&self.files[id.0 as usize].text
|
||||
}
|
||||
|
||||
pub fn line_index(&self, id: FileId) -> &LineIndex {
|
||||
&self.files[id.0 as usize].line_index
|
||||
}
|
||||
}
|
||||
|
||||
impl LineIndex {
|
||||
pub fn new(text: &str) -> Self {
|
||||
let mut line_starts = vec![0];
|
||||
for (offset, c) in text.char_indices() {
|
||||
if c == '\n' {
|
||||
line_starts.push((offset + 1) as u32);
|
||||
}
|
||||
}
|
||||
Self {
|
||||
line_starts,
|
||||
total_len: text.len() as u32,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn offset_to_line_col(&self, offset: u32) -> (u32, u32) {
|
||||
let line = match self.line_starts.binary_search(&offset) {
|
||||
Ok(line) => line as u32,
|
||||
Err(line) => (line - 1) as u32,
|
||||
};
|
||||
let col = offset - self.line_starts[line as usize];
|
||||
(line, col)
|
||||
}
|
||||
|
||||
pub fn line_col_to_offset(&self, line: u32, col: u32) -> Option<u32> {
|
||||
let start = *self.line_starts.get(line as usize)?;
|
||||
let offset = start + col;
|
||||
|
||||
let next_start = self.line_starts.get(line as usize + 1).copied().unwrap_or(self.total_len);
|
||||
if offset < next_start || (offset == next_start && offset == self.total_len) {
|
||||
Some(offset)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct FileId(pub u32);
|
||||
@@ -1,29 +1,7 @@
|
||||
//! Tipos e utilitários de análise usados pelo servidor LSP e pelo pipeline.
|
||||
//!
|
||||
//! Este crate expõe o `FileDB`, repositório de textos do projeto.
|
||||
//! Ele será expandido nos próximos PRs para suportar snapshots e índices.
|
||||
//!
|
||||
//! Exemplo básico:
|
||||
//! ```
|
||||
//! use prometeu_analysis::FileDB;
|
||||
//! let db = FileDB::default();
|
||||
//! assert_eq!(db.len_files(), 0);
|
||||
//! ```
|
||||
use serde::{Deserialize, Serialize};
|
||||
pub mod ids;
|
||||
pub mod span;
|
||||
pub mod file_db;
|
||||
|
||||
/// Banco de arquivos do projeto.
|
||||
///
|
||||
/// Responsável por armazenar textos, URIs/paths e permitir criação de snapshots.
|
||||
/// Implementação mínima no PR-00; funcionalidades serão adicionadas nos PRs seguintes.
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FileDB {
|
||||
// TODO(PR-03+): armazenar textos, URIs/paths, índices de linha e snapshots
|
||||
#[serde(skip)]
|
||||
_placeholder: (),
|
||||
}
|
||||
|
||||
impl FileDB {
|
||||
/// Retorna a quantidade de arquivos conhecidos.
|
||||
/// Implementação temporária para manter a API estável.
|
||||
pub fn len_files(&self) -> usize { 0 }
|
||||
}
|
||||
pub use ids::FileId;
|
||||
pub use span::Span;
|
||||
pub use file_db::{FileDB, LineIndex};
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
use crate::ids::FileId;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Span {
|
||||
pub file: FileId,
|
||||
pub start: u32, // byte offset
|
||||
pub end: u32, // byte offset, exclusive
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
use prometeu_analysis::{FileDB, LineIndex};
|
||||
|
||||
#[test]
|
||||
fn test_line_index_roundtrip() {
|
||||
let text = "line 1\nline 2\nline 3";
|
||||
let index = LineIndex::new(text);
|
||||
|
||||
// Roundtrip for each character
|
||||
for (offset, _) in text.char_indices() {
|
||||
let (line, col) = index.offset_to_line_col(offset as u32);
|
||||
let recovered_offset = index.line_col_to_offset(line, col).expect("Should recover offset");
|
||||
assert_eq!(offset as u32, recovered_offset, "Offset mismatch at line {}, col {}", line, col);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_line_index_boundaries() {
|
||||
let text = "a\nbc\n";
|
||||
let index = LineIndex::new(text);
|
||||
|
||||
// "a" -> (0, 0)
|
||||
assert_eq!(index.offset_to_line_col(0), (0, 0));
|
||||
assert_eq!(index.line_col_to_offset(0, 0), Some(0));
|
||||
|
||||
// "\n" -> (0, 1)
|
||||
assert_eq!(index.offset_to_line_col(1), (0, 1));
|
||||
assert_eq!(index.line_col_to_offset(0, 1), Some(1));
|
||||
|
||||
// "b" -> (1, 0)
|
||||
assert_eq!(index.offset_to_line_col(2), (1, 0));
|
||||
assert_eq!(index.line_col_to_offset(1, 0), Some(2));
|
||||
|
||||
// "c" -> (1, 1)
|
||||
assert_eq!(index.offset_to_line_col(3), (1, 1));
|
||||
assert_eq!(index.line_col_to_offset(1, 1), Some(3));
|
||||
|
||||
// "\n" (second) -> (1, 2)
|
||||
assert_eq!(index.offset_to_line_col(4), (1, 2));
|
||||
assert_eq!(index.line_col_to_offset(1, 2), Some(4));
|
||||
|
||||
// EOF (after last \n) -> (2, 0)
|
||||
assert_eq!(index.offset_to_line_col(5), (2, 0));
|
||||
assert_eq!(index.line_col_to_offset(2, 0), Some(5));
|
||||
|
||||
// Out of bounds
|
||||
assert_eq!(index.line_col_to_offset(2, 1), None);
|
||||
assert_eq!(index.line_col_to_offset(3, 0), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_file_db_upsert_and_access() {
|
||||
let mut db = FileDB::new();
|
||||
let uri = "file:///test.txt";
|
||||
let text = "hello\nworld".to_string();
|
||||
|
||||
let id = db.upsert(uri, text.clone());
|
||||
assert_eq!(db.file_id(uri), Some(id));
|
||||
assert_eq!(db.uri(id), uri);
|
||||
assert_eq!(db.text(id), &text);
|
||||
|
||||
let index = db.line_index(id);
|
||||
assert_eq!(index.offset_to_line_col(6), (1, 0)); // 'w' is at offset 6
|
||||
|
||||
// Update existing file
|
||||
let new_text = "new content".to_string();
|
||||
let same_id = db.upsert(uri, new_text.clone());
|
||||
assert_eq!(id, same_id);
|
||||
assert_eq!(db.text(id), &new_text);
|
||||
}
|
||||
Reference in New Issue
Block a user