Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCODE-247: Replace code lenses with code actions #318

Merged
merged 9 commits into from
Aug 12, 2021
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.

81 changes: 6 additions & 75 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,7 +87,6 @@ export default class PlaygroundController {
'Playground output'
);
this._activeConnectionCodeLensProvider = activeConnectionCodeLensProvider;
this._partialExecutionCodeLensProvider = partialExecutionCodeLensProvider;
this._explorerController = explorerController;

this._connectionController.addEventListener(
Expand Down Expand Up @@ -130,60 +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');

void 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
) {
void 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
)
);
}

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

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

runSelectedPlaygroundBlocks(): Promise<boolean> {
if (
!this._activeTextEditor ||
this._activeTextEditor.document.languageId !== 'mongodb'
) {
void 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) {
void 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
9 changes: 4 additions & 5 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
HelpExplorer,
CollectionTreeItem
} from './explorer';
import CodeActionProvider from './editors/codeActionProvider';
import EXTENSION_COMMANDS from './commands';
import FieldTreeItem from './explorer/fieldTreeItem';
import IndexListTreeItem from './explorer/indexListTreeItem';
Expand All @@ -31,7 +32,6 @@ import SchemaTreeItem from './explorer/schemaTreeItem';
import { StatusView } from './views';
import { StorageController, StorageVariables } from './storage';
import TelemetryService from './telemetry/telemetryService';
import PartialExecutionCodeLensProvider from './editors/partialExecutionCodeLensProvider';
import PlaygroundsTreeItem from './explorer/playgroundsTreeItem';
import PlaygroundResultProvider from './editors/playgroundResultProvider';
import WebviewController from './views/webviewController';
Expand All @@ -41,6 +41,7 @@ const log = createLogger('commands');
// This class is the top-level controller for our extension.
// Commands which the extensions handles are defined in the function `activate`.
export default class MDBExtensionController implements vscode.Disposable {
_codeActionProvider: CodeActionProvider;
_connectionController: ConnectionController;
_context: vscode.ExtensionContext;
_editorsController: EditorsController;
Expand All @@ -55,7 +56,6 @@ export default class MDBExtensionController implements vscode.Disposable {
_webviewController: WebviewController;
_playgroundResultViewProvider: PlaygroundResultProvider;
_activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
_partialExecutionCodeLensProvider: PartialExecutionCodeLensProvider;
_editDocumentCodeLensProvider: EditDocumentCodeLensProvider;

constructor(
Expand Down Expand Up @@ -91,7 +91,6 @@ export default class MDBExtensionController implements vscode.Disposable {
this._activeConnectionCodeLensProvider = new ActiveConnectionCodeLensProvider(
this._connectionController
);
this._partialExecutionCodeLensProvider = new PartialExecutionCodeLensProvider();
this._playgroundController = new PlaygroundController(
context,
this._connectionController,
Expand All @@ -100,9 +99,9 @@ export default class MDBExtensionController implements vscode.Disposable {
this._statusView,
this._playgroundResultViewProvider,
this._activeConnectionCodeLensProvider,
this._partialExecutionCodeLensProvider,
this._explorerController
);
this._codeActionProvider = new CodeActionProvider(this._playgroundController);
this._editorsController = new EditorsController(
context,
this._connectionController,
Expand All @@ -111,7 +110,7 @@ export default class MDBExtensionController implements vscode.Disposable {
this._telemetryService,
this._playgroundResultViewProvider,
this._activeConnectionCodeLensProvider,
this._partialExecutionCodeLensProvider,
this._codeActionProvider,
this._editDocumentCodeLensProvider
);
this._webviewController = new WebviewController(
Expand Down
Loading