Skip to content

Instantly share code, notes, and snippets.

@jscheid
Last active January 28, 2021 02:26
Show Gist options
  • Select an option

  • Save jscheid/87d068e498ea7bb352585912ce01a8d2 to your computer and use it in GitHub Desktop.

Select an option

Save jscheid/87d068e498ea7bb352585912ce01a8d2 to your computer and use it in GitHub Desktop.
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