Skip to content

Commit 1e66047

Browse files
VSCODE-174: Run only mongodb files when using shortcuts or command palette (#167)
* feat: run only mongodb files when using shortcuts or command palette VSCODE-174 * Updated commands conditions and added keybindings Took inspiration from here: https://github.com/Huachao/vscode-restclient/blob/master/package.json. Without including default keybindings it looks like we can't prevent a user from running a command with a keyboard shortcut even when that command isn't displayed in the palette because its `when` condition isn't met. * refactor: update error messages Co-authored-by: Massimiliano Marcon <[email protected]>
1 parent caa3643 commit 1e66047

File tree

3 files changed

+260
-143
lines changed

3 files changed

+260
-143
lines changed

package.json

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@
523523
}
524524
],
525525
"commandPalette": [
526+
{
527+
"command": "mdb.runSelectedPlaygroundBlocks",
528+
"when": "editorTextFocus && editorLangId == mongodb"
529+
},
530+
{
531+
"command": "mdb.runAllPlaygroundBlocks",
532+
"when": "editorTextFocus && editorLangId == mongodb"
533+
},
526534
{
527535
"command": "mdb.refreshPlaygroundsFromTreeView",
528536
"when": "false"
@@ -645,6 +653,20 @@
645653
}
646654
]
647655
},
656+
"keybindings": [
657+
{
658+
"command": "mdb.runSelectedPlaygroundBlocks",
659+
"key": "ctrl+alt+s",
660+
"mac": "cmd+alt+s",
661+
"when": "editorTextFocus && editorLangId == mongodb"
662+
},
663+
{
664+
"command": "mdb.runAllPlaygroundBlocks",
665+
"key": "ctrl+alt+r",
666+
"mac": "cmd+alt+r",
667+
"when": "editorTextFocus && editorLangId == mongodb"
668+
}
669+
],
648670
"capabilities": {
649671
"codeLensProvider": {
650672
"resolveProvider": "true"
@@ -719,7 +741,7 @@
719741
},
720742
"description": "Files and folders to exclude while searching for playground in the the current workspace.",
721743
"default": [
722-
"**/.!(todo|todos|task|tasks)/**",
744+
"**/.*",
723745
"**/_output/**",
724746
"**/bower_components/**",
725747
"**/build/**",

src/editors/playgroundController.ts

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,9 @@ export default class PlaygroundController {
8080
);
8181

8282
vscode.window.onDidChangeActiveTextEditor((editor) => {
83-
if (
84-
editor &&
85-
editor.document &&
86-
editor.document.languageId === 'mongodb'
87-
) {
83+
if (editor?.document.languageId !== 'Log') {
8884
this._activeTextEditor = editor;
89-
log.info('Active editor path', editor.document.uri?.path);
85+
log.info('Active editor path', editor?.document.uri?.path);
9086
}
9187
});
9288

@@ -320,52 +316,79 @@ export default class PlaygroundController {
320316
}
321317

322318
public runSelectedPlaygroundBlocks(): Promise<boolean> {
323-
if (this._activeTextEditor && this._activeTextEditor.document) {
324-
const selections = this._activeTextEditor.selections;
319+
if (
320+
!this._activeTextEditor ||
321+
this._activeTextEditor.document.languageId !== 'mongodb'
322+
) {
323+
vscode.window.showErrorMessage(
324+
`Please open a '.mongodb' playground file before running it.`
325+
);
325326

326-
if (
327-
!selections ||
328-
!Array.isArray(selections) ||
329-
(selections.length === 1 && this.getSelectedText(selections[0]) === '')
330-
) {
331-
vscode.window.showInformationMessage(
332-
'Please select one or more lines in the playground.'
333-
);
327+
return Promise.resolve(false);
328+
}
334329

335-
return Promise.resolve(true);
336-
} else if (this._selectedText) {
337-
this._isPartialRun = true;
338-
this._codeToEvaluate = this._selectedText;
339-
}
330+
const selections = this._activeTextEditor.selections;
331+
332+
if (
333+
!selections ||
334+
!Array.isArray(selections) ||
335+
(selections.length === 1 && this.getSelectedText(selections[0]) === '')
336+
) {
337+
vscode.window.showInformationMessage(
338+
'Please select one or more lines in the playground.'
339+
);
340+
341+
return Promise.resolve(true);
342+
} else if (this._selectedText) {
343+
this._isPartialRun = true;
344+
this._codeToEvaluate = this._selectedText;
340345
}
341346

342347
return this.evaluatePlayground();
343348
}
344349

345350
public runAllPlaygroundBlocks(): Promise<boolean> {
346-
if (this._activeTextEditor) {
347-
this._isPartialRun = false;
348-
this._codeToEvaluate = this.getAllText();
351+
if (
352+
!this._activeTextEditor ||
353+
this._activeTextEditor.document.languageId !== 'mongodb'
354+
) {
355+
vscode.window.showErrorMessage(
356+
`Please open a '.mongodb' playground file before running it.`
357+
);
358+
359+
return Promise.resolve(false);
349360
}
350361

362+
this._isPartialRun = false;
363+
this._codeToEvaluate = this.getAllText();
364+
351365
return this.evaluatePlayground();
352366
}
353367

354368
public runAllOrSelectedPlaygroundBlocks(): Promise<boolean> {
355-
if (this._activeTextEditor && this._activeTextEditor.document) {
356-
const selections = this._activeTextEditor.selections;
369+
if (
370+
!this._activeTextEditor ||
371+
this._activeTextEditor.document.languageId !== 'mongodb'
372+
) {
373+
vscode.window.showErrorMessage(
374+
`Please open a '.mongodb' playground file before running it.`
375+
);
357376

358-
if (
359-
!selections ||
360-
!Array.isArray(selections) ||
361-
(selections.length === 1 && this.getSelectedText(selections[0]) === '')
362-
) {
363-
this._isPartialRun = false;
364-
this._codeToEvaluate = this.getAllText();
365-
} else if (this._selectedText) {
366-
this._isPartialRun = true;
367-
this._codeToEvaluate = this._selectedText;
368-
}
377+
return Promise.resolve(false);
378+
}
379+
380+
const selections = this._activeTextEditor.selections;
381+
382+
if (
383+
!selections ||
384+
!Array.isArray(selections) ||
385+
(selections.length === 1 && this.getSelectedText(selections[0]) === '')
386+
) {
387+
this._isPartialRun = false;
388+
this._codeToEvaluate = this.getAllText();
389+
} else if (this._selectedText) {
390+
this._isPartialRun = true;
391+
this._codeToEvaluate = this._selectedText;
369392
}
370393

371394
return this.evaluatePlayground();

0 commit comments

Comments
 (0)