Skip to content

Commit

Permalink
update vimstuffs
Browse files Browse the repository at this point in the history
  • Loading branch information
dropwhile committed Sep 25, 2014
1 parent 46030ac commit f0104eb
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 57 deletions.
78 changes: 66 additions & 12 deletions dotfiles/vim/autoload/go/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,76 @@ if len(s:goarch) == 0
endif
endif

function! go#complete#PackageMembers(package, member)
silent! let content = system('godoc ' . a:package)
if v:shell_error || !len(content)
return []
endif
let lines = filter(split(content, "\n"),"v:val !~ '^\\s\\+$'")
try
let mx1 = '^\s\+\(\S+\)\s\+=\s\+.*'
let mx2 = '^\%(const\|var\|type\|func\) \([A-Z][^ (]\+\).*'
let candidates =
\ map(filter(copy(lines), 'v:val =~ mx1'), 'substitute(v:val, mx1, "\\1", "")')
\ + map(filter(copy(lines), 'v:val =~ mx2'), 'substitute(v:val, mx2, "\\1", "")')
return filter(candidates, '!stridx(v:val, a:member)')
catch
return []
endtry
endfunction

function! go#complete#Package(ArgLead, CmdLine, CursorPos)
let goroot = $GOROOT
if len(goroot) == 0
" should not occur.
let dirs = []

let words = split(a:CmdLine, '\s\+', 1)
if len(words) > 2
" Complete package members
return go#complete#PackageMembers(words[1], words[2])
endif

