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