This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
129 additions
and
32 deletions.
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
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,65 @@ | ||
import { ReplaySubject, firstValueFrom, using } from 'rxjs' | ||
import { Range, TextEdit, languages } from 'vscode' | ||
import type { URI } from 'vscode-uri' | ||
import type { ExecError, StdErrError } from '../run' | ||
import { run } from '../run' | ||
import { getToolCommand$ } from '../tool-utils' | ||
import type { SuccessResult } from '../types' | ||
import { type ErrorResult } from '../types' | ||
|
||
export function registerQmlFormatter$({ | ||
extensionUri, | ||
}: { | ||
readonly extensionUri: URI | ||
}) { | ||
const formatResult$ = new ReplaySubject< | ||
SuccessResult<string> | ErrorResult<'NotFound'> | ExecError | StdErrError | ||
>(1) | ||
|
||
return using( | ||
() => { | ||
const disposable = languages.registerDocumentFormattingEditProvider( | ||
'qml', | ||
{ | ||
async provideDocumentFormattingEdits(document) { | ||
const getToolCommandResult = await firstValueFrom( | ||
getToolCommand$({ | ||
tool: 'qmlformat', | ||
extensionUri, | ||
resource: document.uri, | ||
}), | ||
) | ||
if (getToolCommandResult.kind !== 'Success') { | ||
formatResult$.next(getToolCommandResult) | ||
return [] | ||
} | ||
|
||
const { command, options } = getToolCommandResult.value | ||
const runResult = await run({ | ||
command: [...command, ...options, document.uri.fsPath], | ||
}) | ||
if (runResult.kind !== 'Success') { | ||
formatResult$.next(runResult) | ||
return [] | ||
} | ||
|
||
const formatted = runResult.value.stdout | ||
const fullRange = document.validateRange( | ||
new Range( | ||
document.lineAt(0).range.start, | ||
document.lineAt(document.lineCount - 1).range.end, | ||
), | ||
) | ||
formatResult$.next({ | ||
kind: 'Success', | ||
value: `Formatted ${document.uri.fsPath}`, | ||
}) | ||
return [TextEdit.replace(fullRange, formatted)] | ||
}, | ||
}, | ||
) | ||
return { unsubscribe: () => disposable.dispose() } | ||
}, | ||
() => formatResult$.asObservable(), | ||
) | ||
} |
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,52 @@ | ||
import * as assert from 'node:assert' | ||
import * as path from 'node:path' | ||
import type { TextDocument, TextEdit } from 'vscode' | ||
import { WorkspaceEdit, commands, window, workspace } from 'vscode' | ||
import { URI } from 'vscode-uri' | ||
import { | ||
E2E_TIMEOUT, | ||
TEST_ASSETS_PATH, | ||
setupE2EEnvironment, | ||
} from '../test-utils' | ||
|
||
suite('format-qml/e2e', () => { | ||
suiteSetup(async function () { | ||
this.timeout(E2E_TIMEOUT) | ||
await setupE2EEnvironment() | ||
}) | ||
|
||
suite('when a qml file is open', () => { | ||
const sampleFilenameNoExt = 'unformatted' | ||
let document: TextDocument | ||
|
||
setup(async function () { | ||
this.timeout(E2E_TIMEOUT) | ||
|
||
document = await workspace.openTextDocument( | ||
URI.file( | ||
path.resolve(TEST_ASSETS_PATH, 'qml', `${sampleFilenameNoExt}.qml`), | ||
), | ||
) | ||
await window.showTextDocument(document) | ||
}) | ||
|
||
teardown(async function () { | ||
this.timeout(E2E_TIMEOUT) | ||
await commands.executeCommand('workbench.action.closeActiveEditor') | ||
}) | ||
|
||
test('should be able to run formatQml command', async () => { | ||
const originalContent = document.getText() | ||
const edits: TextEdit[] = await commands.executeCommand( | ||
'vscode.executeFormatDocumentProvider', | ||
document.uri, | ||
) | ||
|
||
const workspaceEdit = new WorkspaceEdit() | ||
workspaceEdit.set(document.uri, edits) | ||
await workspace.applyEdit(workspaceEdit) | ||
|
||
return assert.notDeepStrictEqual(originalContent, document.getText()) | ||
}).timeout(E2E_TIMEOUT) | ||
}) | ||
}).timeout(E2E_TIMEOUT) |
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