@@ -277,7 +277,17 @@ impl Editor {
277277 // Handle key events
278278 let mut event = reader.next().await;
279279 match event {
280− // TODO: handle resize events
280+ Some(Ok(Event::Resize(width, height))) => {
281+ self.size = (width, height);
282+ let area = Rect::new(0, 0, width, height);
283+ self.surface = Surface::empty(area);
284+ self.cache = Surface::empty(area);
285+
286+ // TODO: simplistic ensure cursor in view for now
287+ self.ensure_cursor_in_view();
288+
289+ self.render();
290+ }
281291 Some(Ok(Event::Key(KeyEvent {
282292 code: KeyCode::Char('q'),
283293 ..
@@ -310,6 +320,9 @@ impl Editor {
310320 } => helix_core::commands::insert_char(state, '\n'),
311321 _ => (), // skip
312322 }
323+ // TODO: simplistic ensure cursor in view for now
324+ self.ensure_cursor_in_view();
325+
313326 self.render();
314327 }
315328 Mode::Normal => {
@@ -318,6 +331,9 @@ impl Editor {
318331 // TODO: handle count other than 1
319332 command(state, 1);
320333
334+ // TODO: simplistic ensure cursor in view for now
335+ self.ensure_cursor_in_view();
336+
321337 self.render();
322338 }
323339 }
@@ -333,6 +349,26 @@ impl Editor {
333349 }
334350 }
335351
352+ fn ensure_cursor_in_view(&mut self) {
353+ if let Some(state) = &mut self.state {
354+ let cursor = state.selection().cursor();
355+ let line = state.doc().char_to_line(cursor) as u16;
356+ let document_end = self.first_line + self.size.1.saturating_sub(1) - 1;
357+
358+ let padding = 5u16;
359+
360+ // TODO: side scroll
361+
362+ if line > document_end.saturating_sub(padding) {
363+ // scroll down
364+ self.first_line += line - (document_end.saturating_sub(padding));
365+ } else if line < self.first_line + padding {
366+ // scroll up
367+ self.first_line = line.saturating_sub(padding);
368+ }
369+ }
370+ }
371+
336372 pub async fn run(&mut self) -> Result<(), Error> {
337373 enable_raw_mode()?;
338374