Struct scribe::buffer::Buffer [] [src]

pub struct Buffer {
    pub path: Option<PathBuf>,
    pub cursor: Cursor,
    // some fields omitted
}

A feature-rich wrapper around an underlying gap buffer.

The buffer type wraps an in-memory buffer, providing file I/O, a bounds-checked moveable cursor, undo/redo history, simple type/format detection, and lexing (producing categorized tokens suitable for syntax-highlighted display).

Fields

path: Option<PathBuf> cursor: Cursor

Methods

impl Buffer
[src]

fn start_operation_group(&mut self)

Tells the buffer to start tracking operations as a single unit, until end_operation_group is called. Any calls to insert or delete occurring within these will be undone/applied together when calling undo/redo, respectively.

fn end_operation_group(&mut self)

Tells the buffer to stop tracking operations as a single unit, since start_operation_group was called. Any calls to insert or delete occurring within these will be undone/applied together when calling undo/redo, respectively.

impl Buffer
[src]

fn insert(&mut self, data: &str)

Inserts data into the buffer at the cursor position.

Examples

use scribe::Buffer;

let mut buffer = Buffer::new();
buffer.insert("scribe");
assert_eq!(buffer.data(), "scribe");

impl Buffer
[src]

fn delete(&mut self)

Deletes a character at the cursor position. If at the end of the current line, it'll try to delete a newline character (joining the lines), succeeding if there's a line below.

Examples

use scribe::Buffer;

let mut buffer = Buffer::new();
buffer.insert("scribe");
buffer.delete();
assert_eq!(buffer.data(), "cribe");

fn delete_range(&mut self, range: Range)

Removes a range of characters from the buffer.

Examples

use scribe::Buffer;
use scribe::buffer::{Position, Range};

// Set up an example buffer.
let mut buffer = Buffer::new();
buffer.insert("scribe library");

// Set up the range we'd like to delete.
let start = Position{ line: 0, offset: 6 };
let end = Position{ line: 0, offset: 14 };
let range = Range::new(start, end);

buffer.delete_range(range);

assert_eq!(buffer.data(), "scribe");

impl Buffer
[src]

fn new() -> Buffer

Creates a new empty buffer. The buffer's cursor is set to the beginning of the buffer.

Examples

use scribe::Buffer;

let buffer = Buffer::new();

fn from_file(path: PathBuf) -> Result<Buffer>

Creates a new buffer by reading the UTF-8 interpreted file contents of the specified path. The buffer's cursor is set to the beginning of the buffer. The buffer data's type will be inferred based on its extension, and an appropriate lexer will be used, if available (see tokens method for further information on why this happens).

Examples

use scribe::Buffer;
use std::path::PathBuf;

let file_path = PathBuf::from("tests/sample/file");
let mut buffer = Buffer::from_file(file_path).unwrap();
assert_eq!(buffer.data(), "it works!\n");

fn data(&mut self) -> String

Returns the contents of the buffer as a string. Caches this string representation to make subsequent requests to an unchanged buffer as fast as possible.

Examples

use scribe::Buffer;

let mut buffer = Buffer::new();
buffer.insert("scribe");
assert_eq!(buffer.data(), "scribe");

fn save(&mut self) -> Option<Error>

Writes the contents of the buffer to its path.

Examples

use scribe::Buffer;

// Set up a buffer and point it to a path.
let mut buffer = Buffer::new();
let write_path = PathBuf::from("my_doc");
buffer.path = Some(write_path.clone());

// Put some data into the buffer and save it.
buffer.insert("scribe");
buffer.save();

fn tokens(&mut self) -> Vec<Token>

Produces a set of tokens based on the buffer data suitable for colorized display, using a lexer for the buffer data's language and/or format. Caches this lexed representation to make subsequent requests to an unchanged buffer as fast as possible.

Examples

use scribe::Buffer;

let mut buffer = Buffer::new();
buffer.insert("scribe data");

// Build the buffer data string back by combining its token lexemes.
let mut data = String::new();
for token in buffer.tokens().iter() {
    data.push_str(&token.lexeme);
}
assert_eq!(data, "scribe data");

fn file_name(&self) -> Option<String>

Returns the file name portion of the buffer's path, if the path is set and its file name is a valid UTF-8 sequence.

Examples

use scribe::Buffer;
use std::path::PathBuf;

let file_path = PathBuf::from("tests/sample/file");
let buffer = Buffer::from_file(file_path).unwrap();
assert_eq!(buffer.file_name().unwrap(), "file");

fn undo(&mut self)

Reverses the last modification to the buffer.

Examples

use scribe::Buffer;
use scribe::buffer::Position;

let mut buffer = Buffer::new();
// Run an initial insert operation.
buffer.insert("scribe");
buffer.cursor.move_to(Position{ line: 0, offset: 6});

// Run a second insert operation.
buffer.insert(" library");
assert_eq!("scribe library", buffer.data());

// Undo the second operation.
buffer.undo();
assert_eq!("scribe", buffer.data());

// Undo the first operation.
buffer.undo();
assert_eq!("", buffer.data());

fn redo(&mut self)

Re-applies the last undone modification to the buffer.

Examples

use scribe::Buffer;

let mut buffer = Buffer::new();
buffer.insert("scribe");

buffer.undo();
assert_eq!("", buffer.data());

buffer.redo();
assert_eq!("scribe", buffer.data());

fn read(&self, range: &Range) -> Option<String>

Tries to read the specified range from the buffer.

Examples

use scribe::Buffer;
use scribe::buffer::{Position, Range};

let mut buffer = Buffer::new();
buffer.insert("scribe");

let range = Range::new(
    Position{ line: 0, offset: 1 },
    Position{ line: 0, offset: 5 }
);
assert_eq!("crib", buffer.read(&range).unwrap());

fn search(&mut self, needle: &str) -> Vec<Position>

Searches the buffer for (and returns positions associated with) occurrences of needle.

Examples

use scribe::Buffer;
use scribe::buffer::Position;

let mut buffer = Buffer::new();
buffer.insert("scribe\nlibrary");

assert_eq!(
    buffer.search("ib"),
    vec![
        Position{ line: 0, offset: 3 },
        Position{ line: 1, offset: 1 }
    ]
);