mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-28 20:01:33 +00:00
Add Vim digraphs (#14887)
Vim digraphs are a way to insert special characters using sequences of two ASCII characters. I've implemented the feature using a new `Digraph` operator, following the example of `AddSurrounds`. There are still a few issues that I'm not sure what the best way to resolve them is. - To insert `ş`, the user must pause between pressing `ctrl-k` and `s ,`, otherwise it triggers the binding for `ctrl-k s`. Is there a way to disable `ctrl-k *` bindings while in insert, replace or waiting mode? - Is there a better way to insert a string at all of the cursors? At the moment I'm constructing the edits manually. - The table of default digraphs is a 1.4k line rust expression. Is this okay as long as it's in its own module? - I'd like a second opinion on how best to structure the settings.json entry. - I have omitted the "meta character" feature as I don't think it makes sense when editing UTF-8 text. Release Notes: - Added support for Vim digraphs. Resolves #11871
This commit is contained in:
parent
b87028956f
commit
26d0a33e79
14 changed files with 1613 additions and 2 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -12034,6 +12034,7 @@ dependencies = [
|
|||
"indoc",
|
||||
"itertools 0.11.0",
|
||||
"language",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"lsp",
|
||||
"multi_buffer",
|
||||
|
|
|
@ -316,6 +316,7 @@
|
|||
"ctrl-u": "editor::DeleteToBeginningOfLine",
|
||||
"ctrl-t": "vim::Indent",
|
||||
"ctrl-d": "vim::Outdent",
|
||||
"ctrl-k": ["vim::PushOperator", { "Digraph": {} }],
|
||||
"ctrl-r": ["vim::PushOperator", "Register"]
|
||||
}
|
||||
},
|
||||
|
@ -325,6 +326,7 @@
|
|||
"escape": "vim::NormalBefore",
|
||||
"ctrl-c": "vim::NormalBefore",
|
||||
"ctrl-[": "vim::NormalBefore",
|
||||
"ctrl-k": ["vim::PushOperator", { "Digraph": {} }],
|
||||
"backspace": "vim::UndoReplace",
|
||||
"tab": "vim::Tab",
|
||||
"enter": "vim::Enter"
|
||||
|
@ -337,7 +339,8 @@
|
|||
"enter": "vim::Enter",
|
||||
"escape": "vim::ClearOperators",
|
||||
"ctrl-c": "vim::ClearOperators",
|
||||
"ctrl-[": "vim::ClearOperators"
|
||||
"ctrl-[": "vim::ClearOperators",
|
||||
"ctrl-k": ["vim::PushOperator", { "Digraph": {} }]
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -922,7 +922,8 @@
|
|||
"vim": {
|
||||
"use_system_clipboard": "always",
|
||||
"use_multiline_find": false,
|
||||
"use_smartcase_find": false
|
||||
"use_smartcase_find": false,
|
||||
"custom_digraphs": {}
|
||||
},
|
||||
// The server to connect to. If the environment variable
|
||||
// ZED_SERVER_URL is set, it will override this setting.
|
||||
|
|
|
@ -25,6 +25,7 @@ editor.workspace = true
|
|||
gpui.workspace = true
|
||||
itertools.workspace = true
|
||||
language.workspace = true
|
||||
lazy_static.workspace = true
|
||||
log.workspace = true
|
||||
multi_buffer.workspace = true
|
||||
nvim-rs = { git = "https://github.com/KillTheMule/nvim-rs", branch = "master", features = [
|
||||
|
|
154
crates/vim/src/digraph.rs
Normal file
154
crates/vim/src/digraph.rs
Normal file
|
@ -0,0 +1,154 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use collections::HashMap;
|
||||
use gpui::AppContext;
|
||||
use lazy_static::lazy_static;
|
||||
use settings::Settings;
|
||||
use ui::WindowContext;
|
||||
|
||||
use crate::{Vim, VimSettings};
|
||||
|
||||
mod default;
|
||||
|
||||
lazy_static! {
|
||||
static ref DEFAULT_DIGRAPHS_MAP: HashMap<String, Arc<str>> = {
|
||||
let mut map = HashMap::default();
|
||||
for &(a, b, c) in default::DEFAULT_DIGRAPHS {
|
||||
let key = format!("{a}{b}");
|
||||
let value = char::from_u32(c).unwrap().to_string().into();
|
||||
map.insert(key, value);
|
||||
}
|
||||
map
|
||||
};
|
||||
}
|
||||
|
||||
fn lookup_digraph(a: char, b: char, cx: &AppContext) -> Arc<str> {
|
||||
let custom_digraphs = &VimSettings::get_global(cx).custom_digraphs;
|
||||
let input = format!("{a}{b}");
|
||||
let reversed = format!("{b}{a}");
|
||||
|
||||
custom_digraphs
|
||||
.get(&input)
|
||||
.or_else(|| DEFAULT_DIGRAPHS_MAP.get(&input))
|
||||
.or_else(|| custom_digraphs.get(&reversed))
|
||||
.or_else(|| DEFAULT_DIGRAPHS_MAP.get(&reversed))
|
||||
.cloned()
|
||||
.unwrap_or_else(|| b.to_string().into())
|
||||
}
|
||||
|
||||
pub fn insert_digraph(first_char: char, second_char: char, cx: &mut WindowContext) {
|
||||
let text = lookup_digraph(first_char, second_char, &cx);
|
||||
|
||||
Vim::update(cx, |vim, cx| vim.pop_operator(cx));
|
||||
if Vim::read(cx).state().editor_input_enabled() {
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.update_active_editor(cx, |_, editor, cx| editor.insert(&text, cx));
|
||||
});
|
||||
} else {
|
||||
Vim::active_editor_input_ignored(text, cx);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use collections::HashMap;
|
||||
use settings::SettingsStore;
|
||||
|
||||
use crate::{
|
||||
state::Mode,
|
||||
test::{NeovimBackedTestContext, VimTestContext},
|
||||
VimSettings,
|
||||
};
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_digraph_insert_mode(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx: NeovimBackedTestContext = NeovimBackedTestContext::new(cx).await;
|
||||
|
||||
cx.set_shared_state("Hellˇo").await;
|
||||
cx.simulate_shared_keystrokes("a ctrl-k o : escape").await;
|
||||
cx.shared_state().await.assert_eq("Helloˇö");
|
||||
|
||||
cx.set_shared_state("Hellˇo").await;
|
||||
cx.simulate_shared_keystrokes("a ctrl-k : o escape").await;
|
||||
cx.shared_state().await.assert_eq("Helloˇö");
|
||||
|
||||
cx.set_shared_state("Hellˇo").await;
|
||||
cx.simulate_shared_keystrokes("i ctrl-k o : escape").await;
|
||||
cx.shared_state().await.assert_eq("Hellˇöo");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_digraph_insert_multicursor(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx: VimTestContext = VimTestContext::new(cx, true).await;
|
||||
|
||||
cx.set_state("Hellˇo wˇorld", Mode::Normal);
|
||||
cx.simulate_keystrokes("a ctrl-k o : escape");
|
||||
cx.assert_state("Helloˇö woˇörld", Mode::Normal);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_digraph_replace(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx: NeovimBackedTestContext = NeovimBackedTestContext::new(cx).await;
|
||||
|
||||
cx.set_shared_state("Hellˇo").await;
|
||||
cx.simulate_shared_keystrokes("r ctrl-k o :").await;
|
||||
cx.shared_state().await.assert_eq("Hellˇö");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_digraph_find(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx: NeovimBackedTestContext = NeovimBackedTestContext::new(cx).await;
|
||||
|
||||
cx.set_shared_state("ˇHellö world").await;
|
||||
cx.simulate_shared_keystrokes("f ctrl-k o :").await;
|
||||
cx.shared_state().await.assert_eq("Hellˇö world");
|
||||
|
||||
cx.set_shared_state("ˇHellö world").await;
|
||||
cx.simulate_shared_keystrokes("t ctrl-k o :").await;
|
||||
cx.shared_state().await.assert_eq("Helˇlö world");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_digraph_replace_mode(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx: NeovimBackedTestContext = NeovimBackedTestContext::new(cx).await;
|
||||
|
||||
cx.set_shared_state("ˇHello").await;
|
||||
cx.simulate_shared_keystrokes(
|
||||
"shift-r ctrl-k a ' ctrl-k e ` ctrl-k i : ctrl-k o ~ ctrl-k u - escape",
|
||||
)
|
||||
.await;
|
||||
cx.shared_state().await.assert_eq("áèïõˇū");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_digraph_custom(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx: VimTestContext = VimTestContext::new(cx, true).await;
|
||||
|
||||
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||
store.update_user_settings::<VimSettings>(cx, |s| {
|
||||
let mut custom_digraphs = HashMap::default();
|
||||
custom_digraphs.insert("|-".into(), "⊢".into());
|
||||
custom_digraphs.insert(":)".into(), "👨💻".into());
|
||||
s.custom_digraphs = Some(custom_digraphs);
|
||||
});
|
||||
});
|
||||
|
||||
cx.set_state("ˇ", Mode::Normal);
|
||||
cx.simulate_keystrokes("a ctrl-k | - escape");
|
||||
cx.assert_state("ˇ⊢", Mode::Normal);
|
||||
|
||||
// Test support for multi-codepoint mappings
|
||||
cx.set_state("ˇ", Mode::Normal);
|
||||
cx.simulate_keystrokes("a ctrl-k : ) escape");
|
||||
cx.assert_state("ˇ👨💻", Mode::Normal);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_digraph_keymap_conflict(cx: &mut gpui::TestAppContext) {
|
||||
let mut cx: NeovimBackedTestContext = NeovimBackedTestContext::new(cx).await;
|
||||
|
||||
cx.set_shared_state("Hellˇo").await;
|
||||
cx.simulate_shared_keystrokes("a ctrl-k s , escape").await;
|
||||
cx.shared_state().await.assert_eq("Helloˇş");
|
||||
}
|
||||
}
|
1366
crates/vim/src/digraph/default.rs
Normal file
1366
crates/vim/src/digraph/default.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -68,6 +68,7 @@ pub enum Operator {
|
|||
Lowercase,
|
||||
Uppercase,
|
||||
OppositeCase,
|
||||
Digraph { first_char: Option<char> },
|
||||
Register,
|
||||
RecordRegister,
|
||||
ReplayRegister,
|
||||
|
@ -309,6 +310,7 @@ impl Operator {
|
|||
Operator::Delete => "d",
|
||||
Operator::Yank => "y",
|
||||
Operator::Replace => "r",
|
||||
Operator::Digraph { .. } => "^K",
|
||||
Operator::FindForward { before: false } => "f",
|
||||
Operator::FindForward { before: true } => "t",
|
||||
Operator::FindBackward { after: false } => "F",
|
||||
|
@ -342,6 +344,7 @@ impl Operator {
|
|||
| Operator::RecordRegister
|
||||
| Operator::ReplayRegister
|
||||
| Operator::Replace
|
||||
| Operator::Digraph { .. }
|
||||
| Operator::ChangeSurrounds { target: Some(_) }
|
||||
| Operator::DeleteSurrounds => true,
|
||||
Operator::Change
|
||||
|
|
|
@ -5,6 +5,7 @@ mod test;
|
|||
|
||||
mod change_list;
|
||||
mod command;
|
||||
mod digraph;
|
||||
mod editor_events;
|
||||
mod insert;
|
||||
mod mode_indicator;
|
||||
|
@ -193,6 +194,7 @@ fn observe_keystrokes(keystroke_event: &KeystrokeEvent, cx: &mut WindowContext)
|
|||
Operator::FindForward { .. }
|
||||
| Operator::FindBackward { .. }
|
||||
| Operator::Replace
|
||||
| Operator::Digraph { .. }
|
||||
| Operator::AddSurrounds { .. }
|
||||
| Operator::ChangeSurrounds { .. }
|
||||
| Operator::DeleteSurrounds
|
||||
|
@ -876,6 +878,19 @@ impl Vim {
|
|||
Mode::Visual | Mode::VisualLine | Mode::VisualBlock => visual_replace(text, cx),
|
||||
_ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
|
||||
},
|
||||
Some(Operator::Digraph { first_char }) => {
|
||||
if let Some(first_char) = first_char {
|
||||
if let Some(second_char) = text.chars().next() {
|
||||
digraph::insert_digraph(first_char, second_char, cx);
|
||||
}
|
||||
} else {
|
||||
let first_char = text.chars().next();
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.pop_operator(cx);
|
||||
vim.push_operator(Operator::Digraph { first_char }, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
Some(Operator::AddSurrounds { target }) => match Vim::read(cx).state().mode {
|
||||
Mode::Normal => {
|
||||
if let Some(target) = target {
|
||||
|
@ -1060,6 +1075,7 @@ struct VimSettings {
|
|||
pub use_system_clipboard: UseSystemClipboard,
|
||||
pub use_multiline_find: bool,
|
||||
pub use_smartcase_find: bool,
|
||||
pub custom_digraphs: HashMap<String, Arc<str>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
|
||||
|
@ -1067,6 +1083,7 @@ struct VimSettingsContent {
|
|||
pub use_system_clipboard: Option<UseSystemClipboard>,
|
||||
pub use_multiline_find: Option<bool>,
|
||||
pub use_smartcase_find: Option<bool>,
|
||||
pub custom_digraphs: Option<HashMap<String, Arc<str>>>,
|
||||
}
|
||||
|
||||
impl Settings for VimSettings {
|
||||
|
|
12
crates/vim/test_data/test_digraph_find.json
Normal file
12
crates/vim/test_data/test_digraph_find.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{"Put":{"state":"ˇHellö world"}}
|
||||
{"Key":"f"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"o"}
|
||||
{"Key":":"}
|
||||
{"Get":{"state":"Hellˇö world","mode":"Normal"}}
|
||||
{"Put":{"state":"ˇHellö world"}}
|
||||
{"Key":"t"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"o"}
|
||||
{"Key":":"}
|
||||
{"Get":{"state":"Helˇlö world","mode":"Normal"}}
|
21
crates/vim/test_data/test_digraph_insert_mode.json
Normal file
21
crates/vim/test_data/test_digraph_insert_mode.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{"Put":{"state":"Hellˇo"}}
|
||||
{"Key":"a"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"o"}
|
||||
{"Key":":"}
|
||||
{"Key":"escape"}
|
||||
{"Get":{"state":"Helloˇö","mode":"Normal"}}
|
||||
{"Put":{"state":"Hellˇo"}}
|
||||
{"Key":"a"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":":"}
|
||||
{"Key":"o"}
|
||||
{"Key":"escape"}
|
||||
{"Get":{"state":"Helloˇö","mode":"Normal"}}
|
||||
{"Put":{"state":"Hellˇo"}}
|
||||
{"Key":"i"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"o"}
|
||||
{"Key":":"}
|
||||
{"Key":"escape"}
|
||||
{"Get":{"state":"Hellˇöo","mode":"Normal"}}
|
7
crates/vim/test_data/test_digraph_keymap_conflict.json
Normal file
7
crates/vim/test_data/test_digraph_keymap_conflict.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{"Put":{"state":"Hellˇo"}}
|
||||
{"Key":"a"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"s"}
|
||||
{"Key":","}
|
||||
{"Key":"escape"}
|
||||
{"Get":{"state":"Helloˇş","mode":"Normal"}}
|
6
crates/vim/test_data/test_digraph_replace.json
Normal file
6
crates/vim/test_data/test_digraph_replace.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{"Put":{"state":"Hellˇo"}}
|
||||
{"Key":"r"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"o"}
|
||||
{"Key":":"}
|
||||
{"Get":{"state":"Hellˇö","mode":"Normal"}}
|
19
crates/vim/test_data/test_digraph_replace_mode.json
Normal file
19
crates/vim/test_data/test_digraph_replace_mode.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{"Put":{"state":"ˇHello"}}
|
||||
{"Key":"shift-r"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"a"}
|
||||
{"Key":"'"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"e"}
|
||||
{"Key":"`"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"i"}
|
||||
{"Key":":"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"o"}
|
||||
{"Key":"~"}
|
||||
{"Key":"ctrl-k"}
|
||||
{"Key":"u"}
|
||||
{"Key":"-"}
|
||||
{"Key":"escape"}
|
||||
{"Get":{"state":"áèïõˇū","mode":"Normal"}}
|
Loading…
Reference in a new issue