Skip to content

Commit

Permalink
Merge pull request #437 from fatih/oracle-improvements
Browse files Browse the repository at this point in the history
Oracle improvements
  • Loading branch information
fatih committed May 25, 2015
2 parents d0b1ff4 + 8760678 commit 3037995
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 32 deletions.
35 changes: 27 additions & 8 deletions autoload/go/oracle.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
" -*- text -*-
" oracle.vim -- Vim integration for the Go oracle.
"
" Part of this plugin was taken directly from the oracle repo, however it's
Expand Down Expand Up @@ -46,7 +45,7 @@ endfun
" via regex.
func! s:qflistSecond(output)
" backup users errorformat, will be restored once we are finished
let old_errorformat = &errorformat
let old_errorformat = &errorformat

" match two possible styles of errorformats:
"
Expand All @@ -56,13 +55,13 @@ func! s:qflistSecond(output)
" We discard line2 and col2 for the first errorformat, because it's not
" useful and quickfix only has the ability to show one line and column
" number
let &errorformat = "%f:%l.%c-%[%^:]%#:\ %m,%f:%l:%c:\ %m"
let &errorformat = "%f:%l.%c-%[%^:]%#:\ %m,%f:%l:%c:\ %m"

" create the quickfix list and open it
cgetexpr split(a:output, "\n")
cwindow

let &errorformat = old_errorformat
let &errorformat = old_errorformat
endfun

func! s:getpos(l, c)
Expand All @@ -85,7 +84,7 @@ func! s:RunOracle(mode, selected) range abort
let unescaped_scopes = split(get(g:, 'go_oracle_scope'))
let scopes = []
for unescaped_scope in unescaped_scopes
call add(scopes, shellescape(unescaped_scope))
call add(scopes, shellescape(unescaped_scope))
endfor
elseif exists('g:go_oracle_include_tests') && pkg != -1
" give import path so it includes all _test.go files too
Expand Down Expand Up @@ -120,7 +119,7 @@ func! s:RunOracle(mode, selected) range abort
" info check Oracle's User Manual section about scopes:
" https://docs.google.com/document/d/1SLk36YRjjMgKqe490mSRzOPYEDe0Y_WQNRv-EiFYUyw/view#heading=h.nwso96pj07q8
for scope in scopes
let cmd .= ' ' . scope
let cmd .= ' ' . scope
endfor

echon "vim-go: " | echohl Identifier | echon "analysing ..." | echohl None
Expand All @@ -138,11 +137,30 @@ func! s:RunOracle(mode, selected) range abort
" package which doesn't build.
redraw | echon "vim-go: " | echohl Statement | echon out | echohl None
return ""
else
endif

return out
endfun
endfunc

function! go#oracle#Scope(...)
if len(a:000)
if len(a:000) == 1 && a:1 == '""'
let g:go_oracle_scope = ""
echon "vim-go: " | echohl Function | echon "oracle scope is cleared"| echohl None
else
let g:go_oracle_scope = join(a:000, ' ')
echon "vim-go: " | echohl Function | echon "oracle scope changed to: '". g:go_oracle_scope ."'" | echohl None
endif

return
endif

if !exists(g:go_oracle_scope)
echon "vim-go: " | echohl Function | echon "oracle scope is not set"| echohl None
else
echon "vim-go: " | echohl Function | echon "current oracle scope: '". g:go_oracle_scope ."'" | echohl None
endif
endfunction

" Show 'implements' relation for selected package
function! go#oracle#Implements(selected)
Expand Down Expand Up @@ -193,3 +211,4 @@ function! go#oracle#Referrers(selected)
endfunction

" vim:ts=4:sw=4:et
"
11 changes: 10 additions & 1 deletion autoload/go/package.vim
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ endfunction

function! go#package#Complete(ArgLead, CmdLine, CursorPos)
let words = split(a:CmdLine, '\s\+', 1)
if len(words) > 2 && words[0] != "GoImportAs"

" do not complete package members for these commands
let neglect_commands = ["GoImportAs", "GoOracleScope"]

