1use crate::{Command, Direction, Key, KeyEvent};
2
3pub fn simple_keymap(event: KeyEvent) -> Option<Command> {
5 use Command::*;
6 let KeyEvent { key, mods } = event;
7 let shift = mods.shift;
8 let word = mods.ctrl || mods.alt;
9 Some(match key {
10 Key::Left if word => Move(Direction::WordLeft, shift),
11 Key::Right if word => Move(Direction::WordRight, shift),
12 Key::Left => Move(Direction::Left, shift),
13 Key::Right => Move(Direction::Right, shift),
14 Key::Up if mods.ctrl && mods.alt => AddCursorAbove,
15 Key::Down if mods.ctrl && mods.alt => AddCursorBelow,
16 Key::Down if mods.alt && shift => DuplicateLine,
17 Key::Up if mods.alt => MoveLineUp,
18 Key::Down if mods.alt => MoveLineDown,
19 Key::Up => Move(Direction::Up, shift),
20 Key::Down => Move(Direction::Down, shift),
21 Key::Home if mods.ctrl => Move(Direction::Top, shift),
22 Key::End if mods.ctrl => Move(Direction::Bottom, shift),
23 Key::Home => Move(Direction::LineStart, shift),
24 Key::End => Move(Direction::LineEnd, shift),
25 Key::PageUp if mods.ctrl && mods.alt => PreviousWorkspace,
27 Key::PageDown if mods.ctrl && mods.alt => NextWorkspace,
28 Key::PageUp if mods.ctrl => PreviousTab,
29 Key::PageDown if mods.ctrl => NextTab,
30 Key::PageUp => Move(Direction::PageUp, shift),
31 Key::PageDown => Move(Direction::PageDown, shift),
32 Key::Char('`') if mods.ctrl && mods.shift => CloseDock,
33 Key::Char('~') if mods.ctrl => CloseDock,
34 Key::Char('`') if mods.ctrl => FocusAgent,
35 Key::Char('b' | 'B') if mods.ctrl && mods.alt => BuildPdf,
36 Key::Char('j' | 'J') if mods.ctrl && mods.alt => ShowInPdf,
37 Key::Char('j') if mods.ctrl => ToggleTerminal,
38 Key::Enter if mods.ctrl && shift => InsertLineAbove,
39 Key::Enter if mods.ctrl => InsertLineBelow,
40 Key::Enter => Newline,
41 Key::Tab if mods.ctrl => NextTab,
43 Key::BackTab if mods.ctrl => PreviousTab,
44 Key::Tab => Indent,
45 Key::BackTab => Outdent,
46 Key::Backspace if word => DeleteWordLeft,
47 Key::Delete if word => DeleteWordRight,
48 Key::Backspace => DeleteBackward,
49 Key::Delete => DeleteForward,
50 Key::Char('F') if mods.alt => FormatDocument,
52 Key::Char('f') if mods.alt && shift => FormatDocument,
53 Key::Char('b') if mods.alt => Move(Direction::WordLeft, shift),
55 Key::Char('f') if mods.alt => Move(Direction::WordRight, shift),
56 Key::Char('z' | 'Z') if mods.ctrl && mods.shift => Redo,
57 Key::Char('-') if mods.ctrl && !shift => GoBack,
60 Key::Char('_' | '-') if mods.ctrl && shift => GoForward,
61 Key::Char('/' | '_' | '7') if mods.ctrl => ToggleComment,
62 Key::Esc => Cancel,
63 Key::F(1) => CommandPalette,
64 Key::F(2) => RenameSymbol,
65 Key::F(5) if mods.alt && shift => PreviousChange,
66 Key::F(5) if mods.alt => NextChange,
67 Key::F(8) if shift => PreviousProblem,
68 Key::F(8) => NextProblem,
69 Key::F(12) if shift => GoToReferences,
70 Key::F(12) => GoToDefinition,
71 Key::Char(' ') if mods.ctrl => Complete,
72 Key::Char('p') if mods.alt => CommandPalette,
73 Key::Char('c') if mods.alt => AddComment,
74 Key::Char('p' | 'P') if mods.ctrl && mods.shift => CommandPalette,
76 Key::Char('f' | 'F') if mods.ctrl && mods.shift => FindInProject,
77 Key::Char('k' | 'K') if mods.ctrl && mods.shift => DeleteLine,
78 Key::Char('l' | 'L') if mods.ctrl && mods.shift => SelectAllMatches,
79 Key::Char(c) if mods.ctrl => match c.to_ascii_lowercase() {
80 'l' => GoToLine,
83 'u' => DeleteToLineStart,
84 'k' => DeleteToLineEnd,
85 'e' => Move(Direction::LineEnd, shift),
86 'q' => Quit,
87 's' => Save,
88 'z' => Undo,
89 'y' => Redo,
90 'c' => Copy,
91 'x' => Cut,
92 'v' => Paste,
93 'a' => SelectAll,
94 'd' => SelectNextMatch,
95 'w' => CloseTab,
96 'p' => QuickOpen,
97 'f' => FindInFile,
98 'r' => ReplaceInFile,
100 'b' => ToggleSidebar,
101 'g' => OpenDiff,
102 '\\' | '4' => SplitPane,
104 'o' => FocusNextPane,
105 'n' => NewDocument,
106 '.' => QuickFix,
107 ',' => OpenSettings,
108 ']' => Indent,
109 '[' => Outdent,
111 _ => return None,
112 },
113 _ => return None,
114 })
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120 use crate::Mods;
121
122 #[test]
123 fn vs_codes_everyday_shortcuts_are_here() {
124 let key = |key, ctrl, shift, alt| {
125 simple_keymap(KeyEvent {
126 key,
127 mods: Mods { ctrl, shift, alt },
128 })
129 };
130 let ctrl = |c| key(Key::Char(c), true, false, false);
131 let ctrl_shift = |c| key(Key::Char(c), true, true, false);
132 assert_eq!(ctrl('a'), Some(Command::SelectAll));
133 assert_eq!(ctrl('n'), Some(Command::NewDocument));
134 assert_eq!(ctrl(','), Some(Command::OpenSettings));
135 assert_eq!(ctrl(']'), Some(Command::Indent));
136 assert_eq!(ctrl_shift('K'), Some(Command::DeleteLine));
137 assert_eq!(ctrl_shift('L'), Some(Command::SelectAllMatches));
138 assert_eq!(
139 key(Key::Enter, true, false, false),
140 Some(Command::InsertLineBelow)
141 );
142 assert_eq!(
143 key(Key::Enter, true, true, false),
144 Some(Command::InsertLineAbove)
145 );
146 assert_eq!(
147 key(Key::Up, true, false, true),
148 Some(Command::AddCursorAbove)
149 );
150 assert_eq!(
151 key(Key::Down, true, false, true),
152 Some(Command::AddCursorBelow)
153 );
154 assert_eq!(
155 key(Key::F(8), false, false, false),
156 Some(Command::NextProblem)
157 );
158 assert_eq!(key(Key::Tab, true, false, false), Some(Command::NextTab));
159 assert_eq!(key(Key::Tab, false, false, false), Some(Command::Indent));
160 }
161
162 #[test]
163 fn ctrl_backslash_splits_in_either_encoding() {
164 let ctrl = |c| KeyEvent {
165 key: Key::Char(c),
166 mods: Mods {
167 ctrl: true,
168 ..Mods::default()
169 },
170 };
171 assert_eq!(simple_keymap(ctrl('\\')), Some(Command::SplitPane));
172 assert_eq!(simple_keymap(ctrl('4')), Some(Command::SplitPane));
173 }
174
175 #[test]
176 fn option_shift_f_formats_and_option_f_still_moves() {
177 let alt = |c, shift| KeyEvent {
178 key: Key::Char(c),
179 mods: Mods {
180 alt: true,
181 shift,
182 ..Mods::default()
183 },
184 };
185 assert_eq!(simple_keymap(alt('F', true)), Some(Command::FormatDocument));
186 assert_eq!(
187 simple_keymap(alt('F', false)),
188 Some(Command::FormatDocument)
189 );
190 assert_eq!(
191 simple_keymap(alt('f', false)),
192 Some(Command::Move(Direction::WordRight, false))
193 );
194 }
195
196 #[test]
197 fn plain_characters_are_text() {
198 assert_eq!(simple_keymap(KeyEvent::plain(Key::Char('s'))), None);
199 }
200
201 #[test]
202 fn ctrl_keys_are_commands() {
203 assert_eq!(simple_keymap(KeyEvent::ctrl('s')), Some(Command::Save));
204 }
205
206 #[test]
207 fn ctrl_p_opens_files_and_ctrl_shift_p_opens_commands() {
208 assert_eq!(simple_keymap(KeyEvent::ctrl('p')), Some(Command::QuickOpen));
209 let event = KeyEvent {
210 key: Key::Char('P'),
211 mods: Mods {
212 ctrl: true,
213 shift: true,
214 ..Mods::default()
215 },
216 };
217 assert_eq!(simple_keymap(event), Some(Command::CommandPalette));
218 }
219
220 #[test]
221 fn mac_text_shortcuts_work() {
222 let alt = |key| KeyEvent {
223 key,
224 mods: Mods {
225 alt: true,
226 ..Mods::default()
227 },
228 };
229 assert_eq!(
230 simple_keymap(alt(Key::Backspace)),
231 Some(Command::DeleteWordLeft)
232 );
233 assert_eq!(
234 simple_keymap(alt(Key::Delete)),
235 Some(Command::DeleteWordRight)
236 );
237 let word_left = Some(Command::Move(Direction::WordLeft, false));
238 assert_eq!(simple_keymap(alt(Key::Char('b'))), word_left);
239 assert_eq!(
240 simple_keymap(KeyEvent::ctrl('u')),
241 Some(Command::DeleteToLineStart)
242 );
243 assert_eq!(simple_keymap(KeyEvent::ctrl('z')), Some(Command::Undo));
244 }
245
246 #[test]
247 fn shift_extends_selection() {
248 let event = KeyEvent {
249 key: Key::Left,
250 mods: Mods {
251 shift: true,
252 ..Mods::default()
253 },
254 };
255 assert_eq!(
256 simple_keymap(event),
257 Some(Command::Move(Direction::Left, true))
258 );
259 }
260}