|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { extensionName } from '../appGlobals' |
| 3 | +import { basename } from 'path' |
| 4 | +import { DebugCodeLens } from './DebugCodeLens' |
| 5 | +import { TestReconciliationState } from '../TestReconciliationState' |
| 6 | +import { TestResultProvider } from '../TestResultProvider' |
| 7 | + |
| 8 | +export class DebugCodeLensProvider implements vscode.CodeLensProvider { |
| 9 | + private _enabled: boolean |
| 10 | + onDidChange: vscode.EventEmitter<void> |
| 11 | + testResultProvider: TestResultProvider |
| 12 | + |
| 13 | + constructor(testResultProvider: TestResultProvider, enabled: boolean) { |
| 14 | + this.testResultProvider = testResultProvider |
| 15 | + this._enabled = enabled |
| 16 | + this.onDidChange = new vscode.EventEmitter() |
| 17 | + } |
| 18 | + |
| 19 | + get enabled() { |
| 20 | + return this._enabled |
| 21 | + } |
| 22 | + |
| 23 | + set enabled(value: boolean) { |
| 24 | + this._enabled = value |
| 25 | + this.onDidChange.fire() |
| 26 | + } |
| 27 | + |
| 28 | + get onDidChangeCodeLenses(): vscode.Event<void> { |
| 29 | + return this.onDidChange.event |
| 30 | + } |
| 31 | + |
| 32 | + provideCodeLenses(document: vscode.TextDocument, _: vscode.CancellationToken): vscode.CodeLens[] { |
| 33 | + const result = [] |
| 34 | + |
| 35 | + if (!this._enabled || document.isUntitled) { |
| 36 | + return result |
| 37 | + } |
| 38 | + |
| 39 | + const filePath = document.fileName |
| 40 | + const testResults = this.testResultProvider.getResults(filePath) |
| 41 | + const fileName = basename(document.fileName) |
| 42 | + for (const test of testResults) { |
| 43 | + if (test.status === TestReconciliationState.KnownSuccess) { |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + const start = new vscode.Position(test.start.line, test.start.column) |
| 48 | + const end = new vscode.Position(test.end.line, test.start.column + 5) |
| 49 | + const range = new vscode.Range(start, end) |
| 50 | + result.push(new DebugCodeLens(range, fileName, test.name)) |
| 51 | + } |
| 52 | + |
| 53 | + return result |
| 54 | + } |
| 55 | + |
| 56 | + resolveCodeLens(codeLens: vscode.CodeLens, _: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens> { |
| 57 | + if (codeLens instanceof DebugCodeLens) { |
| 58 | + codeLens.command = { |
| 59 | + arguments: [codeLens.fileName, codeLens.testName], |
| 60 | + command: `${extensionName}.run-test`, |
| 61 | + title: 'Debug', |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return codeLens |
| 66 | + } |
| 67 | + |
| 68 | + didChange() { |
| 69 | + this.onDidChange.fire() |
| 70 | + } |
| 71 | +} |
0 commit comments