-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add diagnostics for embedded Bash and Python
- Loading branch information
1 parent
b0dd7b0
commit eb81f87
Showing
5 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) 2023 Savoir-faire Linux. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
|
||
import * as vscode from 'vscode' | ||
|
||
import { getOriginalDocRange } from './utils' | ||
import { embeddedLanguageDocsManager } from './EmbeddedLanguageDocsManager' | ||
import { type EmbeddedLanguageType } from '../lib/src/types/embedded-languages' | ||
|
||
const diagnosticCollections = { | ||
bash: vscode.languages.createDiagnosticCollection('bitbake-bash'), | ||
python: vscode.languages.createDiagnosticCollection('bitbake-python') | ||
} | ||
|
||
export const updateDiagnostics = async (uri: vscode.Uri): Promise<void> => { | ||
if (!uri.path.endsWith('.py') && !uri.path.endsWith('.sh')) { | ||
return | ||
} | ||
const originalUri = embeddedLanguageDocsManager.getOriginalUri(uri) | ||
if (originalUri === undefined) { | ||
return | ||
} | ||
const originalTextDocument = await vscode.workspace.openTextDocument(originalUri) | ||
await Promise.all([ | ||
setEmbeddedLanguageDocDiagnostics(originalTextDocument, 'bash'), | ||
setEmbeddedLanguageDocDiagnostics(originalTextDocument, 'python') | ||
]) | ||
} | ||
|
||
const setEmbeddedLanguageDocDiagnostics = async ( | ||
originalTextDocument: vscode.TextDocument, | ||
embeddedLanguageType: EmbeddedLanguageType | ||
): Promise<void> => { | ||
const embeddedLanguageDocInfos = embeddedLanguageDocsManager.getEmbeddedLanguageDocInfos( | ||
originalTextDocument.uri.toString(), | ||
embeddedLanguageType | ||
) | ||
if (embeddedLanguageDocInfos?.uri === undefined) { | ||
return | ||
} | ||
const embeddedLanguageDoc = await vscode.workspace.openTextDocument(embeddedLanguageDocInfos.uri.fsPath) | ||
const dirtyDiagnostics = vscode.languages.getDiagnostics(embeddedLanguageDocInfos.uri) | ||
const cleanDiagnostics: vscode.Diagnostic[] = [] | ||
dirtyDiagnostics.forEach((diagnostic) => { | ||
if (diagnostic.range === undefined) { | ||
cleanDiagnostics.push(diagnostic) | ||
} | ||
const newRange = getOriginalDocRange( | ||
originalTextDocument, | ||
embeddedLanguageDoc, | ||
embeddedLanguageDocInfos.characterIndexes, | ||
diagnostic.range | ||
) | ||
if (newRange === undefined) { | ||
return | ||
} | ||
const newDiagnostic = { | ||
...diagnostic, | ||
range: newRange | ||
} | ||
cleanDiagnostics.push(newDiagnostic) | ||
}) | ||
const diagnosticCollection = diagnosticCollections[embeddedLanguageType] | ||
diagnosticCollection.set(originalTextDocument.uri, cleanDiagnostics) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
integration-tests/project-folder/sources/meta-fixtures/diagnostics.bb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
python () { | ||
error() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) 2023 Savoir-faire Linux. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
|
||
import * as assert from 'assert' | ||
import * as vscode from 'vscode' | ||
import path from 'path' | ||
|
||
suite('Bitbake Diagnostics Test Suite', () => { | ||
const filePath = path.resolve(__dirname, '../../project-folder/sources/meta-fixtures/diagnostics.bb') | ||
const docUri = vscode.Uri.parse(`file://${filePath}`) | ||
|
||
suiteSetup(async function (this: Mocha.Context) { | ||
this.timeout(100000) | ||
const vscodeBitbake = vscode.extensions.getExtension('yocto-project.yocto-bitbake') | ||
if (vscodeBitbake === undefined) { | ||
assert.fail('Bitbake extension is not available') | ||
} | ||
await vscodeBitbake.activate() | ||
await vscode.workspace.openTextDocument(docUri) | ||
}) | ||
|
||
test('Diagnostics', async () => { | ||
await new Promise<vscode.Diagnostic[]>((resolve, reject) => { | ||
let nbChanges = 0 | ||
vscode.languages.onDidChangeDiagnostics((e) => { | ||
const diagnostics = vscode.languages.getDiagnostics(docUri) | ||
if (diagnostics.length > 0) { | ||
resolve(diagnostics) | ||
} | ||
if (nbChanges > 2) { | ||
reject(new Error('Waited too long for diagnostics')) | ||
} | ||
nbChanges++ | ||
}) | ||
}).then((diagnostics) => { | ||
assert.strictEqual(diagnostics.length, 1) | ||
assert.strictEqual(diagnostics[0].source, 'Pylance') | ||
}).catch((err) => { | ||
assert.fail(err) | ||
}) | ||
}).timeout(300000) | ||
}) |