Skip to content

Commit

Permalink
feat: replace code lenses for partial execution with code actions VSC…
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika committed Aug 6, 2021
1 parent c9cdb63 commit 125c595
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 407 deletions.
29 changes: 29 additions & 0 deletions src/editors/codeActionProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as vscode from 'vscode';
import EXTENSION_COMMANDS from '../commands';
import PlaygroundController from './playgroundController';

export default class CodeActionProvider implements vscode.CodeActionProvider {
_playgroundController: PlaygroundController;

static readonly providedCodeActionKinds = [vscode.CodeActionKind.QuickFix];

constructor(playgroundController: PlaygroundController) {
this._playgroundController = playgroundController;
}

provideCodeActions(): vscode.CodeAction[] | undefined {
if (!this._playgroundController._selectedText) {
return;
}

const commandAction = new vscode.CodeAction('Run selected playground blocks', vscode.CodeActionKind.Empty);

commandAction.command = {
command: EXTENSION_COMMANDS.MDB_RUN_SELECTED_PLAYGROUND_BLOCKS,
title: 'Run selected playground blocks',
tooltip: 'Run selected playground blocks'
};

return [commandAction];
}
}
19 changes: 9 additions & 10 deletions src/editors/editorsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import { EJSON } from 'bson';

import ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider';
import CodeActionProvider from './codeActionProvider';
import ConnectionController from '../connectionController';
import CollectionDocumentsCodeLensProvider from './collectionDocumentsCodeLensProvider';
import CollectionDocumentsOperationsStore from './collectionDocumentsOperationsStore';
Expand All @@ -22,7 +23,6 @@ import MongoDBDocumentService, {
DOCUMENT_SOURCE_URI_IDENTIFIER,
VIEW_DOCUMENT_SCHEME
} from './mongoDBDocumentService';
import PartialExecutionCodeLensProvider from './partialExecutionCodeLensProvider';
import PlaygroundController from './playgroundController';
import PlaygroundResultProvider, {
PLAYGROUND_RESULT_SCHEME
Expand All @@ -37,6 +37,7 @@ const log = createLogger('editors controller');
* new editors and the data they need. It also manages active editors.
*/
export default class EditorsController {
_codeActionProvider: CodeActionProvider;
_connectionController: ConnectionController;
_playgroundController: PlaygroundController;
_collectionDocumentsOperationsStore = new CollectionDocumentsOperationsStore();
Expand All @@ -49,7 +50,6 @@ export default class EditorsController {
_telemetryService: TelemetryService;
_playgroundResultViewProvider: PlaygroundResultProvider;
_activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
_partialExecutionCodeLensProvider: PartialExecutionCodeLensProvider;
_editDocumentCodeLensProvider: EditDocumentCodeLensProvider;
_collectionDocumentsCodeLensProvider: CollectionDocumentsCodeLensProvider;

Expand All @@ -61,7 +61,7 @@ export default class EditorsController {
telemetryService: TelemetryService,
playgroundResultViewProvider: PlaygroundResultProvider,
activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider,
partialExecutionCodeLensProvider: PartialExecutionCodeLensProvider,
codeActionProvider: CodeActionProvider,
editDocumentCodeLensProvider: EditDocumentCodeLensProvider
) {
log.info('activating...');
Expand Down Expand Up @@ -90,10 +90,10 @@ export default class EditorsController {
);
this._playgroundResultViewProvider = playgroundResultViewProvider;
this._activeConnectionCodeLensProvider = activeConnectionCodeLensProvider;
this._partialExecutionCodeLensProvider = partialExecutionCodeLensProvider;
this._collectionDocumentsCodeLensProvider = new CollectionDocumentsCodeLensProvider(
this._collectionDocumentsOperationsStore
);
this._codeActionProvider = codeActionProvider;

vscode.workspace.onDidCloseTextDocument((e) => {
const uriParams = new URLSearchParams(e.uri.query);
Expand Down Expand Up @@ -372,12 +372,6 @@ export default class EditorsController {
this._activeConnectionCodeLensProvider
)
);
this._context.subscriptions.push(
vscode.languages.registerCodeLensProvider(
{ language: 'mongodb' },
this._partialExecutionCodeLensProvider
)
);
this._context.subscriptions.push(
vscode.languages.registerCodeLensProvider(
{
Expand All @@ -396,6 +390,11 @@ export default class EditorsController {
this._editDocumentCodeLensProvider
)
);
this._context.subscriptions.push(
vscode.languages.registerCodeActionsProvider('mongodb', this._codeActionProvider, {
providedCodeActionKinds: CodeActionProvider.providedCodeActionKinds
})
);
}

deactivate(): void {
Expand Down
92 changes: 0 additions & 92 deletions src/editors/partialExecutionCodeLensProvider.ts

This file was deleted.

92 changes: 6 additions & 86 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import { createLogger } from '../logging';
import { ExplorerController, ConnectionTreeItem, DatabaseTreeItem } from '../explorer';
import { LanguageServerController } from '../language';
import { OutputChannel, ProgressLocation, TextEditor } from 'vscode';
import PartialExecutionCodeLensProvider, {
isSelectionValidForCodeLens,
getCodeLensLineOffsetForSelection
} from './partialExecutionCodeLensProvider';
import playgroundCreateIndexTemplate from '../templates/playgroundCreateIndexTemplate';
import playgroundCreateCollectionTemplate from '../templates/playgroundCreateCollectionTemplate';
import playgroundCreateCollectionWithTSTemplate from '../templates/playgroundCreateCollectionWithTSTemplate';
Expand Down Expand Up @@ -59,7 +55,6 @@ export default class PlaygroundController {
_languageServerController: LanguageServerController;
_telemetryService: TelemetryService;
_activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
_partialExecutionCodeLensProvider: PartialExecutionCodeLensProvider;
_outputChannel: OutputChannel;
_connectionString?: string;
_selectedText?: string;
Expand All @@ -79,7 +74,6 @@ export default class PlaygroundController {
statusView: StatusView,
playgroundResultViewProvider: PlaygroundResultProvider,
activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider,
partialExecutionCodeLensProvider: PartialExecutionCodeLensProvider,
explorerController: ExplorerController
) {
this._context = context;
Expand All @@ -93,16 +87,8 @@ export default class PlaygroundController {
'Playground output'
);
this._activeConnectionCodeLensProvider = activeConnectionCodeLensProvider;
this._partialExecutionCodeLensProvider = partialExecutionCodeLensProvider;
this._explorerController = explorerController;

this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
() => {
this._disconnectFromServiceProvider();
}
);

this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
() => {
Expand Down Expand Up @@ -137,64 +123,15 @@ export default class PlaygroundController {
.sort((a, b) => (a.start.line > b.start.line ? 1 : -1));

this._selectedText = sortedSelections
.map((selection) => this._getSelectedText(selection))
.map((item) => this._getSelectedText(item))
.join('\n');

this._showCodeLensForSelection(
sortedSelections,
changeEvent.textEditor
);
}
}
);
}

_showCodeLensForSelection(
selections: vscode.Selection[],
editor: vscode.TextEditor
): void {
if (!this._selectedText || this._selectedText.trim().length === 0) {
this._partialExecutionCodeLensProvider.refresh();
return;
}

if (!isSelectionValidForCodeLens(selections, editor.document)) {
this._partialExecutionCodeLensProvider.refresh();
return;
}

const lastSelection = selections[selections.length - 1];
const lastSelectedLineNumber = lastSelection.end.line;
const lastSelectedLineContent = editor.document.lineAt(lastSelectedLineNumber).text || '';
// Add an empty line to the end of the file when the selection includes
// the last line and it is not empty.
// We do this so that we can show the code lens after the line's contents.
if (
lastSelection.end.line === editor.document.lineCount - 1 &&
lastSelectedLineContent.trim().length > 0
) {
editor.edit(edit => {
edit.insert(
new vscode.Position(lastSelection.end.line + 1, 0),
'\n'
);
});
}

const codeLensLineOffset = getCodeLensLineOffsetForSelection(selections, editor);
this._partialExecutionCodeLensProvider.refresh(
new vscode.Range(
lastSelectedLineNumber + codeLensLineOffset, 0,
lastSelectedLineNumber + codeLensLineOffset, 0
)
);
}

_disconnectFromServiceProvider(): void {
this._languageServerController.disconnectFromServiceProvider();
}

async _connectToServiceProvider(): Promise<void> {
// Disconnect if already connected.
await this._languageServerController.disconnectFromServiceProvider();

const dataService = this._connectionController.getActiveDataService();
Expand Down Expand Up @@ -522,34 +459,17 @@ export default class PlaygroundController {
}

runSelectedPlaygroundBlocks(): Promise<boolean> {
if (
!this._activeTextEditor ||
this._activeTextEditor.document.languageId !== 'mongodb'
) {
vscode.window.showErrorMessage(
"Please open a '.mongodb' playground file before running it."
);

return Promise.resolve(false);
}

const selections = this._activeTextEditor.selections;

if (
!selections ||
!Array.isArray(selections) ||
(selections.length === 1 && this._getSelectedText(selections[0]) === '')
) {
if (!this._selectedText) {
vscode.window.showInformationMessage(
'Please select one or more lines in the playground.'
);

return Promise.resolve(true);
} else if (this._selectedText) {
this._isPartialRun = true;
this._codeToEvaluate = this._selectedText;
}

this._isPartialRun = true;
this._codeToEvaluate = this._selectedText;

return this._evaluatePlayground();
}

Expand Down
Loading

0 comments on commit 125c595

Please sign in to comment.