pub struct Document {
pub id: DocumentId,
pub path: Option<PathBuf>,
pub source: DocumentSource,
pub language: EditorLanguage,
pub version: DocumentVersion,
pub dirty: bool,
pub buffer: TextBuffer,
}Expand description
Open document metadata plus rope-backed text.
Fields are public for controlled editor composition. Changing buffer
directly does not increment version or set dirty; session methods should
mediate edits when those invariants matter.
§Examples
use ailloli_ui_editor::{Document, DocumentId, DocumentVersion, EditorLanguage};
use ailloli_ui_text::TextBuffer;
let document = Document::new(DocumentId(1), TextBuffer::from_string("hello"));
assert_eq!(document.version, DocumentVersion(0));
assert_eq!(document.language, EditorLanguage::PlainText);
assert!(!document.dirty);Fields§
§id: DocumentIdStable session-local document identity.
path: Option<PathBuf>Optional local path retained for compatibility and language detection.
source: DocumentSourceCanonical memory/path/URI origin description.
language: EditorLanguageExplicit or detected language.
version: DocumentVersionContent revision; initialized to zero.
dirty: boolWhether content differs from its externally saved version.
buffer: TextBufferRope-backed UTF-8 content.
Implementations§
Source§impl Document
Document construction and language-hint operations.
impl Document
Document construction and language-hint operations.
Sourcepub fn new(id: DocumentId, buffer: TextBuffer) -> Self
pub fn new(id: DocumentId, buffer: TextBuffer) -> Self
Creates a clean, version-zero in-memory plain-text document.
§Examples
use ailloli_ui_editor::{Document, DocumentId, DocumentSource};
use ailloli_ui_text::TextBuffer;
let document = Document::new(DocumentId(2), TextBuffer::new());
assert_eq!(document.source, DocumentSource::Memory);
assert!(document.path.is_none());Sourcepub fn with_path(self, path: impl Into<PathBuf>) -> Self
pub fn with_path(self, path: impl Into<PathBuf>) -> Self
Sets both path and DocumentSource::LocalPath without detection.
§Examples
use ailloli_ui_editor::{Document, DocumentId, DocumentSource};
use ailloli_ui_text::TextBuffer;
let document = Document::new(DocumentId(1), TextBuffer::new()).with_path("src/main.rs");
assert!(matches!(document.source, DocumentSource::LocalPath(_)));
assert_eq!(document.path.unwrap().to_str(), Some("src/main.rs"));Sourcepub fn with_source(self, source: DocumentSource) -> Self
pub fn with_source(self, source: DocumentSource) -> Self
Replaces the origin and synchronizes the optional local path.
Memory preserves an existing legacy path, a local file URI converts
to a host path when possible, and non-file URIs clear path.
§Examples
use ailloli_ui_editor::{Document, DocumentId, DocumentSource};
use ailloli_ui_fs::FileUri;
use ailloli_ui_text::TextBuffer;
let document = Document::new(DocumentId(1), TextBuffer::new())
.with_source(DocumentSource::Uri(FileUri::parse("sftp://host/main.rs")?));
assert!(document.path.is_none());Sourcepub fn with_uri(self, uri: FileUri) -> Self
pub fn with_uri(self, uri: FileUri) -> Self
Convenience wrapper for Self::with_source with a URI origin.
§Examples
use ailloli_ui_editor::{Document, DocumentId, DocumentSource};
use ailloli_ui_fs::FileUri;
use ailloli_ui_text::TextBuffer;
let document = Document::new(DocumentId(1), TextBuffer::new()).with_uri(FileUri::parse("sftp://host/doc")?);
assert!(matches!(document.source, DocumentSource::Uri(_)));Sourcepub fn with_language(self, language: EditorLanguage) -> Self
pub fn with_language(self, language: EditorLanguage) -> Self
Replaces the language without inspecting the path or buffer.
§Examples
use ailloli_ui_editor::{Document, DocumentId, EditorLanguage};
use ailloli_ui_text::TextBuffer;
let document = Document::new(DocumentId(1), TextBuffer::new()).with_language(EditorLanguage::Rust);
assert_eq!(document.language, EditorLanguage::Rust);Sourcepub fn detect_language(&mut self)
pub fn detect_language(&mut self)
Replaces the current language with extension-based detection.
With no hint this selects PlainText; an unrecognized extension selects
Unknown, even if a different explicit language was previously set.
§Examples
use ailloli_ui_editor::{Document, DocumentId, EditorLanguage};
use ailloli_ui_text::TextBuffer;
let mut document = Document::new(DocumentId(1), TextBuffer::new()).with_path("lib.rs");
document.detect_language();
assert_eq!(document.language, EditorLanguage::Rust);Sourcepub fn language_path_hint(&self) -> Option<&Path>
pub fn language_path_hint(&self) -> Option<&Path>
Returns path first, then the source-derived hint.
§Examples
use std::path::Path;
use ailloli_ui_editor::{Document, DocumentId};
use ailloli_ui_text::TextBuffer;
let document = Document::new(DocumentId(1), TextBuffer::new()).with_path("main.rs");
assert_eq!(document.language_path_hint(), Some(Path::new("main.rs")));