|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +export class WitFormatter implements vscode.DocumentFormattingEditProvider { |
| 4 | + private static replaceAll(line: string, replacements: Array<[RegExp, string]>): string { |
| 5 | + let out = line; |
| 6 | + for (const [pattern, repl] of replacements) { |
| 7 | + out = out.replace(pattern, repl); |
| 8 | + } |
| 9 | + return out; |
| 10 | + } |
| 11 | + |
| 12 | + private static ensureSemicolon(line: string): string { |
| 13 | + return line.replace(/\s*;\s*$/, ";"); |
| 14 | + } |
| 15 | + |
| 16 | + private static collapseSpaces(line: string): string { |
| 17 | + return line.replace(/\s+/g, " "); |
| 18 | + } |
| 19 | + |
| 20 | + public provideDocumentFormattingEdits( |
| 21 | + document: vscode.TextDocument, |
| 22 | + options: vscode.FormattingOptions |
| 23 | + ): vscode.TextEdit[] { |
| 24 | + const text = document.getText(); |
| 25 | + const formatted = this.formatWitContent(text, options); |
| 26 | + if (formatted === text) { |
| 27 | + return []; |
| 28 | + } |
| 29 | + const fullRange = new vscode.Range(document.positionAt(0), document.positionAt(text.length)); |
| 30 | + return [vscode.TextEdit.replace(fullRange, formatted)]; |
| 31 | + } |
| 32 | + |
| 33 | + public formatWitContent(content: string, options: vscode.FormattingOptions): string { |
| 34 | + const tabSize = options.tabSize ?? 2; |
| 35 | + const insertSpaces = options.insertSpaces !== false; |
| 36 | + const indentUnit = insertSpaces ? " ".repeat(tabSize) : "\t"; |
| 37 | + const lines = content.split(/\r?\n/); |
| 38 | + const out: string[] = []; |
| 39 | + let indentLevel = 0; |
| 40 | + let inMultiLineTupleAlias = false; |
| 41 | + let aliasGenericDepth = 0; |
| 42 | + let inFuncParams = false; |
| 43 | + for (let i = 0; i < lines.length; i++) { |
| 44 | + const raw = lines[i]; |
| 45 | + const trimmed = raw.trim(); |
| 46 | + if (trimmed === "") { |
| 47 | + out.push(""); |
| 48 | + continue; |
| 49 | + } |
| 50 | + if (/^\}/.test(trimmed)) { |
| 51 | + indentLevel = Math.max(0, indentLevel - 1); |
| 52 | + } |
| 53 | + if (/^\/\//.test(trimmed)) { |
| 54 | + const extra = inMultiLineTupleAlias && aliasGenericDepth > 0 && !/^>>/.test(trimmed) ? 1 : 0; |
| 55 | + out.push(indentUnit.repeat(indentLevel + extra) + trimmed); |
| 56 | + continue; |
| 57 | + } |
| 58 | + const formattedLine = this.formatLine(trimmed); |
| 59 | + const needsTupleExtra = inMultiLineTupleAlias && aliasGenericDepth > 0 && !/^>>/.test(trimmed); |
| 60 | + const needsFuncParamExtra = inFuncParams && !/^\)/.test(trimmed); |
| 61 | + out.push( |
| 62 | + indentUnit.repeat(indentLevel + (needsTupleExtra ? 1 : 0) + (needsFuncParamExtra ? 1 : 0)) + |
| 63 | + formattedLine |
| 64 | + ); |
| 65 | + if (this.isOpeningBrace(trimmed)) { |
| 66 | + indentLevel++; |
| 67 | + } |
| 68 | + if (!inFuncParams && /func\($/.test(trimmed)) { |
| 69 | + let lookahead = i + 1; |
| 70 | + let activate = true; |
| 71 | + while (lookahead < lines.length) { |
| 72 | + const laTrim = lines[lookahead].trim(); |
| 73 | + if (laTrim === "") { |
| 74 | + lookahead++; |
| 75 | + continue; |
| 76 | + } |
| 77 | + if (/^\/\//.test(laTrim)) { |
| 78 | + activate = false; |
| 79 | + } |
| 80 | + break; |
| 81 | + } |
| 82 | + if (activate) { |
| 83 | + inFuncParams = true; |
| 84 | + } |
| 85 | + } else if (inFuncParams && /^\)/.test(trimmed)) { |
| 86 | + inFuncParams = false; |
| 87 | + } |
| 88 | + if (!inMultiLineTupleAlias && /^type\s+[^=]+=.*tuple<\s*$/.test(trimmed)) { |
| 89 | + inMultiLineTupleAlias = true; |
| 90 | + aliasGenericDepth = (trimmed.match(/</g) || []).length - (trimmed.match(/>/g) || []).length; |
| 91 | + } else if (inMultiLineTupleAlias) { |
| 92 | + const opens = (trimmed.match(/</g) || []).length; |
| 93 | + const closes = (trimmed.match(/>/g) || []).length; |
| 94 | + aliasGenericDepth += opens - closes; |
| 95 | + if (aliasGenericDepth <= 0) { |
| 96 | + inMultiLineTupleAlias = false; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return out.join("\n"); |
| 101 | + } |
| 102 | + |
| 103 | + private isOpeningBrace(line: string): boolean { |
| 104 | + return line.endsWith("{") && !line.includes("}"); |
| 105 | + } |
| 106 | + |
| 107 | + private formatLine(line: string): string { |
| 108 | + if (line.startsWith("package ")) return this.formatPackage(line); |
| 109 | + if (line.startsWith("interface ")) return this.formatNamedBlock(line); |
| 110 | + if (line.startsWith("world ")) return this.formatNamedBlock(line); |
| 111 | + if (this.isTypeDecl(line)) return this.formatNamedBlock(line); |
| 112 | + if (line.startsWith("type ") && line.includes("=")) return this.formatTypeAlias(line); |
| 113 | + if (this.isFuncDecl(line)) return this.formatFunc(line); |
| 114 | + if (line.startsWith("import ") || line.startsWith("export ")) return this.formatImportExport(line); |
| 115 | + if (line.startsWith("use ")) return this.formatUse(line); |
| 116 | + if (this.isFieldDecl(line)) return this.formatField(line); |
| 117 | + return line; |
| 118 | + } |
| 119 | + |
| 120 | + private isTypeDecl(line: string): boolean { |
| 121 | + return /^(record|variant|enum|flags|resource)\s+/.test(line); |
| 122 | + } |
| 123 | + private isFuncDecl(line: string): boolean { |
| 124 | + if (line.startsWith("import ") || line.startsWith("export ")) return false; |
| 125 | + return /^[a-zA-Z][\w-]*\s*:\s*func\b/.test(line) || /:\s*func\b/.test(line) || /->/.test(line); |
| 126 | + } |
| 127 | + private isFieldDecl(line: string): boolean { |
| 128 | + const t = line.trim(); |
| 129 | + return /^[a-zA-Z][a-zA-Z0-9-]*\s*[:,(]/.test(t) || /^[a-zA-Z][a-zA-Z0-9-]*\s*,?\s*$/.test(t); |
| 130 | + } |
| 131 | + |
| 132 | + private formatPackage(line: string): string { |
| 133 | + return WitFormatter.ensureSemicolon(WitFormatter.collapseSpaces(line)); |
| 134 | + } |
| 135 | + private formatNamedBlock(line: string): string { |
| 136 | + return WitFormatter.collapseSpaces(line).replace(/\s*{\s*$/, " {"); |
| 137 | + } |
| 138 | + private formatFunc(line: string): string { |
| 139 | + const replacements: Array<[RegExp, string]> = [ |
| 140 | + [/:func/, ": func"], |
| 141 | + [/:\s*func/, ": func"], |
| 142 | + [/func\s*\(/, "func("], |
| 143 | + [/\)\s*->\s*/, ") -> "], |
| 144 | + [/\)->\s*/, ") -> "], |
| 145 | + [/\)->/, ") -> "], |
| 146 | + [/,\s*/g, ", "], |
| 147 | + [/:\s*/g, ": "], |
| 148 | + ]; |
| 149 | + return WitFormatter.ensureSemicolon(WitFormatter.replaceAll(line, replacements)); |
| 150 | + } |
| 151 | + private formatImportExport(line: string): string { |
| 152 | + const base = line.replace(/^(import|export)\s+/, "$1 "); |
| 153 | + if (base.includes(": func") || base.includes(":func")) return this.formatFunc(base); |
| 154 | + return WitFormatter.ensureSemicolon(base); |
| 155 | + } |
| 156 | + private formatUse(line: string): string { |
| 157 | + const replacements: Array<[RegExp, string]> = [ |
| 158 | + [/^use\s+/, "use "], |
| 159 | + [/\s+as\s+/, " as "], |
| 160 | + [/\s+from\s+/, " from "], |
| 161 | + ]; |
| 162 | + return WitFormatter.ensureSemicolon(WitFormatter.replaceAll(line, replacements)); |
| 163 | + } |
| 164 | + private formatTypeAlias(line: string): string { |
| 165 | + const replacements: Array<[RegExp, string]> = [ |
| 166 | + [/^type\s+/, "type "], |
| 167 | + [/\s*=\s*/, " = "], |
| 168 | + ]; |
| 169 | + return WitFormatter.ensureSemicolon(WitFormatter.replaceAll(line, replacements)); |
| 170 | + } |
| 171 | + private formatField(line: string): string { |
| 172 | + const replacements: Array<[RegExp, string]> = [ |
| 173 | + [/:\s*/g, ": "], |
| 174 | + [/,\s*/g, ", "], |
| 175 | + [/,\s*$/, ","], |
| 176 | + ]; |
| 177 | + return WitFormatter.replaceAll(line, replacements); |
| 178 | + } |
| 179 | +} |
0 commit comments