remove comments and adjust some others

This commit is contained in:
Josh Thomas 2025-09-02 23:19:24 -05:00
parent f47d9dfe4d
commit 196a6344fe
4 changed files with 14 additions and 54 deletions

View file

@ -61,6 +61,7 @@ pub trait Db: salsa::Database {
pub struct Database { pub struct Database {
storage: salsa::Storage<Self>, storage: salsa::Storage<Self>,
// TODO: does this need to be an Option?
/// File system for reading file content (checks buffers first, then disk). /// File system for reading file content (checks buffers first, then disk).
fs: Option<Arc<dyn FileSystem>>, fs: Option<Arc<dyn FileSystem>>,
@ -98,7 +99,6 @@ impl Default for Database {
} }
impl Database { impl Database {
/// Create a new database with fresh storage.
pub fn new(file_system: Arc<dyn FileSystem>, files: Arc<DashMap<PathBuf, SourceFile>>) -> Self { pub fn new(file_system: Arc<dyn FileSystem>, files: Arc<DashMap<PathBuf, SourceFile>>) -> Self {
Self { Self {
storage: salsa::Storage::new(None), storage: salsa::Storage::new(None),
@ -109,8 +109,6 @@ impl Database {
} }
} }
/// Create a database instance from an existing storage.
/// This preserves both the file system and files Arc across database operations.
pub fn from_storage( pub fn from_storage(
storage: salsa::Storage<Self>, storage: salsa::Storage<Self>,
file_system: Arc<dyn FileSystem>, file_system: Arc<dyn FileSystem>,
@ -136,9 +134,8 @@ impl Database {
/// Get or create a [`SourceFile`] for the given path. /// Get or create a [`SourceFile`] for the given path.
/// ///
/// This method implements Ruff's pattern for lazy file creation. Files are created /// Files are created with an initial revision of 0 and tracked in the [`Database`]'s
/// with an initial revision of 0 and tracked in the [`Database`]'s `DashMap`. The `Arc` /// `DashMap`. The `Arc` ensures cheap cloning while maintaining thread safety.
/// ensures cheap cloning while maintaining thread safety.
pub fn get_or_create_file(&mut self, path: PathBuf) -> SourceFile { pub fn get_or_create_file(&mut self, path: PathBuf) -> SourceFile {
if let Some(file_ref) = self.files.get(&path) { if let Some(file_ref) = self.files.get(&path) {
// Copy the value (SourceFile is Copy) and drop the guard immediately // Copy the value (SourceFile is Copy) and drop the guard immediately
@ -242,9 +239,6 @@ pub struct SourceFile {
} }
/// Read file content, creating a Salsa dependency on the file's revision. /// Read file content, creating a Salsa dependency on the file's revision.
///
/// **Critical**: The call to `file.revision(db)` creates the dependency chain.
/// Without it, revision changes won't trigger query invalidation.
#[salsa::tracked] #[salsa::tracked]
pub fn source_text(db: &dyn Db, file: SourceFile) -> Arc<str> { pub fn source_text(db: &dyn Db, file: SourceFile) -> Arc<str> {
// This line creates the Salsa dependency on revision! Without this call, // This line creates the Salsa dependency on revision! Without this call,
@ -260,18 +254,6 @@ pub fn source_text(db: &dyn Db, file: SourceFile) -> Arc<str> {
} }
} }
/// Global input configuring ordered template loader roots.
///
/// [`TemplateLoaderOrder`] represents the Django `TEMPLATES[n]['DIRS']` configuration,
/// defining the search order for template resolution. This is a global input that
/// affects template name resolution across the entire project.
#[salsa::input]
pub struct TemplateLoaderOrder {
/// Ordered list of template root directories
#[returns(ref)]
pub roots: Arc<[String]>,
}
/// Represents a file path for Salsa tracking. /// Represents a file path for Salsa tracking.
/// ///
/// [`FilePath`] is a Salsa input entity that tracks a file path for use in /// [`FilePath`] is a Salsa input entity that tracks a file path for use in
@ -347,7 +329,8 @@ pub fn parse_template_by_path(db: &dyn Db, file_path: FilePath) -> Option<Arc<Te
return None; return None;
}; };
// Call the pure parsing function from djls-templates // Call the parsing function from djls-templates
// TODO: Move this whole function into djls-templates
match djls_templates::parse_template(&text) { match djls_templates::parse_template(&text) {
Ok((ast, errors)) => { Ok((ast, errors)) => {
// Convert errors to strings // Convert errors to strings
@ -367,31 +350,6 @@ pub fn parse_template_by_path(db: &dyn Db, file_path: FilePath) -> Option<Arc<Te
} }
} }
/// Get template parsing errors for a file by path.
///
/// This Salsa tracked function extracts just the errors from the parsed template,
/// useful for diagnostics without needing the full AST.
///
/// Reads files through the FileSystem for overlay support.
///
/// Returns an empty vector for non-template files.
#[salsa::tracked]
pub fn template_errors_by_path(db: &dyn Db, file_path: FilePath) -> Arc<[String]> {
parse_template_by_path(db, file_path)
.map_or_else(|| Arc::from(vec![]), |ast| Arc::from(ast.errors.clone()))
}
/// Get template parsing errors for a file.
///
/// This Salsa tracked function extracts just the errors from the parsed template,
/// useful for diagnostics without needing the full AST.
///
/// Returns an empty vector for non-template files.
#[salsa::tracked]
pub fn template_errors(db: &dyn Db, file: SourceFile) -> Arc<[String]> {
parse_template(db, file).map_or_else(|| Arc::from(vec![]), |ast| Arc::from(ast.errors.clone()))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use dashmap::DashMap; use dashmap::DashMap;

View file

@ -30,7 +30,6 @@ pub struct TextDocument {
} }
impl TextDocument { impl TextDocument {
/// Create a new [`TextDocument`] with the given content
#[must_use] #[must_use]
pub fn new(content: String, version: i32, language_id: LanguageId) -> Self { pub fn new(content: String, version: i32, language_id: LanguageId) -> Self {
let line_index = LineIndex::new(&content); let line_index = LineIndex::new(&content);
@ -42,19 +41,16 @@ impl TextDocument {
} }
} }
/// Get the document's content
#[must_use] #[must_use]
pub fn content(&self) -> &str { pub fn content(&self) -> &str {
&self.content &self.content
} }
/// Get the version number
#[must_use] #[must_use]
pub fn version(&self) -> i32 { pub fn version(&self) -> i32 {
self.version self.version
} }
/// Get the language identifier
#[must_use] #[must_use]
pub fn language_id(&self) -> LanguageId { pub fn language_id(&self) -> LanguageId {
self.language_id.clone() self.language_id.clone()
@ -65,6 +61,7 @@ impl TextDocument {
&self.line_index &self.line_index
} }
#[must_use]
pub fn get_line(&self, line: u32) -> Option<String> { pub fn get_line(&self, line: u32) -> Option<String> {
let line_start = *self.line_index.line_starts.get(line as usize)?; let line_start = *self.line_index.line_starts.get(line as usize)?;
let line_end = self let line_end = self
@ -77,6 +74,7 @@ impl TextDocument {
Some(self.content[line_start as usize..line_end as usize].to_string()) Some(self.content[line_start as usize..line_end as usize].to_string())
} }
#[must_use]
pub fn get_text_range(&self, range: Range) -> Option<String> { pub fn get_text_range(&self, range: Range) -> Option<String> {
let start_offset = self.line_index.offset(range.start)? as usize; let start_offset = self.line_index.offset(range.start)? as usize;
let end_offset = self.line_index.offset(range.end)? as usize; let end_offset = self.line_index.offset(range.end)? as usize;
@ -100,6 +98,7 @@ impl TextDocument {
self.version = version; self.version = version;
} }
#[must_use]
pub fn get_template_tag_context(&self, position: Position) -> Option<TemplateTagContext> { pub fn get_template_tag_context(&self, position: Position) -> Option<TemplateTagContext> {
let start = self.line_index.line_starts.get(position.line as usize)?; let start = self.line_index.line_starts.get(position.line as usize)?;
let end = self let end = self
@ -135,10 +134,12 @@ impl TextDocument {
}) })
} }
#[must_use]
pub fn position_to_offset(&self, position: Position) -> Option<u32> { pub fn position_to_offset(&self, position: Position) -> Option<u32> {
self.line_index.offset(position) self.line_index.offset(position)
} }
#[must_use]
pub fn offset_to_position(&self, offset: u32) -> Position { pub fn offset_to_position(&self, offset: u32) -> Position {
self.line_index.position(offset) self.line_index.position(offset)
} }

View file

@ -1,4 +1,4 @@
//! File system abstraction following Ruff's pattern //! Virtual file system abstraction
//! //!
//! This module provides the [`FileSystem`] trait that abstracts file I/O operations. //! This module provides the [`FileSystem`] trait that abstracts file I/O operations.
//! This allows the LSP to work with both real files and in-memory overlays. //! This allows the LSP to work with both real files and in-memory overlays.
@ -121,7 +121,7 @@ impl FileSystem for OsFileSystem {
/// LSP file system that intercepts reads for buffered files. /// LSP file system that intercepts reads for buffered files.
/// ///
/// This implements Ruff's two-layer architecture where Layer 1 (open [`Buffers`]) /// This implements a two-layer architecture where Layer 1 (open [`Buffers`])
/// takes precedence over Layer 2 (Salsa database). When a file is read, /// takes precedence over Layer 2 (Salsa database). When a file is read,
/// this system first checks for a buffer (in-memory content from /// this system first checks for a buffer (in-memory content from
/// [`TextDocument`](crate::document::TextDocument)) and returns that content. /// [`TextDocument`](crate::document::TextDocument)) and returns that content.
@ -137,7 +137,6 @@ pub struct WorkspaceFileSystem {
} }
impl WorkspaceFileSystem { impl WorkspaceFileSystem {
/// Create a new [`WorkspaceFileSystem`] with the given buffer storage and fallback
#[must_use] #[must_use]
pub fn new(buffers: Buffers, disk: Arc<dyn FileSystem>) -> Self { pub fn new(buffers: Buffers, disk: Arc<dyn FileSystem>) -> Self {
Self { buffers, disk } Self { buffers, disk }

View file

@ -3,6 +3,8 @@
//! Detects cursor position context within Django template tags to provide //! Detects cursor position context within Django template tags to provide
//! appropriate completions and auto-closing behavior. //! appropriate completions and auto-closing behavior.
// TODO: is this module in the right spot or even needed?
/// Tracks what closing characters are needed to complete a template tag. /// Tracks what closing characters are needed to complete a template tag.
/// ///
/// Used to determine whether the completion system needs to insert /// Used to determine whether the completion system needs to insert