Commit 1c12442bc7

1c12442bc7bc27cc8d873d7bc015a80c32ad1064

parent: b5596c4189

Unregistered key

Ulyssa <git@ulyssa.dev> · 2026-09-19 04:34 UTC
committer: <noreply@github.com>

Ensure aliases are registered in definition order (#718)

Layout: unified · split

Cargo.lock +1
@@ -2542,6 +2542,7 @@ dependencies = [
25422542 "http-serde",
25432543 "humansize",
25442544 "image",
2545 "indexmap",
25452546 "lazy_static 1.5.0",
25462547 "libc",
25472548 "linkify",
Cargo.toml +1
@@ -46,6 +46,7 @@ html5ever = "0.39.0"
4646http-serde = "2.1.1"
4747humansize = "2.0.0"
4848image = {version = "0.25.6", features = ["serde"], default-features = false}
49indexmap = {version = "2.14", features = ["serde"]}
4950lazy_static = "1.4.0"
5051libc = "0.2"
5152linkify = "0.11.0"
src/commands.rs +22 −2
@@ -1172,7 +1172,10 @@ pub fn add_iamb_commands(cmds: &mut ProgramCommands) {
11721172}
11731173
11741174/// Initialize the default command state.
1175pub fn setup_commands(aliases: &HashMap<String, String>) -> Result<ProgramCommands, CommandError> {
1175///
1176/// Aliases are registered in the order they appear in the configuration file,
1177/// which allows later definitions to possibly refer to earlier ones.
1178pub fn setup_commands(aliases: &Aliases) -> Result<ProgramCommands, CommandError> {
11761179 let mut cmds = ProgramCommands::default();
11771180
11781181 add_iamb_commands(&mut cmds);
@@ -1193,7 +1196,24 @@ mod tests {
11931196 use modalkit::editing::context::EditContext;
11941197
11951198 fn setup_test_commands() -> ProgramCommands {
1196 setup_commands(&HashMap::new()).unwrap()
1199 setup_commands(&Aliases::new()).unwrap()
1200 }
1201
1202 #[test]
1203 fn test_cmd_aliases_order() {
1204 let ctx = EditContext::default();
1205 let aliases = Aliases::from_iter([
1206 ("c".to_string(), "chats".to_string()),
1207 ("cc".to_string(), "c".to_string()),
1208 ]);
1209
1210 let mut cmds = setup_commands(&aliases).unwrap();
1211
1212 let act = WindowAction::Switch(OpenTarget::Application(IambId::ChatList));
1213 let expected = vec![(act.into(), ctx.clone())];
1214
1215 assert_eq!(cmds.input_cmd("c", ctx.clone()).unwrap(), expected);
1216 assert_eq!(cmds.input_cmd("cc", ctx.clone()).unwrap(), expected);
11971217 }
11981218
11991219 #[test]
src/config.rs +28 −10
@@ -8,6 +8,7 @@ use std::io::{BufReader, BufWriter, Write as _};
88use std::process;
99
1010use clap::Parser;
11use indexmap::IndexMap;
1112use lazy_static::lazy_static;
1213use matrix_sdk::EncryptionState;
1314use matrix_sdk::authentication::matrix::MatrixSession;
@@ -26,7 +27,7 @@ use serde::{Deserialize, Deserializer, Serialize};
2627use crate::base::{SortColumn, SortFieldRoom, SortFieldUser, SortOrder};
2728use crate::prelude::*;
2829
29type Aliases = HashMap<String, String>;
30pub type Aliases = IndexMap<String, String>;
3031type Macros = HashMap<VimModes, HashMap<Keys, Keys>>;
3132
3233macro_rules! usage {
@@ -368,20 +369,14 @@ pub struct UserDisplayTunables {
368369
369370pub type UserOverrides = HashMap<OwnedUserId, UserDisplayTunables>;
370371
371fn merge_maps<K, V>(
372 profile: Option<HashMap<K, V>>,
373 global: Option<HashMap<K, V>>,
374) -> Option<HashMap<K, V>>
372fn merge_maps<M, K, V>(profile: Option<M>, global: Option<M>) -> Option<M>
375373where
376 K: Eq + Hash,
374 M: Extend<(K, V)> + IntoIterator<Item = (K, V)>,
377375{
378376 match (global, profile) {
379377 (Some(m), None) | (None, Some(m)) => Some(m),
380378 (Some(mut global), Some(profile)) => {
381 for (k, v) in profile {
382 global.insert(k, v);
383 }
384
379 global.extend(profile);
385380 Some(global)
386381 },
387382 (None, None) => None,
@@ -1832,6 +1827,29 @@ mod tests {
18321827 assert_eq!(aliased, "chats");
18331828 }
18341829
1830 #[test]
1831 fn test_parse_aliases_preserves_order() {
1832 // Aliases are registered in order, which allows alias to refer to earlier ones, so
1833 // ensure we register in user-specified order:
1834 let res: Aliases = toml::from_str("c = \"chats\"\ncc = \"c\"\nd = \"download\"").unwrap();
1835 let order = res.keys().map(String::as_str).collect::<Vec<_>>();
1836
1837 assert_eq!(order, vec!["c", "cc", "d"]);
1838 }
1839
1840 #[test]
1841 fn test_merge_aliases_preserves_order() {
1842 let profile: Aliases = toml::from_str("c = \"chats\"\nz = \"redact\"").unwrap();
1843 let global: Aliases = toml::from_str("c = \"cancel\"\nd = \"download\"").unwrap();
1844
1845 let res = merge_maps(Some(profile), Some(global)).unwrap();
1846 let order = res.keys().map(String::as_str).collect::<Vec<_>>();
1847
1848 // The profile wins for keys in both, but doesn't get to move them.
1849 assert_eq!(order, vec!["c", "d", "z"]);
1850 assert_eq!(res.get("c").unwrap(), "chats");
1851 }
1852
18351853 #[test]
18361854 fn test_parse_macros() {
18371855 let res: Macros = serde_json::from_str("{\"i|c\":{\"jj\":\"<Esc>\"}}").unwrap();
src/prelude.rs +1 −1
@@ -88,7 +88,7 @@ pub use crate::base::{
8888 SendAction,
8989 SpaceAction,
9090};
91pub use crate::config::ApplicationSettings;
91pub use crate::config::{Aliases, ApplicationSettings};
9292pub use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages};
9393pub use crate::preview::{PreviewKind, PreviewManager};
9494pub use crate::worker::Requester;
src/tests.rs +1 −1
@@ -227,7 +227,7 @@ pub fn mock_settings() -> ApplicationSettings {
227227 dirs: mock_dirs(),
228228 layout: Default::default(),
229229 macros: HashMap::default(),
230 aliases: HashMap::default(),
230 aliases: Aliases::default(),
231231 enable_enhanced_keys: false,
232232 }
233233}