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

fix(editors): don't show connect code lens on regular js files VSCODE-538 #789

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,10 @@ export default class ConnectionController {
this.eventEmitter.removeListener(eventType, listener);
}

deactivate() {
this.eventEmitter.removeAllListeners();
}

closeConnectionStringInput() {
this._connectionStringInputCancellationToken?.cancel();
}
Expand Down
36 changes: 19 additions & 17 deletions src/editors/activeConnectionCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
import * as vscode from 'vscode';
import type { TextEditor } from 'vscode';

import EXTENSION_COMMANDS from '../commands';
import type ConnectionController from '../connectionController';
import { isPlayground } from '../utils/playground';
import { getDBFromConnectionString } from '../utils/connection-string-db';
import { DataServiceEventTypes } from '../connectionController';

export default class ActiveConnectionCodeLensProvider
implements vscode.CodeLensProvider
{
_connectionController: ConnectionController;
_onDidChangeCodeLenses: vscode.EventEmitter<void> =
new vscode.EventEmitter<void>();
activeTextEditor?: TextEditor;
_activeConnectionChangedHandler: () => void;

readonly onDidChangeCodeLenses: vscode.Event<void> =
this._onDidChangeCodeLenses.event;

constructor(connectionController: ConnectionController) {
this._connectionController = connectionController;
this.activeTextEditor = vscode.window.activeTextEditor;

vscode.workspace.onDidChangeConfiguration(() => {
this._onDidChangeCodeLenses.fire();
});
}

setActiveTextEditor(activeTextEditor?: TextEditor) {
this.activeTextEditor = activeTextEditor;
this._onDidChangeCodeLenses.fire();
}

refresh(): void {
this._onDidChangeCodeLenses.fire();
}

isPlayground(): boolean {
return isPlayground(this.activeTextEditor?.document.uri);
this._activeConnectionChangedHandler = () => {
this._onDidChangeCodeLenses.fire();
};
this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
this._activeConnectionChangedHandler
);
}

provideCodeLenses(): vscode.CodeLens[] {
if (!this.isPlayground()) {
provideCodeLenses(document: vscode.TextDocument): vscode.CodeLens[] {
if (!isPlayground(document.uri)) {
return [];
}

Expand Down Expand Up @@ -69,4 +64,11 @@ export default class ActiveConnectionCodeLensProvider

return [codeLens];
}

deactivate() {
this._connectionController.removeEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
this._activeConnectionChangedHandler
);
}
}
22 changes: 6 additions & 16 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import vm from 'vm';
import os from 'os';
import transpiler from 'bson-transpilers';

