diff --git a/cmd/micro/runtime.go b/cmd/micro/runtime.go index 5c142ad842..54feb10a9a 100644 --- a/cmd/micro/runtime.go +++ b/cmd/micro/runtime.go @@ -46,6 +46,7 @@ // runtime/plugins/autoclose/utf8/LICENSE // runtime/plugins/autoclose/utf8/README.md // runtime/plugins/autoclose/utf8/utf8.lua +// runtime/plugins/formatter/formatter.lua // runtime/plugins/ftoptions/ftoptions.lua // runtime/plugins/linter/linter.lua // runtime/plugins/literate/README.md @@ -1150,6 +1151,26 @@ func runtimePluginsAutocloseUtf8Utf8Lua() (*asset, error) { return a, nil } +var _runtimePluginsFormatterFormatterLua = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x92\xcb\x6a\xf3\x30\x10\x85\xf7\x7a\x8a\x41\x2b\xeb\x27\x11\xff\x3a\xe0\x45\x1b\x48\x57\x25\xa5\x2d\xdd\x16\x11\x8d\x22\x51\x59\x32\xba\xc4\xf4\xed\x8b\x2c\x27\x4e\x7a\x21\xc4\xe0\x8d\x74\xe6\x3b\x9a\x39\x63\x14\x3c\x60\xda\xf6\xc9\x78\xd7\x50\xe5\x43\x27\x52\xc2\x40\x19\xb4\x2d\x38\x63\x21\x69\x74\x04\x00\xe0\x4e\xca\x9f\xb2\x05\x28\x61\x23\x32\x82\x4e\x12\xf2\x28\x3e\x70\xed\xbb\x4e\x38\x79\x14\xd1\x05\xcc\x72\x1e\xb2\xdb\x9c\xd5\xfe\x67\x84\xa8\xec\x76\x85\x0a\xe7\x77\x0d\x1b\x2d\xd7\x39\xbc\x19\x1c\x1a\xb6\x7a\x11\x07\x6c\x26\xab\xf1\xca\xfa\x9d\xb0\xa0\x12\xb4\xb3\x8a\xdf\x67\xb5\xda\x18\x8b\xaf\x9f\x3d\x4e\x88\xaa\xd3\xc2\x49\x8b\xe3\x81\x51\x63\x55\x0b\x54\x99\xa8\xe9\xdc\x5f\xf9\xaa\x0e\x5a\x30\x9e\xf7\xbe\xc7\xd2\xab\x89\xfa\xdd\x38\x89\x2e\xc1\x72\x00\x0a\x9c\x5f\x3a\xf2\x27\x91\x74\x35\x43\x1b\x71\xe6\xef\xfd\x75\xfa\xde\xab\x2e\xc1\x32\xde\x88\x8e\x1a\xad\xbd\x4e\x8f\xfa\x06\xfa\x09\x14\x30\xe5\x50\xb9\x25\xd5\x79\x8a\x01\x63\xb6\x65\xe2\xd5\x6a\x15\x50\xc8\x86\xfe\x13\xb4\x42\xa6\xd3\x9d\xf5\xb1\x4c\xff\x5b\x82\xcf\xb8\x2d\x6f\x9a\x36\xe5\x94\xba\x77\x63\xb4\x07\x83\x03\x3b\xe6\xf3\xc7\x42\x5e\x74\xfb\xcb\xb6\x14\x70\xf9\xbf\x02\x00\x00\xff\xff\x91\xb4\x28\x18\xd3\x02\x00\x00") + +func runtimePluginsFormatterFormatterLuaBytes() ([]byte, error) { + return bindataRead( + _runtimePluginsFormatterFormatterLua, + "runtime/plugins/formatter/formatter.lua", + ) +} + +func runtimePluginsFormatterFormatterLua() (*asset, error) { + bytes, err := runtimePluginsFormatterFormatterLuaBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "runtime/plugins/formatter/formatter.lua", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + var _runtimePluginsFtoptionsFtoptionsLua = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x90\xb1\x4e\x03\x31\x0c\x86\xf7\x3c\x85\xe5\xa9\x27\xa1\x0e\x30\xdf\x00\x0b\x63\x87\x4a\x2c\x88\x21\xdc\xd9\xad\x45\x6a\x47\x17\x1f\xd5\xbd\x3d\xca\x9d\x8e\x16\x55\x0c\xcd\xe4\x7c\x71\xfe\x2f\x8e\x30\xbc\x92\xef\xb2\x8b\xe9\x06\xd9\x6d\xae\x0a\x36\xd0\xb6\xa0\x92\xc0\x8f\xa4\x01\x00\xe0\xb9\xef\x6f\xdb\x1e\xc0\x87\x91\x9a\x40\xda\x87\xc0\xa3\x76\x15\x83\xe9\x9b\xd0\x79\x97\x49\x37\xdf\x42\xe7\x66\xbe\x2f\x0c\x6a\xfe\x9f\xed\x57\x53\xd7\x40\x3e\x0e\xcb\x76\x0e\xae\x45\xb2\x2e\x26\x60\x87\x16\x6a\xe6\xf6\x65\xe4\xed\x9e\xdc\x45\x0f\xe5\x1d\x59\x12\xf9\x94\x09\x3f\xc2\x2a\xab\xad\x2d\xe0\x29\x7e\x51\x3d\x45\xb0\x61\x65\x07\xc3\xbf\xc2\xfd\xe5\x51\x1e\x3f\x8b\x5b\xc9\xb1\xa3\x3a\x1e\x1a\x33\x2e\x03\x50\x2a\x74\xc9\xcd\x93\x1f\x4d\xaf\x53\x17\xf2\x78\x8b\x9e\xae\xd1\x14\x4f\xe9\x0e\xbb\xae\x72\xed\xe7\x4f\xfe\x09\x00\x00\xff\xff\xbe\x08\x7b\x2b\xb0\x01\x00\x00") func runtimePluginsFtoptionsFtoptionsLuaBytes() ([]byte, error) { @@ -3588,6 +3609,7 @@ var _bindata = map[string]func() (*asset, error){ "runtime/plugins/autoclose/utf8/LICENSE": runtimePluginsAutocloseUtf8License, "runtime/plugins/autoclose/utf8/README.md": runtimePluginsAutocloseUtf8ReadmeMd, "runtime/plugins/autoclose/utf8/utf8.lua": runtimePluginsAutocloseUtf8Utf8Lua, + "runtime/plugins/formatter/formatter.lua": runtimePluginsFormatterFormatterLua, "runtime/plugins/ftoptions/ftoptions.lua": runtimePluginsFtoptionsFtoptionsLua, "runtime/plugins/linter/linter.lua": runtimePluginsLinterLinterLua, "runtime/plugins/literate/README.md": runtimePluginsLiterateReadmeMd, @@ -3803,6 +3825,9 @@ var _bintree = &bintree{nil, map[string]*bintree{ "utf8.lua": &bintree{runtimePluginsAutocloseUtf8Utf8Lua, map[string]*bintree{}}, }}, }}, + "formatter": &bintree{nil, map[string]*bintree{ + "formatter.lua": &bintree{runtimePluginsFormatterFormatterLua, map[string]*bintree{}}, + }}, "ftoptions": &bintree{nil, map[string]*bintree{ "ftoptions.lua": &bintree{runtimePluginsFtoptionsFtoptionsLua, map[string]*bintree{}}, }}, diff --git a/runtime/plugins/formatter/formatter.lua b/runtime/plugins/formatter/formatter.lua new file mode 100644 index 0000000000..7e5531b244 --- /dev/null +++ b/runtime/plugins/formatter/formatter.lua @@ -0,0 +1,31 @@ +if GetOption("formatter") == nil then + AddOption("formatter", false) +end + +MakeCommand("format", "formatter.runFormatter", 0) + +function runFormatter() + CurView():Save(false) + + local ft = CurView().Buf:FileType() + local handle + if ft == "fish" then + handle = io.popen("fish_indent -w " .. CurView().Buf.Path) + elseif ft == "go" then + handle = io.popen("gofmt -s -w " .. CurView().Buf.Path) + elseif ft == "shell" then + handle = io.popen("shfmt -s -w " .. CurView().Buf.Path) + else + return + end + local result = handle:read("*a") + handle:close() + + CurView():ReOpen() +end + +function onSave(view) + if GetOption("formatter") then + runFormatter() + end +end