diff options
Diffstat (limited to 'misc/vim/ftplugin/go/import.vim')
-rw-r--r-- | misc/vim/ftplugin/go/import.vim | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/misc/vim/ftplugin/go/import.vim b/misc/vim/ftplugin/go/import.vim index 6705a476b..1d969e460 100644 --- a/misc/vim/ftplugin/go/import.vim +++ b/misc/vim/ftplugin/go/import.vim @@ -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. @@ -32,7 +32,7 @@ " 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") +if exists("b:did_ftplugin_go_import") finish endif @@ -58,6 +58,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 @@ -83,16 +89,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 @@ -103,7 +124,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 @@ -198,4 +228,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 |