Skip to content

Commit 321ab0c

Browse files
CopilotGordonSmith
andcommitted
Fix code formatting and finalize WIT formatter implementation
Co-authored-by: GordonSmith <966863+GordonSmith@users.noreply.github.com>
1 parent 86eb538 commit 321ab0c

File tree

2 files changed

+108
-140
lines changed

2 files changed

+108
-140
lines changed

src/formatter.ts

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
1414
): vscode.TextEdit[] {
1515
const text = document.getText();
1616
const formatted = this.formatWitContent(text, options);
17-
17+
1818
if (formatted === text) {
1919
return []; // No changes needed
2020
}
21-
22-
const fullRange = new vscode.Range(
23-
document.positionAt(0),
24-
document.positionAt(text.length)
25-
);
26-
21+
22+
const fullRange = new vscode.Range(document.positionAt(0), document.positionAt(text.length));
23+
2724
return [vscode.TextEdit.replace(fullRange, formatted)];
2825
}
2926

@@ -35,32 +32,32 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
3532
const formatted: string[] = [];
3633
let indentLevel = 0;
3734
const indentString = options.insertSpaces ? " ".repeat(options.tabSize) : "\t";
38-
35+
3936
for (let i = 0; i < lines.length; i++) {
4037
const line = lines[i];
4138
const trimmed = line.trim();
42-
39+
4340
// Skip empty lines and preserve them
4441
if (trimmed === "") {
4542
formatted.push("");
4643
continue;
4744
}
48-
45+
4946
// Handle closing braces - decrease indent before processing
5047
if (trimmed === "}" || trimmed === "},") {
5148
indentLevel = Math.max(0, indentLevel - 1);
5249
}
53-
50+
5451
// Format the line with proper indentation and spacing
5552
const formattedLine = this.formatLine(trimmed, indentLevel, indentString);
5653
formatted.push(formattedLine);
57-
54+
5855
// Handle opening braces - increase indent after processing
5956
if (this.isOpeningBrace(trimmed)) {
6057
indentLevel++;
6158
}
6259
}
63-
60+
6461
return formatted.join("\n");
6562
}
6663

@@ -69,20 +66,20 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
6966
*/
7067
private formatLine(line: string, indentLevel: number, indentString: string): string {
7168
const indent = indentString.repeat(indentLevel);
72-
69+
7370
// Handle comments - preserve as-is but with proper indentation
7471
if (line.startsWith("//") || line.startsWith("/*") || line.startsWith("*") || line.startsWith("*/")) {
7572
return indent + line;
7673
}
77-
74+
7875
// Handle doc comments
7976
if (line.startsWith("///")) {
8077
return indent + line;
8178
}
82-
79+
8380
// Format different WIT constructs
8481
let formatted = line;
85-
82+
8683
// Package declaration
8784
if (line.startsWith("package ")) {
8885
formatted = this.formatPackageDeclaration(line);
@@ -119,7 +116,7 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
119116
else if (this.isFieldDeclaration(line)) {
120117
formatted = this.formatFieldDeclaration(line);
121118
}
122-
119+
123120
return indent + formatted;
124121
}
125122

@@ -177,35 +174,40 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
177174
if (line.startsWith("import ") || line.startsWith("export ")) {
178175
return false;
179176
}
180-
181-
return line.includes(":func(") || line.includes(": func(") ||
182-
line.includes(":func()") || line.includes(": func()") ||
183-
line.includes(": func ") || line.endsWith(": func;") ||
184-
line.includes("->") || // Return type indicator
185-
/:\s*func\b/.test(line); // More general func detection
177+
178+
return (
179+
line.includes(":func(") ||
180+
line.includes(": func(") ||
181+
line.includes(":func()") ||
182+
line.includes(": func()") ||
183+
line.includes(": func ") ||
184+
line.endsWith(": func;") ||
185+
line.includes("->") || // Return type indicator
186+
/:\s*func\b/.test(line)
187+
); // More general func detection
186188
}
187189

