mirror of
https://github.com/amix/vimrc
synced 2025-06-16 01:25:00 +08:00
Updated plugins and added vim-abolish
This commit is contained in:
58
sources_non_forked/vim-go/autoload/go/keyify.vim
Normal file
58
sources_non_forked/vim-go/autoload/go/keyify.vim
Normal file
@ -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
|
@ -123,12 +123,11 @@ function! go#lint#Golint(...) abort
|
||||
endif
|
||||
|
||||
if a:0 == 0
|
||||
let goargs = shellescape(expand('%'))
|
||||
let out = go#util#System(bin_path)
|
||||
else
|
||||
let goargs = go#util#Shelljoin(a:000)
|
||||
let out = go#util#System(bin_path . " " . go#util#Shelljoin(a:000))
|
||||
endif
|
||||
|
||||
let out = go#util#System(bin_path . " " . goargs)
|
||||
if empty(out)
|
||||
echon "vim-go: " | echohl Function | echon "[lint] PASS" | echohl None
|
||||
return
|
||||
@ -255,9 +254,13 @@ function s:lint_job(args)
|
||||
caddexpr a:msg
|
||||
let &errorformat = old_errorformat
|
||||
|
||||
" TODO(arslan): cursor still jumps to first error even If I don't want
|
||||
" it. Seems like there is a regression somewhere, but not sure where.
|
||||
" TODO(jinleileiking): give a configure to jump or not
|
||||
let l:winnr = winnr()
|
||||
|
||||
copen
|
||||
|
||||
exe l:winnr . "wincmd w"
|
||||
|
||||
endfunction
|
||||
|
||||
function! s:exit_cb(job, exitval) closure
|
||||
|
@ -33,22 +33,16 @@ function! go#path#GoPath(...) abort
|
||||
let $GOPATH = a:1
|
||||
endfunction
|
||||
|
||||
" Default returns the default GOPATH. If there is a single GOPATH it returns
|
||||
" it. For multiple GOPATHS separated with a the OS specific separator, only
|
||||
" the first one is returned. If GOPATH is not set, it uses the default GOPATH
|
||||
" set starting with GO 1.8. This GOPATH can be retrieved via 'go env GOPATH'
|
||||
" Default returns the default GOPATH. If GOPATH is not set, it uses the
|
||||
" default GOPATH set starting with Go 1.8. This GOPATH can be retrieved via
|
||||
" 'go env GOPATH'
|
||||
function! go#path#Default() abort
|
||||
if $GOPATH == ""
|
||||
" use default GOPATH via go env
|
||||
return go#util#gopath()
|
||||
endif
|
||||
|
||||
let go_paths = split($GOPATH, go#util#PathListSep())
|
||||
if len(go_paths) == 1
|
||||
return $GOPATH
|
||||
endif
|
||||
|
||||
return go_paths[0]
|
||||
return $GOPATH
|
||||
endfunction
|
||||
|
||||
" HasPath checks whether the given path exists in GOPATH environment variable
|
||||
@ -134,8 +128,8 @@ function! go#path#BinPath() abort
|
||||
elseif $GOBIN != ""
|
||||
let bin_path = $GOBIN
|
||||
else
|
||||
" GOPATH (user set or default GO)
|
||||
let bin_path = expand(go#path#Default() . "/bin/")
|
||||
let go_paths = split(go#path#Default(), go#util#PathListSep())
|
||||
let bin_path = expand(go_paths[0] . "/bin/")
|
||||
endif
|
||||
|
||||
return bin_path
|
||||
|
@ -122,11 +122,13 @@ func s:create_cmd(args) abort
|
||||
let l:offset = a:args.offset
|
||||
let l:mode = a:args.mode
|
||||
let l:cmd_args = a:args.cmd_args
|
||||
let l:modifytags_transform = get(g:, 'go_addtags_transform', "snakecase")
|
||||
|
||||
" start constructing the command
|
||||
let cmd = [bin_path]
|
||||
call extend(cmd, ["-format", "json"])
|
||||
call extend(cmd, ["-file", a:args.fname])
|
||||
call extend(cmd, ["-transform", l:modifytags_transform])
|
||||
|
||||
if l:offset != 0
|
||||
call extend(cmd, ["-offset", l:offset])
|
||||
|
@ -94,19 +94,29 @@ function! go#util#osarch() abort
|
||||
return go#util#goos() . '_' . go#util#goarch()
|
||||
endfunction
|
||||
|
||||
" System runs a shell command. It will reset the shell to /bin/sh for Unix-like
|
||||
" systems if it is executable.
|
||||
" System runs a shell command. If possible, it will temporary set
|
||||
" the shell to /bin/sh for Unix-like systems providing a Bourne
|
||||
" POSIX like environment.
|
||||
function! go#util#System(str, ...) abort
|
||||
" Preserve original shell and shellredir values
|
||||
let l:shell = &shell
|
||||
let l:shellredir = &shellredir
|
||||
|
||||
" Use a Bourne POSIX like shell. Some parts of vim-go expect
|
||||
" commands to be executed using bourne semantics #988 and #1276.
|
||||
" Alter shellredir to match bourne. Especially important if login shell
|
||||
" is set to any of the csh or zsh family #1276.
|
||||
if !go#util#IsWin() && executable('/bin/sh')
|
||||
let &shell = '/bin/sh'
|
||||
set shell=/bin/sh shellredir=>%s\ 2>&1
|
||||
endif
|
||||
|
||||
try
|
||||
let l:output = call('system', [a:str] + a:000)
|
||||
return l:output
|
||||
finally
|
||||
" Restore original values
|
||||
let &shell = l:shell
|
||||
let &shellredir = l:shellredir
|
||||
endtry
|
||||
endfunction
|
||||
|
||||
@ -203,7 +213,7 @@ endfunction
|
||||
" snippetcase converts the given word to given preferred snippet setting type
|
||||
" case.
|
||||
function! go#util#snippetcase(word) abort
|
||||
let l:snippet_case = get(g:, 'go_snippet_case_type', "snakecase")
|
||||
let l:snippet_case = get(g:, 'go_addtags_transform', "snakecase")
|
||||
if l:snippet_case == "snakecase"
|
||||
return go#util#snakecase(a:word)
|
||||
elseif l:snippet_case == "camelcase"
|
||||
@ -235,51 +245,6 @@ function! go#util#camelcase(word) abort
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! go#util#AddTags(line1, line2, ...) abort
|
||||
" default is json
|
||||
let l:keys = ["json"]
|
||||
if a:0
|
||||
let l:keys = a:000
|
||||
endif
|
||||
|
||||
let l:line1 = a:line1
|
||||
let l:line2 = a:line2
|
||||
|
||||
" If we're inside a struct and just call this function let us add the tags
|
||||
" to all fields
|
||||
" TODO(arslan): I don't like using patterns. Check if we can move it to
|
||||
" `motion` and do it via AST based position
|
||||
let ln1 = searchpair('struct {', '', '}', 'bcnW')
|
||||
if ln1 == 0
|
||||
echon "vim-go: " | echohl ErrorMsg | echon "cursor is outside the struct" | echohl None
|
||||
return
|
||||
endif
|
||||
|
||||
" searchpair only returns a single position
|
||||
let ln2 = search('}', "cnW")
|
||||
|
||||
" if no range is given we apply for the whole struct
|
||||
if l:line1 == l:line2
|
||||
let l:line1 = ln1 + 1
|
||||
let l:line2 = ln2 - 1
|
||||
endif
|
||||
|
||||
for line in range(l:line1, l:line2)
|
||||
" get the field name (word) that are not part of a commented line
|
||||
let l:matched = matchstr(getline(line), '\(\/\/.*\)\@<!\w\+')
|
||||
if empty(l:matched)
|
||||
continue
|
||||
endif
|
||||
|
||||
let word = go#util#snippetcase(l:matched)
|
||||
let tags = map(copy(l:keys), 'printf("%s:%s", v:val,"\"'. word .'\"")')
|
||||
let updated_line = printf("%s `%s`", getline(line), join(tags, " "))
|
||||
|
||||
" finally, update the line inplace
|
||||
call setline(line, updated_line)
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
" TODO(arslan): I couldn't parameterize the highlight types. Check if we can
|
||||
" simplify the following functions
|
||||
"
|
||||
|
Reference in New Issue
Block a user