forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
411 lines (358 loc) · 11.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
JSONObject
} from '@phosphor/coreutils';
import {
Menu
} from '@phosphor/widgets';
import {
JupyterLab, JupyterLabPlugin
} from '@jupyterlab/application';
import {
ICommandPalette, IMainMenu
} from '@jupyterlab/apputils';
import {
IEditorServices
} from '@jupyterlab/codeeditor';
import {
editorServices, CodeMirrorEditor, Mode
} from '@jupyterlab/codemirror';
import {
ISettingRegistry, IStateDB
} from '@jupyterlab/coreutils';
import {
IEditorTracker
} from '@jupyterlab/fileeditor';
/**
* The command IDs used by the codemirror plugin.
*/
namespace CommandIDs {
export
const matchBrackets = 'codemirror:match-brackets';
export
const changeKeyMap = 'codemirror:change-keymap';
export
const changeTheme = 'codemirror:change-theme';
export
const changeMode = 'codemirror:change-mode';
export
const changeTabs = 'codemirror:change-tabs';
export
const search = 'codemirror:search';
export
const searchReplace = 'codemirror:search-replace';
export
const jumpToLine = 'codemirror:jump-to-line';
};
/**
* The editor services.
*/
export
const servicesPlugin: JupyterLabPlugin<IEditorServices> = {
id: IEditorServices.name,
provides: IEditorServices,
activate: (): IEditorServices => editorServices
};
/**
* The editor commands.
*/
export
const commandsPlugin: JupyterLabPlugin<void> = {
id: 'jupyter.services.codemirror-commands',
requires: [IEditorTracker, IMainMenu, ICommandPalette, IStateDB, ISettingRegistry],
activate: activateEditorCommands,
autoStart: true
};
/**
* Export the plugins as default.
*/
const plugins: JupyterLabPlugin<any>[] = [commandsPlugin, servicesPlugin];
export default plugins;
/**
* Set up the editor widget menu and commands.
*/
function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMenu: IMainMenu, palette: ICommandPalette, state: IStateDB, settingRegistry: ISettingRegistry): void {
const { commands, restored } = app;
const { id } = commandsPlugin;
let theme: string = CodeMirrorEditor.DEFAULT_THEME;
let keyMap: string = 'default';
let matchBrackets = false;
// Annotate the plugin settings.
settingRegistry.annotate(id, '', {
iconClass: 'jp-ImageTextEditor',
iconLabel: 'CodeMirror',
label: 'CodeMirror'
});
settingRegistry.annotate(id, 'keyMap', { label: 'Key Map' });
settingRegistry.annotate(id, 'matchBrackets', { label: 'Match Brackets' });
settingRegistry.annotate(id, 'theme', { label: 'Theme' });
/**
* Update the setting values.
*/
function updateSettings(settings: ISettingRegistry.ISettings): void {
const cached = settings.get('matchBrackets') as boolean | null;
matchBrackets = cached === null ? false : !!cached;
keyMap = settings.get('keyMap') as string | null || keyMap;
theme = settings.get('theme') as string | null || theme;
}
/**
* Update the settings of the current tracker instances.
*/
function updateTracker(): void {
tracker.forEach(widget => {
if (widget.editor instanceof CodeMirrorEditor) {
let cm = widget.editor.editor;
cm.setOption('keyMap', keyMap);
cm.setOption('theme', theme);
cm.setOption('matchBrackets', matchBrackets);
}
});
}
// Fetch the initial state of the settings.
Promise.all([settingRegistry.load(id), restored]).then(([settings]) => {
updateSettings(settings);
updateTracker();
settings.changed.connect(() => {
updateSettings(settings);
updateTracker();
});
});
/**
* Handle the settings of new widgets.
*/
tracker.widgetAdded.connect((sender, widget) => {
if (widget.editor instanceof CodeMirrorEditor) {
let cm = widget.editor.editor;
cm.setOption('keyMap', keyMap);
cm.setOption('theme', theme);
cm.setOption('matchBrackets', matchBrackets);
}
});
// Update the command registry when the codemirror state changes.
tracker.currentChanged.connect(() => {
if (tracker.size <= 1) {
commands.notifyCommandChanged(CommandIDs.matchBrackets);
}
});
/**
* A test for whether the tracker has an active widget.
*/
function hasWidget(): boolean {
return tracker.currentWidget !== null;
}
/**
* A test for whether editor has focus.
*/
function isEditorVisible(): boolean {
return (tracker.currentWidget !== null &&
tracker.currentWidget.editor instanceof CodeMirrorEditor &&
tracker.currentWidget.isVisible);
}
/**
* Create a menu for the editor.
*/
function createMenu(): Menu {
const menu = new Menu({ commands });
const themeMenu = new Menu({ commands });
const keyMapMenu = new Menu({ commands });
const modeMenu = new Menu({ commands });
const tabMenu = new Menu({ commands });
menu.title.label = 'Editor';
themeMenu.title.label = 'Theme';
keyMapMenu.title.label = 'Key Map';
modeMenu.title.label = 'Language';
tabMenu.title.label = 'Tabs';
commands.addCommand(CommandIDs.changeTheme, {
label: args => args['theme'] as string,
execute: args => {
theme = args['theme'] as string || CodeMirrorEditor.DEFAULT_THEME;
tracker.forEach(widget => {
if (widget.editor instanceof CodeMirrorEditor) {
let cm = widget.editor.editor;
cm.setOption('theme', theme);
}
});
return settingRegistry.set(id, 'theme', theme);
},
isEnabled: hasWidget,
isToggled: args => args['theme'] === theme
});
commands.addCommand(CommandIDs.changeKeyMap, {
label: args => {
let title = args['keyMap'] as string;
return title === 'sublime' ? 'Sublime Text' : title;
},
execute: args => {
keyMap = args['keyMap'] as string || 'default';
tracker.forEach(widget => {
if (widget.editor instanceof CodeMirrorEditor) {
let cm = widget.editor.editor;
cm.setOption('keyMap', keyMap);
}
});
return settingRegistry.set(id, 'keyMap', keyMap);
},
isEnabled: hasWidget,
isToggled: args => args['keyMap'] === keyMap
});
commands.addCommand(CommandIDs.changeMode, {
label: args => args['name'] as string,
execute: args => {
let mode = args['mode'] as string;
if (mode) {
let widget = tracker.currentWidget;
let spec = Mode.findByName(mode);
if (spec) {
widget.model.mimeType = spec.mime;
}
}
},
isEnabled: hasWidget,
isToggled: args => {
let widget = tracker.currentWidget;
if (!widget) {
return false;
}
let mime = widget.model.mimeType;
let spec = Mode.findByMIME(mime);
let mode = spec && spec.mode;
return args['mode'] === mode;
}
});
commands.addCommand(CommandIDs.changeTabs, {
label: args => args['name'] as string,
execute: args => {
let widget = tracker.currentWidget;
if (!widget) {
return;
}
let editor = widget.editor as CodeMirrorEditor;
let size = args['size'] as number || 4;
let tabs = !!args['tabs'];
editor.editor.setOption('indentWithTabs', tabs);
editor.editor.setOption('indentUnit', size);
},
isEnabled: hasWidget,
isToggled: args => {
let widget = tracker.currentWidget;
if (!widget) {
return false;
}
let tabs = !!args['tabs'];
let size = args['size'] as number || 4;
let editor = widget.editor as CodeMirrorEditor;
if (editor.editor.getOption('indentWithTabs') !== tabs) {
return false;
}
return editor.editor.getOption('indentUnit') === size;
}
});
let args: JSONObject = { tabs: true, size: 4, name: 'Indent with Tab' };
tabMenu.addItem({ command: CommandIDs.changeTabs, args });
palette.addItem({
command: CommandIDs.changeTabs, args, category: 'Editor'
});
for (let size of [1, 2, 4, 8]) {
let args: JSONObject = {
tabs: false, size, name: `Spaces: ${size} `
};
tabMenu.addItem({ command: CommandIDs.changeTabs, args });
palette.addItem({
command: CommandIDs.changeTabs, args, category: 'Editor'
});
}
Mode.getModeInfo().sort((a, b) => {
return a.name.localeCompare(b.name);
}).forEach(spec => {
modeMenu.addItem({
command: CommandIDs.changeMode,
args: {...spec}
});
});
[
'jupyter', 'default', 'abcdef', 'base16-dark', 'base16-light',
'hopscotch', 'material', 'mbo', 'mdn-like', 'seti', 'the-matrix',
'xq-light', 'zenburn'
].forEach(name => themeMenu.addItem({
command: CommandIDs.changeTheme,
args: { theme: name }
}));
['default', 'sublime', 'vim', 'emacs'].forEach(name => {
keyMapMenu.addItem({
command: CommandIDs.changeKeyMap,
args: { keyMap: name }
});
});
menu.addItem({ type: 'submenu', submenu: modeMenu });
menu.addItem({ type: 'submenu', submenu: tabMenu });
menu.addItem({ type: 'separator' });
menu.addItem({ command: 'editor:line-numbers' });
menu.addItem({ command: 'editor:word-wrap' });
menu.addItem({ command: CommandIDs.matchBrackets });
menu.addItem({ type: 'submenu', submenu: keyMapMenu });
menu.addItem({ type: 'submenu', submenu: themeMenu });
menu.addItem({ type: 'separator' });
menu.addItem({ command: CommandIDs.search });
menu.addItem({ command: CommandIDs.searchReplace });
menu.addItem({ command: CommandIDs.jumpToLine });
return menu;
}
mainMenu.addMenu(createMenu(), { rank: 30 });
commands.addCommand(CommandIDs.matchBrackets, {
execute: () => {
matchBrackets = !matchBrackets;
tracker.forEach(widget => {
const editor = widget.editor;
if (editor instanceof CodeMirrorEditor) {
const cm = editor.editor;
cm.setOption('matchBrackets', matchBrackets);
}
});
return settingRegistry.set(id, 'matchBrackets', matchBrackets);
},
label: 'Match Brackets',
isEnabled: hasWidget,
isToggled: () => matchBrackets
});
commands.addCommand(CommandIDs.search, {
label: 'Search',
execute: args => {
let widget = tracker.currentWidget;
if (widget.editor instanceof CodeMirrorEditor) {
widget.editor.execCommand("find");
}
},
isEnabled: isEditorVisible
});
commands.addCommand(CommandIDs.searchReplace, {
label: 'Search and Replace',
execute: args => {
let widget = tracker.currentWidget;
if (widget.editor instanceof CodeMirrorEditor) {
widget.editor.execCommand("replace");
}
},
isEnabled: isEditorVisible
});
commands.addCommand(CommandIDs.jumpToLine, {
label: 'Jump to Line',
execute: args => {
let widget = tracker.currentWidget;
if (widget.editor instanceof CodeMirrorEditor) {
widget.editor.execCommand("jumpToLine");
}
},
isEnabled: isEditorVisible
});
[
'editor:line-numbers',
'editor:line-wrap',
CommandIDs.matchBrackets,
CommandIDs.search,
CommandIDs.searchReplace,
CommandIDs.jumpToLine,
'editor:create-console',
'editor:run-code'
].forEach(command => palette.addItem({ command, category: 'Editor' }));
}