Struct scribe::Workspace

source ·
pub struct Workspace {
    pub path: PathBuf,
    pub current_buffer: Option<Buffer>,
    pub syntax_set: SyntaxSet,
    /* private fields */
}
Expand description

An owned collection of buffers and associated path, representing a running editor environment.

Fields§

§path: PathBuf§current_buffer: Option<Buffer>§syntax_set: SyntaxSet

Implementations§

source§

impl Workspace

source

pub fn new(path: &Path, syntax_definitions: Option<&Path>) -> Result<Workspace>

Creates a new empty workspace for the specified path.

source

pub fn add_buffer(&mut self, buf: Buffer)

Adds a buffer to the workspace, inserting it after the current buffer, populates its id field with a unique value (relative to the workspace), and selects it.

§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(directory_path, None).unwrap();

// Add a buffer to the workspace.
let buf = Buffer::from_file(file_path).unwrap();
workspace.add_buffer(buf);
source

pub fn open_buffer(&mut self, path: &Path) -> Result<()>

Opens a buffer at the specified path, inserting it after the current buffer, and selects it. The path is converted to its canonical, absolute equivalent; if a buffer with the specified path already exists, it is selected, rather than opening a duplicate buffer. Any errors encountered while opening the buffer are returned.

§Examples
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(directory_path, None).unwrap();

// Open a buffer in the workspace.
workspace.open_buffer(file_path.clone());
source

pub fn current_buffer_path(&self) -> Option<&Path>

Returns a reference to the current buffer’s path.

If the path can be represented relative to the workspace path, a relative path will be returned. Otherwise, the buffer path is returned as-is.

§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(directory_path, None).unwrap();

// Add a buffer to the workspace.
let buf = Buffer::from_file(file_path).unwrap();
workspace.add_buffer(buf);

assert_eq!(workspace.current_buffer_path(), Some(Path::new("file")));
source

pub fn close_current_buffer(&mut self)

Removes the currently selected buffer from the collection. If the workspace is empty, this method does nothing.

§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(directory_path, None).unwrap();

// Add a buffer to the workspace.
let buf = Buffer::from_file(file_path).unwrap();
workspace.add_buffer(buf);

// Close the current buffer.
workspace.close_current_buffer();
source

pub fn previous_buffer(&mut self)

Selects the previous buffer in the workspace (buffers are ordered as they are added to the workspace). If the currently selected buffer is the first in the collection, this will wrap and select the last 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(directory_path, None).unwrap();

// Add a buffer to the workspace.
let buf = Buffer::from_file(file_path).unwrap();
workspace.add_buffer(buf);

// Select the previous buffer.
workspace.previous_buffer();
source

pub fn next_buffer(&mut self)

Selects the next buffer in the workspace (buffers are ordered as they are added to the workspace). If the currently selected buffer is the last in the collection, this will wrap and select the first 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(directory_path, None).unwrap();

// Add a buffer to the workspace.
let buf = Buffer::from_file(file_path).unwrap();
workspace.add_buffer(buf);

// Select the next buffer.
workspace.next_buffer();
source

pub fn current_buffer_tokens(&self) -> Result<TokenSet<'_>>

Configures and returns a tokenizer that can be used to iterate over the tokens of the current buffer. The workspace SyntaxSet is checked for a definition to do the tokenizing, using on the buffer’s extension if present, and falling back to a plain text definition, otherwise.

Returns None if there is no current buffer, or if a syntax definition cannot be found.

§Examples
use scribe::Buffer;
use scribe::Workspace;
use std::path::{Path, PathBuf};

// Set up the paths we'll use.
let directory_path = Path::new("tests/sample");

// Create a workspace.
let mut workspace = Workspace::new(directory_path, None).unwrap();

assert!(workspace.current_buffer_tokens().is_err());

// Add a buffer without a path to the workspace.
let mut buf = Buffer::new();
buf.insert("hi");
workspace.add_buffer(buf);
assert_eq!(
  workspace.current_buffer_tokens().unwrap().iter().unwrap().count(),
  1
);
source

pub fn update_current_syntax(&mut self) -> Result<()>

Updates the current buffer’s syntax definition.

If a buffer is added to a workspace and is assigned a plain text syntax definition (because the buffer has no path or file extension, or because there is no better definition for its path extension), and its path is changed, this method can be used to attempt the assignment again, in hopes for a more accurate match.

§Examples
extern crate syntect;
extern crate scribe;

use scribe::Buffer;
use scribe::Workspace;
use std::path::{Path, PathBuf};

// Create a workspace.
let workspace_path = Path::new("tests/sample");
let mut workspace = Workspace::new(workspace_path, None).unwrap();

// Add a buffer without a path to the workspace.
let buf = Buffer::new();
workspace.add_buffer(buf);

assert_eq!(
    workspace.current_buffer.as_ref().unwrap().syntax_definition.as_ref().unwrap().name,
    "Plain Text"
);

// Add a path and update the syntax definition.
let buffer_path = PathBuf::from("mod.rs");
workspace.current_buffer.as_mut().unwrap().path = Some(buffer_path);
workspace.update_current_syntax();

assert_eq!(
    workspace.current_buffer.as_ref().unwrap().syntax_definition.as_ref().unwrap().name,
    "Rust"
);

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.