Skip to content

Commit

Permalink
Use document filter for the code lens provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo committed May 21, 2020
1 parent c40d395 commit 8ac7d4c
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 30 deletions.
1 change: 1 addition & 0 deletions extension.bundle.ts
Expand Up @@ -3,6 +3,7 @@

export { activate, deactivate } from './src/extension';
export * from './src/codelens/TestCodeLensProvider';
export * from './src/codelens/TestCodeLensController';
export * from './src/runners/models';
export * from './src/testResultManager';
export * from './src/protocols';
Expand Down
146 changes: 146 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -358,6 +358,7 @@
"@types/mocha": "^2.2.48",
"@types/node": "^6.14.10",
"@types/pug": "^2.0.4",
"@types/sinon": "^9.0.3",
"@types/vscode": "1.44.0",
"bootstrap": "^4.4.1",
"filemanager-webpack-plugin": "^2.0.5",
Expand All @@ -367,6 +368,7 @@
"gulp-tslint": "^8.1.4",
"mocha": "^7.1.2",
"pug-loader": "^2.4.0",
"sinon": "^9.0.2",
"ts-loader": "^5.4.5",
"tslint": "^5.20.1",
"typescript": "^2.8.3",
Expand All @@ -384,4 +386,4 @@
"winston": "^3.2.1",
"winston-transport": "^4.3.0"
}
}
}
25 changes: 18 additions & 7 deletions src/codelens/TestCodeLensController.ts
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { ConfigurationChangeEvent, Disposable, languages, workspace } from 'vscode';
import { ConfigurationChangeEvent, Disposable, languages, workspace, RelativePattern, DocumentSelector } from 'vscode';
import { ENABLE_EDITOR_SHORTCUTS_KEY } from '../constants/configs';
import { TestCodeLensProvider } from './TestCodeLensProvider';

Expand All @@ -22,6 +22,22 @@ class TestCodeLensController implements Disposable {
this.setCodeLensVisibility();
}

public registerCodeLensProvider(patterns: RelativePattern[]): void {
if (this.registeredProvider) {
this.registeredProvider.dispose();
}

const documentSelector: DocumentSelector = patterns.map((p: RelativePattern) => {
return {
language: 'java',
scheme: 'file',
pattern: p,
};
});

this.registeredProvider = languages.registerCodeLensProvider(documentSelector, this.internalProvider);
}

public refresh(): void {
this.internalProvider.refresh();
}
Expand All @@ -35,12 +51,7 @@ class TestCodeLensController implements Disposable {
}

private setCodeLensVisibility(): void {
if (this.isCodeLensEnabled() && !this.registeredProvider) {
this.registeredProvider = languages.registerCodeLensProvider({ scheme: 'file', language: 'java' }, this.internalProvider);
} else if (!this.isCodeLensEnabled() && this.registeredProvider) {
this.registeredProvider.dispose();
this.registeredProvider = undefined;
}
this.internalProvider.setIsActivated(this.isCodeLensEnabled());
}

