diff --git a/src/LogOutputManager.spec.ts b/src/LogOutputManager.spec.ts index dc3dcea4..e274c718 100644 --- a/src/LogOutputManager.spec.ts +++ b/src/LogOutputManager.spec.ts @@ -27,25 +27,18 @@ import { util } from 'brighterscript'; describe('LogOutputManager ', () => { let logOutputManagerMock: Sinon.SinonMock; let logOutputManager: LogOutputManager; - let languagesMock: Sinon.SinonMock; let outputChannelMock: Sinon.SinonMock; let logDocumentLinkProviderMock: Sinon.SinonMock; - let collectionMock: Sinon.SinonMock; let declarationProviderMock: Sinon.SinonMock; beforeEach(() => { sinon.restore(); const outputChannel = new vscode.OutputChannel(); - const debugCollection = new vscode.DebugCollection(); const logDocumentLinkProvider = new LogDocumentLinkProvider(); const declarationProvider = new DeclarationProvider(); outputChannelMock = sinon.mock(outputChannel); logDocumentLinkProviderMock = sinon.mock(logDocumentLinkProvider); - collectionMock = sinon.mock(debugCollection); declarationProviderMock = sinon.mock(declarationProvider); - languagesMock = sinon.mock(vscode.languages); - languagesMock.expects('createDiagnosticCollection').returns(debugCollection); - collectionMock.expects('clear'); logOutputManager = new LogOutputManager(outputChannel, vscode.context, logDocumentLinkProvider, declarationProvider); logOutputManagerMock = sinon.mock(logOutputManager); }); @@ -55,38 +48,30 @@ describe('LogOutputManager ', () => { }); it('tests onDidStartDebugSession clear flag', () => { - collectionMock.expects('clear').once(); logOutputManager.isClearingConsoleOnChannelStart = true; logOutputManager.onDidStartDebugSession(); outputChannelMock.verify(); - collectionMock.verify(); logOutputManagerMock.verify(); }); it('tests onDidStartDebugSession no clear flag', () => { - collectionMock.expects('clear').never(); logOutputManager.isClearingConsoleOnChannelStart = false; logOutputManager.onDidStartDebugSession(); outputChannelMock.verify(); - collectionMock.verify(); logOutputManagerMock.verify(); }); it('tests onDidReceiveDebugSessionCustomEvent - LaunchStartEvent - clear flag', async () => { - collectionMock.expects('clear').once(); logOutputManager.isClearingOutputOnLaunch = true; await logOutputManager.onDidReceiveDebugSessionCustomEvent({ event: 'LaunchStartEvent' }); outputChannelMock.verify(); - collectionMock.verify(); logOutputManagerMock.verify(); }); it('tests onDidReceiveDebugSessionCustomEvent - LaunchStartEvent - no clear flag', async () => { - collectionMock.expects('clear').never(); logOutputManager.isClearingOutputOnLaunch = false; await logOutputManager.onDidReceiveDebugSessionCustomEvent(new LaunchStartEvent({} as any)); outputChannelMock.verify(); - collectionMock.verify(); logOutputManagerMock.verify(); }); @@ -94,34 +79,18 @@ describe('LogOutputManager ', () => { outputChannelMock.expects('appendLine').once(); await logOutputManager.onDidReceiveDebugSessionCustomEvent(new LogOutputEvent('test 1')); outputChannelMock.verify(); - collectionMock.verify(); logOutputManagerMock.verify(); }); it('tests onDidReceiveDebugSessionCustomEvent - error - empty', async () => { await logOutputManager.onDidReceiveDebugSessionCustomEvent({ event: '', body: {} }); outputChannelMock.verify(); - collectionMock.verify(); logOutputManagerMock.verify(); }); it('tests onDidReceiveDebugSessionCustomEvent - error - undefined', async () => { await logOutputManager.onDidReceiveDebugSessionCustomEvent({ event: '' }); outputChannelMock.verify(); - collectionMock.verify(); - logOutputManagerMock.verify(); - }); - - it('tests onDidReceiveDebugSessionCustomEvent - errors', async () => { - logOutputManagerMock.expects('addDiagnosticForError').once(); - let compileErrors: BSDebugDiagnostic[] = [{ - path: 'path1', - message: 'message1', - range: util.createRange(1, 2, 3, 4) - }]; - await logOutputManager.onDidReceiveDebugSessionCustomEvent(new DiagnosticsEvent(compileErrors)); - outputChannelMock.verify(); - collectionMock.verify(); logOutputManagerMock.verify(); }); @@ -130,13 +99,11 @@ describe('LogOutputManager ', () => { logOutputManager.appendLine('test1', true); logOutputManager.appendLine('test2', true); logOutputManager.appendLine('test3', true); - collectionMock.expects('clear').once(); outputChannelMock.expects('clear').once(); logOutputManager.clearOutput(); outputChannelMock.verify(); - collectionMock.verify(); }); it('tests output indexes are cleared', () => { @@ -151,7 +118,6 @@ describe('LogOutputManager ', () => { logOutputManager.markOutput(); logOutputManagerMock.verify(); - collectionMock.verify(); logOutputManagerMock = sinon.mock(logOutputManager); logOutputManager.clearOutput(); @@ -164,7 +130,6 @@ describe('LogOutputManager ', () => { logOutputManager.markOutput(); logOutputManagerMock.verify(); - collectionMock.verify(); }); }); @@ -175,7 +140,6 @@ describe('LogOutputManager ', () => { .withExactArgs('test1').once(); logOutputManager.appendLine('test1', true); outputChannelMock.verify(); - collectionMock.verify(); }); it('splits multiple lines', () => { @@ -187,7 +151,6 @@ describe('LogOutputManager ', () => { logOutputManager.appendLine('test1\ntest2', true); outputChannelMock.verify(); - collectionMock.verify(); }); }); @@ -204,7 +167,6 @@ describe('LogOutputManager ', () => { logOutputManager.markOutput(); logOutputManagerMock.verify(); - collectionMock.verify(); }); }); diff --git a/src/LogOutputManager.ts b/src/LogOutputManager.ts index 5c526b87..9a3dc361 100644 --- a/src/LogOutputManager.ts +++ b/src/LogOutputManager.ts @@ -1,5 +1,4 @@ import * as vscode from 'vscode'; -import type { DiagnosticCollection } from 'vscode'; import type { BSDebugDiagnostic } from 'roku-debug'; import { isChanperfEvent, isDiagnosticsEvent, isLaunchStartEvent, isLogOutputEvent, isPopupMessageEvent, isRendezvousEvent } from 'roku-debug'; import type { DeclarationProvider } from './DeclarationProvider'; @@ -24,7 +23,6 @@ export class LogOutputManager { docLinkProvider, private declarationProvider: DeclarationProvider ) { - this.collection = vscode.languages.createDiagnosticCollection('BrightScript'); this.outputChannel = outputChannel; this.docLinkProvider = docLinkProvider; @@ -103,7 +101,6 @@ export class LogOutputManager { public isClearingOutputOnLaunch: boolean; public isClearingConsoleOnChannelStart: boolean; public hyperlinkFormat: string; - private collection: DiagnosticCollection; private outputChannel: vscode.OutputChannel; private docLinkProvider: LogDocumentLinkProvider; private debugStartRegex: RegExp; @@ -247,22 +244,6 @@ export class LogOutputManager { this.clearOutput(); } - } else if (isDiagnosticsEvent(e)) { - let errorsByPath = {}; - for (const diagnostic of e.body.diagnostics) { - if (diagnostic.path) { - if (!errorsByPath[diagnostic.path]) { - errorsByPath[diagnostic.path] = []; - } - errorsByPath[diagnostic.path].push(diagnostic); - } - } - for (const path in errorsByPath) { - if (errorsByPath.hasOwnProperty(path)) { - const errors = errorsByPath[path]; - await this.addDiagnosticForError(path, errors); - } - } } } @@ -299,39 +280,6 @@ export class LogOutputManager { methods[severity](message); } - public async addDiagnosticForError(path: string, diagnostics: BSDebugDiagnostic[]) { - //TODO get the actual folder - let documentUri: vscode.Uri; - let uri = vscode.Uri.file(path); - let doc = await vscode.workspace.openTextDocument(uri); // calls back - if (doc !== undefined) { - documentUri = doc.uri; - } - // console.log("got " + documentUri); - - //debug crap - for some reason - using this URI works - using the one from the path does not :() - // const document = vscode.window.activeTextEditor.document; - // const currentDocumentUri = document.uri; - // console.log("currentDocumentUri " + currentDocumentUri); - if (documentUri !== undefined) { - let result: vscode.Diagnostic[] = []; - for (const diagnostic of diagnostics) { - result.push({ - code: diagnostic.code, - message: diagnostic.message, - source: diagnostic.source, - severity: diagnostic.severity, - tags: diagnostic.tags, - range: new vscode.Range( - new vscode.Position(diagnostic.range.start.line, diagnostic.range.start.character), - new vscode.Position(diagnostic.range.end.line, diagnostic.range.end.character) - ) - }); - } - this.collection.set(documentUri, result); - } - } - /** * Log output methods */ @@ -487,7 +435,6 @@ export class LogOutputManager { this.allLogLines = []; this.displayedLogLines = []; this.outputChannel.clear(); - this.collection.clear(); this.docLinkProvider.resetCustomLinks(); } diff --git a/src/extension.ts b/src/extension.ts index c733d938..0f17ac94 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -26,6 +26,7 @@ import { isChannelPublishedEvent, isChanperfEvent, isDiagnosticsEvent, isDebugSe import { RtaManager } from './managers/RtaManager'; import { WebviewViewProviderManager } from './managers/WebviewViewProviderManager'; import { ViewProviderId } from './viewProviders/ViewProviderId'; +import { DiagnosticManager } from './managers/DiagnosticManager'; const EXTENSION_ID = 'RokuCommunity.brightscript'; @@ -44,6 +45,7 @@ export class Extension { private brightScriptCommands: BrightScriptCommands; private rtaManager: RtaManager; private webviewViewProviderManager: WebviewViewProviderManager; + private diagnosticManager = new DiagnosticManager(); public async activate(context: vscode.ExtensionContext) { const currentExtensionVersion = extensions.getExtension(EXTENSION_ID)?.packageJSON.version as string; @@ -171,6 +173,7 @@ export class Extension { if (e.type === 'brightscript') { logOutputManager.onDidStartDebugSession(); } + this.diagnosticManager.clear(); }); vscode.debug.onDidTerminateDebugSession((e) => { @@ -182,6 +185,7 @@ export class Extension { void this.remoteControlManager.setRemoteControlMode(false, 'launch'); } } + this.diagnosticManager.clear(); }); vscode.debug.onDidReceiveDebugSessionCustomEvent(async (e) => { @@ -208,7 +212,7 @@ export class Extension { context.subscriptions.push(vscode.languages.registerDefinitionProvider(xmlSelector, new BrightScriptXmlDefinitionProvider(definitionRepo))); await this.whatsNewManager.showWelcomeOrWhatsNewIfRequired(); - await languageServerPromise; + //await languageServerPromise; } private async debugSessionCustomEventHandler(e: any, context: vscode.ExtensionContext, docLinkProvider: LogDocumentLinkProvider, logOutputManager: LogOutputManager, rendezvousViewProvider: RendezvousViewProvider) { @@ -249,6 +253,21 @@ export class Extension { selection: util.toRange(firstDiagnostic.range) }); } + + let errorsByPath = {}; + for (const diagnostic of e.body.diagnostics) { + if (diagnostic.path) { + if (!errorsByPath[diagnostic.path]) { + errorsByPath[diagnostic.path] = []; + } + errorsByPath[diagnostic.path].push(diagnostic); + } + } + for (const path in errorsByPath) { + if (errorsByPath.hasOwnProperty(path)) { + await this.diagnosticManager.addDiagnosticForError(path, errorsByPath[path]).catch(() => { }); + } + } } } diff --git a/src/managers/DiagnosticManager.ts b/src/managers/DiagnosticManager.ts new file mode 100644 index 00000000..e4b7374d --- /dev/null +++ b/src/managers/DiagnosticManager.ts @@ -0,0 +1,42 @@ +import type { BSDebugDiagnostic } from 'roku-debug'; +import * as vscode from 'vscode'; + +export class DiagnosticManager { + + private collection = vscode.languages.createDiagnosticCollection('BrightScript'); + + public clear() { + this.collection.clear(); + } + + public async addDiagnosticForError(path: string, diagnostics: BSDebugDiagnostic[]) { + let documentUri: vscode.Uri; + let uri = vscode.Uri.file(path); + let doc = await vscode.workspace.openTextDocument(uri); + if (doc !== undefined) { + documentUri = doc.uri; + } + + if (documentUri !== undefined) { + let result: vscode.Diagnostic[] = []; + for (const diagnostic of diagnostics) { + + result.push({ + code: diagnostic.code, + message: diagnostic.message, + source: diagnostic.source, + //the DiagnosticSeverity.Error from vscode-languageserver-types starts at 1, but vscode.DiagnosticSeverity.Error starts at 0. So subtract 1 to make them compatible + severity: diagnostic.severity - 1, + tags: diagnostic.tags, + range: new vscode.Range( + new vscode.Position(diagnostic.range.start.line, diagnostic.range.start.character), + new vscode.Position(diagnostic.range.end.line, diagnostic.range.end.character) + ) + }); + } + this.collection.set(documentUri, result); + } + } + + +}