1use collections::HashMap;
2use std::{ops::Range, sync::LazyLock};
3use tree_sitter::{Query, QueryMatch};
4
5use crate::MigrationPatterns;
6use crate::patterns::{
7 KEYMAP_ACTION_ARRAY_ARGUMENT_AS_OBJECT_PATTERN, KEYMAP_ACTION_ARRAY_PATTERN,
8 KEYMAP_ACTION_STRING_PATTERN, KEYMAP_CONTEXT_PATTERN,
9};
10
11pub const KEYMAP_PATTERNS: MigrationPatterns = &[
12 (
13 KEYMAP_ACTION_ARRAY_PATTERN,
14 replace_array_with_single_string,
15 ),
16 (
17 KEYMAP_ACTION_ARRAY_ARGUMENT_AS_OBJECT_PATTERN,
18 replace_action_argument_object_with_single_value,
19 ),
20 (KEYMAP_ACTION_STRING_PATTERN, replace_string_action),
21 (KEYMAP_CONTEXT_PATTERN, rename_context_key),
22];
23
24fn replace_array_with_single_string(
25 contents: &str,
26 mat: &QueryMatch,
27 query: &Query,
28) -> Option<(Range<usize>, String)> {
29 let array_ix = query.capture_index_for_name("array")?;
30 let action_name_ix = query.capture_index_for_name("action_name")?;
31 let argument_ix = query.capture_index_for_name("argument")?;
32
33 let action_name = contents.get(
34 mat.nodes_for_capture_index(action_name_ix)
35 .next()?
36 .byte_range(),
37 )?;
38 let argument = contents.get(
39 mat.nodes_for_capture_index(argument_ix)
40 .next()?
41 .byte_range(),
42 )?;
43
44 let replacement = TRANSFORM_ARRAY.get(&(action_name, argument))?;
45 let replacement_as_string = format!("\"{replacement}\"");
46 let range_to_replace = mat.nodes_for_capture_index(array_ix).next()?.byte_range();
47
48 Some((range_to_replace, replacement_as_string))
49}
50
51static TRANSFORM_ARRAY: LazyLock<HashMap<(&str, &str), &str>> = LazyLock::new(|| {
52 HashMap::from_iter([
53 // activate
54 (
55 ("workspace::ActivatePaneInDirection", "Up"),
56 "workspace::ActivatePaneUp",
57 ),
58 (
59 ("workspace::ActivatePaneInDirection", "Down"),
60 "workspace::ActivatePaneDown",
61 ),
62 (
63 ("workspace::ActivatePaneInDirection", "Left"),
64 "workspace::ActivatePaneLeft",
65 ),
66 (
67 ("workspace::ActivatePaneInDirection", "Right"),
68 "workspace::ActivatePaneRight",
69 ),
70 // swap
71 (
72 ("workspace::SwapPaneInDirection", "Up"),
73 "workspace::SwapPaneUp",
74 ),
75 (
76 ("workspace::SwapPaneInDirection", "Down"),
77 "workspace::SwapPaneDown",
78 ),
79 (
80 ("workspace::SwapPaneInDirection", "Left"),
81 "workspace::SwapPaneLeft",
82 ),
83 (
84 ("workspace::SwapPaneInDirection", "Right"),
85 "workspace::SwapPaneRight",
86 ),
87 // menu
88 (
89 ("app_menu::NavigateApplicationMenuInDirection", "Left"),
90 "app_menu::ActivateMenuLeft",
91 ),
92 (
93 ("app_menu::NavigateApplicationMenuInDirection", "Right"),
94 "app_menu::ActivateMenuRight",
95 ),
96 // vim push
97 (("vim::PushOperator", "Change"), "vim::PushChange"),
98 (("vim::PushOperator", "Delete"), "vim::PushDelete"),
99 (("vim::PushOperator", "Yank"), "vim::PushYank"),
100 (("vim::PushOperator", "Replace"), "vim::PushReplace"),
101 (
102 ("vim::PushOperator", "DeleteSurrounds"),
103 "vim::PushDeleteSurrounds",
104 ),
105 (("vim::PushOperator", "Mark"), "vim::PushMark"),
106 (("vim::PushOperator", "Indent"), "vim::PushIndent"),
107 (("vim::PushOperator", "Outdent"), "vim::PushOutdent"),
108 (("vim::PushOperator", "AutoIndent"), "vim::PushAutoIndent"),
109 (("vim::PushOperator", "Rewrap"), "vim::PushRewrap"),
110 (
111 ("vim::PushOperator", "ShellCommand"),
112 "vim::PushShellCommand",
113 ),
114 (("vim::PushOperator", "Lowercase"), "vim::PushLowercase"),
115 (("vim::PushOperator", "Uppercase"), "vim::PushUppercase"),
116 (
117 ("vim::PushOperator", "OppositeCase"),
118 "vim::PushOppositeCase",
119 ),
120 (("vim::PushOperator", "Register"), "vim::PushRegister"),
121 (
122 ("vim::PushOperator", "RecordRegister"),
123 "vim::PushRecordRegister",
124 ),
125 (
126 ("vim::PushOperator", "ReplayRegister"),
127 "vim::PushReplayRegister",
128 ),
129 (
130 ("vim::PushOperator", "ReplaceWithRegister"),
131 "vim::PushReplaceWithRegister",
132 ),
133 (
134 ("vim::PushOperator", "ToggleComments"),
135 "vim::PushToggleComments",
136 ),
137 // vim switch
138 (("vim::SwitchMode", "Normal"), "vim::SwitchToNormalMode"),
139 (("vim::SwitchMode", "Insert"), "vim::SwitchToInsertMode"),
140 (("vim::SwitchMode", "Replace"), "vim::SwitchToReplaceMode"),
141 (("vim::SwitchMode", "Visual"), "vim::SwitchToVisualMode"),
142 (
143 ("vim::SwitchMode", "VisualLine"),
144 "vim::SwitchToVisualLineMode",
145 ),
146 (
147 ("vim::SwitchMode", "VisualBlock"),
148 "vim::SwitchToVisualBlockMode",
149 ),
150 (
151 ("vim::SwitchMode", "HelixNormal"),
152 "vim::SwitchToHelixNormalMode",
153 ),
154 // vim resize
155 (("vim::ResizePane", "Widen"), "vim::ResizePaneRight"),
156 (("vim::ResizePane", "Narrow"), "vim::ResizePaneLeft"),
157 (("vim::ResizePane", "Shorten"), "vim::ResizePaneDown"),
158 (("vim::ResizePane", "Lengthen"), "vim::ResizePaneUp"),
159 ])
160});
161
162/// [ "editor::FoldAtLevel", { "level": 1 } ] -> [ "editor::FoldAtLevel", 1 ]
163fn replace_action_argument_object_with_single_value(
164 contents: &str,
165 mat: &QueryMatch,
166 query: &Query,
167) -> Option<(Range<usize>, String)> {
168 let array_ix = query.capture_index_for_name("array")?;
169 let action_name_ix = query.capture_index_for_name("action_name")?;
170 let argument_key_ix = query.capture_index_for_name("argument_key")?;
171 let argument_value_ix = query.capture_index_for_name("argument_value")?;
172
173 let action_name = contents.get(
174 mat.nodes_for_capture_index(action_name_ix)
175 .next()?
176 .byte_range(),
177 )?;
178 let argument_key = contents.get(
179 mat.nodes_for_capture_index(argument_key_ix)
180 .next()?
181 .byte_range(),
182 )?;
183 let argument_value = contents.get(
184 mat.nodes_for_capture_index(argument_value_ix)
185 .next()?
186 .byte_range(),
187 )?;
188
189 let new_action_name = UNWRAP_OBJECTS.get(&action_name)?.get(&argument_key)?;
190
191 let range_to_replace = mat.nodes_for_capture_index(array_ix).next()?.byte_range();
192 let replacement = format!("[\"{}\", {}]", new_action_name, argument_value);
193 Some((range_to_replace, replacement))
194}
195
196/// "ctrl-k ctrl-1": [ "editor::PushOperator", { "Object": {} } ] -> [ "editor::vim::PushObject", {} ]
197static UNWRAP_OBJECTS: LazyLock<HashMap<&str, HashMap<&str, &str>>> = LazyLock::new(|| {
198 HashMap::from_iter([
199 (
200 "editor::FoldAtLevel",
201 HashMap::from_iter([("level", "editor::FoldAtLevel")]),
202 ),
203 (
204 "vim::PushOperator",
205 HashMap::from_iter([
206 ("Object", "vim::PushObject"),
207 ("FindForward", "vim::PushFindForward"),
208 ("FindBackward", "vim::PushFindBackward"),
209 ("Sneak", "vim::PushSneak"),
210 ("SneakBackward", "vim::PushSneakBackward"),
211 ("AddSurrounds", "vim::PushAddSurrounds"),
212 ("ChangeSurrounds", "vim::PushChangeSurrounds"),
213 ("Jump", "vim::PushJump"),
214 ("Digraph", "vim::PushDigraph"),
215 ("Literal", "vim::PushLiteral"),
216 ]),
217 ),
218 ])
219});
220
221fn replace_string_action(
222 contents: &str,
223 mat: &QueryMatch,
224 query: &Query,
225) -> Option<(Range<usize>, String)> {
226 let action_name_ix = query.capture_index_for_name("action_name")?;
227 let action_name_node = mat.nodes_for_capture_index(action_name_ix).next()?;
228 let action_name_range = action_name_node.byte_range();
229 let action_name = contents.get(action_name_range.clone())?;
230
231 if let Some(new_action_name) = STRING_REPLACE.get(&action_name) {
232 return Some((action_name_range, new_action_name.to_string()));
233 }
234
235 None
236}
237
238/// "ctrl-k ctrl-1": "inline_completion::ToggleMenu" -> "edit_prediction::ToggleMenu"
239static STRING_REPLACE: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
240 HashMap::from_iter([
241 (
242 "inline_completion::ToggleMenu",
243 "edit_prediction::ToggleMenu",
244 ),
245 ("editor::NextInlineCompletion", "editor::NextEditPrediction"),
246 (
247 "editor::PreviousInlineCompletion",
248 "editor::PreviousEditPrediction",
249 ),
250 (
251 "editor::AcceptPartialInlineCompletion",
252 "editor::AcceptPartialEditPrediction",
253 ),
254 ("editor::ShowInlineCompletion", "editor::ShowEditPrediction"),
255 (
256 "editor::AcceptInlineCompletion",
257 "editor::AcceptEditPrediction",
258 ),
259 (
260 "editor::ToggleInlineCompletions",
261 "editor::ToggleEditPrediction",
262 ),
263 ])
264});
265
266fn rename_context_key(
267 contents: &str,
268 mat: &QueryMatch,
269 query: &Query,
270) -> Option<(Range<usize>, String)> {
271 let context_predicate_ix = query.capture_index_for_name("context_predicate")?;
272 let context_predicate_range = mat
273 .nodes_for_capture_index(context_predicate_ix)
274 .next()?
275 .byte_range();
276 let old_predicate = contents.get(context_predicate_range.clone())?.to_string();
277 let mut new_predicate = old_predicate.to_string();
278 for (old_key, new_key) in CONTEXT_REPLACE.iter() {
279 new_predicate = new_predicate.replace(old_key, new_key);
280 }
281 if new_predicate != old_predicate {
282 Some((context_predicate_range, new_predicate))
283 } else {
284 None
285 }
286}
287
288/// "context": "Editor && inline_completion && !showing_completions" -> "Editor && edit_prediction && !showing_completions"
289pub static CONTEXT_REPLACE: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
290 HashMap::from_iter([
291 ("inline_completion", "edit_prediction"),
292 (
293 "inline_completion_requires_modifier",
294 "edit_prediction_requires_modifier",
295 ),
296 ])
297});