Last active
June 23, 2021 01:00
-
-
Save gosoccerboy5/5d84d9ad08adbf16afc57b847e443e23 to your computer and use it in GitHub Desktop.
A recreation of replace-indentation.html on my formatting-help repository.
This file contains 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
import "dart:html"; | |
final input = querySelector("#input") as TextAreaElement; | |
final output = querySelector("#output") as TextAreaElement; | |
final button = querySelector("#format") as ButtonElement; | |
final number = querySelector("#spaces") as InputElement; | |
final oldNumber = querySelector("#oldspaces") as InputElement; | |
// Setup the variables | |
void main() { | |
input.focus(); // Allow instant pasting of code | |
final inputOnInput = (Event event) { | |
final String? match = | |
RegExp(r"(^|\n) +").firstMatch(input.value as String)?[0]; | |
oldNumber.value = (match == null | |
? 2 | |
: (match.length - ( | |
RegExp("^ +").hasMatch(input.value!.split("\n")[0]) ? 0 : 1)) | |
).toString(); | |
number.value = oldNumber.value == "4" ? "2" : "4"; | |
}; // We make this a variable so we can call it | |
input.addEventListener("input", inputOnInput); // Add the event listener for the input element | |
inputOnInput(Event("input")); // We want to call the function automatically | |
button.addEventListener("click", (event) { | |
output.value = (input.value?.replaceAllMapped( | |
RegExp(r"(\n|^) +"), // Matches newline/string-start, then a positive number of spaces | |
(n) => | |
"\n" + // we still want a newline | |
" " * (n[0]!.substring(1).length * | |
int.parse(number.value!) / | |
int.parse(oldNumber.value!) | |
// Mathy stuff that gets the right amount of indentation | |
).floor() // We can't multiply a string by a decimal number | |
) | |
); | |
}); | |
} |
This file contains 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
<style> | |
textarea { | |
width: 500px; | |
height: 200px; | |
} | |
html { | |
color: white; | |
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Google Sans", Roboto, Ubuntu, sans-serif; | |
} | |
</style> | |
<h1>Do you have to deal with formatters that give you an indentation level you don't like?</h1> | |
<h2>Well this is the tool for you!</h2> | |
<p>Note: doesnt work for tabs</p> | |
<p>Just paste your code into the top area, set the amount of spaces you want to get to, and click the button!</p> | |
<textarea id="input"></textarea> | |
<br> | |
<button id="format">Format with spaces</button> | |
<br> | |
From <input id="oldspaces" type="number"> spaces to <input id="spaces" type="number"> spaces | |
<br> | |
<textarea id="output"></textarea> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://dartpad.dev/5d84d9ad08adbf16afc57b847e443e23?null_safety=true
Created with ❤️ by Dartpad, which allows you to run small Dart applications, even interacting with HTML.