Commit bb5ed210 authored by Daan Planken's avatar Daan Planken

Rustfmt changes

parent 14c9dd88
......@@ -26,7 +26,6 @@ impl Editor {
Char('L') => Some((self.buffers.current_buffer()[y].len() - 1, y)),
Char('H') => Some((0, y)),
Char('t') => {
let ch = self.get_char();
if let Some(o) = self.next_ocur(ch, n.d()) {
......@@ -36,7 +35,6 @@ impl Editor {
}
}
Char('f') => {
let ch = self.get_char();
if let Some(o) = self.previous_ocur(ch, n.d()) {
......@@ -70,11 +68,13 @@ impl Editor {
Char('j') => Some(self.down_unbounded(n.d())),
Char('k') => Some(self.up_unbounded(n.d())),
Char('g') => Some((0, n.or(1) as isize - 1)),
Char('G') => Some((self.buffers.current_buffer()[y].len() as isize, self.buffers.current_buffer().len() as isize - 1)),
Char('G') => Some((
self.buffers.current_buffer()[y].len() as isize,
self.buffers.current_buffer().len() as isize - 1,
)),
Char('L') => Some(to_signed_pos((self.buffers.current_buffer()[y].len(), y))),
Char('H') => Some((0, y as isize)),
Char('t') => {
let ch = self.get_char();
if let Some(o) = self.next_ocur(ch, n.d()) {
......@@ -84,7 +84,6 @@ impl Editor {
}
}
Char('f') => {
let ch = self.get_char();
if let Some(o) = self.previous_ocur(ch, n.d()) {
......
......@@ -24,10 +24,8 @@ impl Editor {
/// Get position after a given position, i.e. a generalisation of .next()
#[inline]
pub fn after(&self, n: usize, (x, y): (usize, usize)) -> Option<(usize, usize)> {
// TODO: Make this more idiomatic {
if x + n < self.buffers.current_buffer()[y].len() {
Some((x + n, y))
} else {
if y + 1 >= self.buffers.current_buffer().len() {
......@@ -48,7 +46,6 @@ impl Editor {
}
}
}
}
}
// }
......@@ -76,7 +73,6 @@ impl Editor {
ry -= 1;
} else if ry == 0 {
return None;
}
}
}
......@@ -134,15 +130,18 @@ impl Editor {
#[inline]
pub fn down_unbounded(&self, n: usize) -> (isize, isize) {
(self.cursor().x as isize, self.y() as isize + n as isize)
}
/// Get n'th next ocurrence of a given charecter (relatively to the cursor)
pub fn next_ocur(&self, c: char, n: usize) -> Option<usize> {
let mut dn = 0;
let mut x = self.x();
let mut x = self.x();
for (i, ch) in self.buffers.current_buffer()[self.y()].chars().skip(x).enumerate() {
for (i, ch) in self.buffers.current_buffer()[self.y()]
.chars()
.skip(x)
.enumerate()
{
if ch == c {
if i > 0 {
dn += 1;
......@@ -160,10 +159,15 @@ impl Editor {
/// Get n'th previous ocurrence of a given charecter (relatively to the cursor)
pub fn previous_ocur(&self, c: char, n: usize) -> Option<usize> {
let mut dn = 0;
let mut x = self.x();
let y = self.y();
for (i, ch) in self.buffers.current_buffer()[y].chars().rev().skip(self.buffers.current_buffer()[y].len() - x).enumerate() {
let mut x = self.x();
let y = self.y();
for (i, ch) in self.buffers.current_buffer()[y]
.chars()
.rev()
.skip(self.buffers.current_buffer()[y].len() - x)
.enumerate()
{
if ch == c {
dn += 1;
if dn == n {
......@@ -181,9 +185,13 @@ impl Editor {
/// whitespace. An empty line is also considered to be a WORD."
pub fn _next_word_forward(&self, n: usize) -> Option<usize> {
let mut dn = 0;
let mut x = self.x();
let mut x = self.x();
for (i, ch) in self.buffers.current_buffer()[self.y()].chars().skip(x).enumerate() {
for (i, ch) in self.buffers.current_buffer()[self.y()]
.chars()
.skip(x)
.enumerate()
{
if ch.is_whitespace() {
dn += 1;
if dn == n {
......
......@@ -29,15 +29,13 @@ impl Editor {
/// Convert a position value to a bounded position value
#[inline]
pub fn bound(&self, (x, mut y): (usize, usize), tight: bool) -> (usize, usize) {
y = if y >= self.buffers.current_buffer().len() {
self.buffers.current_buffer().len() - 1
} else {
y
};
let ln = self.buffers.current_buffer()[y].len() + if tight {0} else {1};
let ln = self.buffers.current_buffer()[y].len() + if tight { 0 } else { 1 };
if x >= ln {
if ln == 0 {
(0, y)
......@@ -59,7 +57,6 @@ impl Editor {
/// axis is bounded.
#[inline]
pub fn bound_ver(&self, (x, mut y): (usize, usize)) -> (usize, usize) {
// Is this premature optimization? Yes, yes it is!
y = if y > self.buffers.current_buffer().len() - 1 {
self.buffers.current_buffer().len() - 1
......
use state::editor::Editor;
use io::parse::{Inst, Parameter};
use state::mode::{Mode, CommandMode, PrimitiveMode};
use edit::insert::{InsertOptions, InsertMode};
use state::mode::{CommandMode, Mode, PrimitiveMode};
use edit::insert::{InsertMode, InsertOptions};
use io::redraw::RedrawTask;
use edit::buffer::TextBuffer;
use core::prompt::PromptCommand;
......@@ -28,7 +28,7 @@ impl Editor {
let left = self.left(1);
self.goto(left);
self.cursor_mut().mode = Mode::Command(CommandMode::Normal);
},
}
(Primitive(Insert(_)), Char(' ')) if self.key_state.shift => {
let left = self.left(1);
self.goto(left);
......@@ -38,60 +38,50 @@ impl Editor {
self.cursor_mut().mode = Mode::Command(CommandMode::Normal)
}
(_, Char(' ')) if self.key_state.alt => self.next_cursor(),
_ if self.key_state.alt => {
if let Some(m) = self.to_motion(Inst(para, cmd)) {
self.goto(m);
}
}
_ if self.key_state.alt => if let Some(m) = self.to_motion(Inst(para, cmd)) {
self.goto(m);
},
(Command(Normal), Char('i')) => {
self.cursor_mut().mode =
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
}
(Command(Normal), Char('I')) => {
self.cursor_mut().x = 0;
self.cursor_mut().mode =
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
}
(Command(Normal), Char('a')) => {
let pos = self.right(1, false);
self.goto(pos);
self.cursor_mut().mode =
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
}
(Command(Normal), Char('A')) => {
let pos = (self.buffers.current_buffer()[self.y()].len(), self.y());
//let pos = self.right(1, false);
self.goto(pos);
self.cursor_mut().mode =
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
}
(Command(Normal), Char('o')) => {
let y = self.y();
let ind = if self.options.autoindent {
self.buffers
.current_buffer()
.get_indent(y)
.to_owned()
self.buffers.current_buffer().get_indent(y).to_owned()
} else {
String::new()
};
let last = ind.len();
self.buffers.current_buffer_mut().insert_line(y + 1, ind.into());
self.buffers
.current_buffer_mut()
.insert_line(y + 1, ind.into());
self.goto((last, y + 1));
self.cursor_mut().mode =
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
}
(Command(Normal), Char('h')) => {
let left = self.left(n);
......@@ -154,10 +144,9 @@ impl Editor {
current_buffer.raw_buffer[y].insert(x, c);
}
(Command(Normal), Char('R')) => {
self.cursor_mut().mode =
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Replace,
}));
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Replace,
}));
}
(Command(Normal), Char('d')) => {
let ins = self.get_inst();
......@@ -169,10 +158,9 @@ impl Editor {
let ins = self.get_inst();
if let Some(m) = self.to_motion_unbounded(ins) {
self.remove_rb(m);
self.cursor_mut().mode =
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Insert,
}));
}
}
(Command(Normal), Char('G')) => {
......@@ -191,17 +179,13 @@ impl Editor {
mov = true;
}
}
}
(Command(Normal), Char('b')) => {
// Branch cursor
if self.buffers
.current_buffer_info()
.cursors
.len() < 255 {
if self.buffers.current_buffer_info().cursors.len() < 255 {
let cursor = self.cursor().clone();
let current_cursor_index = self.buffers.current_buffer_info().current_cursor as
usize;
let current_cursor_index =
self.buffers.current_buffer_info().current_cursor as usize;
self.buffers
.current_buffer_info_mut()
.cursors
......@@ -213,10 +197,7 @@ impl Editor {
}
(Command(Normal), Char('B')) => {
// Delete cursor
if self.buffers
.current_buffer_info()
.cursors
.len() > 1 {
if self.buffers.current_buffer_info().cursors.len() > 1 {
let current_cursor_index = self.buffers.current_buffer_info().current_cursor;
self.buffers
.current_buffer_info_mut()
......@@ -262,12 +243,10 @@ impl Editor {
(Command(Normal), Char('z')) => {
let Inst(param, cmd) = self.get_inst();
match param {
Parameter::Null => {
if let Some(m) = self.to_motion(Inst(param, cmd)) {
self.buffers.current_buffer_info_mut().scroll_y = m.1;
self.goto(m);
}
}
Parameter::Null => if let Some(m) = self.to_motion(Inst(param, cmd)) {
self.buffers.current_buffer_info_mut().scroll_y = m.1;
self.goto(m);
},
Parameter::Int(n) => {
self.buffers.current_buffer_info_mut().scroll_y = n;
}
......@@ -281,14 +260,12 @@ impl Editor {
(Command(Normal), Char('~')) => {
self.invert_chars(n);
}
(Command(Normal), Char('.')) => {
if let Some(inst) = self.previous_instruction {
self.exec(inst);
} else {
self.status_bar.msg = "No previous command".into();
self.redraw_task = RedrawTask::StatusBar;
}
}
(Command(Normal), Char('.')) => if let Some(inst) = self.previous_instruction {
self.exec(inst);
} else {
self.status_bar.msg = "No previous command".into();
self.redraw_task = RedrawTask::StatusBar;
},
(Command(Normal), Char(c)) => {
self.status_bar.msg = format!("Unknown command: {}", c);
self.redraw_task = RedrawTask::StatusBar;
......@@ -296,7 +273,7 @@ impl Editor {
(Primitive(Insert(opt)), k) => self.insert(k, opt),
(Primitive(Prompt), Char('\n')) => {
self.cursor_mut().mode = Command(Normal);
if let Some(cmd) = PromptCommand::parse(&self.prompt.clone()) {;
if let Some(cmd) = PromptCommand::parse(&self.prompt.clone()) {
self.invoke(cmd);
self.prompt = String::new();
self.redraw_task = RedrawTask::StatusBar;
......
use io::file::FileStatus;
use io::redraw::RedrawTask;
use state::editor::{Buffer, BufferManager, Editor};
use edit::buffer::{TextBuffer, SplitBuffer};
use edit::buffer::{SplitBuffer, TextBuffer};
use std::process::exit;
......@@ -10,32 +10,32 @@ pub enum PromptCommand<'a> {
/// Set an option.
Set {
/// The option to set.
option: &'a str
option: &'a str,
},
/// Unset an option.
Unset {
/// The option to unset.
option: &'a str
option: &'a str,
},
/// Toggle an option.
Toggle {
/// The option to toggle.
option: &'a str
option: &'a str,
},
/// Get whether an option is set or not.
Get {
/// The option to get.
option: &'a str
option: &'a str,
},
/// Open the specified file in a new buffer.
Open {
/// The path to open.
path: &'a str
path: &'a str,
},
/// Write the current buffer to the specified path.
Write {
/// The path to write to.
path: &'a str
path: &'a str,
},
/// List the available buffers.
ListBuffers,
......@@ -44,7 +44,7 @@ pub enum PromptCommand<'a> {
/// Switch to the nth buffer.
SwitchToBuffer {
/// The index of the buffer to switch to.
buffer_index: usize
buffer_index: usize,
},
/// Display help in a new buffer.
Help,
......@@ -77,12 +77,14 @@ impl<'a> PromptCommand<'a> {
let rest: String = bn.chars().skip(1).collect();
if let Ok(number) = rest.parse::<usize>() {
SwitchToBuffer { buffer_index: number }
SwitchToBuffer {
buffer_index: number,
}
} else {
return None;
}
},
_ => return None
}
_ => return None,
})
}
}
......@@ -98,46 +100,45 @@ impl Editor {
Ok(()) => format!("Option set: {}", option),
Err(()) => format!("Option does not exist: {}", option),
}
},
}
Unset { option } => {
self.status_bar.msg = match self.options.unset(option) {
Ok(()) => format!("Option unset: {}", option),
Err(()) => format!("Option does not exist: {}", option),
}
},
}
Toggle { option } => {
self.status_bar.msg = match self.options.toggle(option) {
Ok(()) => format!("Option toggled: {}", option),
Err(()) => format!("Option does not exist: {}", option),
}
},
}
Get { option } => {
self.status_bar.msg = match self.options.get(option) {
Some(true) => format!("Option set: {}", option),
Some(false) => format!("Option unset: {}", option),
None => format!("Option does not exist: {}", option),
}
},
}
Open { path } => {
self.status_bar.msg = match self.open(path) {
FileStatus::NotFound => format!("File {} could not be opened", path),
FileStatus::Ok => format!("File {} opened", path),
_ => unreachable!(),
}
},
}
Write { path } => {
if self.options.get("readonly") == Some(true) {
// TODO: add override (w!)
self.status_bar.msg = format!("File {} is opened in readonly mode", path)
}
else {
} else {
self.status_bar.msg = match self.write(path) {
FileStatus::NotFound => format!("File {} could not be opened", path),
FileStatus::Ok => format!("File {} written", path),
FileStatus::Other => format!("Couldn't write {}", path),
}
}
},
}
ListBuffers => {
let description = get_buffers_description(&self.buffers);
let mut new_buffer: Buffer = SplitBuffer::from_str(&description).into();
......@@ -147,27 +148,25 @@ impl Editor {
let new_buffer_index = self.buffers.new_buffer(new_buffer);
self.buffers.switch_to(new_buffer_index);
self.redraw_task = RedrawTask::Full;
},
SwitchToBuffer { buffer_index: ix } => {
if !self.buffers.is_buffer_index_valid(ix) {
self.status_bar.msg = format!("Invalid buffer #{}", ix);
} else {
self.buffers.switch_to(ix);
self.redraw_task = RedrawTask::Full;
self.status_bar.msg = format!("Switched to buffer #{}", ix);
}
}
SwitchToBuffer { buffer_index: ix } => if !self.buffers.is_buffer_index_valid(ix) {
self.status_bar.msg = format!("Invalid buffer #{}", ix);
} else {
self.buffers.switch_to(ix);
self.redraw_task = RedrawTask::Full;
self.status_bar.msg = format!("Switched to buffer #{}", ix);
},
DeleteBuffer => {
let ix = self.buffers.current_buffer_index();
self.buffers.delete_buffer(ix);
self.redraw_task = RedrawTask::Full;
},
}
Help => {
self.open("/apps/sodium/help.txt");
},
}
Quit => {
exit(0);
},
}
}
self.hint();
......@@ -181,8 +180,7 @@ fn get_buffers_description(buffers: &BufferManager) -> String {
format!("b{}\t\t\t{}", i, title)
}
let descriptions =
buffers
let descriptions = buffers
.iter()
// don't include transient buffers like the one
// this is going to be shown in
......@@ -192,5 +190,8 @@ fn get_buffers_description(buffers: &BufferManager) -> String {
.collect::<Vec<_>>()
.join("\n");
format!("Buffers\n=====================================\n\n{}", descriptions)
format!(
"Buffers\n=====================================\n\n{}",
descriptions
)
}
......@@ -75,17 +75,18 @@ pub trait TextBuffer<'a> {
pub struct SplitBuffer {
before: Vec<String>,
after: Vec<String>,
#[cfg(debug)]
_hinted_since_edit: bool,
#[cfg(debug)] _hinted_since_edit: bool,
}
impl SplitBuffer {
fn up(&mut self) {
self.after.push(self.before.pop().expect("Popped last element"));
self.after
.push(self.before.pop().expect("Popped last element"));
}
fn down(&mut self) {
self.before.push(self.after.pop().expect("Popped last element"));
self.before
.push(self.after.pop().expect("Popped last element"));
}
fn y(&self) -> usize {
......@@ -207,7 +208,7 @@ impl<'a> TextBuffer<'a> for SplitBuffer {
for c in ln.chars() {
match c {
'\t' | ' ' => len += 1,
_ => break,
_ => break,
}
}
&ln[..len]
......@@ -226,7 +227,6 @@ impl Index<usize> for SplitBuffer {
}
}
impl IndexMut<usize> for SplitBuffer {
fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut String {
#[cfg(debug)]
fn debug_check(b: &mut SplitBuffer) {
......
......@@ -7,7 +7,7 @@ impl Editor {
/// Delete a character.
#[inline]
pub fn delete(&mut self) {
let &Cursor{ x, y, .. } = self.cursor();
let &Cursor { x, y, .. } = self.cursor();
if x == self.buffers.current_buffer()[y].len() {
if y + 1 < self.buffers.current_buffer().len() {
let s = self.buffers.current_buffer_mut().remove_line(y + 1);
......
......@@ -26,7 +26,7 @@ impl Editor {
let (mut x, mut y) = self.pos();
match (mode, k) {
(InsertMode::Insert, Key::Char('\n')) => {
let first_part = self.buffers.current_buffer()[y][..x].to_owned();
let first_part = self.buffers.current_buffer()[y][..x].to_owned();
let second_part = self.buffers.current_buffer()[y][x..].to_owned();
self.buffers.current_buffer_mut()[y] = first_part;
......@@ -38,11 +38,13 @@ impl Editor {
};
let begin = nl.len();
self.buffers.current_buffer_mut().insert_line(y + 1, nl + &second_part);
self.buffers
.current_buffer_mut()
.insert_line(y + 1, nl + &second_part);
self.redraw_task = RedrawTask::LinesAfter(y);
self.goto((begin, y + 1));
},
}
(InsertMode::Insert, Key::Backspace) => self.backspace(),
(InsertMode::Insert, Key::Char(c)) => {
self.buffers.current_buffer_mut()[y].insert(x, c);
......@@ -50,7 +52,7 @@ impl Editor {
self.redraw_task = RedrawTask::Lines(y..y + 1);
let right = self.right(1, false);
self.goto(right);
},
}
(InsertMode::Replace, Key::Char(c)) => {
if x == self.buffers.current_buffer()[y].len() {
let next = self.next(1);
......@@ -77,8 +79,8 @@ impl Editor {
self.goto(p);
}
self.redraw_task = RedrawTask::Lines(y..y + 1);
},
_ => {},
}
_ => {}
}
self.hint();
......@@ -90,5 +92,4 @@ impl Editor {
self.insert(Key::Char(c), opt);
}
}
}
......@@ -7,7 +7,7 @@ impl Editor {
/// defines a position on the same line, only the characters from the current position to the
/// motion's position are removed.
pub fn remove_rb<'a>(&mut self, (x, y): (isize, isize)) {
if y == (self.y() as isize ) {
if y == (self.y() as isize) {
let (x, y) = self.bound((x as usize, y as usize), false);
// Single line mode
let (a, b) = if self.x() > x {
......
use edit::buffer::{TextBuffer, SplitBuffer};
use edit::buffer::{SplitBuffer, TextBuffer};
use state::editor::{Buffer, Editor};
use std::fs::File;
use std::io::{Read, Write};
......@@ -36,7 +36,9 @@ impl Editor {
pub fn write(&mut self, path: &str) -> FileStatus {
self.buffers.current_buffer_info_mut().title = Some(path.into());
if let Some(mut file) = File::create(path).ok() {
if file.write(self.buffers.current_buffer().to_string().as_bytes()).is_ok() {
if file.write(self.buffers.current_buffer().to_string().as_bytes())
.is_ok()
{
FileStatus::Ok
} else {
FileStatus::Other
......
......@@ -19,11 +19,13 @@ impl Editor {
let w = self.window.width();
let h = self.window.height();
if self.buffers.current_buffer_info().scroll_y > 0 && pos_y <= self.buffers.current_buffer_info().scroll_y {
if self.buffers.current_buffer_info().scroll_y > 0
&& pos_y <= self.buffers.current_buffer_info().scroll_y
{
self.buffers.current_buffer_info_mut().scroll_y = pos_y - 1;
}
let window_lines = (h as usize/16) - 2;
let window_lines = (h as usize / 16) - 2;
if pos_y > self.buffers.current_buffer_info().scroll_y + window_lines {
self.buffers.current_buffer_info_mut().scroll_y = pos_y - window_lines;
......@@ -34,31 +36,32 @@ impl Editor {
(current_buffer.scroll_x, current_buffer.scroll_y)
};
// Redraw window
self.window.set(Color::rgb(25, 25, 25));
if self.options.line_marker {
self.window.rect(0,
(pos_y - scroll_y) as i32 * 16,
w,
16,
Color::rgb(45, 45, 45));
self.window.rect(
0,
(pos_y - scroll_y) as i32 * 16,
w,
16,
Color::rgb(45, 45, 45),
);
}
self.window.rect(8 * (pos_x - scroll_x) as i32,
16 * (pos_y - scroll_y) as i32,
8,
16,
Color::rgb(255, 255, 255));
self.window.rect(
8 * (pos_x - scroll_x) as i32,
16 * (pos_y - scroll_y) as i32,
8,
16,
Color::rgb(255, 255, 255),
);
let mut string = false;
for (y, row) in self.buffers
.current_buffer()
.lines()
.enumerate() {
for (y, row) in self.buffers.current_buffer().lines().enumerate() {
for (x, c) in row.chars().enumerate() {
// TODO: Move outta here
let color = if self.options.highlight {
......@@ -68,8 +71,22 @@ impl Editor {
(226, 225, 167) //(167, 222, 156)
}
_ if string => (226, 225, 167), //(167, 222, 156)
'!' | '@' | '#' | '$' | '%' | '^' | '&' | '|' | '*' | '+' | '-' | '/' |