Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include keyify support #1258

Merged
merged 5 commits into from
May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions autoload/go/keyify.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function! go#keyify#Keyify()
let old_gopath = $GOPATH
let $GOPATH = go#path#Detect()
let bin_path = go#path#CheckBinPath("keyify")
let fname = fnamemodify(expand("%"), ':p:gs?\\?/?')

if empty(bin_path) || !exists('*json_decode')
let $GOPATH = old_gopath
return
endif

" Get result of command as json, that contains `start`, `end` and `replacement`
let command = printf("%s -json %s:#%s", bin_path, fname, go#util#OffsetCursor())
let output = go#util#System(command)
silent! let result = json_decode(output)

" We want to output the error message in case the result isn't a JSON
if type(result) != type({})
call go#util#EchoError(s:chomp(output))
let $GOPATH = old_gopath
return
endif

" Because keyify returns the byte before the region we want, we goto the
" byte after that
execute "goto" result.start + 1
let start = getpos('.')
execute "goto" result.end
let end = getpos('.')

let vis_start = getpos("'<")
let vis_end = getpos("'>")

" Replace contents between start and end with `replacement`
call setpos("'<", start)
call setpos("'>", end)

let select = 'gv'

" Make sure the visual mode is 'v', to avoid some bugs
normal! gv
if mode() !=# 'v'
let select .= 'v'
endif

silent! execute "normal!" select."\"=result.replacement\<cr>p"

" Replacement text isn't aligned, so it needs fix
normal! '<v'>=

call setpos("'<", vis_start)
call setpos("'>", vis_end)
let $GOPATH = old_gopath
endfunction

function! s:chomp(string)
return substitute(a:string, '\n\+$', '', '')
endfunction
15 changes: 15 additions & 0 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,22 @@ CTRL-t

Toggles |'g:go_template_autocreate'|.

*:GoKeyify*
:GoKeyify

Uses `keyify` to turn unkeyed struct literals into keyed ones.

For example:
>
Person{"John", "Smith"}
<
Becomes:
>
Person{
Name: "John",
Surname: "Smith",
}
<
==============================================================================
MAPPINGS *go-mappings*

Expand Down
3 changes: 3 additions & 0 deletions ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ command! -nargs=* -buffer -complete=customlist,go#impl#Complete GoImpl call go#i
" -- template
command! -nargs=0 GoTemplateAutoCreateToggle call go#template#ToggleAutoCreate()

" -- keyify
command! -nargs=0 GoKeyify call go#keyify#Keyify()

" vim: sw=2 ts=2 et
1 change: 1 addition & 0 deletions plugin/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let s:packages = [
\ "github.com/fatih/gomodifytags",
\ "github.com/zmb3/gogetdoc",
\ "github.com/josharian/impl",
\ "github.com/dominikh/go-tools/cmd/keyify",
\ ]

" These commands are available on any filetypes
Expand Down