diff options
author | Skyler Hawthorne | 2022-02-25 08:36:54 +0000 |
---|---|---|
committer | GitHub | 2022-02-25 08:36:54 +0000 |
commit | a494f47a5df543a3ab8d6530a5acbc2a5bd04d44 (patch) | |
tree | 084ef1b24c593d58d6616b37e073073d93009ff3 /helix-view | |
parent | b935fac9576cf333e22b82e40da8c4d73c8e547d (diff) |
Configurable auto pairs (#1624)
* impl auto pairs config
Implements configuration for which pairs of tokens get auto completed.
In order to help with this, the logic for when *not* to auto complete
has been generalized from a specific hardcoded list of characters to
simply testing if the next/prev char is alphanumeric.
It is possible to configure a global list of pairs as well as at the
language level. The language config will take precedence over the
global config.
* rename AutoPair -> Pair
* clean up insert_char command
* remove Rc
* remove some explicit cloning with another impl
* fix lint
* review comments
* global auto-pairs = false takes precedence over language settings
* make clippy happy
* print out editor config on startup
* move auto pairs accessor into Document
* rearrange auto pair doc comment
* use pattern in Froms
Diffstat (limited to 'helix-view')
-rw-r--r-- | helix-view/src/document.rs | 27 | ||||
-rw-r--r-- | helix-view/src/editor.rs | 19 |
2 files changed, 40 insertions, 6 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index f13338ba..671ceb75 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, bail, Context, Error}; +use helix_core::auto_pairs::AutoPairs; use serde::de::{self, Deserialize, Deserializer}; use serde::Serialize; use std::cell::Cell; @@ -20,7 +21,7 @@ use helix_core::{ }; use helix_lsp::util::LspFormatting; -use crate::{DocumentId, ViewId}; +use crate::{DocumentId, Editor, ViewId}; /// 8kB of buffer space for encoding and decoding `Rope`s. const BUF_SIZE: usize = 8192; @@ -98,7 +99,7 @@ pub struct Document { pub line_ending: LineEnding, syntax: Option<Syntax>, - // /// Corresponding language scope name. Usually `source.<lang>`. + /// Corresponding language scope name. Usually `source.<lang>`. pub(crate) language: Option<Arc<LanguageConfiguration>>, /// Pending changes since last history commit. @@ -946,6 +947,28 @@ impl Document { self.diagnostics .sort_unstable_by_key(|diagnostic| diagnostic.range); } + + /// Get the document's auto pairs. If the document has a recognized + /// language config with auto pairs configured, returns that; + /// otherwise, falls back to the global auto pairs config. If the global + /// config is false, then ignore language settings. + pub fn auto_pairs<'a>(&'a self, editor: &'a Editor) -> Option<&'a AutoPairs> { + let global_config = (editor.auto_pairs).as_ref(); + + // NOTE: If the user specifies the global auto pairs config as false, then + // we want to disable it globally regardless of language settings + #[allow(clippy::question_mark)] + { + if global_config.is_none() { + return None; + } + } + + match &self.language { + Some(lang) => lang.as_ref().auto_pairs.as_ref().or(global_config), + None => global_config, + } + } } impl Default for Document { diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index d44dc1c6..85d9be67 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -13,6 +13,7 @@ use futures_util::future; use futures_util::stream::select_all::SelectAll; use tokio_stream::wrappers::UnboundedReceiverStream; +use log::debug; use std::{ borrow::Cow, collections::{BTreeMap, HashMap}, @@ -29,7 +30,10 @@ use anyhow::{bail, Error}; pub use helix_core::diagnostic::Severity; pub use helix_core::register::Registers; -use helix_core::syntax; +use helix_core::{ + auto_pairs::AutoPairs, + syntax::{self, AutoPairConfig}, +}; use helix_core::{Position, Selection}; use helix_dap as dap; @@ -98,8 +102,10 @@ pub struct Config { pub line_number: LineNumber, /// Middle click paste support. Defaults to true. pub middle_click_paste: bool, - /// Automatic insertion of pairs to parentheses, brackets, etc. Defaults to true. - pub auto_pairs: bool, + /// Automatic insertion of pairs to parentheses, brackets, + /// etc. Optionally, this can be a list of 2-tuples to specify a + /// global list of characters to pair. Defaults to true. + pub auto_pairs: AutoPairConfig, /// Automatic auto-completion, automatically pop up without user trigger. Defaults to true. pub auto_completion: bool, /// Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. Defaults to 400ms. @@ -217,7 +223,7 @@ impl Default for Config { }, line_number: LineNumber::Absolute, middle_click_paste: true, - auto_pairs: true, + auto_pairs: AutoPairConfig::default(), auto_completion: true, idle_timeout: Duration::from_millis(400), completion_trigger_len: 2, @@ -289,6 +295,7 @@ pub struct Editor { pub autoinfo: Option<Info>, pub config: Config, + pub auto_pairs: Option<AutoPairs>, pub idle_timer: Pin<Box<Sleep>>, pub last_motion: Option<Motion>, @@ -312,6 +319,9 @@ impl Editor { config: Config, ) -> Self { let language_servers = helix_lsp::Registry::new(); + let auto_pairs = (&config.auto_pairs).into(); + + debug!("Editor config: {config:#?}"); // HAXX: offset the render area height by 1 to account for prompt/commandline area.height -= 1; @@ -337,6 +347,7 @@ impl Editor { idle_timer: Box::pin(sleep(config.idle_timeout)), last_motion: None, config, + auto_pairs, exit_code: 0, } } |