factor editor event handling into function

pull/1/head
Skyler Hawthorne 2 years ago
parent aaa1450678
commit 8c667ef8de

@ -287,9 +287,6 @@ impl Application {
where where
S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin, S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
{ {
#[cfg(feature = "integration")]
let mut idle_handled = false;
loop { loop {
if self.editor.should_close() { if self.editor.should_close() {
return false; return false;
@ -315,56 +312,19 @@ impl Application {
self.render(); self.render();
} }
event = self.editor.wait_event() => { event = self.editor.wait_event() => {
log::debug!("received editor event: {:?}", event); let _idle_handled = self.handle_editor_event(event).await;
match event { // for integration tests only, reset the idle timer after every
EditorEvent::DocumentSave(event) => { // event to signal when test events are done processing
self.handle_document_write(event); #[cfg(feature = "integration")]
self.render(); {
} if _idle_handled {
EditorEvent::ConfigEvent(event) => { return true;
self.handle_config_events(event);
self.render();
}
EditorEvent::LanguageServerMessage((id, call)) => {
self.handle_language_server_message(call, id).await;
// limit render calls for fast language server messages
let last = self.editor.language_servers.incoming.is_empty();
if last || self.last_render.elapsed() > LSP_DEADLINE {
self.render();
self.last_render = Instant::now();
}
}
EditorEvent::DebuggerEvent(payload) => {
let needs_render = self.editor.handle_debugger_message(payload).await;
if needs_render {
self.render();
}
}
EditorEvent::IdleTimer => {
self.editor.clear_idle_timer();
self.handle_idle_timeout();
#[cfg(feature = "integration")]
{
log::debug!("idle handled");
idle_handled = true;
}
} }
}
}
}
// for integration tests only, reset the idle timer after every self.editor.reset_idle_timer();
// event to signal when test events are done processing }
#[cfg(feature = "integration")]
{
if idle_handled {
return true;
} }
self.editor.reset_idle_timer();
} }
} }
} }
@ -517,6 +477,49 @@ impl Application {
} }
} }
#[inline(always)]
pub async fn handle_editor_event(&mut self, event: EditorEvent) -> bool {
log::debug!("received editor event: {:?}", event);
match event {
EditorEvent::DocumentSave(event) => {
self.handle_document_write(event);
self.render();
}
EditorEvent::ConfigEvent(event) => {
self.handle_config_events(event);
self.render();
}
EditorEvent::LanguageServerMessage((id, call)) => {
self.handle_language_server_message(call, id).await;
// limit render calls for fast language server messages
let last = self.editor.language_servers.incoming.is_empty();
if last || self.last_render.elapsed() > LSP_DEADLINE {
self.render();
self.last_render = Instant::now();
}
}
EditorEvent::DebuggerEvent(payload) => {
let needs_render = self.editor.handle_debugger_message(payload).await;
if needs_render {
self.render();
}
}
EditorEvent::IdleTimer => {
self.editor.clear_idle_timer();
self.handle_idle_timeout();
#[cfg(feature = "integration")]
{
return true;
}
}
}
false
}
pub fn handle_terminal_events(&mut self, event: Result<CrosstermEvent, crossterm::ErrorKind>) { pub fn handle_terminal_events(&mut self, event: Result<CrosstermEvent, crossterm::ErrorKind>) {
let mut cx = crate::compositor::Context { let mut cx = crate::compositor::Context {
editor: &mut self.editor, editor: &mut self.editor,

@ -14,7 +14,7 @@ mod statusline;
mod text; mod text;
use crate::compositor::{Component, Compositor}; use crate::compositor::{Component, Compositor};
use crate::job; use crate::job::{self, Callback};
pub use completion::Completion; pub use completion::Completion;
pub use editor::EditorView; pub use editor::EditorView;
pub use markdown::Markdown; pub use markdown::Markdown;
@ -121,7 +121,7 @@ pub fn regex_prompt(
if event == PromptEvent::Validate { if event == PromptEvent::Validate {
let callback = async move { let callback = async move {
let call: job::Callback = Box::new( let call: job::Callback = Callback::EditorCompositor(Box::new(
move |_editor: &mut Editor, compositor: &mut Compositor| { move |_editor: &mut Editor, compositor: &mut Compositor| {
let contents = Text::new(format!("{}", err)); let contents = Text::new(format!("{}", err));
let size = compositor.size(); let size = compositor.size();
@ -135,7 +135,7 @@ pub fn regex_prompt(
compositor.replace_or_push("invalid-regex", popup); compositor.replace_or_push("invalid-regex", popup);
}, },
); ));
Ok(call) Ok(call)
}; };

Loading…
Cancel
Save