pub struct BufferSet { /* private fields */ }Expand description
A registry of open buffers keyed by id — phase 1 single-threaded view.
Implementations§
Source§impl BufferSet
impl BufferSet
pub fn new() -> Self
pub fn next_id(&mut self) -> BufferId
Sourcepub fn find_by_path(&self, path: impl AsRef<Path>) -> Option<BufferId>
pub fn find_by_path(&self, path: impl AsRef<Path>) -> Option<BufferId>
The buffer already holding path, if one is open.
DERIVED, deliberately — not a cached HashMap<PathBuf, BufferId>.
An index would be a second source of truth for “which buffer holds
this file”, and Buffer::save_as is reachable through
get_mut, so that index could silently desync the
moment anyone renames a buffer. A scan over the open buffers cannot
desync because there is nothing to keep in sync; an editor holds tens
of buffers, not millions, so the cost is noise.
Comparison is canonical where the filesystem allows it, so ./foo.rs,
foo.rs and an absolute path are one file. A path that cannot be
canonicalised (it does not exist yet — opening a NEW file) falls back
to a literal comparison, which is the best answer available.
Sourcepub fn open(&mut self, path: impl AsRef<Path>) -> Result<BufferId, BufferError>
pub fn open(&mut self, path: impl AsRef<Path>) -> Result<BufferId, BufferError>
Open path, or return the buffer already holding it.
This USED to mint a new id unconditionally, so opening the same file
twice produced two independent buffers with divergent undo stacks and
modified flags — edits split between them, and whichever saved last
won. It was latent only because nothing but the initial CLI argument
called it; a picker, files.open, or a goto-definition makes it
reachable immediately.
Tier: only-mitigated, and honestly so. Duplicate-by-open is now
unreachable, but BufferSet does not own path assignment — a caller
holding two get_mut handles can still save_as them onto one path.
Making that unrepresentable means moving path mutation behind this
type, which belongs with the madoguchi dispatch seam (see
docs/backlog-plan.md §V Phase 1), not here.
pub fn scratch(&mut self, src: &str) -> BufferId
Sourcepub fn close(&mut self, id: BufferId) -> Option<Buffer>
pub fn close(&mut self, id: BufferId) -> Option<Buffer>
Remove a buffer, returning it.
None when no such buffer is open. Deliberately says nothing about
which buffer becomes active next, or about refusing to close the last
one: BufferSet does not own active, and a type that invented a
policy it cannot enforce would be a second, weaker source of truth.
The interpreter owns both, and keeps “there is always an active
buffer” true by opening a scratch when the set empties.
Unsaved changes are NOT checked here either, for the same reason — whether a modified buffer may close is policy.