Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buffer_paths and current_buffer_index workspace methods #25

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,40 @@ impl Workspace {
})
}

/// Returns the current buffer's index, relative to all open buffers. Can be
/// used alongside the `buffer_paths` method to know which path corresponds
/// to the current buffer.
///
/// # Examples
///
/// ```
/// use scribe::Buffer;
/// use scribe::Workspace;
/// use std::path::Path;
///
/// // Set up the paths we'll use.
/// let directory_path = Path::new("tests/sample");
/// let file_path = Path::new("tests/sample/file");
///
/// // Create a workspace.
/// let mut workspace = Workspace::new(
/// Path::new("tests/sample"),
/// None
/// ).unwrap();
///
/// // Add buffers to the workspace.
/// for path in ["tests/sample/file", "tests/sample/file2"] {
/// let buf = Buffer::from_file(Path::new(path)).unwrap();
/// workspace.add_buffer(buf);
/// }
///
/// // Second buffer is selected as it was most recently added.
/// assert_eq!(workspace.current_buffer_index(), Some(1));
/// ```
pub fn current_buffer_index(&self) -> Option<usize> {
self.current_buffer_index
}

/// Removes the currently selected buffer from the collection.
/// If the workspace is empty, this method does nothing.
///
Expand Down Expand Up @@ -307,6 +341,51 @@ impl Workspace {
Ok(TokenSet::new(data, syntax_definition, &self.syntax_set))
}

/// Returns path references to all buffers in the workspace.
///
/// If a buffer's path can be represented relative to the workspace path,
/// a relative path will be returned. Otherwise, it will be returned as-is.
///
/// # Examples
///
/// ```
/// use scribe::Buffer;
/// use scribe::Workspace;
/// use std::path::Path;
///
/// // Create a workspace.
/// let mut workspace = Workspace::new(
/// Path::new("tests/sample"),
/// None
/// ).unwrap();
///
/// // Add buffers to the workspace.
/// for path in ["tests/sample/file", "tests/sample/file2"] {
/// let buf = Buffer::from_file(Path::new(path)).unwrap();
/// workspace.add_buffer(buf);
/// }
///
/// assert_eq!(workspace.buffer_paths(), [
/// Some(Path::new("file")),
/// Some(Path::new("file2"))
/// ]);
/// ```
pub fn buffer_paths(&mut self) -> Vec<Option<&Path>> {
self.buffers
.iter()
.enumerate()
.map(|(i, buf)| {
if self.current_buffer_index == Some(i) {
self.current_buffer_path()
} else {
buf.path
.as_deref()
.and_then(|path| path.strip_prefix(&self.path).ok().or(Some(path)))
}
})
.collect::<Vec<_>>()
}

/// Updates the current buffer's syntax definition.
///
/// If a buffer is added to a workspace and is assigned a plain text syntax
Expand Down
Loading