@jupyterlab/fileeditor-extension 4.0.0-alpha.2 → 4.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/commands.js CHANGED
@@ -1,9 +1,13 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
- import { Clipboard, sessionContextDialogs } from '@jupyterlab/apputils';
4
- import { CodeEditor } from '@jupyterlab/codeeditor';
3
+ import { selectAll } from '@codemirror/commands';
4
+ import { findNext, gotoLine } from '@codemirror/search';
5
+ import { Clipboard, MainAreaWidget, sessionContextDialogs } from '@jupyterlab/apputils';
6
+ import { CodeViewerWidget } from '@jupyterlab/codeeditor';
5
7
  import { MarkdownCodeBlocks, PathExt } from '@jupyterlab/coreutils';
8
+ import { nullTranslator } from '@jupyterlab/translation';
6
9
  import { consoleIcon, copyIcon, cutIcon, LabIcon, markdownIcon, pasteIcon, redoIcon, textEditorIcon, undoIcon } from '@jupyterlab/ui-components';
10
+ import { find } from '@lumino/algorithm';
7
11
  const autoClosingBracketsNotebook = 'notebook:toggle-autoclosing-brackets';
8
12
  const autoClosingBracketsConsole = 'console:toggle-autoclosing-brackets';
9
13
  /**
@@ -35,56 +39,40 @@ export var CommandIDs;
35
39
  CommandIDs.copy = 'fileeditor:copy';
36
40
  CommandIDs.paste = 'fileeditor:paste';
37
41
  CommandIDs.selectAll = 'fileeditor:select-all';
42
+ CommandIDs.invokeCompleter = 'completer:invoke-file';
43
+ CommandIDs.selectCompleter = 'completer:select-file';
44
+ CommandIDs.openCodeViewer = 'code-viewer:open';
45
+ CommandIDs.changeTheme = 'fileeditor:change-theme';
46
+ CommandIDs.changeLanguage = 'fileeditor:change-language';
47
+ CommandIDs.find = 'fileeditor:find';
48
+ CommandIDs.goToLine = 'fileeditor:go-to-line';
38
49
  })(CommandIDs || (CommandIDs = {}));
39
50
  /**
40
51
  * The name of the factory that creates editor widgets.
41
52
  */
42
53
  export const FACTORY = 'Editor';
43
- const userSettings = [
44
- 'autoClosingBrackets',
45
- 'cursorBlinkRate',
46
- 'fontFamily',
47
- 'fontSize',
48
- 'lineHeight',
49
- 'lineNumbers',
50
- 'lineWrap',
51
- 'matchBrackets',
52
- 'readOnly',
53
- 'insertSpaces',
54
- 'tabSize',
55
- 'wordWrapColumn',
56
- 'rulers',
57
- 'codeFolding'
58
- ];
59
- function filterUserSettings(config) {
60
- const filteredConfig = Object.assign({}, config);
61
- // Delete parts of the config that are not user settings (like handlePaste).
62
- for (let k of Object.keys(config)) {
63
- if (!userSettings.includes(k)) {
64
- delete config[k];
65
- }
66
- }
67
- return filteredConfig;
68
- }
69
- let config = filterUserSettings(CodeEditor.defaultConfig);
70
54
  /**
71
55
  * A utility class for adding commands and menu items,
72
56
  * for use by the File Editor extension or other Editor extensions.
73
57
  */
74
58
  export var Commands;
