@@ -1,10 +1,11 @@
1−use crate::{commands, keymap, theme::Theme, Args};
1+use crate::Args;
2use helix_core::{
2use helix_core::{
3 state::coords_at_pos,
3 state::coords_at_pos,
4 state::Mode,
4 state::Mode,
5 syntax::{HighlightConfiguration, HighlightEvent, Highlighter},
5 syntax::{HighlightConfiguration, HighlightEvent, Highlighter},
8+use helix_view::{commands, keymap, View};
10 io::{self, stdout, Write},
11 io::{self, stdout, Write},
@@ -31,19 +32,12 @@ type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;
32static EX: smol::Executor = smol::Executor::new();
33static EX: smol::Executor = smol::Executor::new();
37− pub size: (u16, u16),
40pub struct Editor {
35pub struct Editor {
41 terminal: Terminal,
36 terminal: Terminal,
42 view: Option<View>,
37 view: Option<View>,
43 size: (u16, u16),
38 size: (u16, u16),
44 surface: Surface,
39 surface: Surface,
45 cache: Surface,
40 cache: Surface,
49impl Editor {
43impl Editor {
@@ -53,7 +47,6 @@ impl Editor {
53 let mut terminal = Terminal::new(backend)?;
47 let mut terminal = Terminal::new(backend)?;
54 let size = terminal::size().unwrap();
48 let size = terminal::size().unwrap();
55 let area = Rect::new(0, 0, size.0, size.1);
49 let area = Rect::new(0, 0, size.0, size.1);
56− let theme = Theme::default();
58 let mut editor = Editor {
51 let mut editor = Editor {
@@ -61,7 +54,6 @@ impl Editor {
62 surface: Surface::empty(area),
55 surface: Surface::empty(area),
63 cache: Surface::empty(area),
56 cache: Surface::empty(area),
65 // TODO; move to state
57 // TODO; move to state
@@ -73,20 +65,7 @@ impl Editor {
75 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
67 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
76− let mut state = State::load(path)?;
68+ self.view = Some(View::open(path, self.size)?);
81− .configure(self.theme.scopes());
89− self.view = Some(view);
@@ -102,7 +81,7 @@ impl Editor {
103 // clear with background color
82 // clear with background color
104 self.surface
83 self.surface
105− .set_style(area, self.theme.get("ui.background"));
84+ .set_style(area, view.theme.get("ui.background").into());
107 let offset = 5 + 1; // 5 linenr + 1 gutter
86 let offset = 5 + 1; // 5 linenr + 1 gutter
108 let viewport = Rect::new(offset, 0, self.size.0, self.size.1 - 1); // - 1 for statusline
87 let viewport = Rect::new(offset, 0, self.size.0, self.size.1 - 1); // - 1 for statusline
@@ -161,7 +140,9 @@ impl Editor {
161 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
140 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
163 let style = match spans.first() {
142 let style = match spans.first() {
164− Some(span) => self.theme.get(self.theme.scopes()[span.0].as_str()),
144+ view.theme.get(view.theme.scopes()[span.0].as_str()).into()
165 None => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
146 None => Style::default().fg(Color::Rgb(164, 160, 232)), // lavender
@@ -202,7 +183,7 @@ impl Editor {
204 let mut line = 0;
185 let mut line = 0;
205− let style = self.theme.get("ui.linenr");
186+ let style: Style = view.theme.get("ui.linenr").into();
206 for i in view.first_line..(last_line as u16) {
187 for i in view.first_line..(last_line as u16) {
207 self.surface
188 self.surface
208 .set_stringn(0, line, format!("{:>5}", i + 1), 5, style); // lavender
189 .set_stringn(0, line, format!("{:>5}", i + 1), 5, style); // lavender
@@ -241,7 +222,7 @@ impl Editor {
242 self.surface.set_style(
223 self.surface.set_style(
243 Rect::new(0, self.size.1 - 1, self.size.0, 1),
224 Rect::new(0, self.size.1 - 1, self.size.0, 1),
244− self.theme.get("ui.statusline"),
225+ view.theme.get("ui.statusline").into(),
246 // TODO: unfocused one with different color
227 // TODO: unfocused one with different color
247 let text_color = Style::default().fg(Color::Rgb(219, 191, 239)); // lilac
228 let text_color = Style::default().fg(Color::Rgb(219, 191, 239)); // lilac
@@ -296,7 +277,9 @@ impl Editor {
296 self.cache = Surface::empty(area);
277 self.cache = Surface::empty(area);
298 // TODO: simplistic ensure cursor in view for now
279 // TODO: simplistic ensure cursor in view for now
280+ if let Some(view) = &mut self.view {
299− self.ensure_cursor_in_view();
281+ view.ensure_cursor_in_view()
301 self.render();
284 self.render();
@@ -333,18 +316,19 @@ impl Editor {
333 _ => (), // skip
316 _ => (), // skip
335 // TODO: simplistic ensure cursor in view for now
318 // TODO: simplistic ensure cursor in view for now
336− self.ensure_cursor_in_view();
319+ view.ensure_cursor_in_view();
338 self.render();
321 self.render();
340 Mode::Normal => {
323 Mode::Normal => {
341 // TODO: handle modes and sequences (`gg`)
324 // TODO: handle modes and sequences (`gg`)
325+ let keys = vec![event];
342− if let Some(command) = keymap.get(&event) {
326+ if let Some(command) = keymap[&Mode::Normal].get(&keys) {
343 // TODO: handle count other than 1
327 // TODO: handle count other than 1
344 command(view, 1);
328 command(view, 1);
346 // TODO: simplistic ensure cursor in view for now
330 // TODO: simplistic ensure cursor in view for now
347− self.ensure_cursor_in_view();
331+ view.ensure_cursor_in_view();
349 self.render();
333 self.render();
@@ -361,26 +345,6 @@ impl Editor {
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;
372− // TODO: side scroll
374− if line > document_end.saturating_sub(padding) {
376− view.first_line += line - (document_end.saturating_sub(padding));
377− } else if line < view.first_line + padding {
379− view.first_line = line.saturating_sub(padding);
384 pub async fn run(&mut self) -> Result<(), Error> {
348 pub async fn run(&mut self) -> Result<(), Error> {
385 enable_raw_mode()?;
349 enable_raw_mode()?;