Commit e021fe40 authored by Ticki's avatar Ticki

Huge refactoring in Sodium fix bug

parent bea6176f
......@@ -27,3 +27,5 @@ The bug causing these two bugs, is localised to be in position.rs. It resolves b
4) Press a and go to append mode
5) Type text
6) Out of bound (index) error
- [ ] When typing the first char in a line in normal insert mode, it wont go to the next char.
......@@ -15,8 +15,6 @@ pub struct Editor {
pub scroll_y: usize,
/// The window
pub window: Window,
/// The key state
pub key_state: KeyState,
/// The status bar
pub status_bar: StatusBar,
/// The prompt
......@@ -43,7 +41,6 @@ impl Editor {
scroll_x: 0,
scroll_y: 0,
window: *window,
key_state: KeyState::new(),
status_bar: StatusBar::new(),
prompt: String::new(),
options: Options::new(),
......
......@@ -10,14 +10,8 @@ impl Editor {
use super::CommandMode::*;
let n = para.d();
match cmd {
Ctrl(b) => self.key_state.ctrl = b,
Alt(b) => self.key_state.alt = b,
Shift(b) => self.key_state.shift = b,
_ => {},
}
if cmd == Char(' ') && self.key_state.shift {
if cmd.key == Key::Char(' ') && cmd.shift {
let mode = self.cursor().mode;
......@@ -27,11 +21,11 @@ impl Editor {
}
self.cursor_mut().mode = Mode::Command(CommandMode::Normal);
} else if self.key_state.alt && cmd == Key::Char(' ') {
} else if cmd.alt && cmd.key == Key::Char(' ') {
self.next_cursor();
} else if self.key_state.alt {
} else if cmd.alt {
if let Some(m) = self.to_motion(Inst(para, cmd)) {
self.goto(m);
......@@ -39,7 +33,7 @@ impl Editor {
} else {
match self.cursor().mode {
Command(Normal) => match cmd {
Command(Normal) => match cmd.key {
Char('i') => {
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(
InsertOptions {
......@@ -120,6 +114,7 @@ impl Editor {
Char('d') => {
let ins = self.next_inst();
if let Some(m) = self.to_motion(ins) {
debugln!("Delete (x, y) : ({}, {})", m.0, m.1);
self.remove_rb(m);
}
},
......@@ -181,10 +176,10 @@ impl Editor {
_ => {},
},
Primitive(Insert(opt)) => {
self.insert(cmd, opt);
self.insert(cmd.key, opt);
},
Primitive(Prompt) => {
match cmd {
match cmd.key {
Char('\n') => {
self.cursor_mut().mode = Command(Normal);
let cmd = self.prompt.clone();
......
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Debug)]
/// A key
pub enum Key {
Char(char),
Alt(bool),
Shift(bool),
Ctrl(bool),
// TODO: Space modifier?
Backspace,
Escape,
......@@ -13,5 +10,15 @@ pub enum Key {
Up,
Down,
Tab,
Null,
Unknown(u8),
}
#[derive(Copy, Clone, PartialEq)]
pub struct Cmd {
pub key: Key,
pub ctrl: bool,
pub alt: bool,
pub shift: bool,
}
......@@ -5,26 +5,33 @@ impl Editor {
pub fn to_motion(&mut self, Inst(n, cmd): Inst) -> Option<(usize, usize)> {
use super::Key::*;
match cmd {
Char('h') => Some(self.left(n.d())),
Char('l') => Some(self.right(n.d())),
Char('j') => Some(self.down(n.d())),
Char('k') => Some(self.up(n.d())),
Char('g') => Some((0, n.or(1) - 1)),
Char('G') => Some((0, self.text.len() - 1)),
Char('L') => Some(self.ln_end()),
Char('H') => Some((0, self.y())),
Char('t') => {
_ => match cmd.key {
Char('h') => Some(self.left(n.d())),
Char('l') => Some(self.right(n.d())),
Char('j') => Some(self.down(n.d())),
Char('k') => Some(self.up(n.d())),
Char('g') => Some((0, n.or(1) - 1)),
Char('G') => Some((0, self.text.len() - 1)),
Char('L') => Some(self.ln_end()),
Char('H') => Some((0, self.y())),
Char('t') => {
let ch = self.next_char();
let ch = self.next_char();
// ~v~ Optimize (sorry, Knuth)
if let Some(o) = self.next_ocur(ch, n.d()) {
Some(o)
} else {
// ~v~ Optimize (sorry, Knuth)
if let Some(o) = self.next_ocur(ch, n.d()) {
Some(o)
} else {
debug!("No ocur found!");
None
}
},
Char(c) => {
debug!("Motion not found: {}", c);
None
}
},
_ => None,
},
_ => None,
}
}
}
}
......@@ -3,7 +3,7 @@ use redox::*;
#[derive(Copy, Clone)]
/// An instruction
pub struct Inst(pub Parameter, pub Key);
pub struct Inst(pub Parameter, pub Cmd);
/// A numeral parameter
#[derive(Copy, Clone)]
......@@ -49,16 +49,23 @@ impl Editor {
let mut n = 0;
let mut unset = true;
let mut ctrl = false;
let mut alt = false;
let mut shift = false;
let mut key = Key::Null;
loop {
if let EventOption::Key(k) = self.window.poll().unwrap_or(Event::new()).to_option() {
let c = k.character;
match c {
'\0' => {
return Inst(Parameter::Null, match k.scancode {
K_ALT => Key::Alt(k.pressed),
K_CTRL => Key::Ctrl(k.pressed),
K_LEFT_SHIFT | K_RIGHT_SHIFT => Key::Shift(k.pressed),
s if k.pressed => match s {
// HERES THE BUG! It returns a modifier (which it shouldnt)
match k.scancode {
K_ALT => alt = k.pressed,
K_CTRL => ctrl = k.pressed,
K_LEFT_SHIFT | K_RIGHT_SHIFT => shift = k.pressed,
s if k.pressed => key = match s {
K_BKSP => Key::Backspace,
K_LEFT => Key::Left,
K_RIGHT => Key::Right,
......@@ -68,13 +75,13 @@ impl Editor {
K_ESC => Key::Escape,
_ => Key::Unknown(s),
},
s => Key::Unknown(s),
})
s => key = Key::Unknown(s),
}
}
_ => if k.pressed {
match self.cursor().mode {
Mode::Primitive(_) => {
return Inst(Parameter::Null, Key::Char(c));
key = Key::Char(c);
},
Mode::Command(_) => {
n = match c {
......@@ -120,7 +127,9 @@ impl Editor {
},
_ => {
return Inst(if unset { Parameter::Null } else { Parameter::Int(n) }, Key::Char(c));
key = Key::Char(c);
n
//return Inst(if unset { Parameter::Null } else { Parameter::Int(n) }, Key::Char(c));
}
}
}
......@@ -129,6 +138,16 @@ impl Editor {
},
}
}
if key != Key::Null {
return Inst(if unset { Parameter::Null } else { Parameter::Int(n) }, {
Cmd {
key: key,
ctrl: ctrl,
alt: alt,
shift: shift,
}
});
}
}
}
......
use super::*;
use redox::*;
impl Editor {
/// Remove from a given motion (row based)
pub fn remove_rb<'a>(&mut self, (x, y): (usize, usize)) {
debugln!("Y: {}, Bounded y: {}", y, self.y());
if y == self.y() {
// Single line mode
let (a, b) = if self.x() < x {
(self.x(), x)
} else {
let (a, b) = if self.x() > x {
(x, self.x())
} else {
(self.x(), x)
};
debugln!("A: {}, B: {}", a, b);
for _ in a..b {
self.text[y].remove(a);
}
......@@ -23,6 +26,8 @@ impl Editor {
for _ in a..(b + 1) {
if self.text.len() > 1 {
self.text.remove(a);
} else {
self.text[0] = VecDeque::new();
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment