Created
April 20, 2017 08:25
-
-
Save pmorch/39d1bab04090216628cadb3e39f01177 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/perl -w | |
use strict; | |
use IPC::Open2; | |
=head1 prettier_indent | |
From: | |
A way to keep initial indentation when formatting a "snippet" - or part of a | |
file https://github.com/prettier/prettier/issues/1324#issuecomment-294878202 | |
Use case: I'm using Neoformat for vim, where I select a snippet (e.g. the | |
insides of an if-statement), and run it through prettier. I'd like to keep the | |
indentation of the first line and reformat the snippet accordingly. Assume this | |
code: | |
.... | |
if (true) { | |
doSomething(); doSomethingElse(); | |
} | |
.... | |
Assume the doSomething(); doSomethingElse(); line starts on column 7 (First | |
column is column 1). | |
Expected result: doSomething() starts on column 7 after being run through | |
prettier and doSomethingElse() also starts on column 7 on the next line. | |
Actual result: Both lines start on column 1, not column 7. | |
=head1 Workaround (this script) | |
I can create a wrapper script that checks the amount of indentation of the | |
first line. Subtract this number from 80 and give that to --print-width. Run | |
prettier, and subsequently re-indent the entire prettier output that amount of | |
spaces. | |
But it would be handy if prettier would do that for me - perhaps behind an | |
option. | |
=head2 Limitations | |
This only accepts input on stdin and formats output to stdout (that is the only | |
thing I needed with prettier in vim) | |
=cut | |
my $firstLine = <STDIN>; | |
my $printWidth; | |
my @args = @ARGV; | |
# Poor-man's argv handing. Just need any --print-width arg value | |
foreach ( my $i = 0 ; $i < scalar @args - 1 ; $i++ ) { | |
if ( $ARGV[$i] eq '--print-width' ) { | |
$printWidth = $args[ $i + 1 ]; | |
$printWidth =~ /^\d+$/ | |
or die "Expected a numerical value for --print-width, " | |
. "not '$printWidth'"; | |
# Remove the original --print-width arg | |
splice @args, $i, 2; | |
last; | |
} | |
} | |
$printWidth //= 80; | |
my $initialWhitespace; | |
if ( ($initialWhitespace) = ( $firstLine =~ /^([ \t]+)/ ) ) { | |
$initialWhitespace =~ s/\t/ ' ' x 8 /ge; | |
my $firstLineIndent = length($initialWhitespace); | |
$printWidth -= $firstLineIndent; | |
} | |
my ( $childOut, $childIn ); | |
my $pid = open2( $childOut, $childIn, | |
'prettier', '--print-width', $printWidth, @args ); | |
print $childIn $firstLine, "\n"; | |
print $childIn <STDIN>; | |
close $childIn; | |
waitpid( $pid, 0 ); | |
if ( !$initialWhitespace ) { | |
print <$childOut>; | |
} | |
while ( my $line = <$childOut> ) { | |
# Anything but whitespace? | |
if ( $line =~ /./ ) { | |
print $initialWhitespace, $line; | |
} else { | |
print $line; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment