From 6f8bf67fa6c3bf8d1d9995b9bda46a3197664803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 28 Mar 2022 10:50:41 +0900 Subject: [PATCH] helix-view now builds on WASM --- Cargo.lock | 1 - helix-view/Cargo.toml | 13 ++++++++----- helix-view/src/clipboard.rs | 5 +++-- helix-view/src/document.rs | 8 ++++++-- helix-view/src/editor.rs | 15 +++++++++++++-- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 89cd73d12..f14317292 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -938,7 +938,6 @@ dependencies = [ "helix-core", "helix-dap", "helix-lsp", - "helix-tui", "log", "once_cell", "serde", diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index 76281f8be..3074fb781 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -11,8 +11,9 @@ homepage = "https://helix-editor.com" [features] # default = ["dap", "lsp"] -lsp = ["helix-lsp"] -dap = ["helix-dap", "tokio-stream"] +lsp = ["helix-lsp", "tokio-runtime"] +dap = ["helix-dap", "tokio-stream", "tokio-runtime"] +tokio-runtime = ["tokio"] term = ["crossterm"] [dependencies] @@ -22,6 +23,7 @@ helix-core = { version = "0.6", path = "../helix-core" } helix-lsp = { version = "0.6", path = "../helix-lsp", optional = true } helix-dap = { version = "0.6", path = "../helix-dap", optional = true } tokio-stream = { version = "0.1", optional = true } +tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"], optional = true } crossterm = { version = "0.23", optional = true } @@ -31,7 +33,6 @@ url = "2" arc-swap = { version = "1.5.0" } -tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] } futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false } slotmap = "1" @@ -43,10 +44,12 @@ serde_json = "1.0" toml = "0.5" log = "~0.4" +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] which = "4.2" [target.'cfg(windows)'.dependencies] clipboard-win = { version = "4.4", features = ["std"] } -[dev-dependencies] -helix-tui = { path = "../helix-tui" } +# TODO: graphics.rs tests rely on this, but we should remove that +# [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +# helix-tui = { path = "../helix-tui" } diff --git a/helix-view/src/clipboard.rs b/helix-view/src/clipboard.rs index 8cd7983ba..cab69b489 100644 --- a/helix-view/src/clipboard.rs +++ b/helix-view/src/clipboard.rs @@ -14,6 +14,7 @@ pub trait ClipboardProvider: std::fmt::Debug { fn set_contents(&mut self, contents: String, clipboard_type: ClipboardType) -> Result<()>; } +#[cfg(not(target_arch = "wasm32"))] macro_rules! command_provider { (paste => $get_prg:literal $( , $get_arg:literal )* ; copy => $set_prg:literal $( , $set_arg:literal )* ; ) => {{ Box::new(provider::command::Provider { @@ -75,13 +76,13 @@ pub fn get_clipboard_provider() -> Box { } } -#[cfg(target_os = "wasm32")] +#[cfg(target_arch = "wasm32")] pub fn get_clipboard_provider() -> Box { // TODO: Box::new(provider::NopProvider::new()) } -#[cfg(not(any(windows, target_os = "wasm32", target_os = "macos")))] +#[cfg(not(any(windows, target_os = "macos", target_arch = "wasm32")))] pub fn get_clipboard_provider() -> Box { use provider::command::{env_var_is_set, exists, is_exit_success}; // TODO: support for user-defined provider, probably when we have plugin support by setting a diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index adf0afc50..b0e5306d3 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -257,6 +257,7 @@ pub fn from_reader( Ok((rope, encoding)) } +#[cfg(feature = "tokio-runtime")] // The documentation and implementation of this function should be up-to-date with // its sibling function, `from_reader()`. // @@ -474,11 +475,11 @@ fn save_impl>( // mark changes up to now as saved self.reset_modified(); + #[cfg(feature = "tokio-runtime")] let encoding = self.encoding; // We encode the file according to the `Document`'s encoding. async move { - use tokio::fs::File; if let Some(parent) = path.parent() { // TODO: display a prompt asking the user if the directories should be created if !parent.exists() { @@ -500,7 +501,9 @@ fn save_impl>( } } - let mut file = File::create(path).await?; + #[cfg(feature = "tokio-runtime")] + let mut file = tokio::fs::File::create(path).await?; + #[cfg(feature = "tokio-runtime")] to_writer(&mut file, encoding, &text).await?; #[cfg(feature = "lsp")] @@ -918,6 +921,7 @@ pub fn path(&self) -> Option<&PathBuf> { } /// File path as a URL. + #[cfg(feature = "lsp")] pub fn url(&self) -> Option { Url::from_file_path(self.path()?).ok() } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 02a7285c4..c3c9fa914 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -15,15 +15,19 @@ io::stdin, num::NonZeroUsize, path::{Path, PathBuf}, - pin::Pin, sync::Arc, }; +#[cfg(feature = "tokio-runtime")] +use std::pin::Pin; +#[cfg(feature = "tokio-runtime")] use tokio::{ sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}, - time::{sleep, Duration, Instant, Sleep}, + time::{sleep, Instant, Sleep}, }; +use std::time::Duration; + use anyhow::{bail, Error}; pub use helix_core::diagnostic::Severity; @@ -459,6 +463,7 @@ pub struct Editor { pub config: Box>, pub auto_pairs: Option, + #[cfg(feature = "tokio-runtime")] pub idle_timer: Pin>, pub last_motion: Option, pub pseudo_pending: Option, @@ -467,6 +472,8 @@ pub struct Editor { pub exit_code: i32, + // TODO: do this via a signal flag instead + #[cfg(feature = "tokio-runtime")] pub config_events: (UnboundedSender, UnboundedReceiver), } @@ -526,6 +533,7 @@ pub fn new( clipboard_provider: get_clipboard_provider(), status_msg: None, autoinfo: None, + #[cfg(feature = "tokio-runtime")] idle_timer: Box::pin(sleep(conf.idle_timeout)), last_motion: None, last_completion: None, @@ -533,6 +541,7 @@ pub fn new( config, auto_pairs, exit_code: 0, + #[cfg(feature = "tokio-runtime")] config_events: unbounded_channel(), } } @@ -541,6 +550,7 @@ pub fn config(&self) -> DynGuard { self.config.load() } + #[cfg(feature = "tokio-runtime")] pub fn clear_idle_timer(&mut self) { // equivalent to internal Instant::far_future() (30 years) self.idle_timer @@ -548,6 +558,7 @@ pub fn clear_idle_timer(&mut self) { .reset(Instant::now() + Duration::from_secs(86400 * 365 * 30)); } + #[cfg(feature = "tokio-runtime")] pub fn reset_idle_timer(&mut self) { let config = self.config(); self.idle_timer