Skip to content
Browse files

Create and use a wrapper script to run gofmt

The wrapper script emits the original contents in case of a syntax error, for
now, and automatically injects/removes a 'package ' line, which makes it also
suitable as equalprg (for the '=' operation in normal mode).  Use this wrapper
script for the on-write gofmt run, and set equalprg to it as well.

Signed-off-by: Chris Larson <chris_larson@mentor.com>
  • Loading branch information...
1 parent 6264bfd commit 16e44d61d6957a0be201209ce6b878514dfe44bd @kergoth committed Aug 28, 2010
Showing with 28 additions and 1 deletion.
  1. +6 −1 after/ftplugin/go/fmt.vim
  2. +22 −0 bin/mygofmt
View
7 after/ftplugin/go/fmt.vim
@@ -1,9 +1,14 @@
+let $PATH = $MYVIMRUNTIME . "/bin:" . $PATH
+
function! Goformat()
+ if executable("mygofmt")
let regel=line(".")
- silent %!gofmt
+ silent %!mygofmt
call cursor(regel, 1)
+ endif
endfunction
command! Fmt call Goformat()
+set equalprg=mygofmt
au BufWrite *.go :Fmt
View
22 bin/mygofmt
@@ -0,0 +1,22 @@
+#!/bin/sh
+tmpname=`basename $0`
+
+infile=`mktemp -q /tmp/$tmpname.XXXXXX || exit 1`
+trap "rm -f $infile" EXIT
+cat >>$infile
+
+outfile=`mktemp -q /tmp/$tmpname.XXXXXX || exit 1`
+trap "rm -f $outfile $infile" EXIT
+
+{
+ if ! grep -q "^package " "$infile"; then
+ echo "package gofmt"
+ fi
+ cat $infile
+} | gofmt "$@" | sed -n -e '/^package gofmt/{n; n;}; p' >>$outfile
+
+if [ $? -ne 0 ]; then
+ cat $infile
+else
+ cat $outfile
+fi

0 comments on commit 16e44d6

Please sign in to comment.
Something went wrong with that request. Please try again.