import type ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider';
import type PlaygroundSelectedCodeActionProvider from './playgroundSelectedCodeActionProvider';
import type ConnectionController from '../connectionController';
import { DataServiceEventTypes } from '../connectionController';
Expand Down Expand Up @@ -127,11 +126,11 @@ export default class PlaygroundController {

_isPartialRun = false;

private _activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
private _playgroundResultViewColumn?: vscode.ViewColumn;
private _playgroundResultTextDocument?: vscode.TextDocument;
private _statusView: StatusView;
private _playgroundResultViewProvider: PlaygroundResultProvider;
private _activeConnectionChangedHandler: () => void;

private _codeToEvaluate = '';

Expand All @@ -141,7 +140,6 @@ export default class PlaygroundController {
telemetryService,
statusView,
playgroundResultViewProvider,
activeConnectionCodeLensProvider,
exportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider,
}: {
Expand All @@ -150,7 +148,6 @@ export default class PlaygroundController {
telemetryService: TelemetryService;
statusView: StatusView;
playgroundResultViewProvider: PlaygroundResultProvider;
activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
playgroundSelectedCodeActionProvider: PlaygroundSelectedCodeActionProvider;
}) {
Expand All @@ -160,16 +157,16 @@ export default class PlaygroundController {
this._telemetryService = telemetryService;
this._statusView = statusView;
this._playgroundResultViewProvider = playgroundResultViewProvider;
this._activeConnectionCodeLensProvider = activeConnectionCodeLensProvider;
this._exportToLanguageCodeLensProvider = exportToLanguageCodeLensProvider;
this._playgroundSelectedCodeActionProvider =
playgroundSelectedCodeActionProvider;

this._activeConnectionChangedHandler = () => {
void this._activeConnectionChanged();
};
this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
() => {
void this._activeConnectionChanged();
}
this._activeConnectionChangedHandler
);

const onDidChangeActiveTextEditor = (
Expand All @@ -189,9 +186,6 @@ export default class PlaygroundController {

if (isPlaygroundEditor) {
this._activeTextEditor = editor;
this._activeConnectionCodeLensProvider.setActiveTextEditor(
this._activeTextEditor
);
this._playgroundSelectedCodeActionProvider.setActiveTextEditor(
this._activeTextEditor
);
Expand Down Expand Up @@ -270,8 +264,6 @@ export default class PlaygroundController {
const connectionId = this._connectionController.getActiveConnectionId();
let mongoClientOption;

this._activeConnectionCodeLensProvider.refresh();

if (dataService && connectionId) {
mongoClientOption =
this._connectionController.getMongoClientConnectionOptions();
Expand Down Expand Up @@ -893,9 +885,7 @@ export default class PlaygroundController {
deactivate(): void {
this._connectionController.removeEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
() => {
// No action is required after removing the listener.
}
this._activeConnectionChangedHandler
);
}
}
3 changes: 2 additions & 1 deletion src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export default class MDBExtensionController implements vscode.Disposable {
telemetryService: this._telemetryService,
statusView: this._statusView,
playgroundResultViewProvider: this._playgroundResultViewProvider,
activeConnectionCodeLensProvider: this._activeConnectionCodeLensProvider,
exportToLanguageCodeLensProvider: this._exportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider:
this._playgroundSelectedCodeActionProvider,
Expand Down Expand Up @@ -878,5 +877,7 @@ export default class MDBExtensionController implements vscode.Disposable {
this._telemetryService.deactivate();
this._editorsController.deactivate();
this._webviewController.deactivate();
this._activeConnectionCodeLensProvider.deactivate();
this._connectionController.deactivate();
}
}
49 changes: 26 additions & 23 deletions src/test/suite/editors/activeConnectionCodeLensProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as vscode from 'vscode';
import { beforeEach, afterEach } from 'mocha';
import chai from 'chai';
import { expect } from 'chai';
import sinon from 'sinon';
import type { DataService } from 'mongodb-data-service';
import path from 'path';

import ActiveConnectionCodeLensProvider from '../../../editors/activeConnectionCodeLensProvider';
import ConnectionController from '../../../connectionController';
Expand All @@ -12,8 +13,6 @@ import { ExtensionContextStub } from '../stubs';
import TelemetryService from '../../../telemetry/telemetryService';
import { TEST_DATABASE_URI } from '../dbTestHelper';

const expect = chai.expect;

suite('Active Connection CodeLens Provider Test Suite', () => {
const extensionContextStub = new ExtensionContextStub();
const testStorageController = new StorageController(extensionContextStub);
Expand Down Expand Up @@ -42,19 +41,23 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
});

suite('the MongoDB playground in JS', () => {
const mockFileName = path.join('nonexistent', 'playground-test.mongodb.js');
const mockDocumentUri = vscode.Uri.from({
path: mockFileName,
scheme: 'untitled',
});
const mockTextDoc: vscode.TextDocument = {
uri: mockDocumentUri,
} as Pick<vscode.TextDocument, 'uri'> as vscode.TextDocument;

suite('user is not connected', () => {
beforeEach(() => {
testCodeLensProvider.setActiveTextEditor(
vscode.window.activeTextEditor
);
const fakeShowQuickPick = sandbox.fake();
sandbox.replace(vscode.window, 'showQuickPick', fakeShowQuickPick);
const fakeIsPlayground = sandbox.fake.returns(true);
sandbox.replace(testCodeLensProvider, 'isPlayground', fakeIsPlayground);
});

test('show disconnected message in code lenses', () => {
const codeLens = testCodeLensProvider.provideCodeLenses();
const codeLens = testCodeLensProvider.provideCodeLenses(mockTextDoc);

expect(codeLens).to.be.an('array');
expect(codeLens.length).to.be.equal(1);
Expand Down Expand Up @@ -89,16 +92,11 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
} as unknown as DataService;

testConnectionController.setActiveDataService(activeDataServiceStub);
testCodeLensProvider.setActiveTextEditor(
vscode.window.activeTextEditor
);
sandbox.replace(
testConnectionController,
'getActiveConnectionName',
sandbox.fake.returns('fakeName')
);
const fakeIsPlayground = sandbox.fake.returns(true);
sandbox.replace(testCodeLensProvider, 'isPlayground', fakeIsPlayground);
});

test('show active connection in code lenses', () => {
Expand All @@ -109,7 +107,7 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
url: TEST_DATABASE_URI,
})
);
const codeLens = testCodeLensProvider.provideCodeLenses();
const codeLens = testCodeLensProvider.provideCodeLenses(mockTextDoc);

expect(codeLens).to.be.an('array');
expect(codeLens.length).to.be.equal(1);
Expand All @@ -131,7 +129,7 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
url: `${TEST_DATABASE_URI}/fakeDBName`,
})
);
const codeLens = testCodeLensProvider.provideCodeLenses();
const codeLens = testCodeLensProvider.provideCodeLenses(mockTextDoc);
expect(codeLens).to.be.an('array');
expect(codeLens.length).to.be.equal(1);
expect(codeLens[0].command?.title).to.be.equal(
Expand All @@ -147,16 +145,23 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
});

suite('the regular JS file', () => {
const mockFileName = path.join('nonexistent', 'playground-test.js');
const mockDocumentUri = vscode.Uri.from({
path: mockFileName,
scheme: 'untitled',
});
const mockTextDoc: vscode.TextDocument = {
uri: mockDocumentUri,
} as Pick<vscode.TextDocument, 'uri'> as vscode.TextDocument;

suite('user is not connected', () => {
beforeEach(() => {
const fakeShowQuickPick = sandbox.fake();
sandbox.replace(vscode.window, 'showQuickPick', fakeShowQuickPick);
const fakeIsPlayground = sandbox.fake.returns(false);
sandbox.replace(testCodeLensProvider, 'isPlayground', fakeIsPlayground);
});

test('show not show the active connection code lenses', () => {
const codeLens = testCodeLensProvider.provideCodeLenses();
const codeLens = testCodeLensProvider.provideCodeLenses(mockTextDoc);

expect(codeLens).to.be.an('array');
expect(codeLens.length).to.be.equal(0);
Expand Down Expand Up @@ -191,12 +196,10 @@ suite('Active Connection CodeLens Provider Test Suite', () => {
'getActiveConnectionName',
sandbox.fake.returns('fakeName')
);
const fakeIsPlayground = sandbox.fake.returns(false);
sandbox.replace(testCodeLensProvider, 'isPlayground', fakeIsPlayground);
});

test('show not show the active connection code lensess', () => {
const codeLens = testCodeLensProvider.provideCodeLenses();
test('show not show the active connection code lenses', () => {
const codeLens = testCodeLensProvider.provideCodeLenses(mockTextDoc);

expect(codeLens).to.be.an('array');
expect(codeLens.length).to.be.equal(0);
Expand Down
8 changes: 0 additions & 8 deletions src/test/suite/editors/playgroundController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { v4 as uuidv4 } from 'uuid';
import path from 'path';
import chaiAsPromised from 'chai-as-promised';

import ActiveDBCodeLensProvider from '../../../editors/activeConnectionCodeLensProvider';
import PlaygroundSelectedCodeActionProvider from '../../../editors/playgroundSelectedCodeActionProvider';
import ConnectionController from '../../../connectionController';
import EditDocumentCodeLensProvider from '../../../editors/editDocumentCodeLensProvider';
Expand Down Expand Up @@ -50,7 +49,6 @@ suite('Playground Controller Test Suite', function () {
let testConnectionController: ConnectionController;
let testEditDocumentCodeLensProvider: EditDocumentCodeLensProvider;
let testPlaygroundResultProvider: PlaygroundResultProvider;
let testActiveDBCodeLensProvider: ActiveDBCodeLensProvider;
let testExportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
let testCodeActionProvider: PlaygroundSelectedCodeActionProvider;
let languageServerControllerStub: LanguageServerController;
Expand Down Expand Up @@ -78,9 +76,6 @@ suite('Playground Controller Test Suite', function () {
testConnectionController,
testEditDocumentCodeLensProvider
);
testActiveDBCodeLensProvider = new ActiveDBCodeLensProvider(
testConnectionController
);
testExportToLanguageCodeLensProvider =
new ExportToLanguageCodeLensProvider();
testCodeActionProvider = new PlaygroundSelectedCodeActionProvider();
Expand All @@ -95,7 +90,6 @@ suite('Playground Controller Test Suite', function () {
telemetryService: testTelemetryService,
statusView: testStatusView,
playgroundResultViewProvider: testPlaygroundResultProvider,
activeConnectionCodeLensProvider: testActiveDBCodeLensProvider,
exportToLanguageCodeLensProvider: testExportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider: testCodeActionProvider,
});
Expand Down Expand Up @@ -350,7 +344,6 @@ suite('Playground Controller Test Suite', function () {
telemetryService: testTelemetryService,
statusView: testStatusView,
playgroundResultViewProvider: testPlaygroundResultProvider,
activeConnectionCodeLensProvider: testActiveDBCodeLensProvider,
exportToLanguageCodeLensProvider:
testExportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider: testCodeActionProvider,
Expand All @@ -368,7 +361,6 @@ suite('Playground Controller Test Suite', function () {
telemetryService: testTelemetryService,
statusView: testStatusView,
playgroundResultViewProvider: testPlaygroundResultProvider,
activeConnectionCodeLensProvider: testActiveDBCodeLensProvider,
exportToLanguageCodeLensProvider:
testExportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider: testCodeActionProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { beforeEach, afterEach } from 'mocha';
import chai from 'chai';
import sinon from 'sinon';

import ActiveConnectionCodeLensProvider from '../../../editors/activeConnectionCodeLensProvider';
import ExportToLanguageCodeLensProvider from '../../../editors/exportToLanguageCodeLensProvider';
import PlaygroundSelectedCodeActionProvider from '../../../editors/playgroundSelectedCodeActionProvider';
import { LanguageServerController } from '../../../language';
Expand Down Expand Up @@ -50,10 +49,6 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
TEST_DATABASE_URI
);

const activeConnectionCodeLensProvider =
new ActiveConnectionCodeLensProvider(
mdbTestExtension.testExtensionController._connectionController
);
const testExportToLanguageCodeLensProvider =
new ExportToLanguageCodeLensProvider();

Expand All @@ -69,7 +64,6 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
playgroundResultViewProvider:
mdbTestExtension.testExtensionController
._playgroundResultViewProvider,
activeConnectionCodeLensProvider,
exportToLanguageCodeLensProvider:
testExportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider: testCodeActionProvider,
Expand Down
Loading
Loading