Skip to content

Commit 3037995

Browse files
committed
Merge pull request #437 from fatih/oracle-improvements
Oracle improvements
2 parents d0b1ff4 + 8760678 commit 3037995

File tree

5 files changed

+85
-32
lines changed

5 files changed

+85
-32
lines changed

autoload/go/oracle.vim

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
" -*- text -*-
21
" oracle.vim -- Vim integration for the Go oracle.
32
"
43
" Part of this plugin was taken directly from the oracle repo, however it's
@@ -46,7 +45,7 @@ endfun
4645
" via regex.
4746
func! s:qflistSecond(output)
4847
" backup users errorformat, will be restored once we are finished
49-
let old_errorformat = &errorformat
48+
let old_errorformat = &errorformat
5049

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

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

65-
let &errorformat = old_errorformat
64+
let &errorformat = old_errorformat
6665
endfun
6766

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

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

143142
return out
144-
endfun
143+
endfunc
144+
145+
function! go#oracle#Scope(...)
146+
if len(a:000)
147+
if len(a:000) == 1 && a:1 == '""'
148+
let g:go_oracle_scope = ""
149+
echon "vim-go: " | echohl Function | echon "oracle scope is cleared"| echohl None
150+
else
151+
let g:go_oracle_scope = join(a:000, ' ')
152+
echon "vim-go: " | echohl Function | echon "oracle scope changed to: '". g:go_oracle_scope ."'" | echohl None
153+
endif
154+
155+
return
156+
endif
145157

158+
if !exists(g:go_oracle_scope)
159+
echon "vim-go: " | echohl Function | echon "oracle scope is not set"| echohl None
160+
else
161+
echon "vim-go: " | echohl Function | echon "current oracle scope: '". g:go_oracle_scope ."'" | echohl None
162+
endif
163+
endfunction
146164

147165
" Show 'implements' relation for selected package
148166
function! go#oracle#Implements(selected)
@@ -193,3 +211,4 @@ function! go#oracle#Referrers(selected)
193211
endfunction
194212

195213
" vim:ts=4:sw=4:et
214+
"

autoload/go/package.vim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ endfunction
112112

113113
function! go#package#Complete(ArgLead, CmdLine, CursorPos)
114114
let words = split(a:CmdLine, '\s\+', 1)
115-
if len(words) > 2 && words[0] != "GoImportAs"
115+
116+
" do not complete package members for these commands
117+
let neglect_commands = ["GoImportAs", "GoOracleScope"]
118+
119+
if len(words) > 2 && index(neglect_commands, words[0]) == -1
116120
" Complete package members
117121
return go#package#CompleteMembers(words[1], words[2])
118122
endif
@@ -138,6 +142,11 @@ function! go#package#Complete(ArgLead, CmdLine, CursorPos)
138142
endif
139143
let i = substitute(substitute(i[len(r)+1:], '[\\]', '/', 'g'),
140144
\ '\.a$', '', 'g')
145+
146+
" without this the result can have duplicates in form of
147+
" 'encoding/json' and '/encoding/json/'
148+
let i = go#util#StripPathSep(i)
149+
141150
let ret[i] = i
142151
endfor
143152
endfor

autoload/go/util.vim

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
11
" PathSep returns the appropriate OS specific path separator.
22
function! go#util#PathSep()
3-
if go#util#IsWin()
4-
return '\'
5-
endif
6-
return '/'
3+
if go#util#IsWin()
4+
return '\'
5+
endif
6+
return '/'
77
endfunction
88

99
" PathListSep returns the appropriate OS specific path list separator.
1010
function! go#util#PathListSep()
11-
if go#util#IsWin()
12-
return ";"
13-
endif
14-
return ":"
11+
if go#util#IsWin()
12+
return ";"
13+
endif
14+
return ":"
1515
endfunction
1616

1717
" LineEnding returns the correct line ending, based on the current fileformat
1818
function! go#util#LineEnding()
19-
if &fileformat == 'dos'
20-
return "\r\n"
21-
elseif &fileformat == 'mac'
22-
return "\r"
23-
endif
19+
if &fileformat == 'dos'
20+
return "\r\n"
21+
elseif &fileformat == 'mac'
22+
return "\r"
23+
endif
2424

25-
return "\n"
25+
return "\n"
2626
endfunction
2727

2828
" IsWin returns 1 if current OS is Windows or 0 otherwise
2929
function! go#util#IsWin()
30-
let win = ['win16', 'win32', 'win32unix', 'win64', 'win95']
31-
for w in win
32-
if (has(w))
33-
return 1
34-
endif
35-
endfor
36-
37-
return 0
30+
let win = ['win16', 'win32', 'win32unix', 'win64', 'win95']
31+
for w in win
32+
if (has(w))
33+
return 1
34+
endif
35+
endfor
36+
37+
return 0
3838
endfunction
39+
40+
" StripPath strips the path's last character if it's a path separator.
41+
" example: '/foo/bar/' -> '/foo/bar'
42+
function! go#util#StripPathSep(path)
43+
let last_char = strlen(a:path) - 1
44+
if a:path[last_char] == go#util#PathSep()
45+
return strpart(a:path, 0, last_char)
46+
endif
47+
48+
return a:path
49+
endfunction
50+
51+
" vim:ts=4:sw=4:et

doc/vim-go.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ COMMANDS *go-commands*
304304
Rename the identifier under the cursor to the desired new name. If no
305305
argument is given a prompt will ask for the desired identifier.
306306

307+
308+
*:GoOracleScope*
309+
:GoOracleScope [path1] [path2] ...
310+
311+
Changes the custom |g:go_oracle_scope| setting and overrides it with the
312+
given import paths. The custom scope is cleared (unset) if `""` is given
313+
as the only path. If no arguments is given it prints the current custom
314+
scope.
315+
307316
*:GoCallees*
308317
:GoCallees
309318

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

655664
Use this option to define the scope of the analysis to be passed for Oracle
656665
related commands, such as |GoImplements|, |GoCallers|, etc.. By default it's
657-
not set, so only the current packages go files are passed as scope. For more
666+
not set, so only the current packages go files are passed as scope. You can
667+
change it on-the-fly with |GoOracleScope|. For more
658668
info please have look at Oracle's User Manual:
659669
https://docs.google.com/document/d/1SLk36YRjjMgKqe490mSRzOPYEDe0Y_WQNRv-EiFYUyw/view#heading=h.nwso96pj07q8
660670
>

ftplugin/go/commands.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ command! -range=% GoFreevars call go#oracle#Freevars(<count>)
5454
command! -range=% GoChannelPeers call go#oracle#ChannelPeers(<count>)
5555
command! -range=% GoReferrers call go#oracle#Referrers(<count>)
5656

57+
command! -nargs=* -complete=customlist,go#package#Complete GoOracleScope call go#oracle#Scope(<f-args>)
58+
5759
" tool
5860
command! -nargs=0 GoFiles echo go#tool#Files()
5961
command! -nargs=0 GoDeps echo go#tool#Deps()

0 commit comments

Comments
 (0)