188190
/**
189191
* Format function declarations
190192
*/
191193
private formatFunctionDeclaration(line: string): string {
192194
let formatted = line;
193-
195+
194196
// Add space after colon if missing
195197
formatted = formatted.replace(/:func/, ": func");
196198
formatted = formatted.replace(/:\s*func/, ": func");
197-
199+
198200
// Format function parameters and return types
199201
formatted = formatted.replace(/func\s*\(/, "func(");
200202
formatted = formatted.replace(/\)\s*->\s*/, ") -> ");
201203
formatted = formatted.replace(/\)->\s*/, ") -> ");
202204
formatted = formatted.replace(/\)->/, ") -> ");
203205
formatted = formatted.replace(/,\s*/g, ", ");
204206
formatted = formatted.replace(/:\s*/g, ": ");
205-
207+
206208
// Handle trailing semicolon
207209
formatted = formatted.replace(/\s*;\s*$/, ";");
208-
210+
209211
return formatted;
210212
}
211213

@@ -214,25 +216,27 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
214216
*/
215217
private isFieldDeclaration(line: string): boolean {
216218
// Field declarations typically have: name: type, or name(type), or just name,
217-
return /^\s*[a-zA-Z][a-zA-Z0-9-]*\s*[:,(]/.test(line.trim()) ||
218-
/^\s*[a-zA-Z][a-zA-Z0-9-]*\s*,?\s*$/.test(line.trim());
219+
return (
220+
/^\s*[a-zA-Z][a-zA-Z0-9-]*\s*[:,(]/.test(line.trim()) ||
221+
/^\s*[a-zA-Z][a-zA-Z0-9-]*\s*,?\s*$/.test(line.trim())
222+
);
219223
}
220224

221225
/**
222226
* Format field declarations
223227
*/
224228
private formatFieldDeclaration(line: string): string {
225229
let formatted = line;
226-
230+
227231
// Add space after colon
228232
formatted = formatted.replace(/:\s*/g, ": ");
229-
233+
230234
// Add space after comma
231235
formatted = formatted.replace(/,\s*/g, ", ");
232-
236+
233237
// Handle trailing comma
234238
formatted = formatted.replace(/,\s*$/, ",");
235-
239+
236240
return formatted;
237241
}
238242

@@ -241,27 +245,27 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
241245
*/
242246
private formatImportExport(line: string): string {
243247
let formatted = line;
244-
248+
245249
// Normalize spaces after import/export keyword
246250
formatted = formatted.replace(/^(import|export)\s+/, "$1 ");
247-
251+
248252
// If this is a function export/import, format it completely here
249253
if (formatted.includes(": func") || formatted.includes(":func")) {
250254
// Add space after colon if missing
251255
formatted = formatted.replace(/:func/, ": func");
252256
formatted = formatted.replace(/:\s*func/, ": func");
253-
257+
254258
// Format function parameters and return types
255259
formatted = formatted.replace(/func\s*\(/, "func(");
256260
formatted = formatted.replace(/\)\s*->\s*/, ") -> ");
257261
formatted = formatted.replace(/\)->\s*/, ") -> ");
258262
formatted = formatted.replace(/\)->/, ") -> ");
259263
formatted = formatted.replace(/,\s*/g, ", ");
260264
}
261-
265+
262266
// Handle trailing semicolon
263267
formatted = formatted.replace(/\s*;\s*$/, ";");
264-
268+
265269
return formatted;
266270
}
267271

@@ -270,17 +274,17 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
270274
*/
271275
private formatUseStatement(line: string): string {
272276
let formatted = line;
273-
277+
274278
// Add space after use
275279
formatted = formatted.replace(/^use\s+/, "use ");
276-
280+
277281
// Format as/from keywords
278282
formatted = formatted.replace(/\s+as\s+/, " as ");
279283
formatted = formatted.replace(/\s+from\s+/, " from ");
280-
284+
281285
// Handle trailing semicolon
282286
formatted = formatted.replace(/\s*;\s*$/, ";");
283-
287+
284288
return formatted;
285289
}
286290

@@ -289,18 +293,18 @@ export class WitFormatter implements vscode.DocumentFormattingEditProvider {
289293
*/
290294
private formatTypeAlias(line: string): string {
291295
let formatted = line;
292-
296+
293297
// Format type keyword
294298
if (formatted.startsWith("type ")) {
295299
formatted = formatted.replace(/^type\s+/, "type ");
296300
}
297-
301+
298302
// Format equals sign
299303
formatted = formatted.replace(/\s*=\s*/, " = ");
300-
304+
301305
// Handle trailing semicolon
302306
formatted = formatted.replace(/\s*;\s*$/, ";");
303-
307+
304308
return formatted;
305309
}
306-
}
310+
}

0 commit comments

Comments
 (0)