75
59
  (function (Commands) {
60
+ let config = {};
61
+ let scrollPastEnd = true;
76
62
  /**
77
63
  * Accessor function that returns the createConsole function for use by Create Console commands
78
64
  */
79
- function getCreateConsoleFunction(commands) {
65
+ function getCreateConsoleFunction(commands, languages) {
80
66
  return async function createConsole(widget, args) {
81
- var _a;
67
+ var _a, _b, _c;
82
68
  const options = args || {};
83
69
  const console = await commands.execute('console:create', {
84
70
  activate: options['activate'],
85
71
  name: (_a = widget.context.contentsModel) === null || _a === void 0 ? void 0 : _a.name,
86
72
  path: widget.context.path,
87
- preferredLanguage: widget.context.model.defaultKernelLanguage,
73
+ // Default value is an empty string -> using OR operator
74
+ preferredLanguage: widget.context.model.defaultKernelLanguage ||
75
+ ((_c = (_b = languages.findByFileName(widget.context.path)) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : ''),
88
76
  ref: widget.id,
89
77
  insertMode: 'split-bottom'
90
78
  });
@@ -99,9 +87,20 @@ export var Commands;
99
87
  * Update the setting values.
100
88
  */
101
89
  function updateSettings(settings, commands) {
102
- config = filterUserSettings(Object.assign(Object.assign({}, CodeEditor.defaultConfig), settings.get('editorConfig').composite));
90
+ var _a;
91
+ config =
92
+ (_a = settings.get('editorConfig').composite) !== null && _a !== void 0 ? _a : {};
93
+ scrollPastEnd = settings.get('scrollPasteEnd').composite;
103
94
  // Trigger a refresh of the rendered commands
104
- commands.notifyCommandChanged();
95
+ commands.notifyCommandChanged(CommandIDs.lineNumbers);
96
+ commands.notifyCommandChanged(CommandIDs.currentLineNumbers);
97
+ commands.notifyCommandChanged(CommandIDs.lineWrap);
98
+ commands.notifyCommandChanged(CommandIDs.currentLineWrap);
99
+ commands.notifyCommandChanged(CommandIDs.changeTabs);
100
+ commands.notifyCommandChanged(CommandIDs.matchBrackets);
101
+ commands.notifyCommandChanged(CommandIDs.currentMatchBrackets);
102
+ commands.notifyCommandChanged(CommandIDs.autoClosingBrackets);
103
+ commands.notifyCommandChanged(CommandIDs.changeLanguage);
105
104
  }
106
105
  Commands.updateSettings = updateSettings;
107
106
  /**
@@ -119,22 +118,19 @@ export var Commands;
119
118
  */
120
119
  function updateWidget(widget) {
121
120
  const editor = widget.editor;
122
- let editorOptions = {};
123
- Object.keys(config).forEach((key) => {
124
- editorOptions[key] = config[key];
125
- });
126
- editor.setOptions(editorOptions);
121
+ editor.setOptions({ ...config, scrollPastEnd });
127
122
  }
128
123
  Commands.updateWidget = updateWidget;
129
124
  /**
130
125
  * Wrapper function for adding the default File Editor commands
131
126
  */
132
- function addCommands(commands, settingRegistry, trans, id, isEnabled, tracker, browserFactory, consoleTracker, sessionDialogs) {
127
+ function addCommands(commands, settingRegistry, trans, id, isEnabled, tracker, defaultBrowser, extensions, languages, themes, consoleTracker, sessionDialogs, mainMenu) {
133
128
  /**
134
129
  * Add a command to change font size for File Editor
135
130
  */
136
131
  commands.addCommand(CommandIDs.changeFontSize, {
137
132
  execute: args => {
133
+ var _a;
138
134
  const delta = Number(args['delta']);
139
135
  if (Number.isNaN(delta)) {
140
136
  console.error(`${CommandIDs.changeFontSize}: delta arg must be a number`);
@@ -142,7 +138,8 @@ export var Commands;
142
138
  }
143
139
  const style = window.getComputedStyle(document.documentElement);
144
140
  const cssSize = parseInt(style.getPropertyValue('--jp-code-font-size'), 10);
145
- const currentSize = config.fontSize || cssSize;
141
+ const currentSize = ((_a = config['customStyles']['fontSize']) !== null && _a !== void 0 ? _a : extensions.baseConfiguration['customStyles']['fontSize']) ||
142
+ cssSize;
146
143
  config.fontSize = currentSize + delta;
147
144
  return settingRegistry
148
145
  .set(id, 'editorConfig', config)
@@ -151,8 +148,11 @@ export var Commands;
151
148
  });
152
149
  },
153
150
  label: args => {
154
- var _a;
155
- if (((_a = args.delta) !== null && _a !== void 0 ? _a : 0) > 0) {
151
+ const delta = Number(args['delta']);
152
+ if (Number.isNaN(delta)) {
153
+ console.error(`${CommandIDs.changeFontSize}: delta arg must be a number`);
154
+ }
155
+ if (delta > 0) {
156
156
  return args.isMenu
157
157
  ? trans.__('Increase Text Editor Font Size')
158
158
  : trans.__('Increase Font Size');
@@ -168,20 +168,23 @@ export var Commands;
168
168
  * Add the Line Numbers command
169
169
  */
170
170
  commands.addCommand(CommandIDs.lineNumbers, {
171
- execute: () => {
172
- config.lineNumbers = !config.lineNumbers;
173
- return settingRegistry
174
- .set(id, 'editorConfig', config)
175
- .catch((reason) => {
171
+ execute: async () => {
172
+ var _a;
173
+ config.lineNumbers = !((_a = config.lineNumbers) !== null && _a !== void 0 ? _a : extensions.baseConfiguration.lineNumbers);
174
+ try {
175
+ return await settingRegistry.set(id, 'editorConfig', config);
176
+ }
177
+ catch (reason) {
176
178
  console.error(`Failed to set ${id}: ${reason.message}`);
177
- });
179
+ }
178
180
  },
179
181
  isEnabled,
180
- isToggled: () => config.lineNumbers,
181
- label: trans.__('Line Numbers')
182
+ isToggled: () => { var _a; return (_a = config.lineNumbers) !== null && _a !== void 0 ? _a : extensions.baseConfiguration.lineNumbers; },
183
+ label: trans.__('Show Line Numbers')
182
184
  });
183
185
  commands.addCommand(CommandIDs.currentLineNumbers, {
184
186
  label: trans.__('Show Line Numbers'),
187
+ caption: trans.__('Show the line numbers for the current file.'),
185
188
  execute: () => {
186
189
  const widget = tracker.currentWidget;
187
190
  if (!widget) {
@@ -194,44 +197,48 @@ export var Commands;
194
197
  isToggled: () => {
195
198
  var _a;
196
199
  const widget = tracker.currentWidget;
197
- return (_a = widget === null || widget === void 0 ? void 0 : widget.content.editor.getOption('lineNumbers')) !== null && _a !== void 0 ? _a : false;
200
+ return ((_a = widget === null || widget === void 0 ? void 0 : widget.content.editor.getOption('lineNumbers')) !== null && _a !== void 0 ? _a : false);
198
201
  }
199
202
  });
200
203
  /**
201
204
  * Add the Word Wrap command
202
205
  */
203
206
  commands.addCommand(CommandIDs.lineWrap, {
204
- execute: args => {
205
- config.lineWrap = args['mode'] || 'off';
206
- return settingRegistry
207
- .set(id, 'editorConfig', config)
208
- .catch((reason) => {
207
+ execute: async (args) => {
208
+ var _a;
209
+ config.lineWrap = (_a = args['mode']) !== null && _a !== void 0 ? _a : false;
210
+ try {
211
+ return await settingRegistry.set(id, 'editorConfig', config);
212
+ }
213
+ catch (reason) {
209
214
  console.error(`Failed to set ${id}: ${reason.message}`);
210
- });
215
+ }
211
216
  },
212
217
  isEnabled,
213
218
  isToggled: args => {
214
- const lineWrap = args['mode'] || 'off';
215
- return config.lineWrap === lineWrap;
219
+ var _a, _b;
220
+ const lineWrap = (_a = args['mode']) !== null && _a !== void 0 ? _a : false;
221
+ return (lineWrap ===
222
+ ((_b = config.lineWrap) !== null && _b !== void 0 ? _b : extensions.baseConfiguration.lineWrap));
216
223
  },
217
224
  label: trans.__('Word Wrap')
218
225
  });
219
226
  commands.addCommand(CommandIDs.currentLineWrap, {
220
227
  label: trans.__('Wrap Words'),
228
+ caption: trans.__('Wrap words for the current file.'),
221
229
  execute: () => {
222
230
  const widget = tracker.currentWidget;
223
231
  if (!widget) {
224
232
  return;
225
233
  }
226
234
  const oldValue = widget.content.editor.getOption('lineWrap');
227
- const newValue = oldValue === 'off' ? 'on' : 'off';
228
- widget.content.editor.setOption('lineWrap', newValue);
235
+ widget.content.editor.setOption('lineWrap', !oldValue);
229
236
  },
230
237
  isEnabled,
231
238
  isToggled: () => {
232
239
  var _a;
233
240
  const widget = tracker.currentWidget;
234
- return (_a = (widget === null || widget === void 0 ? void 0 : widget.content.editor.getOption('lineWrap')) !== 'off') !== null && _a !== void 0 ? _a : false;
241
+ return ((_a = widget === null || widget === void 0 ? void 0 : widget.content.editor.getOption('lineWrap')) !== null && _a !== void 0 ? _a : false);
235
242
  }
236
243
  });
237
244
  /**
@@ -240,46 +247,55 @@ export var Commands;
240
247
  commands.addCommand(CommandIDs.changeTabs, {
241
248
  label: args => {
242
249
  var _a;
243
- if (args.insertSpaces) {
244
- return trans._n('Spaces: %1', 'Spaces: %1', (_a = args.size) !== null && _a !== void 0 ? _a : 0);
250
+ if (args.size) {
251
+ return trans.__('Spaces: %1', (_a = args.size) !== null && _a !== void 0 ? _a : '');
245
252
  }
246
253
  else {
247
254
  return trans.__('Indent with Tab');
248
255
  }
249
256
  },
250
- execute: args => {
251
- config.tabSize = args['size'] || 4;
252
- config.insertSpaces = !!args['insertSpaces'];
253
- return settingRegistry
254
- .set(id, 'editorConfig', config)
255
- .catch((reason) => {
257
+ execute: async (args) => {
258
+ var _a;
259
+ config.indentUnit =
260
+ args['size'] !== undefined
261
+ ? ((_a = args['size']) !== null && _a !== void 0 ? _a : '4').toString()
262
+ : 'Tab';
263
+ try {
264
+ return await settingRegistry.set(id, 'editorConfig', config);
265
+ }
266
+ catch (reason) {
256
267
  console.error(`Failed to set ${id}: ${reason.message}`);
257
- });
268
+ }
258
269
  },
259
270
  isToggled: args => {
260
- const insertSpaces = !!args['insertSpaces'];
261
- const size = args['size'] || 4;
262
- return config.insertSpaces === insertSpaces && config.tabSize === size;
271
+ var _a;
272
+ const currentIndentUnit = (_a = config.indentUnit) !== null && _a !== void 0 ? _a : extensions.baseConfiguration.indentUnit;
273
+ return args['size']
274
+ ? args['size'] === currentIndentUnit
275
+ : 'Tab' == currentIndentUnit;
263
276
  }
264
277
  });
265
278
  /**
266
279
  * Add the Match Brackets command
267
280
  */
268
281
  commands.addCommand(CommandIDs.matchBrackets, {
269
- execute: () => {
270
- config.matchBrackets = !config.matchBrackets;
271
- return settingRegistry
272
- .set(id, 'editorConfig', config)
273
- .catch((reason) => {
282
+ execute: async () => {
283
+ var _a;
284
+ config.matchBrackets = !((_a = config.matchBrackets) !== null && _a !== void 0 ? _a : extensions.baseConfiguration.matchBrackets);
285
+ try {
286
+ return await settingRegistry.set(id, 'editorConfig', config);
287
+ }
288
+ catch (reason) {
274
289
  console.error(`Failed to set ${id}: ${reason.message}`);
275
- });
290
+ }
276
291
  },
277
292
  label: trans.__('Match Brackets'),
278
293
  isEnabled,
279
- isToggled: () => config.matchBrackets
294
+ isToggled: () => { var _a; return (_a = config.matchBrackets) !== null && _a !== void 0 ? _a : extensions.baseConfiguration.matchBrackets; }
280
295
  });
281
296
  commands.addCommand(CommandIDs.currentMatchBrackets, {
282
297
  label: trans.__('Match Brackets'),
298
+ caption: trans.__('Change match brackets for the current file.'),
283
299
  execute: () => {
284
300
  const widget = tracker.currentWidget;
285
301
  if (!widget) {
@@ -292,24 +308,28 @@ export var Commands;
292
308
  isToggled: () => {
293
309
  var _a;
294
310
  const widget = tracker.currentWidget;
295
- return (_a = widget === null || widget === void 0 ? void 0 : widget.content.editor.getOption('matchBrackets')) !== null && _a !== void 0 ? _a : false;
311
+ return ((_a = widget === null || widget === void 0 ? void 0 : widget.content.editor.getOption('matchBrackets')) !== null && _a !== void 0 ? _a : false);
296
312
  }
297
313
  });
298
314
  /**
299
315
  * Add the Auto Close Brackets for Text Editor command
300
316
  */
301
317
  commands.addCommand(CommandIDs.autoClosingBrackets, {
302
- execute: args => {
303
- var _a;
304
- config.autoClosingBrackets = !!((_a = args['force']) !== null && _a !== void 0 ? _a : !config.autoClosingBrackets);
305
- return settingRegistry
306
- .set(id, 'editorConfig', config)
307
- .catch((reason) => {
318
+ execute: async (args) => {
319
+ var _a, _b;
320
+ config.autoClosingBrackets = !!((_a = args['force']) !== null && _a !== void 0 ? _a : !((_b = config.autoClosingBrackets) !== null && _b !== void 0 ? _b : extensions.baseConfiguration.autoClosingBrackets));
321
+ try {
322
+ return await settingRegistry.set(id, 'editorConfig', config);
323
+ }
324
+ catch (reason) {
308
325
  console.error(`Failed to set ${id}: ${reason.message}`);
309
- });
326
+ }
310
327
  },
311
- label: trans.__('Auto Close Brackets for Text Editor'),
312
- isToggled: () => config.autoClosingBrackets
328
+ label: trans.__('Auto Close Brackets in Text Editor'),
329
+ isToggled: () => {
330
+ var _a;
331
+ return (_a = config.autoClosingBrackets) !== null && _a !== void 0 ? _a : extensions.baseConfiguration.autoClosingBrackets;
332
+ }
313
333
  });
314
334
  commands.addCommand(CommandIDs.autoClosingBracketsUniversal, {
315
335
  execute: () => {
@@ -338,6 +358,87 @@ export var Commands;
338
358
  commands.isToggled(autoClosingBracketsNotebook) ||
339
359
  commands.isToggled(autoClosingBracketsConsole)
340
360
  });
361
+ /**
362
+ * Create a menu for the editor.
363
+ */
364
+ commands.addCommand(CommandIDs.changeTheme, {
365
+ label: args => {
366
+ var _a, _b, _c, _d;
367
+ return (_d = (_c = (_b = ((_a = args.displayName) !== null && _a !== void 0 ? _a : args.theme)) !== null && _b !== void 0 ? _b : config.theme) !== null && _c !== void 0 ? _c : extensions.baseConfiguration.theme) !== null && _d !== void 0 ? _d : trans.__('Editor Theme');
368
+ },
369
+ execute: async (args) => {
370
+ var _a;
371
+ config.theme = (_a = args['theme']) !== null && _a !== void 0 ? _a : config.theme;
372
+ try {
373
+ return await settingRegistry.set(id, 'editorConfig', config);
374
+ }
375
+ catch (reason) {
376
+ console.error(`Failed to set theme - ${reason.message}`);
377
+ }
378
+ },
379
+ isToggled: args => { var _a; return args['theme'] === ((_a = config.theme) !== null && _a !== void 0 ? _a : extensions.baseConfiguration.theme); }
380
+ });
381
+ commands.addCommand(CommandIDs.find, {
382
+ label: trans.__('Find…'),
383
+ execute: () => {
384
+ const widget = tracker.currentWidget;
385
+ if (!widget) {
386
+ return;
387
+ }
388
+ const editor = widget.content.editor;
389
+ editor.execCommand(findNext);
390
+ },
391
+ isEnabled
392
+ });
393
+ commands.addCommand(CommandIDs.goToLine, {
394
+ label: trans.__('Go to Line…'),
395
+ execute: args => {
396
+ const widget = tracker.currentWidget;
397
+ if (!widget) {
398
+ return;
399
+ }
400
+ const editor = widget.content.editor;
401
+ const line = args['line'];
402
+ const column = args['column'];
403
+ if (line !== undefined || column !== undefined) {
404
+ editor.setCursorPosition({
405
+ line: (line !== null && line !== void 0 ? line : 1) - 1,
406
+ column: (column !== null && column !== void 0 ? column : 1) - 1
407
+ });
408
+ }
409
+ else {
410
+ editor.execCommand(gotoLine);
411
+ }
412
+ },
413
+ isEnabled
414
+ });
415
+ commands.addCommand(CommandIDs.changeLanguage, {
416
+ label: args => {
417
+ var _a, _b;
418
+ return (_b = ((_a = args['displayName']) !== null && _a !== void 0 ? _a : args['name'])) !== null && _b !== void 0 ? _b : trans.__('Change editor language.');
419
+ },
420
+ execute: args => {
421
+ const name = args['name'];
422
+ const widget = tracker.currentWidget;
423
+ if (name && widget) {
424
+ const spec = languages.findByName(name);
425
+ if (spec) {
426
+ widget.content.model.mimeType = spec.mime;
427
+ }
428
+ }
429
+ },
430
+ isEnabled,
431
+ isToggled: args => {
432
+ const widget = tracker.currentWidget;
433
+ if (!widget) {
434
+ return false;
435
+ }
436
+ const mime = widget.content.model.mimeType;
437
+ const spec = languages.findByMIME(mime);
438
+ const name = spec && spec.name;
439
+ return args['name'] === name;
440
+ }
441
+ });
341
442
  /**
342
443
  * Add the replace selection for text editor command
343
444
  */
@@ -363,7 +464,7 @@ export var Commands;
363
464
  if (!widget) {
364
465
  return;
365
466
  }
366
- return getCreateConsoleFunction(commands)(widget, args);
467
+ return getCreateConsoleFunction(commands, languages)(widget, args);
367
468
  },
368
469
  isEnabled,
369
470
  icon: consoleIcon,
@@ -409,10 +510,10 @@ export var Commands;
409
510
  // Get the selected code from the editor.
410
511
  const start = editor.getOffsetAt(selection.start);
411
512
  const end = editor.getOffsetAt(selection.end);
412
- code = editor.model.value.text.substring(start, end);
513
+ code = editor.model.sharedModel.getSource().substring(start, end);
413
514
  }
414
515
  else if (MarkdownCodeBlocks.isMarkdown(extension)) {
415
- const { text } = editor.model.value;
516
+ const text = editor.model.sharedModel.getSource();
416
517
  const blocks = MarkdownCodeBlocks.findMarkdownCodeBlocks(text);
417
518
  for (const block of blocks) {
418
519
  if (block.startLine <= start.line && start.line <= block.endLine) {
@@ -427,8 +528,8 @@ export var Commands;
427
528
  code = editor.getLine(selection.start.line);
428
529
  const cursor = editor.getCursorPosition();
429
530
  if (cursor.line + 1 === editor.lineCount) {
430
- const text = editor.model.value.text;
431
- editor.model.value.text = text + '\n';
531
+ const text = editor.model.sharedModel.getSource();
532
+ editor.model.sharedModel.setSource(text + '\n');
432
533
  }
433
534
  editor.setCursorPosition({
434
535
  line: cursor.line + 1,
@@ -458,7 +559,7 @@ export var Commands;
458
559
  }
459
560
  let code = '';
460
561
  const editor = widget.editor;
461
- const text = editor.model.value.text;
562
+ const text = editor.model.sharedModel.getSource();
462
563
  const path = widget.context.path;
463
564
  const extension = PathExt.extname(path);
464
565
  if (MarkdownCodeBlocks.isMarkdown(extension)) {
@@ -530,7 +631,7 @@ export var Commands;
530
631
  },
531
632
  execute: args => {
532
633
  var _a;
533
- const cwd = args.cwd || browserFactory.defaultBrowser.model.path;
634
+ const cwd = args.cwd || defaultBrowser.model.path;
534
635
  return createNew(commands, cwd, (_a = args.fileExt) !== null && _a !== void 0 ? _a : 'txt');
535
636
  }
536
637
  });
@@ -544,7 +645,7 @@ export var Commands;
544
645
  caption: trans.__('Create a new markdown file'),
545
646
  icon: args => (args['isPalette'] ? undefined : markdownIcon),
546
647
  execute: args => {
547
- const cwd = args['cwd'] || browserFactory.defaultBrowser.model.path;
648
+ const cwd = args['cwd'] || defaultBrowser.model.path;
548
649
  return createNew(commands, cwd, 'md');
549
650
  }
550
651
  });
@@ -697,13 +798,40 @@ export var Commands;
697
798
  return;
698
799
  }
699
800
  const editor = widget.editor;
700
- editor.execCommand('selectAll');
801
+ editor.execCommand(selectAll);
701
802
  },
702
803
  isEnabled: () => { var _a; return Boolean(isEnabled() && ((_a = tracker.currentWidget) === null || _a === void 0 ? void 0 : _a.content)); },
703
804
  label: trans.__('Select All')
704
805
  });
705
806
  }
706
807
  Commands.addCommands = addCommands;
808
+ function addCompleterCommands(commands, editorTracker, manager, translator) {
809
+ const trans = (translator !== null && translator !== void 0 ? translator : nullTranslator).load('jupyterlab');
810
+ commands.addCommand(CommandIDs.invokeCompleter, {
811
+ label: trans.__('Display the completion helper.'),
812
+ execute: () => {
813
+ const id = editorTracker.currentWidget && editorTracker.currentWidget.id;
814
+ if (id) {
815
+ return manager.invoke(id);
816
+ }
817
+ }
818
+ });
819
+ commands.addCommand(CommandIDs.selectCompleter, {
820
+ label: trans.__('Select the completion suggestion.'),
821
+ execute: () => {
822
+ const id = editorTracker.currentWidget && editorTracker.currentWidget.id;
823
+ if (id) {
824
+ return manager.select(id);
825
+ }
826
+ }
827
+ });
828
+ commands.addKeyBinding({
829
+ command: CommandIDs.selectCompleter,
830
+ keys: ['Enter'],
831
+ selector: '.jp-FileEditor .jp-mod-completer-active'
832
+ });
833
+ }
834
+ Commands.addCompleterCommands = addCompleterCommands;
707
835
  /**
708
836
  * Helper function to check if there is a text selection in the editor
709
837
  */
@@ -720,27 +848,26 @@ export var Commands;
720
848
  const selectionObj = editor.getSelection();
721
849
  const start = editor.getOffsetAt(selectionObj.start);
722
850
  const end = editor.getOffsetAt(selectionObj.end);
723
- const text = editor.model.value.text.substring(start, end);
851
+ const text = editor.model.sharedModel.getSource().substring(start, end);
724
852
  return text;
725
853
  }
726
854
  /**
727
855
  * Function to create a new untitled text file, given the current working directory.
728
856
  */
729
- function createNew(commands, cwd, ext = 'txt') {
730
- return commands
731
- .execute('docmanager:new-untitled', {
857
+ async function createNew(commands, cwd, ext = 'txt') {
858
+ const model = await commands.execute('docmanager:new-untitled', {
732
859
  path: cwd,
733
860
  type: 'file',
734
861
  ext
735
- })
736
- .then(model => {
737
- if (model != undefined) {
738
- return commands.execute('docmanager:open', {
739
- path: model.path,
740
- factory: FACTORY
741
- });
742
- }
743
862
  });
863
+ if (model != undefined) {
864
+ const widget = (await commands.execute('docmanager:open', {
865
+ path: model.path,
866
+ factory: FACTORY
867
+ }));
868
+ widget.isUntitled = true;
869
+ return widget;
870
+ }
744
871
  }
745
872
  /**
746
873
  * Wrapper function for adding the default launcher items for File Editor
@@ -802,14 +929,12 @@ export var Commands;
802
929
  function addChangeTabsCommandsToPalette(palette, trans) {
803
930
  const paletteCategory = trans.__('Text Editor');
804
931
  const args = {
805
- insertSpaces: false,
806
932
  size: 4
807
933
  };
808
934
  const command = CommandIDs.changeTabs;
809
935
  palette.addItem({ command, args, category: paletteCategory });
810
936
  for (const size of [1, 2, 4, 8]) {
811
937
  const args = {
812
- insertSpaces: true,
813
938
  size
814
939
  };
815
940
  palette.addItem({ command, args, category: paletteCategory });
@@ -860,7 +985,7 @@ export var Commands;
860
985
  for (let ext of availableKernelFileTypes) {
861
986
  palette.addItem({
862
987
  command: CommandIDs.createNew,
863
- args: Object.assign(Object.assign({}, ext), { isPalette: true }),
988
+ args: { ...ext, isPalette: true },
864
989
  category: paletteCategory
865
990
  });
866
991
  }
@@ -936,5 +1061,43 @@ export var Commands;
936
1061
  });
937
1062
  }
938
1063
  Commands.addCodeRunnersToRunMenu = addCodeRunnersToRunMenu;
1064
+ function addOpenCodeViewerCommand(app, editorServices, tracker, trans) {
1065
+ const openCodeViewer = async (args) => {
1066
+ var _a;
1067
+ const func = editorServices.factoryService.newDocumentEditor;
1068
+ const factory = options => {
1069
+ return func(options);
1070
+ };
1071
+ // Derive mimetype from extension
1072
+ let mimetype = args.mimeType;
1073
+ if (!mimetype && args.extension) {
1074
+ mimetype = editorServices.mimeTypeService.getMimeTypeByFilePath(`temp.${args.extension.replace(/\\.$/, '')}`);
1075
+ }
1076
+ const widget = CodeViewerWidget.createCodeViewer({
1077
+ factory,
1078
+ content: args.content,
1079
+ mimeType: mimetype
1080
+ });
1081
+ widget.title.label = args.label || trans.__('Code Viewer');
1082
+ widget.title.caption = widget.title.label;
1083
+ // Get the fileType based on the mimetype to determine the icon
1084
+ const fileType = find(app.docRegistry.fileTypes(), fileType => mimetype ? fileType.mimeTypes.includes(mimetype) : false);
1085
+ widget.title.icon = (_a = fileType === null || fileType === void 0 ? void 0 : fileType.icon) !== null && _a !== void 0 ? _a : textEditorIcon;
1086
+ if (args.widgetId) {
1087
+ widget.id = args.widgetId;
1088
+ }
1089
+ const main = new MainAreaWidget({ content: widget });
1090
+ await tracker.add(main);
1091
+ app.shell.add(main, 'main');
1092
+ return widget;
1093
+ };
1094
+ app.commands.addCommand(CommandIDs.openCodeViewer, {
1095
+ label: trans.__('Open Code Viewer'),
1096
+ execute: (args) => {
1097
+ return openCodeViewer(args);
1098
+ }
1099
+ });
1100
+ }
1101
+ Commands.addOpenCodeViewerCommand = addOpenCodeViewerCommand;
939
1102
  })(Commands || (Commands = {}));
940
1103
  //# sourceMappingURL=commands.js.map