|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import * as vscode from 'vscode'; |
| 4 | +import {CodeLensProvider, TextDocument, CancellationToken, CodeLens, SymbolInformation} from 'vscode'; |
| 5 | +import * as telemetryContracts from '../common/telemetryContracts'; |
| 6 | +import {Tests} from './common/contracts'; |
| 7 | +import {getDiscoveredTests} from './common/testUtils'; |
| 8 | + |
| 9 | +export class TestFileCodeLensProvider implements CodeLensProvider { |
| 10 | + constructor(private context: vscode.ExtensionContext) { |
| 11 | + // The command has been defined in the package.json file |
| 12 | + // Now provide the implementation of the command with registerCommand |
| 13 | + // The commandId parameter must match the command field in package.json |
| 14 | + vscode.commands.registerCommand('extension.sayHello', () => { |
| 15 | + // The code you place here will be executed every time your command is executed |
| 16 | + |
| 17 | + // Display a message box to the user |
| 18 | + vscode.window.showInformationMessage('Hello World!'); |
| 19 | + vscode.commands.executeCommand('vscode.executeCodeLensProvider', vscode.window.activeTextEditor.document.uri); |
| 20 | + }); |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + // private _onDidChange = new vscode.EventEmitter<CodeLensProvider>(); |
| 25 | + // get onDidChange(): vscode.Event<CodeLensProvider> { |
| 26 | + // return this._onDidChange.event; |
| 27 | + // } |
| 28 | + |
| 29 | + // public update() { |
| 30 | + // this._onDidChange.fire(this); |
| 31 | + // } |
| 32 | + |
| 33 | + public provideCodeLenses(document: TextDocument, token: CancellationToken): Thenable<CodeLens[]> { |
| 34 | + let testItems = getDiscoveredTests(); |
| 35 | + if (!testItems) { |
| 36 | + return Promise.resolve([]); |
| 37 | + } |
| 38 | + |
| 39 | + let items: CodeLens[] = []; |
| 40 | + return vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', document.uri).then((symbols: vscode.SymbolInformation[]) => { |
| 41 | + return symbols.filter(symbol => { |
| 42 | + return symbol.kind === vscode.SymbolKind.Function || |
| 43 | + symbol.kind === vscode.SymbolKind.Method || |
| 44 | + symbol.kind === vscode.SymbolKind.Class; |
| 45 | + }).map(symbol => { |
| 46 | + // let lens = new CodeLens(symbol.location.range);//, { command: 'python.sortImports', title: 'Wow, 1, 2, 3', arguments: [] }); |
| 47 | + // return lens; |
| 48 | + return <CodeLens>{ range: symbol.location.range, isResolved: false } as CodeLens; |
| 49 | + }); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + private counter = 0; |
| 54 | + public resolveCodeLens(codeLens: CodeLens, token: CancellationToken): Thenable<CodeLens> { |
| 55 | + return new Promise<CodeLens>(resolve => { |
| 56 | + codeLens.isResolved = true; |
| 57 | + codeLens.command = { command: 'extension.sayHello', title: 'Wow ' + this.counter++, arguments: [] }; |
| 58 | + resolve(codeLens); |
| 59 | + }); |
| 60 | + // return codeLens; |
| 61 | + } |
| 62 | +} |
0 commit comments