private isCodeLensEnabled(): boolean {
Expand Down
9 changes: 7 additions & 2 deletions src/codelens/TestCodeLensProvider.ts
Expand Up @@ -6,23 +6,28 @@ import { JavaTestRunnerCommands } from '../constants/commands';
import { logger } from '../logger/logger';
import { ITestItem, TestLevel } from '../protocols';
import { ITestResult, TestStatus } from '../runners/models';
import { testFileWatcher } from '../testFileWatcher';
import { testItemModel } from '../testItemModel';
import { testResultManager } from '../testResultManager';

export class TestCodeLensProvider implements CodeLensProvider, Disposable {
private onDidChangeCodeLensesEmitter: EventEmitter<void> = new EventEmitter<void>();
private isActivated: boolean = true;

get onDidChangeCodeLenses(): Event<void> {
return this.onDidChangeCodeLensesEmitter.event;
}

public setIsActivated(isActivated: boolean): void {
this.isActivated = isActivated;
this.refresh();
}

public refresh(): void {
this.onDidChangeCodeLensesEmitter.fire();
}

public async provideCodeLenses(document: TextDocument, _token: CancellationToken): Promise<CodeLens[]> {
if (!testFileWatcher.isOnTestSourcePath(document.uri.fsPath)) {
if (!this.isActivated) {
return [];
}

Expand Down
18 changes: 5 additions & 13 deletions src/testFileWatcher.ts
Expand Up @@ -13,7 +13,7 @@ import { getTestSourcePaths } from './utils/commandUtils';

class TestFileWatcher implements Disposable {

private testSourcePaths: string[] = [];
private patterns: RelativePattern[] = [];
private disposables: Disposable[] = [];
private registerListenersDebounce: (() => Promise<void>) & _.Cancelable = _.debounce(this.registerListenersInternal, 2 * 1000 /*ms*/);

Expand All @@ -23,17 +23,6 @@ class TestFileWatcher implements Disposable {
} else {
await this.registerListenersInternal();
}
testCodeLensController.refresh();
}

public isOnTestSourcePath(documentPath: string): boolean {
for (const sourcePath of this.testSourcePaths) {
if (documentPath.startsWith(sourcePath)) {
return true;
}
}

return false;
}

public dispose(): void {
Expand All @@ -43,6 +32,7 @@ class TestFileWatcher implements Disposable {
}
}
this.disposables = [];
this.patterns = [];
}

protected async registerListenersInternal(): Promise<void> {
Expand All @@ -52,8 +42,8 @@ class TestFileWatcher implements Disposable {
const sourcePaths: string[] = await getTestSourcePaths(workspace.workspaceFolders.map((workspaceFolder: WorkspaceFolder) => workspaceFolder.uri.toString()));
for (const sourcePath of sourcePaths) {
const normalizedPath: string = Uri.file(sourcePath).fsPath;
this.testSourcePaths.push(normalizedPath);
const pattern: RelativePattern = new RelativePattern(normalizedPath, '**/*.{[jJ][aA][vV][aA]}');
this.patterns.push(pattern);
const watcher: FileSystemWatcher = workspace.createFileSystemWatcher(pattern, true /* ignoreCreateEvents */);
this.registerWatcherListeners(watcher);
this.disposables.push(watcher);
Expand All @@ -64,6 +54,8 @@ class TestFileWatcher implements Disposable {
this.registerWatcherListeners(watcher);
this.disposables.push(watcher);
}
testCodeLensController.registerCodeLensProvider(this.patterns);
} else {
}
}

Expand Down
20 changes: 14 additions & 6 deletions test/maven-junit4-suite/testFileWatcher.test.ts
@@ -1,20 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { commands, TextDocument, workspace, extensions } from 'vscode';
import { Uris } from '../shared';
import { testFileWatcher } from '../../extension.bundle';
import { commands, extensions, RelativePattern } from 'vscode';
import { testCodeLensController, testFileWatcher } from '../../extension.bundle';
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as path from "path";
import { Uris } from '../shared';

suite('Test File Watcher Tests', function() {

const sandbox = sinon.createSandbox();

suiteSetup(async function() {
await extensions.getExtension('vscjava.vscode-java-test')!.activate();
});

test("Should check source file is a test file or not", async function() {
const document: TextDocument = await workspace.openTextDocument(Uris.JUNIT4_TEST);
assert.ok(testFileWatcher.isOnTestSourcePath(document.uri.fsPath));
test("Should correctly setup code lens provider", async function() {
let spy: sinon.SinonSpy = sandbox.spy(testCodeLensController, 'registerCodeLensProvider');
await testFileWatcher.registerListeners();
const args: RelativePattern[] = spy.getCall(0).args[0];
assert.ok(args.length === 2);
assert.ok(path.relative(args[0].base, path.join(Uris.JUNIT4_TEST_PACKAGE, '..')));
spy.restore();
});

teardown(async function() {
Expand Down

0 comments on commit 8ac7d4c

Please sign in to comment.