if len(words) > 2 && index(neglect_commands, words[0]) == -1
" Complete package members
return go#package#CompleteMembers(words[1], words[2])
endif
Expand All @@ -138,6 +142,11 @@ function! go#package#Complete(ArgLead, CmdLine, CursorPos)
endif
let i = substitute(substitute(i[len(r)+1:], '[\\]', '/', 'g'),
\ '\.a$', '', 'g')

" without this the result can have duplicates in form of
" 'encoding/json' and '/encoding/json/'
let i = go#util#StripPathSep(i)

let ret[i] = i
endfor
endfor
Expand Down
57 changes: 35 additions & 22 deletions autoload/go/util.vim
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
" PathSep returns the appropriate OS specific path separator.
function! go#util#PathSep()
if go#util#IsWin()
return '\'
endif
return '/'
if go#util#IsWin()
return '\'
endif
return '/'
endfunction

" PathListSep returns the appropriate OS specific path list separator.
function! go#util#PathListSep()
if go#util#IsWin()
return ";"
endif
return ":"
if go#util#IsWin()
return ";"
endif
return ":"
endfunction

" LineEnding returns the correct line ending, based on the current fileformat
function! go#util#LineEnding()
if &fileformat == 'dos'
return "\r\n"
elseif &fileformat == 'mac'
return "\r"
endif
if &fileformat == 'dos'
return "\r\n"
elseif &fileformat == 'mac'
return "\r"
endif

return "\n"
return "\n"
endfunction

" IsWin returns 1 if current OS is Windows or 0 otherwise
function! go#util#IsWin()
let win = ['win16', 'win32', 'win32unix', 'win64', 'win95']
for w in win
if (has(w))
return 1
endif
endfor

return 0
let win = ['win16', 'win32', 'win32unix', 'win64', 'win95']
for w in win
if (has(w))
return 1
endif
endfor

return 0
endfunction

" StripPath strips the path's last character if it's a path separator.
" example: '/foo/bar/' -> '/foo/bar'
function! go#util#StripPathSep(path)
let last_char = strlen(a:path) - 1
if a:path[last_char] == go#util#PathSep()
return strpart(a:path, 0, last_char)
endif

return a:path
endfunction

" vim:ts=4:sw=4:et
12 changes: 11 additions & 1 deletion doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ COMMANDS *go-commands*
Rename the identifier under the cursor to the desired new name. If no
argument is given a prompt will ask for the desired identifier.


*:GoOracleScope*
:GoOracleScope [path1] [path2] ...

Changes the custom |g:go_oracle_scope| setting and overrides it with the
given import paths. The custom scope is cleared (unset) if `""` is given
as the only path. If no arguments is given it prints the current custom
scope.

*:GoCallees*
:GoCallees

Expand Down Expand Up @@ -654,7 +663,8 @@ is used. Use "neosnippet" for neosnippet.vim: >

Use this option to define the scope of the analysis to be passed for Oracle
related commands, such as |GoImplements|, |GoCallers|, etc.. By default it's
not set, so only the current packages go files are passed as scope. For more
not set, so only the current packages go files are passed as scope. You can
change it on-the-fly with |GoOracleScope|. For more
info please have look at Oracle's User Manual:
https://docs.google.com/document/d/1SLk36YRjjMgKqe490mSRzOPYEDe0Y_WQNRv-EiFYUyw/view#heading=h.nwso96pj07q8
>
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ command! -range=% GoFreevars call go#oracle#Freevars(<count>)
command! -range=% GoChannelPeers call go#oracle#ChannelPeers(<count>)
command! -range=% GoReferrers call go#oracle#Referrers(<count>)

command! -nargs=* -complete=customlist,go#package#Complete GoOracleScope call go#oracle#Scope(<f-args>)

" tool
command! -nargs=0 GoFiles echo go#tool#Files()
command! -nargs=0 GoDeps echo go#tool#Deps()
Expand Down

0 comments on commit 3037995

Please sign in to comment.