├── README.md ├── autoload └── lua_format.vim └── plugin └── lua_format.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-lua-format 2 | 3 | Lua vim formatter supported by [LuaFormatter](https://github.com/Koihik/LuaFormatter). 4 | 5 | ## Install 6 | 7 | Use [Vundle](https://github.com/VundleVim/Vundle.vim) to get the *vim-lua-format* plugin. After installing it you need to add the following 8 | lines in your `.vimrc` file: 9 | 10 | ```vim 11 | autocmd FileType lua nnoremap :call LuaFormat() 12 | autocmd BufWrite *.lua call LuaFormat() 13 | ``` 14 | 15 | And it's done! 16 | 17 | Then press `` or simply save some `*.lua` file to format the Lua code automatically. 18 | 19 | __NOTE__ if you need to use the `LuaFormat()` function directly from command mode, you should call it explicitly as `:call LuaFormat()` 20 | 21 | ## Features 22 | 23 | Reformats your Lua source code. 24 | 25 | ## Extension Settings 26 | 27 | * `.lua-format`: Specifies the style config file. [Style Options](https://github.com/Koihik/LuaFormatter/wiki/Style-Config) 28 | 29 | The `.lua-format` file must be in the source or parent directory. If there is no configuration file the default settings are used. 30 | 31 | ## Known Issues 32 | 33 | You may have an error that claims unknown `-i` or `-si` options. This is happening because some versions of `lua-formatter` uses different flags. 34 | 35 | So if you get any error about unknown flag, just change it to the correct flag in [flags](https://github.com/jefersonf/vim-lua-format/blob/e94e10b969bf42b76e2558d079a2765dca5baa79/autoload/lua_format.vim#L40) string variable at `lua_format#format()` function. 36 | -------------------------------------------------------------------------------- /autoload/lua_format.vim: -------------------------------------------------------------------------------- 1 | " help function for formatters 2 | function lua_format#CopyDiffToBuffer(input, output, bufname) 3 | " prevent out of range in cickle 4 | let min_len = min([len(a:input), len(a:output)]) 5 | 6 | " copy all lines, that was changed 7 | for i in range(0, min_len - 1) 8 | let output_line = a:output[i] 9 | let input_line = a:input[i] 10 | if input_line !=# output_line 11 | call setline(i + 1, output_line) " lines calculate from 1, items - from 0 12 | end 13 | endfor 14 | 15 | " in this case we have to handle all lines, that was in range 16 | if len(a:input) != len(a:output) 17 | if min_len == len(a:output) " remove all extra lines from input 18 | call deletebufline(a:bufname, min_len + 1, "$") 19 | else " append all extra lines from output 20 | call append("$", a:output[min_len:]) 21 | end 22 | end 23 | 24 | " XXX if formatting is a long operation and we call after format start some 25 | " other command, then window will display invalid data. For prevent this we 26 | " just redraw the windows 27 | redraw! 28 | endfunction 29 | 30 | 31 | 32 | function lua_format#format() 33 | let input = getline(1, "$") 34 | 35 | " in case of some error formatter print to stderr error message and exit 36 | " with 0 code, so we need redirect stderr to file, for read message in case 37 | " of some error. So let create a temporary file 38 | let error_file = tempname() 39 | 40 | let flags = " -i " 41 | 42 | " we can use config file for formatting which we have to set manually 43 | let config_file = findfile(".lua-format", ".;") 44 | if empty(config_file) == 0 " append config_file to flags 45 | let flags = flags . " -c " . config_file 46 | end 47 | 48 | let output_str=system("lua-format " . flags . " 2> " . error_file, input) 49 | 50 | if empty(output_str) == 0 " all right 51 | let output = split(output_str, "\n") 52 | call lua_format#CopyDiffToBuffer(input, output, bufname("%")) 53 | 54 | " also clear lbuffer 55 | lexpr "" 56 | lwindow 57 | else " we got error 58 | let errors = readfile(error_file) 59 | 60 | " insert filename of current buffer in front of list. Need for errorformat 61 | let source_file = bufname("%") 62 | call insert(errors, source_file) 63 | 64 | set efm=%+P%f,line\ %l:%c\ %m,%-Q 65 | lexpr errors 66 | lwindow 5 67 | end 68 | 69 | call delete(error_file) 70 | endfunction 71 | -------------------------------------------------------------------------------- /plugin/lua_format.vim: -------------------------------------------------------------------------------- 1 | function! LuaFormat() 2 | call lua_format#format() 3 | endfunction 4 | --------------------------------------------------------------------------------