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
|
||||
}
|
||||
Reference in New Issue
Block a user