Skip to content

Commit 86eb538

Browse files
CopilotGordonSmith
andcommitted
Implement WIT document formatter with comprehensive tests
Co-authored-by: GordonSmith <966863+GordonSmith@users.noreply.github.com>
1 parent c91b391 commit 86eb538

File tree

4 files changed

+726
-0
lines changed

4 files changed

+726
-0
lines changed

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@
181181
"command": "wit-idl.extractCoreWasm",
182182
"title": "Extract Core Wasm",
183183
"category": "WIT"
184+
},
185+
{
186+
"command": "wit-idl.formatDocument",
187+
"title": "Format Document",
188+
"category": "WIT"
184189
}
185190
],
186191
"submenus": [
@@ -196,6 +201,11 @@
196201
"when": "resourceExtname == .wit",
197202
"group": "navigation"
198203
},
204+
{
205+
"command": "wit-idl.formatDocument",
206+
"when": "resourceExtname == .wit",
207+
"group": "1_modification@10"
208+
},
199209
{
200210
"submenu": "wit-idl.generateBindings.submenu",
201211
"when": "resourceExtname == .wit || witIdl.isWasmComponent",
@@ -289,6 +299,10 @@
289299
{
290300
"command": "wit-idl.extractCoreWasm",
291301
"when": "witIdl.isWasmComponent"
302+
},
303+
{
304+
"command": "wit-idl.formatDocument",
305+
"when": "editorLangId == wit"
292306
}
293307
]
294308
},
@@ -301,6 +315,11 @@
301315
{
302316
"command": "wit-idl.syntaxCheckWorkspace",
303317
"key": "shift+f7"
318+
},
319+
{
320+
"command": "wit-idl.formatDocument",
321+
"key": "shift+alt+f",
322+
"when": "editorTextFocus && editorLangId == wit"
304323
}
305324
],
306325
"customEditors": [

src/extension.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
22
import * as path from "node:path";
33
import * as fs from "node:fs";
44
import { WitSyntaxValidator } from "./validator.js";
5+
import { WitFormatter } from "./formatter.js";
56
import { isWasmComponentFile } from "./wasmDetection.js";
67
import {
78
getWitBindgenVersionFromWasm,
@@ -319,6 +320,34 @@ export function activate(context: vscode.ExtensionContext) {
319320
}
320321
});
321322

323+
// Register WIT formatter
324+
const witFormatter = new WitFormatter();
325+
const formattingProvider = vscode.languages.registerDocumentFormattingEditProvider("wit", witFormatter);
326+
327+
// Register format document command
328+
const formatDocumentCommand = vscode.commands.registerCommand("wit-idl.formatDocument", async () => {
329+
const activeEditor = vscode.window.activeTextEditor;
330+
331+
if (!activeEditor) {
332+
vscode.window.showWarningMessage("No active editor found");
333+
return;
334+
}
335+
336+
if (activeEditor.document.languageId !== "wit") {
337+
vscode.window.showWarningMessage("Active file is not a WIT file");
338+
return;
339+
}
340+
341+
try {
342+
await vscode.commands.executeCommand("editor.action.formatDocument");
343+
} catch (error) {
344+
console.error("Failed to format document:", error);
345+
vscode.window.showErrorMessage(
346+
`Failed to format document: ${error instanceof Error ? error.message : String(error)}`
347+
);
348+
}
349+
});
350+
322351
// Implement extractWit command
323352
const extractWitCommand = vscode.commands.registerCommand("wit-idl.extractWit", async (resource?: vscode.Uri) => {
324353
try {
@@ -725,9 +754,11 @@ export function activate(context: vscode.ExtensionContext) {
725754
providerDisposable,
726755
witExtractProvider,
727756
provider,
757+
formattingProvider,
728758
syntaxCheckCommand,
729759
syntaxCheckWorkspaceCommand,
730760
showVersionCommand,
761+
formatDocumentCommand,
731762
extractWitCommand,
732763
extractCoreWasmCommand,
733764
generateRustBindingsCommand,

0 commit comments

Comments
 (0)