if executable('go')
let goroot = substitute(system('go env GOROOT'), '\n', '', 'g')
if v:shell_error
echomsg '''go env GOROOT'' failed'
endif
else
let goroot = $GOROOT
endif

if len(goroot) != 0 && isdirectory(goroot)
let dirs += [goroot]
endif

let pathsep = ':'
if s:goos == 'windows'
let pathsep = ';'
endif
let workspaces = split($GOPATH, pathsep)
if workspaces != []
let dirs += workspaces
endif

if len(dirs) == 0
" should not happen
return []
endif

let ret = {}
let root = expand(goroot.'/pkg/'.s:goos.'_'.s:goarch)
for i in split(globpath(root, a:ArgLead.'*'), "\n")
if isdirectory(i)
let i .= '/'
elseif i !~ '\.a$'
continue
endif
let i = substitute(substitute(i[len(root)+1:], '[\\]', '/', 'g'), '\.a$', '', 'g')
let ret[i] = i
for dir in dirs
" this may expand to multiple lines
let root = split(expand(dir . '/pkg/' . s:goos . '_' . s:goarch), "\n")
call add(root, expand(dir . '/src'))
for r in root
for i in split(globpath(r, a:ArgLead.'*'), "\n")
if isdirectory(i)
let i .= '/'
elseif i !~ '\.a$'
continue
endif
let i = substitute(substitute(i[len(r)+1:], '[\\]', '/', 'g'), '\.a$', '', 'g')
let ret[i] = i
endfor
endfor
endfor
return sort(keys(ret))
endfunction
1 change: 0 additions & 1 deletion dotfiles/vim/ftdetect/gofiletype.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ endfunction
au BufNewFile *.go setlocal filetype=go fileencoding=utf-8 fileformat=unix
au BufRead *.go call s:gofiletype_pre()
au BufReadPost *.go call s:gofiletype_post()
au BufRead,BufNewFile *.go set filetype=go sw=4 ts=4 noet
31 changes: 28 additions & 3 deletions dotfiles/vim/ftplugin/go/fmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,35 @@
" It tries to preserve cursor position and avoids
" replacing the buffer with stderr output.
"
" Options:
"
" g:go_fmt_commands [default=1]
"
" Flag to indicate whether to enable the commands listed above.
"
" g:gofmt_command [default="gofmt"]
"
" Flag naming the gofmt executable to use.
"
if exists("b:did_ftplugin_go_fmt")
finish
endif

if !exists("g:go_fmt_commands")
let g:go_fmt_commands = 1
endif

command! -buffer Fmt call s:GoFormat()
if !exists("g:gofmt_command")
let g:gofmt_command = "gofmt"
endif

if g:go_fmt_commands
command! -buffer Fmt call s:GoFormat()
endif

function! s:GoFormat()
let view = winsaveview()
silent %!gofmt
silent execute "%!" . g:gofmt_command
if v:shell_error
let errors = []
for line in getline(1, line('$'))
Expand All @@ -34,11 +57,13 @@ function! s:GoFormat()
endif
undo
if !empty(errors)
call setloclist(0, errors, 'r')
call setqflist(errors, 'r')
endif
echohl Error | echomsg "Gofmt returned error" | echohl None
endif
call winrestview(view)
endfunction

let b:did_ftplugin_go_fmt = 1

" vim:ts=4:sw=4:et
13 changes: 0 additions & 13 deletions dotfiles/vim/ftplugin/go/godoc.vim

This file was deleted.

73 changes: 61 additions & 12 deletions dotfiles/vim/ftplugin/go/import.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
" in the current Go buffer, using proper style and ordering.
" If {path} is already being imported, an error will be
" displayed and the buffer will be untouched.
"
"
" :ImportAs {localname} {path}
"
" Same as Import, but uses a custom local name for the package.
Expand All @@ -24,23 +24,40 @@
" imported, an error will be displayed and the buffer will be
" untouched.
"
" In addition to these commands, there are also two shortcuts mapped:
" If you would like to add shortcuts, you can do so by doing the following:
"
" Import fmt
" au Filetype go nnoremap <buffer> <LocalLeader>f :Import fmt<CR>
"
" \f - Runs :Import fmt
" \F - Runs :Drop fmt
" Drop fmt
" au Filetype go nnoremap <buffer> <LocalLeader>F :Drop fmt<CR>
"
" The backslash is the default maplocalleader, so it is possible that
" Import the word under your cursor
" au Filetype go nnoremap <buffer> <LocalLeader>k
" \ :exe 'Import ' . expand('<cword>')<CR>
"
" The backslash '\' is the default maplocalleader, so it is possible that
" your vim is set to use a different character (:help maplocalleader).
"
if exists("b:did_ftplugin")
" Options:
"
" g:go_import_commands [default=1]
"
" Flag to indicate whether to enable the commands listed above.
"
if exists("b:did_ftplugin_go_import")
finish
endif

command! -buffer -nargs=? -complete=customlist,go#complete#Package Drop call s:SwitchImport(0, '', <f-args>)
command! -buffer -nargs=1 -complete=customlist,go#complete#Package Import call s:SwitchImport(1, '', <f-args>)
command! -buffer -nargs=* -complete=customlist,go#complete#Package ImportAs call s:SwitchImport(1, <f-args>)
map <buffer> <LocalLeader>f :Import fmt<CR>
map <buffer> <LocalLeader>F :Drop fmt<CR>
if !exists("g:go_import_commands")
let g:go_import_commands = 1
endif

if g:go_import_commands
command! -buffer -nargs=? -complete=customlist,go#complete#Package Drop call s:SwitchImport(0, '', <f-args>)
command! -buffer -nargs=1 -complete=customlist,go#complete#Package Import call s:SwitchImport(1, '', <f-args>)
command! -buffer -nargs=* -complete=customlist,go#complete#Package ImportAs call s:SwitchImport(1, <f-args>)
endif

function! s:SwitchImport(enabled, localname, path)
let view = winsaveview()
Expand All @@ -58,6 +75,12 @@ function! s:SwitchImport(enabled, localname, path)
return
endif

" Extract any site prefix (e.g. github.com/).
" If other imports with the same prefix are grouped separately,
" we will add this new import with them.
" Only up to and including the first slash is used.
let siteprefix = matchstr(path, "^[^/]*/")

let qpath = '"' . path . '"'
if a:localname != ''
let qlocalpath = a:localname . ' ' . qpath
Expand All @@ -83,16 +106,31 @@ function! s:SwitchImport(enabled, localname, path)
let appendstr = qlocalpath
let indentstr = 1
let appendline = line
let firstblank = -1
let lastprefix = ""
while line <= line("$")
let line = line + 1
let linestr = getline(line)
let m = matchlist(getline(line), '^\()\|\(\s\+\)\(\S*\s*\)"\(.\+\)"\)')
if empty(m)
if siteprefix == "" && a:enabled
" must be in the first group
break
endif
" record this position, but keep looking
if firstblank < 0
let firstblank = line
endif
continue
endif
if m[1] == ')'
" if there's no match, add it to the first group
if appendline < 0 && firstblank >= 0
let appendline = firstblank
endif
break
endif
let lastprefix = matchstr(m[4], "^[^/]*/")
if a:localname != '' && m[3] != ''
let qlocalpath = printf('%-' . (len(m[3])-1) . 's %s', a:localname, qpath)
endif
Expand All @@ -103,7 +141,16 @@ function! s:SwitchImport(enabled, localname, path)
let deleteline = line
break
elseif m[4] < path
let appendline = line
" don't set candidate position if we have a site prefix,
" we've passed a blank line, and this doesn't share the same
" site prefix.
if siteprefix == "" || firstblank < 0 || match(m[4], "^" . siteprefix) >= 0
let appendline = line
endif
elseif siteprefix != "" && match(m[4], "^" . siteprefix) >= 0
" first entry of site group
let appendline = line - 1
break
endif
endwhile
break
Expand Down Expand Up @@ -198,4 +245,6 @@ function! s:Error(s)
echohl Error | echo a:s | echohl None
endfunction

let b:did_ftplugin_go_import = 1

" vim:ts=4:sw=4:et
20 changes: 16 additions & 4 deletions dotfiles/vim/indent/go.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ if exists("*GoIndent")
finish
endif

" The shiftwidth() function is relatively new.
" Don't require it to exist.
if exists('*shiftwidth')
func s:sw()
return shiftwidth()
endfunc
else
func s:sw()
return &shiftwidth
endfunc
endif

function! GoIndent(lnum)
let prevlnum = prevnonblank(a:lnum-1)
if prevlnum == 0
Expand All @@ -40,25 +52,25 @@ function! GoIndent(lnum)

if prevl =~ '[({]\s*$'
" previous line opened a block
let ind += &sw
let ind += s:sw()
endif
if prevl =~# '^\s*\(case .*\|default\):$'
" previous line is part of a switch statement
let ind += &sw
let ind += s:sw()
endif
" TODO: handle if the previous line is a label.

if thisl =~ '^\s*[)}]'
" this line closed a block
let ind -= &sw
let ind -= s:sw()
endif

" Colons are tricky.
" We want to outdent if it's part of a switch ("case foo:" or "default:").
" We ignore trying to deal with jump labels because (a) they're rare, and
" (b) they're hard to disambiguate from a composite literal key.
if thisl =~# '^\s*\(case .*\|default\):$'
let ind -= &sw
let ind -= s:sw()
endif

return ind
Expand Down
Loading

0 comments on commit f0104eb

Please sign in to comment.