Skip to content

Commit

Permalink
Fix: Handle "files.trimTrailingWhitespace" settings with embedded lan…
Browse files Browse the repository at this point in the history
…guages
  • Loading branch information
idillon-sfl committed Dec 14, 2023
1 parent f52d23e commit 2d81a2e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
1 change: 0 additions & 1 deletion client/src/language/EmbeddedLanguageDocsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export default class EmbeddedLanguageDocsManager {
const workspaceEdit = new WorkspaceEdit()
workspaceEdit.replace(uri, fullRange, embeddedLanguageDoc.content)
await workspace.applyEdit(workspaceEdit)
await document.save()
this.registerEmbeddedLanguageDocInfos(embeddedLanguageDoc, uri)
}

Expand Down
4 changes: 3 additions & 1 deletion integration-tests/project-folder/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
],
"python.analysis.extraPaths": [
"${workspaceFolder}/sources/poky/bitbake/lib"
]
],
"task.saveBeforeRun": "never",
"files.trimTrailingWhitespace": true, // Make sure it does not interfere with embedded language documents
}
50 changes: 43 additions & 7 deletions integration-tests/src/tests/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,73 @@
import * as assert from 'assert'
import * as vscode from 'vscode'
import path from 'path'
import { afterEach } from 'mocha'

suite('Bitbake Diagnostics Test Suite', () => {
const filePath = path.resolve(__dirname, '../../project-folder/sources/meta-fixtures/diagnostics.bb')
const docUri = vscode.Uri.parse(`file://${filePath}`)

let disposables: vscode.Disposable[] = []

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)
})

afterEach(function () {
for (const disposable of disposables) {
disposable.dispose()
}
disposables = []
})

test('Diagnostics', async () => {
await new Promise<vscode.Diagnostic[]>((resolve, reject) => {
void vscode.workspace.openTextDocument(docUri)
await new Promise<vscode.Diagnostic[]>((resolve) => {
let nbChanges = 0
vscode.languages.onDidChangeDiagnostics((e) => {
const disposable = vscode.languages.onDidChangeDiagnostics((e) => {
if (e.uris.some((uri) => uri.toString() === docUri.toString())) {
nbChanges++
}
const diagnostics = vscode.languages.getDiagnostics(docUri)
if (diagnostics.length > 0) {
if (nbChanges === 3) {
resolve(diagnostics)
}
if (nbChanges > 2) {
reject(new Error('Waited too long for diagnostics'))
})
disposables.push(disposable)
}).then((diagnostics) => {
assert.strictEqual(diagnostics.length, 1)
assert.strictEqual(diagnostics[0].source, 'Pylance')
assert.deepEqual(diagnostics[0].range, new vscode.Range(1, 4, 1, 9))
}).catch((err) => {
assert.fail(err)
})

// Test that the diagnostics are updated properly when the file is modified
// We previously had issues when vscode settings "files.trimTrailingWhitespace" is on
const workspaceEdit = new vscode.WorkspaceEdit()
workspaceEdit.insert(docUri, new vscode.Position(1, 0), '\n')
await vscode.workspace.applyEdit(workspaceEdit)
await new Promise<vscode.Diagnostic[]>((resolve) => {
let nbChanges = 0
const disposable = vscode.languages.onDidChangeDiagnostics((e) => {
if (e.uris.some((uri) => uri.toString() === docUri.toString())) {
nbChanges++
}
const diagnostics = vscode.languages.getDiagnostics(docUri)
if (nbChanges === 2) {
resolve(diagnostics)
}
nbChanges++
})
disposables.push(disposable)
}).then((diagnostics) => {
assert.strictEqual(diagnostics.length, 1)
assert.strictEqual(diagnostics[0].source, 'Pylance')
assert.deepEqual(diagnostics[0].range, new vscode.Range(2, 4, 2, 9))
}).catch((err) => {
assert.fail(err)
})
Expand Down

0 comments on commit 2d81a2e

Please sign in to comment.