aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src
diff options
context:
space:
mode:
authorBlaž Hrastnik2022-06-21 16:59:02 +0000
committerGitHub2022-06-21 16:59:02 +0000
commit19dccade7c44619bfa414a711fe72a612e4ca358 (patch)
treed0a502bc095c1619cfb94236782f42d595af0197 /helix-term/src
parenta17626a822b36d4de3146c2d410f976e19dd189c (diff)
parent458b89e21dcf76bbf9ca6ba237bd334f4922722d (diff)
Merge pull request #2359 from dead10ck/test-harness
Integration testing harness
Diffstat (limited to 'helix-term/src')
-rw-r--r--helix-term/src/application.rs157
-rw-r--r--helix-term/src/commands.rs13
-rw-r--r--helix-term/src/commands/lsp.rs6
-rw-r--r--helix-term/src/commands/typed.rs14
-rw-r--r--helix-term/src/compositor.rs26
-rw-r--r--helix-term/src/job.rs21
-rw-r--r--helix-term/src/main.rs28
-rw-r--r--helix-term/src/ui/mod.rs2
8 files changed, 191 insertions, 76 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs
index 0c8de2ab..48e9c275 100644
--- a/helix-term/src/application.rs
+++ b/helix-term/src/application.rs
@@ -1,4 +1,5 @@
use arc_swap::{access::Map, ArcSwap};
+use futures_util::Stream;
use helix_core::{
config::{default_syntax_loader, user_syntax_loader},
pos_at_coords, syntax, Selection,
@@ -24,10 +25,10 @@ use std::{
time::{Duration, Instant},
};
-use anyhow::Error;
+use anyhow::{Context, Error};
use crossterm::{
- event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream},
+ event::{DisableMouseCapture, EnableMouseCapture, Event},
execute, terminal,
tty::IsTty,
};
@@ -39,9 +40,11 @@ use {
#[cfg(windows)]
type Signals = futures_util::stream::Empty<()>;
+const LSP_DEADLINE: Duration = Duration::from_millis(16);
+
pub struct Application {
compositor: Compositor,
- editor: Editor,
+ pub editor: Editor,
config: Arc<ArcSwap<Config>>,
@@ -53,32 +56,39 @@ pub struct Application {
signals: Signals,
jobs: Jobs,
lsp_progress: LspProgressMap,
+ last_render: Instant,
+}
+
+#[cfg(feature = "integration")]
+fn setup_integration_logging() {
+ let level = std::env::var("HELIX_LOG_LEVEL")
+ .map(|lvl| lvl.parse().unwrap())
+ .unwrap_or(log::LevelFilter::Info);
+
+ // Separate file config so we can include year, month and day in file logs
+ let _ = fern::Dispatch::new()
+ .format(|out, message, record| {
+ out.finish(format_args!(
+ "{} {} [{}] {}",
+ chrono::Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"),
+ record.target(),
+ record.level(),
+ message
+ ))
+ })
+ .level(level)
+ .chain(std::io::stdout())
+ .apply();
}
impl Application {
- pub fn new(args: Args) -> Result<Self, Error> {
+ pub fn new(args: Args, config: Config) -> Result<Self, Error> {
+ #[cfg(feature = "integration")]
+ setup_integration_logging();
+
use helix_view::editor::Action;
let config_dir = helix_loader::config_dir();
- if !config_dir.exists() {
- std::fs::create_dir_all(&config_dir).ok();
- }
-
- let config = match std::fs::read_to_string(config_dir.join("config.toml")) {
- Ok(config) => toml::from_str(&config)
- .map(crate::keymap::merge_keys)
- .unwrap_or_else(|err| {
- eprintln!("Bad config: {}", err);
- eprintln!("Press <ENTER> to continue with default config");
- use std::io::Read;
- // This waits for an enter press.
- let _ = std::io::stdin().read(&mut []);
- Config::default()
- }),
- Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
- Err(err) => return Err(Error::new(err)),
- };
-
let theme_loader = std::sync::Arc::new(theme::Loader::new(
&config_dir,
&helix_loader::runtime_dir(),
@@ -116,7 +126,7 @@ impl Application {
});
let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
- let mut compositor = Compositor::new()?;
+ let mut compositor = Compositor::new().context("build compositor")?;
let config = Arc::new(ArcSwap::from_pointee(config));
let mut editor = Editor::new(
compositor.size(),
@@ -135,26 +145,28 @@ impl Application {
if args.load_tutor {
let path = helix_loader::runtime_dir().join("tutor.txt");
- editor.open(path, Action::VerticalSplit)?;
+ editor.open(&path, Action::VerticalSplit)?;
// Unset path to prevent accidentally saving to the original tutor file.
doc_mut!(editor).set_path(None)?;
} else if !args.files.is_empty() {
let first = &args.files[0].0; // we know it's not empty
if first.is_dir() {
- std::env::set_current_dir(&first)?;
+ std::env::set_current_dir(&first).context("set current dir")?;
editor.new_file(Action::VerticalSplit);
let picker = ui::file_picker(".".into(), &config.load().editor);
compositor.push(Box::new(overlayed(picker)));
} else {
let nr_of_files = args.files.len();
- editor.open(first.to_path_buf(), Action::VerticalSplit)?;
+ editor.open(first, Action::VerticalSplit)?;
for (file, pos) in args.files {
if file.is_dir() {
return Err(anyhow::anyhow!(
"expected a path to file, found a directory. (to open a directory pass it as first argument)"
));
} else {
- let doc_id = editor.open(file, Action::Load)?;
+ let doc_id = editor
+ .open(&file, Action::Load)
+ .context(format!("open '{}'", file.to_string_lossy()))?;
// with Action::Load all documents have the same view
let view_id = editor.tree.focus;
let doc = editor.document_mut(doc_id).unwrap();
@@ -168,7 +180,7 @@ impl Application {
let (view, doc) = current!(editor);
align_view(doc, view, Align::Center);
}
- } else if stdin().is_tty() {
+ } else if stdin().is_tty() || cfg!(feature = "integration") {
editor.new_file(Action::VerticalSplit);
} else if cfg!(target_os = "macos") {
// On Linux and Windows, we allow the output of a command to be piped into the new buffer.
@@ -186,7 +198,8 @@ impl Application {
#[cfg(windows)]
let signals = futures_util::stream::empty();
#[cfg(not(windows))]
- let signals = Signals::new(&[signal::SIGTSTP, signal::SIGCONT])?;
+ let signals =
+ Signals::new(&[signal::SIGTSTP, signal::SIGCONT]).context("build signal handler")?;
let app = Self {
compositor,
@@ -200,40 +213,57 @@ impl Application {
signals,
jobs: Jobs::new(),
lsp_progress: LspProgressMap::new(),
+ last_render: Instant::now(),
};
Ok(app)
}
fn render(&mut self) {
+ let compositor = &mut self.compositor;
+
let mut cx = crate::compositor::Context {
editor: &mut self.editor,
jobs: &mut self.jobs,
scroll: None,
};
- self.compositor.render(&mut cx);
+ compositor.render(&mut cx);
}
- pub async fn event_loop(&mut self) {
- let mut reader = EventStream::new();
- let mut last_render = Instant::now();
- let deadline = Duration::from_secs(1) / 60;
-
+ pub async fn event_loop<S>(&mut self, input_stream: &mut S)
+ where
+ S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
+ {
self.render();
+ self.last_render = Instant::now();
loop {
- if self.editor.should_close() {
+ if !self.event_loop_until_idle(input_stream).await {
break;
}
+ }
+ }
+
+ pub async fn event_loop_until_idle<S>(&mut self, input_stream: &mut S) -> bool
+ where
+ S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
+ {
+ #[cfg(feature = "integration")]
+ let mut idle_handled = false;
+
+ loop {
+ if self.editor.should_close() {
+ return false;
+ }
use futures_util::StreamExt;
tokio::select! {
biased;
- event = reader.next() => {
- self.handle_terminal_events(event)
+ Some(event) = input_stream.next() => {
+ self.handle_terminal_events(event);
}
Some(signal) = self.signals.next() => {
self.handle_signals(signal).await;
@@ -242,9 +272,10 @@ impl Application {
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 || last_render.elapsed() > deadline {
+
+ if last || self.last_render.elapsed() > LSP_DEADLINE {
self.render();
- last_render = Instant::now();
+ self.last_render = Instant::now();
}
}
Some(payload) = self.editor.debugger_events.next() => {
@@ -269,8 +300,24 @@ impl Application {
// idle timeout
self.editor.clear_idle_timer();
self.handle_idle_timeout();
+
+ #[cfg(feature = "integration")]
+ {
+ idle_handled = true;
+ }
}
}
+
+ // for integration tests only, reset the idle timer after every
+ // event to make a signal when test events are done processing
+ #[cfg(feature = "integration")]
+ {
+ if idle_handled {
+ return true;
+ }
+
+ self.editor.reset_idle_timer();
+ }
}
}
@@ -370,7 +417,7 @@ impl Application {
}
}
- pub fn handle_terminal_events(&mut self, event: Option<Result<Event, crossterm::ErrorKind>>) {
+ pub fn handle_terminal_events(&mut self, event: Result<Event, crossterm::ErrorKind>) {
let mut cx = crate::compositor::Context {
editor: &mut self.editor,
jobs: &mut self.jobs,
@@ -378,15 +425,14 @@ impl Application {
};
// Handle key events
let should_redraw = match event {
- Some(Ok(Event::Resize(width, height))) => {
+ Ok(Event::Resize(width, height)) => {
self.compositor.resize(width, height);
self.compositor
.handle_event(Event::Resize(width, height), &mut cx)
}
- Some(Ok(event)) => self.compositor.handle_event(event, &mut cx),
- Some(Err(x)) => panic!("{}", x),
- None => panic!(),
+ Ok(event) => self.compositor.handle_event(event, &mut cx),
+ Err(x) => panic!("{}", x),
};
if should_redraw && !self.editor.should_close() {
@@ -740,7 +786,10 @@ impl Application {
Ok(())
}
- pub async fn run(&mut self) -> Result<i32, Error> {
+ pub async fn run<S>(&mut self, input_stream: &mut S) -> Result<i32, Error>
+ where
+ S: Stream<Item = crossterm::Result<crossterm::event::Event>> + Unpin,
+ {
self.claim_term().await?;
// Exit the alternate screen and disable raw mode before panicking
@@ -755,16 +804,20 @@ impl Application {
hook(info);
}));
- self.event_loop().await;
+ self.event_loop(input_stream).await;
+ self.close().await?;
+ self.restore_term()?;
+
+ Ok(self.editor.exit_code)
+ }
- self.jobs.finish().await;
+ pub async fn close(&mut self) -> anyhow::Result<()> {
+ self.jobs.finish().await?;
if self.editor.close_language_servers(None).await.is_err() {
log::error!("Timed out waiting for language servers to shutdown");
};
- self.restore_term()?;
-
- Ok(self.editor.exit_code)
+ Ok(())
}
}
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 68c585b0..9239b49f 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -1026,7 +1026,7 @@ fn goto_file_impl(cx: &mut Context, action: Action) {
for sel in paths {
let p = sel.trim();
if !p.is_empty() {
- if let Err(e) = cx.editor.open(PathBuf::from(p), action) {
+ if let Err(e) = cx.editor.open(&PathBuf::from(p), action) {
cx.editor.set_error(format!("Open file failed: {:?}", e));
}
}
@@ -1855,7 +1855,7 @@ fn global_search(cx: &mut Context) {
}
},
move |cx, (line_num, path), action| {
- match cx.editor.open(path.into(), action) {
+ match cx.editor.open(path, action) {
Ok(_) => {}
Err(e) => {
cx.editor.set_error(format!(
@@ -2100,10 +2100,17 @@ fn insert_mode(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
enter_insert_mode(doc);
+ log::trace!(
+ "entering insert mode with sel: {:?}, text: {:?}",
+ doc.selection(view.id),
+ doc.text().to_string()
+ );
+
let selection = doc
.selection(view.id)
.clone()
.transform(|range| Range::new(range.to(), range.from()));
+
doc.set_selection(view.id, selection);
}
@@ -2449,8 +2456,8 @@ fn normal_mode(cx: &mut Context) {
graphemes::prev_grapheme_boundary(text, range.to()),
)
});
- doc.set_selection(view.id, selection);
+ doc.set_selection(view.id, selection);
doc.restore_cursor = false;
}
}
diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs
index 3ee3c54a..b8c5e5d1 100644
--- a/helix-term/src/commands/lsp.rs
+++ b/helix-term/src/commands/lsp.rs
@@ -61,7 +61,7 @@ fn jump_to_location(
return;
}
};
- let _id = editor.open(path, action).expect("editor.open failed");
+ let _id = editor.open(&path, action).expect("editor.open failed");
let (view, doc) = current!(editor);
let definition_pos = location.range.start;
// TODO: convert inside server
@@ -114,7 +114,7 @@ fn sym_picker(
return;
}
};
- if let Err(err) = cx.editor.open(path, action) {
+ if let Err(err) = cx.editor.open(&path, action) {
let err = format!("failed to open document: {}: {}", uri, err);
log::error!("{}", err);
cx.editor.set_error(err);
@@ -383,7 +383,7 @@ pub fn apply_workspace_edit(
};
let current_view_id = view!(editor).id;
- let doc_id = match editor.open(path, Action::Load) {
+ let doc_id = match editor.open(&path, Action::Load) {
Ok(doc_id) => doc_id,
Err(err) => {
let err = format!("failed to open document: {}: {}", uri, err);
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index 58256c7d..19c6a5dc 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -50,7 +50,7 @@ fn open(
ensure!(!args.is_empty(), "wrong argument count");
for arg in args {
let (path, pos) = args::parse_file(arg);
- let _ = cx.editor.open(path, Action::Replace)?;
+ let _ = cx.editor.open(&path, Action::Replace)?;
let (view, doc) = current!(cx.editor);
let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true));
doc.set_selection(view.id, pos);
@@ -233,6 +233,7 @@ fn write_impl(
doc.detect_language(cx.editor.syn_loader.clone());
let _ = cx.editor.refresh_language_server(id);
}
+
Ok(())
}
@@ -422,6 +423,7 @@ fn write_quit(
event: PromptEvent,
) -> anyhow::Result<()> {
write_impl(cx, args.first(), false)?;
+ helix_lsp::block_on(cx.jobs.finish())?;
quit(cx, &[], event)
}
@@ -819,7 +821,7 @@ fn vsplit(
} else {
for arg in args {
cx.editor
- .open(PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
+ .open(&PathBuf::from(arg.as_ref()), Action::VerticalSplit)?;
}
}
@@ -838,7 +840,7 @@ fn hsplit(
} else {
for arg in args {
cx.editor
- .open(PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?;
+ .open(&PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?;
}
}
@@ -923,7 +925,7 @@ fn tutor(
_event: PromptEvent,
) -> anyhow::Result<()> {
let path = helix_loader::runtime_dir().join("tutor.txt");
- cx.editor.open(path, Action::Replace)?;
+ cx.editor.open(&path, Action::Replace)?;
// Unset path to prevent accidentally saving to the original tutor file.
doc_mut!(cx.editor).set_path(None)?;
Ok(())
@@ -1150,7 +1152,7 @@ fn open_config(
_event: PromptEvent,
) -> anyhow::Result<()> {
cx.editor
- .open(helix_loader::config_file(), Action::Replace)?;
+ .open(&helix_loader::config_file(), Action::Replace)?;
Ok(())
}
@@ -1159,7 +1161,7 @@ fn open_log(
_args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
- cx.editor.open(helix_loader::log_file(), Action::Replace)?;
+ cx.editor.open(&helix_loader::log_file(), Action::Replace)?;
Ok(())
}
diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs
index e3cec643..61a3bfaf 100644
--- a/helix-term/src/compositor.rs
+++ b/helix-term/src/compositor.rs
@@ -5,6 +5,9 @@ use helix_core::Position;
use helix_view::graphics::{CursorKind, Rect};
use crossterm::event::Event;
+
+#[cfg(feature = "integration")]
+use tui::backend::TestBackend;
use tui::buffer::Buffer as Surface;
pub type Callback = Box<dyn FnOnce(&mut Compositor, &mut Context)>;
@@ -63,11 +66,21 @@ pub trait Component: Any + AnyComponent {
}
}
-use anyhow::Error;
+use anyhow::Context as AnyhowContext;
+use tui::backend::Backend;
+
+#[cfg(not(feature = "integration"))]
+use tui::backend::CrosstermBackend;
+
+#[cfg(not(feature = "integration"))]
use std::io::stdout;
-use tui::backend::{Backend, CrosstermBackend};
+
+#[cfg(not(feature = "integration"))]
type Terminal = tui::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
+#[cfg(feature = "integration")]
+type Terminal = tui::terminal::Terminal<TestBackend>;
+
pub struct Compositor {
layers: Vec<Box<dyn Component>>,
terminal: Terminal,
@@ -76,9 +89,14 @@ pub struct Compositor {
}
impl Compositor {
- pub fn new() -> Result<Self, Error> {
+ pub fn new() -> anyhow::Result<Self> {
+ #[cfg(not(feature = "integration"))]
let backend = CrosstermBackend::new(stdout());
- let terminal = Terminal::new(backend)?;
+
+ #[cfg(feature = "integration")]
+ let backend = TestBackend::new(120, 150);
+
+ let terminal = Terminal::new(backend).context("build terminal")?;
Ok(Self {
layers: Vec::new(),
terminal,
diff --git a/helix-term/src/job.rs b/helix-term/src/job.rs
index a6a77021..e5147992 100644
--- a/helix-term/src/job.rs
+++ b/helix-term/src/job.rs
@@ -2,7 +2,7 @@ use helix_view::Editor;
use crate::compositor::Compositor;
-use futures_util::future::{self, BoxFuture, Future, FutureExt};
+use futures_util::future::{BoxFuture, Future, FutureExt};
use futures_util::stream::{FuturesUnordered, StreamExt};
pub type Callback = Box<dyn FnOnce(&mut Editor, &mut Compositor) + Send>;
@@ -93,8 +93,21 @@ impl Jobs {
}
/// Blocks until all the jobs that need to be waited on are done.
- pub async fn finish(&mut self) {
- let wait_futures = std::mem::take(&mut self.wait_futures);
- wait_futures.for_each(|_| future::ready(())).await
+ pub async fn finish(&mut self) -> anyhow::Result<()> {
+ log::debug!("waiting on jobs...");
+ let mut wait_futures = std::mem::take(&mut self.wait_futures);
+ while let (Some(job), tail) = wait_futures.into_future().await {
+ match job {
+ Ok(_) => {
+ wait_futures = tail;
+ }
+ Err(e) => {
+ self.wait_futures = tail;
+ return Err(e);
+ }
+ }
+ }
+
+ Ok(())
}
}
diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs
index 58a90131..7b26fb11 100644
--- a/helix-term/src/main.rs
+++ b/helix-term/src/main.rs
@@ -1,6 +1,8 @@
-use anyhow::{Context, Result};
+use anyhow::{Context, Error, Result};
+use crossterm::event::EventStream;
use helix_term::application::Application;
use helix_term::args::Args;
+use helix_term::config::Config;
use std::path::PathBuf;
fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
@@ -110,10 +112,30 @@ FLAGS:
setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
+ let config_dir = helix_loader::config_dir();
+ if !config_dir.exists() {
+ std::fs::create_dir_all(&config_dir).ok();
+ }
+
+ let config = match std::fs::read_to_string(config_dir.join("config.toml")) {
+ Ok(config) => toml::from_str(&config)
+ .map(helix_term::keymap::merge_keys)
+ .unwrap_or_else(|err| {
+ eprintln!("Bad config: {}", err);
+ eprintln!("Press <ENTER> to continue with default config");
+ use std::io::Read;
+ // This waits for an enter press.
+ let _ = std::io::stdin().read(&mut []);
+ Config::default()
+ }),
+ Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
+ Err(err) => return Err(Error::new(err)),
+ };
+
// TODO: use the thread local executor to spawn the application task separately from the work pool
- let mut app = Application::new(args).context("unable to create new application")?;
+ let mut app = Application::new(args, config).context("unable to create new application")?;
- let exit_code = app.run().await?;
+ let exit_code = app.run(&mut EventStream::new()).await?;
Ok(exit_code)
}
diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs
index 23d0dca0..76ddaf89 100644
--- a/helix-term/src/ui/mod.rs
+++ b/helix-term/src/ui/mod.rs
@@ -175,7 +175,7 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
path.strip_prefix(&root).unwrap_or(path).to_string_lossy()
},
move |cx, path: &PathBuf, action| {
- if let Err(e) = cx.editor.open(path.into(), action) {
+ if let Err(e) = cx.editor.open(path, action) {
let err = if let Some(err) = e.source() {
format!("{}", err)
} else {