@jupyterlab/fileeditor-extension 4.0.0-alpha.9 → 4.0.0-beta.1

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