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