Last active
January 28, 2021 02:26
-
-
Save jscheid/87d068e498ea7bb352585912ce01a8d2 to your computer and use it in GitHub Desktop.
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 prettier = require("prettier"); | |
| const chalk = require("chalk"); | |
| const normal = chalk.grey; | |
| const highlight = chalk.white.bgBlue.bold; | |
| function showNewline(s) { | |
| return s.replace(/\n/g, "\u23ce\n"); | |
| } | |
| function printWithHighlight({ formatted, cursorOffset }) { | |
| process.stdout.write( | |
| normal(showNewline(formatted.substring(0, cursorOffset))) + | |
| highlight( | |
| showNewline(formatted.substring(cursorOffset, cursorOffset + 1)) | |
| ) + | |
| normal(showNewline(formatted.substring(cursorOffset + 1))) + | |
| "\n" | |
| ); | |
| } | |
| function showCursor(parser, code) { | |
| for (let cursorOffset = 0; cursorOffset < code.length; ++cursorOffset) { | |
| console.log(`--- cursorOffset ${cursorOffset}`); | |
| printWithHighlight({ formatted: code, cursorOffset }); | |
| printWithHighlight( | |
| prettier.formatWithCursor(code, { parser, cursorOffset }) | |
| ); | |
| } | |
| } | |
| showCursor( | |
| "ruby", | |
| ` | |
| unless false | |
| true | |
| end.to_s | |
| ` | |
| ); | |
| /* | |
| showCursor( | |
| "babel", | |
| ` | |
| function foo ( x ) { | |
| return "bar" ; | |
| } | |
| ` | |
| ); | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment