diff options
author | Jan Hrastnik | 2021-06-19 12:51:53 +0000 |
---|---|---|
committer | Jan Hrastnik | 2021-06-19 12:51:53 +0000 |
commit | cdd9347457f0608346894cd0aab35b412cb59a7b (patch) | |
tree | 468078c37311cb1c7f9e7d4bd8a03c493d25e669 /helix-view | |
parent | 97323dc2f90f81afc82bd929d111abda540bebe5 (diff) | |
parent | 2cbec2b0470d0759578929b224c445b69617b6b6 (diff) |
Merge remote-tracking branch 'origin/master' into line_ending_detection
Diffstat (limited to 'helix-view')
-rw-r--r-- | helix-view/Cargo.toml | 9 | ||||
-rw-r--r-- | helix-view/src/document.rs | 112 | ||||
-rw-r--r-- | helix-view/src/editor.rs | 49 | ||||
-rw-r--r-- | helix-view/src/input.rs | 226 | ||||
-rw-r--r-- | helix-view/src/lib.rs | 11 | ||||
-rw-r--r-- | helix-view/src/macros.rs | 29 |
6 files changed, 390 insertions, 46 deletions
diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index 593f00e0..7f18e9a2 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -4,6 +4,10 @@ version = "0.2.0" authors = ["Blaž Hrastnik <blaz@mxxn.io>"] edition = "2018" license = "MPL-2.0" +description = "UI abstractions for use in backends" +categories = ["editor"] +repository = "https://github.com/helix-editor/helix" +homepage = "https://helix-editor.com" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -13,8 +17,8 @@ default = ["term"] [dependencies] anyhow = "1" -helix-core = { path = "../helix-core" } -helix-lsp = { path = "../helix-lsp"} +helix-core = { version = "0.2", path = "../helix-core" } +helix-lsp = { version = "0.2", path = "../helix-lsp"} # Conversion traits tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"], optional = true } @@ -23,6 +27,7 @@ once_cell = "1.8" url = "2" tokio = { version = "1", features = ["full"] } +futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false } slotmap = "1" diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index fe06d09d..49d270e4 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -1,7 +1,11 @@ -use anyhow::{Context, Error}; +use anyhow::{anyhow, Context, Error}; +use serde::de::{self, Deserialize, Deserializer}; use std::cell::Cell; +use std::collections::HashMap; +use std::fmt::Display; use std::future::Future; use std::path::{Component, Path, PathBuf}; +use std::str::FromStr; use std::sync::Arc; use helix_core::{ @@ -15,8 +19,6 @@ use helix_core::{ use crate::{DocumentId, ViewId}; -use std::collections::HashMap; - #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum Mode { Normal, @@ -24,6 +26,40 @@ pub enum Mode { Insert, } +impl Display for Mode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Mode::Normal => f.write_str("normal"), + Mode::Select => f.write_str("select"), + Mode::Insert => f.write_str("insert"), + } + } +} + +impl FromStr for Mode { + type Err = Error; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + match s { + "normal" => Ok(Mode::Normal), + "select" => Ok(Mode::Select), + "insert" => Ok(Mode::Insert), + _ => Err(anyhow!("Invalid mode '{}'", s)), + } + } +} + +// toml deserializer doesn't seem to recognize string as enum +impl<'de> Deserialize<'de> for Mode { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + s.parse().map_err(de::Error::custom) + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum IndentStyle { Tabs, @@ -105,6 +141,36 @@ where } } +/// Expands tilde `~` into users home directory if avilable, otherwise returns the path +/// unchanged. The tilde will only be expanded when present as the first component of the path +/// and only slash follows it. +pub fn expand_tilde(path: &Path) -> PathBuf { + let mut components = path.components().peekable(); + if let Some(Component::Normal(c)) = components.peek() { + if c == &"~" { + if let Ok(home) = helix_core::home_dir() { + // it's ok to unwrap, the path starts with `~` + return home.join(path.strip_prefix("~").unwrap()); + } + } + } + + path.to_path_buf() +} + +/// Replaces users home directory from `path` with tilde `~` if the directory +/// is available, otherwise returns the path unchanged. +pub fn fold_home_dir(path: &Path) -> PathBuf { + if let Ok(home) = helix_core::home_dir() { + if path.starts_with(&home) { + // it's ok to unwrap, the path starts with home dir + return PathBuf::from("~").join(path.strip_prefix(&home).unwrap()); + } + } + + path.to_path_buf() +} + /// Normalize a path, removing things like `.` and `..`. /// /// CAUTION: This does not resolve symlinks (unlike @@ -115,6 +181,7 @@ where /// needs to improve on. /// Copied from cargo: https://github.com/rust-lang/cargo/blob/070e459c2d8b79c5b2ac5218064e7603329c92ae/crates/cargo-util/src/paths.rs#L81 pub fn normalize_path(path: &Path) -> PathBuf { + let path = expand_tilde(path); let mut components = path.components().peekable(); let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() { components.next(); @@ -141,12 +208,17 @@ pub fn normalize_path(path: &Path) -> PathBuf { ret } -// Returns the canonical, absolute form of a path with all intermediate components normalized. -// -// This function is used instead of `std::fs::canonicalize` because we don't want to verify -// here if the path exists, just normalize it's components. +/// Returns the canonical, absolute form of a path with all intermediate components normalized. +/// +/// This function is used instead of `std::fs::canonicalize` because we don't want to verify +/// here if the path exists, just normalize it's components. pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> { - std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path))) + let normalized = normalize_path(path); + if normalized.is_absolute() { + Ok(normalized) + } else { + std::env::current_dir().map(|current_dir| current_dir.join(normalized)) + } } use helix_lsp::lsp; @@ -210,10 +282,11 @@ impl Document { pub fn format(&mut self, view_id: ViewId) { if let Some(language_server) = self.language_server() { // TODO: await, no blocking - let transaction = helix_lsp::block_on( - language_server - .text_document_formatting(self.identifier(), lsp::FormattingOptions::default()), - ) + let transaction = helix_lsp::block_on(language_server.text_document_formatting( + self.identifier(), + lsp::FormattingOptions::default(), + None, + )) .map(|edits| { helix_lsp::util::generate_transaction_from_edits( self.text(), @@ -696,12 +769,19 @@ impl Document { &self.selections[&view_id] } - pub fn relative_path(&self) -> Option<&Path> { + pub fn relative_path(&self) -> Option<PathBuf> { let cwdir = std::env::current_dir().expect("couldn't determine current directory"); - self.path - .as_ref() - .map(|path| path.strip_prefix(cwdir).unwrap_or(path)) + self.path.as_ref().map(|path| { + let path = fold_home_dir(path); + if path.is_relative() { + path + } else { + path.strip_prefix(cwdir) + .map(|p| p.to_path_buf()) + .unwrap_or(path) + } + }) } // pub fn slice<R>(&self, range: R) -> RopeSlice where R: RangeBounds { diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 24f43c0e..db8ae87a 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -2,7 +2,9 @@ use crate::{theme::Theme, tree::Tree, Document, DocumentId, RegisterSelection, V use tui::layout::Rect; use tui::terminal::CursorKind; +use futures_util::future; use std::path::PathBuf; +use std::time::Duration; use slotmap::SlotMap; @@ -101,19 +103,19 @@ impl Editor { match action { Action::Replace => { - let view = self.view(); + let view = view!(self); let jump = ( view.doc, self.documents[view.doc].selection(view.id).clone(), ); - let view = self.view_mut(); + let view = view_mut!(self); view.jumps.push(jump); view.last_accessed_doc = Some(view.doc); view.doc = id; view.first_line = 0; - let (view, doc) = self.current(); + let (view, doc) = current!(self); // initialize selection for view let selection = doc @@ -238,27 +240,6 @@ impl Editor { self.tree.is_empty() } - pub fn current(&mut self) -> (&mut View, &mut Document) { - let view = self.tree.get_mut(self.tree.focus); - let doc = &mut self.documents[view.doc]; - (view, doc) - } - - pub fn current_with_registers(&mut self) -> (&mut View, &mut Document, &mut Registers) { - let view = self.tree.get_mut(self.tree.focus); - let doc = &mut self.documents[view.doc]; - let registers = &mut self.registers; - (view, doc, registers) - } - - pub fn view(&self) -> &View { - self.tree.get(self.tree.focus) - } - - pub fn view_mut(&mut self) -> &mut View { - self.tree.get_mut(self.tree.focus) - } - pub fn ensure_cursor_in_view(&mut self, id: ViewId) { let view = self.tree.get_mut(id); let doc = &self.documents[view.doc]; @@ -280,7 +261,7 @@ impl Editor { pub fn cursor(&self) -> (Option<Position>, CursorKind) { const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter - let view = self.view(); + let view = view!(self); let doc = &self.documents[view.doc]; let cursor = doc.selection(view.id).cursor(); if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) { @@ -291,4 +272,22 @@ impl Editor { (None, CursorKind::Hidden) } } + + /// Closes language servers with timeout. The default timeout is 500 ms, use + /// `timeout` parameter to override this. + pub async fn close_language_servers( + &self, + timeout: Option<u64>, + ) -> Result<(), tokio::time::error::Elapsed> { + tokio::time::timeout( + Duration::from_millis(timeout.unwrap_or(500)), + future::join_all( + self.language_servers + .iter_clients() + .map(|client| client.force_shutdown()), + ), + ) + .await + .map(|_| ()) + } } diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs new file mode 100644 index 00000000..ab417819 --- /dev/null +++ b/helix-view/src/input.rs @@ -0,0 +1,226 @@ +//! Input event handling, currently backed by crossterm. +use anyhow::{anyhow, Error}; +use crossterm::event; +use serde::de::{self, Deserialize, Deserializer}; +use std::fmt; + +pub use crossterm::event::{KeyCode, KeyModifiers}; + +/// Represents a key event. +// We use a newtype here because we want to customize Deserialize and Display. +#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Copy, Hash)] +pub struct KeyEvent { + pub code: KeyCode, + pub modifiers: KeyModifiers, +} + +impl fmt::Display for KeyEvent { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!( + "{}{}{}", + if self.modifiers.contains(KeyModifiers::SHIFT) { + "S-" + } else { + "" + }, + if self.modifiers.contains(KeyModifiers::ALT) { + "A-" + } else { + "" + }, + if self.modifiers.contains(KeyModifiers::CONTROL) { + "C-" + } else { + "" + }, + ))?; + match self.code { + KeyCode::Backspace => f.write_str("backspace")?, + KeyCode::Enter => f.write_str("ret")?, + KeyCode::Left => f.write_str("left")?, + KeyCode::Right => f.write_str("right")?, + KeyCode::Up => f.write_str("up")?, + KeyCode::Down => f.write_str("down")?, + KeyCode::Home => f.write_str("home")?, + KeyCode::End => f.write_str("end")?, + KeyCode::PageUp => f.write_str("pageup")?, + KeyCode::PageDown => f.write_str("pagedown")?, + KeyCode::Tab => f.write_str("tab")?, + KeyCode::BackTab => f.write_str("backtab")?, + KeyCode::Delete => f.write_str("del")?, + KeyCode::Insert => f.write_str("ins")?, + KeyCode::Null => f.write_str("null")?, + KeyCode::Esc => f.write_str("esc")?, + KeyCode::Char('<') => f.write_str("lt")?, + KeyCode::Char('>') => f.write_str("gt")?, + KeyCode::Char('+') => f.write_str("plus")?, + KeyCode::Char('-') => f.write_str("minus")?, + KeyCode::Char(';') => f.write_str("semicolon")?, + KeyCode::Char('%') => f.write_str("percent")?, + KeyCode::F(i) => f.write_fmt(format_args!("F{}", i))?, + KeyCode::Char(c) => f.write_fmt(format_args!("{}", c))?, + }; + Ok(()) + } +} + +impl std::str::FromStr for KeyEvent { + type Err = Error; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + let mut tokens: Vec<_> = s.split('-').collect(); + let code = match tokens.pop().ok_or_else(|| anyhow!("Missing key code"))? { + "backspace" => KeyCode::Backspace, + "space" => KeyCode::Char(' '), + "ret" => KeyCode::Enter, + "lt" => KeyCode::Char('<'), + "gt" => KeyCode::Char('>'), + "plus" => KeyCode::Char('+'), + "minus" => KeyCode::Char('-'), + "semicolon" => KeyCode::Char(';'), + "percent" => KeyCode::Char('%'), + "left" => KeyCode::Left, + "right" => KeyCode::Right, + "up" => KeyCode::Down, + "home" => KeyCode::Home, + "end" => KeyCode::End, + "pageup" => KeyCode::PageUp, + "pagedown" => KeyCode::PageDown, + "tab" => KeyCode::Tab, + "backtab" => KeyCode::BackTab, + "del" => KeyCode::Delete, + "ins" => KeyCode::Insert, + "null" => KeyCode::Null, + "esc" => KeyCode::Esc, + single if single.len() == 1 => KeyCode::Char(single.chars().next().unwrap()), + function if function.len() > 1 && function.starts_with('F') => { + let function: String = function.chars().skip(1).collect(); + let function = str::parse::<u8>(&function)?; + (function > 0 && function < 13) + .then(|| KeyCode::F(function)) + .ok_or_else(|| anyhow!("Invalid function key '{}'", function))? + } + invalid => return Err(anyhow!("Invalid key code '{}'", invalid)), + }; + + let mut modifiers = KeyModifiers::empty(); + for token in tokens { + let flag = match token { + "S" => KeyModifiers::SHIFT, + "A" => KeyModifiers::ALT, + "C" => KeyModifiers::CONTROL, + _ => return Err(anyhow!("Invalid key modifier '{}-'", token)), + }; + + if modifiers.contains(flag) { + return Err(anyhow!("Repeated key modifier '{}-'", token)); + } + modifiers.insert(flag); + } + + Ok(KeyEvent { code, modifiers }) + } +} + +impl<'de> Deserialize<'de> for KeyEvent { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + s.parse().map_err(de::Error::custom) + } +} + +impl From<event::KeyEvent> for KeyEvent { + fn from(event::KeyEvent { code, modifiers }: event::KeyEvent) -> KeyEvent { + KeyEvent { code, modifiers } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn parsing_unmodified_keys() { + assert_eq!( + str::parse::<KeyEvent>("backspace").unwrap(), + KeyEvent { + code: KeyCode::Backspace, + modifiers: KeyModifiers::NONE + } + ); + + assert_eq!( + str::parse::<KeyEvent>("left").unwrap(), + KeyEvent { + code: KeyCode::Left, + modifiers: KeyModifiers::NONE + } + ); + + assert_eq!( + str::parse::<KeyEvent>(",").unwrap(), + KeyEvent { + code: KeyCode::Char(','), + modifiers: KeyModifiers::NONE + } + ); + + assert_eq!( + str::parse::<KeyEvent>("w").unwrap(), + KeyEvent { + code: KeyCode::Char('w'), + modifiers: KeyModifiers::NONE + } + ); + + assert_eq!( + str::parse::<KeyEvent>("F12").unwrap(), + KeyEvent { + code: KeyCode::F(12), + modifiers: KeyModifiers::NONE + } + ); + } + + #[test] + fn parsing_modified_keys() { + assert_eq!( + str::parse::<KeyEvent>("S-minus").unwrap(), + KeyEvent { + code: KeyCode::Char('-'), + modifiers: KeyModifiers::SHIFT + } + ); + + assert_eq!( + str::parse::<KeyEvent>("C-A-S-F12").unwrap(), + KeyEvent { + code: KeyCode::F(12), + modifiers: KeyModifiers::SHIFT | KeyModifiers::CONTROL | KeyModifiers::ALT + } + ); + + assert_eq!( + str::parse::<KeyEvent>("S-C-2").unwrap(), + KeyEvent { + code: KeyCode::Char('2'), + modifiers: KeyModifiers::SHIFT | KeyModifiers::CONTROL + } + ); + } + + #[test] + fn parsing_nonsensical_keys_fails() { + assert!(str::parse::<KeyEvent>("F13").is_err()); + assert!(str::parse::<KeyEvent>("F0").is_err()); + assert!(str::parse::<KeyEvent>("aaa").is_err()); + assert!(str::parse::<KeyEvent>("S-S-a").is_err()); + assert!(str::parse::<KeyEvent>("C-A-S-C-1").is_err()); + assert!(str::parse::<KeyEvent>("FU").is_err()); + assert!(str::parse::<KeyEvent>("123").is_err()); + assert!(str::parse::<KeyEvent>("S--").is_err()); + } +} diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index 7e253320..8b635700 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -1,13 +1,18 @@ +#[macro_use] +pub mod macros; + pub mod document; pub mod editor; +pub mod input; pub mod register_selection; pub mod theme; pub mod tree; pub mod view; -use slotmap::new_key_type; -new_key_type! { pub struct DocumentId; } -new_key_type! { pub struct ViewId; } +slotmap::new_key_type! { + pub struct DocumentId; + pub struct ViewId; +} pub use document::Document; pub use editor::Editor; diff --git a/helix-view/src/macros.rs b/helix-view/src/macros.rs new file mode 100644 index 00000000..a06d37e7 --- /dev/null +++ b/helix-view/src/macros.rs @@ -0,0 +1,29 @@ +#[macro_export] +macro_rules! current { + ( $( $editor:ident ).+ ) => {{ + let view = $crate::view_mut!( $( $editor ).+ ); + let doc = &mut $( $editor ).+ .documents[view.doc]; + (view, doc) + }}; +} + +#[macro_export] +macro_rules! doc_mut { + ( $( $editor:ident ).+ ) => {{ + $crate::current!( $( $editor ).+ ).1 + }}; +} + +#[macro_export] +macro_rules! view_mut { + ( $( $editor:ident ).+ ) => {{ + $( $editor ).+ .tree.get_mut($( $editor ).+ .tree.focus) + }}; +} + +#[macro_export] +macro_rules! view { + ( $( $editor:ident ).+ ) => {{ + $( $editor ).+ .tree.get($( $editor ).+ .tree.focus) + }}; +} |