@@ -1,10 +1,11 @@
1−use crate::{commands, keymap, theme::Theme, Args};
1+use crate::Args;
22use helix_core::{
33 state::coords_at_pos,
44 state::Mode,
55 syntax::{HighlightConfiguration, HighlightEvent, Highlighter},
66 State,
77};
8+use helix_view::{commands, keymap, View};
89
910use std::{
1011 io::{self, stdout, Write},
@@ -31,19 +32,12 @@ type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;
3132
3233static EX: smol::Executor = smol::Executor::new();
3334
34−pub struct View {
35− pub state: State,
36− pub first_line: u16,
37− pub size: (u16, u16),
38−}
39−
4035pub struct Editor {
4136 terminal: Terminal,
4237 view: Option<View>,
4338 size: (u16, u16),
4439 surface: Surface,
4540 cache: Surface,
46− theme: Theme,
4741}
4842
4943impl Editor {
@@ -53,7 +47,6 @@ impl Editor {
5347 let mut terminal = Terminal::new(backend)?;
5448 let size = terminal::size().unwrap();
5549 let area = Rect::new(0, 0, size.0, size.1);
56− let theme = Theme::default();
5750
5851 let mut editor = Editor {
5952 terminal,
@@ -61,7 +54,6 @@ impl Editor {
6154 size,
6255 surface: Surface::empty(area),
6356 cache: Surface::empty(area),
64− theme,
6557 // TODO; move to state
6658 };
6759
@@ -73,20 +65,7 @@ impl Editor {
7365 }
7466
7567 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
76− let mut state = State::load(path)?;
77− state
78− .syntax
79− .as_mut()
80− .unwrap()
81− .configure(self.theme.scopes());
82−
83− let view = View {
84− state,
85− first_line: 0,
86− size: self.size,
87− };
88−
89− self.view = Some(view);
68+ self.view = Some(View::open(path, self.size)?);
9069 Ok(())
9170 }
9271
@@ -102,7 +81,7 @@ impl Editor {
10281
10382 // clear with background color
10483 self.surface
105− .set_style(area, self.theme.get("ui.background"));
84+ .set_style(area, view.theme.get("ui.background").into());
10685
10786 let offset = 5 + 1; // 5 linenr + 1 gutter
10887 let viewport = Rect::new(offset, 0, self.size.0, self.size.1 - 1); // - 1 for statusline
@@ -161,7 +140,9 @@ impl Editor {
161140 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
162141
163142 let style = match spans.first() {
164− Some(span) => self.theme.get(self.theme.scopes()[span.0].as_str()),
143+ Some(span) => {
144+ view.theme.get(view.theme.scopes()[span.0].as_str()).into()
145+ }
165146 None => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
166147 };
167148
@@ -202,7 +183,7 @@ impl Editor {
202183 }
203184
204185 let mut line = 0;
205− let style = self.theme.get("ui.linenr");
186+ let style: Style = view.theme.get("ui.linenr").into();
206187 for i in view.first_line..(last_line as u16) {
207188 self.surface
208189 .set_stringn(0, line, format!("{:>5}", i + 1), 5, style); // lavender
@@ -241,7 +222,7 @@ impl Editor {
241222 };
242223 self.surface.set_style(
243224 Rect::new(0, self.size.1 - 1, self.size.0, 1),
244− self.theme.get("ui.statusline"),
225+ view.theme.get("ui.statusline").into(),
245226 );
246227 // TODO: unfocused one with different color
247228 let text_color = Style::default().fg(Color::Rgb(219, 191, 239)); // lilac
@@ -296,7 +277,9 @@ impl Editor {
296277 self.cache = Surface::empty(area);
297278
298279 // TODO: simplistic ensure cursor in view for now
299− self.ensure_cursor_in_view();
280+ if let Some(view) = &mut self.view {
281+ view.ensure_cursor_in_view()
282+ };
300283
301284 self.render();
302285 }
@@ -333,18 +316,19 @@ impl Editor {
333316 _ => (), // skip
334317 }
335318 // TODO: simplistic ensure cursor in view for now
336− self.ensure_cursor_in_view();
319+ view.ensure_cursor_in_view();
337320
338321 self.render();
339322 }
340323 Mode::Normal => {
341324 // TODO: handle modes and sequences (`gg`)
342− if let Some(command) = keymap.get(&event) {
325+ let keys = vec![event];
326+ if let Some(command) = keymap[&Mode::Normal].get(&keys) {
343327 // TODO: handle count other than 1
344328 command(view, 1);
345329
346330 // TODO: simplistic ensure cursor in view for now
347− self.ensure_cursor_in_view();
331+ view.ensure_cursor_in_view();
348332
349333 self.render();
350334 }
@@ -361,26 +345,6 @@ impl Editor {
361345 }
362346 }
363347
364− fn ensure_cursor_in_view(&mut self) {
365− if let Some(view) = &mut self.view {
366− let cursor = view.state.selection().cursor();
367− let line = view.state.doc().char_to_line(cursor) as u16;
368− let document_end = view.first_line + self.size.1.saturating_sub(1) - 1;
369−
370− let padding = 5u16;
371−
372− // TODO: side scroll
373−
374− if line > document_end.saturating_sub(padding) {
375− // scroll down
376− view.first_line += line - (document_end.saturating_sub(padding));
377− } else if line < view.first_line + padding {
378− // scroll up
379− view.first_line = line.saturating_sub(padding);
380− }
381− }
382− }
383−
384348 pub async fn run(&mut self) -> Result<(), Error> {
385349 enable_raw_mode()?;
386350