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

pub struct Buffer {
    pub id: Option<usize>,
    pub path: Option<PathBuf>,
    pub cursor: Cursor,
    pub syntax_definition: Option<SyntaxDefinition>,
    // 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

Methods

impl Buffer
[src]

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.

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]

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]

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");

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]

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();

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). The provided path is converted to its canonical, absolute equivalent, and stored alongside the buffer data.

Examples

use scribe::Buffer;
use std::path::Path;

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

Returns the contents of the buffer as a string.

Examples

use scribe::Buffer;

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

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();

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.

Returns the scope stack for the token at the cursor location.

Examples

use scribe::Buffer;
use scribe::buffer::{Position, Scope, ScopeStack};

// Set up a buffer with Rust source content and
// move the cursor to something of interest.
let mut buffer = Buffer::new();
buffer.insert("struct Buffer");
buffer.cursor.move_to(Position{ line: 0, offset: 7 });

// Omitted code to set up workspace / buffer syntax definition.
assert_eq!(
    workspace.current_buffer().unwrap().current_scope().unwrap(),
    ScopeStack::from_vec(
        vec![
            Scope::new("source.rust").unwrap(),
            Scope::new("meta.struct.rust").unwrap(),
            Scope::new("entity.name.struct.rust").unwrap()
        ]
    )
);

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::Path;

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

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());

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());

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());

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 }
    ]
);

Whether or not the buffer has been modified since being read from or written to disk. Buffers without paths are always considered modified.

Examples

use scribe::Buffer;
use std::path::Path;

let file_path = Path::new("tests/sample/file");
let mut buffer = Buffer::from_file(file_path).unwrap();

assert!(!buffer.modified());

// Inserting data into a buffer will flag it as modified.
buffer.insert("scribe");
assert!(buffer.modified());

// Undoing the modification reverses the flag.
buffer.undo();
assert!(!buffer.modified());

// Buffers without paths are always modified.
buffer = Buffer::new();
assert!(buffer.modified());

The number of lines in the buffer, including trailing newlines.

Examples

use scribe::Buffer;

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

assert_eq!(buffer.line_count(), 3);

Reloads the buffer from disk, discarding any in-memory modifications and history, as well as resetting the cursor to its initial (0,0) position. The buffer's ID and syntax definition are persisted.

Examples

use scribe::buffer::{Buffer, Position};
use std::path::Path;

let file_path = Path::new("tests/sample/file");
let mut buffer = Buffer::from_file(file_path).unwrap();
buffer.insert("scribe\nlibrary\n");
buffer.reload();

assert_eq!(buffer.data(), "it works!\n");
assert_eq!(*buffer.cursor, Position{ line: 0, offset: 0 });