package autocmd

import (
	"nvim-go/commands"
	"nvim-go/config"

	"github.com/garyburd/neovim-go/vim"
	"github.com/garyburd/neovim-go/vim/plugin"
)

func init() {
	plugin.HandleAutocmd("BufWritePre",
		&plugin.AutocmdOptions{Pattern: "*.go", Group: "nvim-go", Eval: "[getcwd(), expand('%:p:h'), expand('%:p')]"}, autocmdBufWritePre)
}

type bufwritepreEval struct {
	Cwd  string `msgpack:",array"`
	Dir  string
	File string
}

func autocmdBufWritePre(v *vim.Vim, eval bufwritepreEval) error {
	if config.IferrAutosave {
		var env = commands.CmdIferrEval{
			Cwd:  eval.Cwd,
			File: eval.File,
		}
		go commands.Iferr(v, env)
	}

	if config.MetalinterAutosave {
		go commands.Metalinter(v, eval.Cwd)
	}

	if config.FmtAsync {
		go commands.Fmt(v, eval.Dir)
	} else {
		return commands.Fmt(v, eval.Dir)
	}

	return nil
}

package autocmd

import (
	"nvim-go/commands"
	"nvim-go/config"

	"github.com/garyburd/neovim-go/vim"
	"github.com/garyburd/neovim-go/vim/plugin"
)

func init() {
	plugin.HandleAutocmd("BufWritePre",
		&plugin.AutocmdOptions{Pattern: "*.go", Group: "nvim-go", Eval: "[getcwd(), expand('%:p')]"}, autocmdBufWritePre)
}

type bufwritepreEval struct {
	Cwd  string `msgpack:",array"`
	File string
}

func autocmdBufWritePre(v *vim.Vim, eval bufwritepreEval) error {
	if config.IferrAutosave {
		var env = commands.CmdIferrEval{
			Cwd:  eval.Cwd,
			File: eval.File,
		}
		go commands.Iferr(v, env)
	}

	if config.MetalinterAutosave {
		go commands.Metalinter(v, eval.Cwd)
	}

	if config.FmtAsync {
		go commands.Fmt(v, eval.Cwd)
	} else {
		return commands.Fmt(v, eval.Cwd)
	}
	return nil
}

--- 
+++ 
@@ -10,11 +10,12 @@
 
 func init() {
 	plugin.HandleAutocmd("BufWritePre",
-		&plugin.AutocmdOptions{Pattern: "*.go", Group: "nvim-go", Eval: "[getcwd(), expand('%:p')]"}, autocmdBufWritePre)
+		&plugin.AutocmdOptions{Pattern: "*.go", Group: "nvim-go", Eval: "[getcwd(), expand('%:p:h'), expand('%:p')]"}, autocmdBufWritePre)
 }
 
 type bufwritepreEval struct {
 	Cwd  string `msgpack:",array"`
+	Dir  string
 	File string
 }
 
@@ -32,9 +33,10 @@
 	}
 
 	if config.FmtAsync {
-		go commands.Fmt(v, eval.Cwd)
+		go commands.Fmt(v, eval.Dir)
 	} else {
-		return commands.Fmt(v, eval.Cwd)
+		return commands.Fmt(v, eval.Dir)
 	}
+
 	return nil
 }
Fix Eval to use current buffer path for GoFmt

Signed-off-by: Koichi Shiraishi <13fbd79c3d390e5d6585a21e11ff5ec1970cff0c@zchee.io>
