Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Set default behavior to automatically normalize line endings.
*.wit text eol=lf
*.snap text eol=lf
*.ts text eol=lf
*.json text eol=lf
*.mjs text eol=lf
Cargo.toml text eol=lf
README.md text eol=lf
LICENSE text eol=lf# Set default behavior to automatically normalize line endings.
* text=auto

19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@
"command": "wit-idl.extractCoreWasm",
"title": "Extract Core Wasm",
"category": "WIT"
},
{
"command": "wit-idl.formatDocument",
"title": "Format Document",
"category": "WIT"
}
],
"submenus": [
Expand All @@ -196,6 +201,11 @@
"when": "resourceExtname == .wit",
"group": "navigation"
},
{
"command": "wit-idl.formatDocument",
"when": "resourceExtname == .wit",
"group": "1_modification@10"
},
{
"submenu": "wit-idl.generateBindings.submenu",
"when": "resourceExtname == .wit || witIdl.isWasmComponent",
Expand Down Expand Up @@ -289,6 +299,10 @@
{
"command": "wit-idl.extractCoreWasm",
"when": "witIdl.isWasmComponent"
},
{
"command": "wit-idl.formatDocument",
"when": "editorLangId == wit"
}
]
},
Expand All @@ -301,6 +315,11 @@
{
"command": "wit-idl.syntaxCheckWorkspace",
"key": "shift+f7"
},
{
"command": "wit-idl.formatDocument",
"key": "shift+alt+f",
"when": "editorTextFocus && editorLangId == wit"
}
],
"customEditors": [
Expand Down
177 changes: 177 additions & 0 deletions scripts/format-test-wit-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";

function replaceAllSeq(line, replacements) {
let out = line;
for (const [re, repl] of replacements) out = out.replace(re, repl);
return out;
}
function ensureSemicolon(line) {
return line.replace(/\s*;\s*$/, ";");
}
function collapseSpaces(line) {
return line.replace(/\s+/g, " ");
}

function isOpeningBrace(line) {
return line.endsWith("{") && !line.includes("}");
}

function formatFunc(line) {
const replacements = [
[/:func/, ": func"],
[/:\s*func/, ": func"],
[/func\s*\(/, "func("],
[/\)\s*->\s*/, ") -> "],
[/\)->\s*/, ") -> "],
[/\)->/, ") -> "],
[/,\s*/g, ", "],
[/:\s*/g, ": "],
];
return ensureSemicolon(replaceAllSeq(line, replacements));
}
function formatPackage(line) {
return ensureSemicolon(collapseSpaces(line));
}
function formatNamedBlock(line) {
return collapseSpaces(line).replace(/\s*{\s*$/, " {");
}
function formatImportExport(line) {
const base = line.replace(/^(import|export)\s+/, "$1 ");
return base.includes(": func") || base.includes(":func") ? formatFunc(base) : ensureSemicolon(base);
}
function formatUse(line) {
const repl = [
[/^use\s+/, "use "],
[/\s+as\s+/, " as "],
[/\s+from\s+/, " from "],
];
return ensureSemicolon(replaceAllSeq(line, repl));
}
function formatTypeAlias(line) {
const repl = [
[/^type\s+/, "type "],
[/\s*=\s*/, " = "],
];
return ensureSemicolon(replaceAllSeq(line, repl));
}
function formatField(line) {
return replaceAllSeq(line, [
[/::?/g, (m) => (m.endsWith(":") ? ": " : m)],
[/:\s*/g, ": "],
[/,\s*/g, ", "],
[/,\s*$/, ","],
]);
}
function isTypeDecl(line) {
return /^(record|variant|enum|flags|resource)\s+/.test(line);
}
function isFuncDecl(line) {
if (line.startsWith("import ") || line.startsWith("export ")) return false;
return /^[a-zA-Z][\w-]*\s*:\s*func\b/.test(line) || /:\s*func\b/.test(line) || /->/.test(line);
}
function isFieldDecl(line) {
const t = line.trim();
return /^[a-zA-Z][a-zA-Z0-9-]*\s*[:,(]/.test(t) || /^[a-zA-Z][a-zA-Z0-9-]*\s*,?\s*$/.test(t);
}

function formatLine(line) {
if (line.startsWith("package ")) return formatPackage(line);
if (line.startsWith("interface ")) return formatNamedBlock(line);
if (line.startsWith("world ")) return formatNamedBlock(line);
if (isTypeDecl(line)) return formatNamedBlock(line);
if (line.startsWith("type ") && line.includes("=")) return formatTypeAlias(line);
if (isFuncDecl(line)) return formatFunc(line);
if (line.startsWith("import ") || line.startsWith("export ")) return formatImportExport(line);
if (line.startsWith("use ")) return formatUse(line);
if (isFieldDecl(line)) return formatField(line);
return line;
}

function formatContent(content, tabSize = 2, insertSpaces = true) {
const indentUnit = insertSpaces ? " ".repeat(tabSize) : "\t";
const lines = content.split(/\r?\n/);
const out = [];
let indentLevel = 0;
let inMultiLineTupleAlias = false;
let aliasGenericDepth = 0;
let inFuncParams = false;
for (let i = 0; i < lines.length; i++) {
const raw = lines[i];
const trimmed = raw.trim();
if (trimmed === "") {
out.push("");
continue;
}
if (/^\}/.test(trimmed)) indentLevel = Math.max(0, indentLevel - 1);
if (/^\/\//.test(trimmed)) {
const extra = inMultiLineTupleAlias && aliasGenericDepth > 0 && !/^>>/.test(trimmed) ? 1 : 0;
out.push(indentUnit.repeat(indentLevel + extra) + trimmed);
continue;
}
const formattedLine = formatLine(trimmed);
const needsTupleExtra = inMultiLineTupleAlias && aliasGenericDepth > 0 && !/^>>/.test(trimmed);
const needsFuncParamExtra = inFuncParams && !/^\)/.test(trimmed);
out.push(
indentUnit.repeat(indentLevel + (needsTupleExtra ? 1 : 0) + (needsFuncParamExtra ? 1 : 0)) + formattedLine
);
if (isOpeningBrace(trimmed)) indentLevel++;
if (!inFuncParams && /func\($/.test(trimmed)) {
let lookahead = i + 1;
let activate = true;
while (lookahead < lines.length) {
const laTrim = lines[lookahead].trim();
if (laTrim === "") {
lookahead++;
continue;
}
if (/^\/\//.test(laTrim)) {
activate = false;
}
break;
}
if (activate) inFuncParams = true;
} else if (inFuncParams && /^\)/.test(trimmed)) {
inFuncParams = false;
}
if (!inMultiLineTupleAlias && /^type\s+[^=]+=.*tuple<\s*$/.test(trimmed)) {
inMultiLineTupleAlias = true;
aliasGenericDepth = (trimmed.match(/</g) || []).length - (trimmed.match(/>/g) || []).length;
} else if (inMultiLineTupleAlias) {
const opens = (trimmed.match(/</g) || []).length;
const closes = (trimmed.match(/>/g) || []).length;
aliasGenericDepth += opens - closes;
if (aliasGenericDepth <= 0) inMultiLineTupleAlias = false;
}
}
return out.join("\n");
}

function walk(dir, acc) {
for (const entry of fs.readdirSync(dir)) {
const full = path.join(dir, entry);
const stat = fs.statSync(full);
if (stat.isDirectory()) walk(full, acc);
else if (entry.endsWith(".wit")) acc.push(full);
}
return acc;
}

const root = path.resolve("tests");
if (!fs.existsSync(root)) {
console.error("tests directory not found");
process.exit(1);
}
const files = walk(root, []);
let changed = 0;
for (const f of files) {
const original = fs.readFileSync(f, "utf8");
const formatted = formatContent(original);
if (formatted !== original) {
fs.writeFileSync(f, formatted, "utf8");
changed++;
console.log("Formatted", path.relative(process.cwd(), f));
}
}
console.log(`Processed ${files.length} .wit files. Changed ${changed}.`);
Loading
Loading