Commit 1c12442bc7
Unregistered key
Layout: unified · split
Cargo.lock +1
| @@ -2542,6 +2542,7 @@ dependencies = [ | ||
| 2542 | 2542 | "http-serde", |
| 2543 | 2543 | "humansize", |
| 2544 | 2544 | "image", |
| 2545 | "indexmap", | |
| 2545 | 2546 | "lazy_static 1.5.0", |
| 2546 | 2547 | "libc", |
| 2547 | 2548 | "linkify", |
Cargo.toml +1
| @@ -46,6 +46,7 @@ html5ever = "0.39.0" | ||
| 46 | 46 | http-serde = "2.1.1" |
| 47 | 47 | humansize = "2.0.0" |
| 48 | 48 | image = {version = "0.25.6", features = ["serde"], default-features = false} |
| 49 | indexmap = {version = "2.14", features = ["serde"]} | |
| 49 | 50 | lazy_static = "1.4.0" |
| 50 | 51 | libc = "0.2" |
| 51 | 52 | linkify = "0.11.0" |
src/commands.rs +22 −2
| @@ -1172,7 +1172,10 @@ pub fn add_iamb_commands(cmds: &mut ProgramCommands) { | ||
| 1172 | 1172 | } |
| 1173 | 1173 | |
| 1174 | 1174 | /// Initialize the default command state. |
| 1175 | pub 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. | |
| 1178 | pub fn setup_commands(aliases: &Aliases) -> Result<ProgramCommands, CommandError> { | |
| 1176 | 1179 | let mut cmds = ProgramCommands::default(); |
| 1177 | 1180 | |
| 1178 | 1181 | add_iamb_commands(&mut cmds); |
| @@ -1193,7 +1196,24 @@ mod tests { | ||
| 1193 | 1196 | use modalkit::editing::context::EditContext; |
| 1194 | 1197 | |
| 1195 | 1198 | 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); | |
| 1197 | 1217 | } |
| 1198 | 1218 | |
| 1199 | 1219 | #[test] |
src/config.rs +28 −10
| @@ -8,6 +8,7 @@ use std::io::{BufReader, BufWriter, Write as _}; | ||
| 8 | 8 | use std::process; |
| 9 | 9 | |
| 10 | 10 | use clap::Parser; |
| 11 | use indexmap::IndexMap; | |
| 11 | 12 | use lazy_static::lazy_static; |
| 12 | 13 | use matrix_sdk::EncryptionState; |
| 13 | 14 | use matrix_sdk::authentication::matrix::MatrixSession; |
| @@ -26,7 +27,7 @@ use serde::{Deserialize, Deserializer, Serialize}; | ||
| 26 | 27 | use crate::base::{SortColumn, SortFieldRoom, SortFieldUser, SortOrder}; |
| 27 | 28 | use crate::prelude::*; |
| 28 | 29 | |
| 29 | type Aliases = HashMap<String, String>; | |
| 30 | pub type Aliases = IndexMap<String, String>; | |
| 30 | 31 | type Macros = HashMap<VimModes, HashMap<Keys, Keys>>; |
| 31 | 32 | |
| 32 | 33 | macro_rules! usage { |
| @@ -368,20 +369,14 @@ pub struct UserDisplayTunables { | ||
| 368 | 369 | |
| 369 | 370 | pub type UserOverrides = HashMap<OwnedUserId, UserDisplayTunables>; |
| 370 | 371 | |
| 371 | fn merge_maps<K, V>( | |
| 372 | profile: Option<HashMap<K, V>>, | |
| 373 | global: Option<HashMap<K, V>>, | |
| 374 | ) -> Option<HashMap<K, V>> | |
| 372 | fn merge_maps<M, K, V>(profile: Option<M>, global: Option<M>) -> Option<M> | |
| 375 | 373 | where |
| 376 | K: Eq + Hash, | |
| 374 | M: Extend<(K, V)> + IntoIterator<Item = (K, V)>, | |
| 377 | 375 | { |
| 378 | 376 | match (global, profile) { |
| 379 | 377 | (Some(m), None) | (None, Some(m)) => Some(m), |
| 380 | 378 | (Some(mut global), Some(profile)) => { |
| 381 | for (k, v) in profile { | |
| 382 | global.insert(k, v); | |
| 383 | } | |
| 384 | ||
| 379 | global.extend(profile); | |
| 385 | 380 | Some(global) |
| 386 | 381 | }, |
| 387 | 382 | (None, None) => None, |
| @@ -1832,6 +1827,29 @@ mod tests { | ||
| 1832 | 1827 | assert_eq!(aliased, "chats"); |
| 1833 | 1828 | } |
| 1834 | 1829 | |
| 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 | ||
| 1835 | 1853 | #[test] |
| 1836 | 1854 | fn test_parse_macros() { |
| 1837 | 1855 | 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::{ | ||
| 88 | 88 | SendAction, |
| 89 | 89 | SpaceAction, |
| 90 | 90 | }; |
| 91 | pub use crate::config::ApplicationSettings; | |
| 91 | pub use crate::config::{Aliases, ApplicationSettings}; | |
| 92 | 92 | pub use crate::message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages}; |
| 93 | 93 | pub use crate::preview::{PreviewKind, PreviewManager}; |
| 94 | 94 | pub use crate::worker::Requester; |
src/tests.rs +1 −1
| @@ -227,7 +227,7 @@ pub fn mock_settings() -> ApplicationSettings { | ||
| 227 | 227 | dirs: mock_dirs(), |
| 228 | 228 | layout: Default::default(), |
| 229 | 229 | macros: HashMap::default(), |
| 230 | aliases: HashMap::default(), | |
| 230 | aliases: Aliases::default(), | |
| 231 | 231 | enable_enhanced_keys: false, |
| 232 | 232 | } |
| 233 | 233 | } |