Last active
April 26, 2023 12:06
-
-
Save colelawrence/f1625b8fc733554055b7819eba43de68 to your computer and use it in GitHub Desktop.
Format slint files using Deno
This file contains hidden or 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
const ignore: string[] = [ | |
// "ui/generated.slint" | |
]; | |
const dirs = { ui: "ui" }; | |
const whitespaceRE = /^(\s*)(.*?)(\/*|\/\/)?(.*)$/; | |
const indentBy = " "; | |
file: for await (const file of Deno.readDir(dirs.ui)) { | |
if ( | |
file.isFile && | |
file.name.endsWith(".slint") && | |
!ignore.find((ignored) => ignored.endsWith(file.name)) | |
) { | |
const filename = `${dirs.ui}/${file.name}`; | |
const sourceText = await Deno.readTextFile(filename); | |
let indentation = 0; | |
let changes = 0; | |
const formatedLines: string[] = []; | |
for (const [line, ln] of sourceText | |
.split(/\n/g) | |
.map((line, idx) => [line, idx + 1] as const)) { | |
const found = whitespaceRE.exec(line); | |
if (!found) { | |
console.error( | |
`Unparsed line ${JSON.stringify(line)}, skipping ${filename}:${ln}` | |
); | |
continue file; | |
} | |
const [, _whitespace, beforeComment, hasComment, afterComment] = found; | |
if (hasComment && hasComment !== "//") { | |
console.error( | |
`Unable to handle comments ${JSON.stringify( | |
line | |
)}, skipping ${filename}:${ln}` | |
); | |
continue file; | |
} | |
// console.log({ | |
// beforeComment, | |
// hasComment, | |
// afterComment, | |
// }) | |
let rmIndentBy = 0; | |
let addIndentBy = 0; | |
let closesBy = 0; | |
let seenNonCloser = false; | |
for (const c of (hasComment ? beforeComment : afterComment).split("")) { | |
if (c === "}" || c === ")" || c === "]") { | |
rmIndentBy += 1; | |
if (!seenNonCloser) { | |
closesBy += 1; | |
} | |
} else { | |
seenNonCloser = true; | |
if (c === "{" || c === "(" || c === "[") addIndentBy += 1; | |
} | |
} | |
const lineContent = ( | |
hasComment ? beforeComment + hasComment + afterComment : afterComment | |
).trimStart(); | |
const modifiedLine = lineContent | |
? indentBy.repeat(indentation - closesBy) + lineContent | |
: ""; // empty line is no spaces | |
const changeIndentation = addIndentBy - rmIndentBy; | |
if (changeIndentation) { | |
indentation += changeIndentation; | |
// console.error(`Unable to change indentation by (${changeIndentation}) ${JSON.stringify(line)}, skipping ${filename}:${ln}`) | |
// continue file | |
} | |
if (modifiedLine !== line) changes++; | |
formatedLines.push(modifiedLine); | |
} | |
if (changes > 0) { | |
const updatedFile = formatedLines.join("\n"); | |
console.log(`Updates for ${filename}\n`, updatedFile); | |
const encoder = new TextEncoder(); | |
await Deno.writeFile(filename, encoder.encode(updatedFile)); | |
} else { | |
console.log(`No updates for ${filename}`); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment