Skip to content

Instantly share code, notes, and snippets.

@gosoccerboy5
Last active June 24, 2021 16:08
Show Gist options
  • Save gosoccerboy5/6081062503e8b6154177ff1eb7ab1d21 to your computer and use it in GitHub Desktop.
Save gosoccerboy5/6081062503e8b6154177ff1eb7ab1d21 to your computer and use it in GitHub Desktop.
A recreation of curly-brackets.html on my formatting-help repository.
import "dart:html";
final input = querySelector("#input") as TextAreaElement;
final output = querySelector("#output") as TextAreaElement;
final btn = querySelector("#button") as ButtonElement;
final radio = querySelector("#sameline") as RadioButtonInputElement;
// Setup elements
void main() {
btn.addEventListener("click", (Event event) {
if (radio.checked as bool) {
output.value = input.value?.replaceAll(RegExp(" *\n *{"), " {");
// Remove the newline
} else {
output.value = input.value?.replaceAllMapped(
RegExp(r"^.*{\s*$", multiLine: true),
(n) {
var m = (n[0] as String).trimRight(); // m stands for match
return (
m.substring(0, m.length - 1) + "\n" +
(RegExp(r"^\s?").firstMatch(m)![0]! *
RegExp(r"^\s*").firstMatch(m)![0]!.length) +
"{"
);
// All we do is return a curly bracket on a new line
// That's indented on the same level as the last statement
}
);
}
}); // Onclick event for the button
input.focus(); // Instantly focus on the input area
}
<h1>Change placement of curly brackets</h1>
<h2>Are you ever annoyed when someone places their curly brackets/braces on a newline, <br>but you like them on the same line?</h2>
<p>Well this is for you!</p>
<textarea id="input" placeholder="Input text here..."></textarea>
<button id="button">Change curly bracket placement</button>
<br>
<div>
<input type="radio" name="select" id="sameline" checked>
<label for="sameline">Move curly bracket to same line</label>
</div>
<div>
<input type="radio" name="select" id="newline">
<label for="newline">Move curly bracket to new line</label>
</div>
<br>
<textarea id="output" placeholder="Output here."></textarea>
<style>
textarea {
width: 500px;
height: 200px;
}
body {
margin: 10px;
font-family: sans-serif;
tab-size: 4;
-moz-tab-size: 4;
-webkit-tab-size: 4;
}
</style>
@gosoccerboy5
Copy link
Author

https://dartpad.dev/6081062503e8b6154177ff1eb7ab1d21?null_safety=true

Made with ❤️ by Dartpad, the official online IDE/code runner for Dart, which allows even HTML to be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment