Skip to content

Commit c6fe045

Browse files
committed
another go to upstream map
1 parent 2c5fd59 commit c6fe045

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

autoload/aceforeverd/cmd/essential.vim

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ endfunction
99

1010
function! aceforeverd#cmd#essential#setup() abort
1111
command! -nargs=? Apache silent call <SID>apache(<args>)
12+
command! -bang -nargs=0 Gdep call aceforeverd#keymap#browse#try_open(<bang>0)
1213
endfunction

autoload/aceforeverd/keymap/browse.vim

+40-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,60 @@
1-
let g:browse_fmt_pattern = {
1+
" Default to <quote>username/repo<quote>, for filetype vim,lua,tmux
2+
let g:browse_uri_pattern = {
3+
\ '*': '[''"]\zs[^''"/ ]\+\/[^''"/ ]\+\ze[''"]',
4+
\ 'yaml': 'uses:\s\+\zs[^@]\+\/[^@]\+\ze',
5+
\ 'gomod': '\v\zs([[:alnum:]\._-]+)(\/([[:alnum:]\._-])+)*\ze\s+v.*',
6+
\ 'gosum': '\v^\zs([[:alnum:]\._-]+)(\/([[:alnum:]\._-])+)*\ze\s+v.*',
7+
\ 'go': '\v"\zs([[:alnum:]\._-]+)(\/([[:alnum:]\._-])+)*\ze"',
8+
\ }
9+
let g:browse_uri_fmt = {
210
\ 'go': 'https://pkg.go.dev/%s',
11+
\ 'gomod': 'https://pkg.go.dev/%s',
12+
\ 'gosum': 'https://pkg.go.dev/%s',
13+
\ '*': 'https://github.com/%s',
14+
\ }
15+
16+
" For gomod/gosum filetype: ^domain/sub[/sub ...]
17+
" since we want remove the optional version suffix (v\d+) in the url, it's important to use '{-}' to match subpath as few as possible,
18+
" then the suffix removed selection (if exists) if preferred in regex engine.
19+
" It is not perfect when dealing with upstream string 'github.com/uname/repo/subpath', this path never exists.
20+
let g:browse_upstream_pattern = {
21+
\ '*': '\v[''"]\zs[^''"/ ]+\/[^''"/ ]+\ze[''"]',
22+
\ 'gosum': '\v^\zs[[:alnum:]_-]+(\.([[:alnum:]_-])+)+(\/[[:alnum:]_\.-]+){-}(\ze\/v\d+|\ze)\s+v.*',
23+
\ 'gomod': '\v\zs[[:alnum:]_-]+(\.([[:alnum:]_-])+)+(\/[[:alnum:]_\.-]+){-}(\ze\/v\d+|\ze)\s+v.*',
24+
\ }
25+
let g:browse_upstream_fmt = {
26+
\ '*': 'https://github.com/%s',
27+
\ 'go': 'https://%s',
328
\ 'gomod': 'https://%s',
429
\ 'gosum': 'https://%s',
5-
\ '*': 'https://github.com/%s',
630
\ }
731

8-
function! aceforeverd#keymap#browse#try_open() abort
9-
let l:uri_list = aceforeverd#keymap#browse#get_uri()
10-
if empty(l:uri_list)
11-
echomsg 'no plugin uri found here, line ' . line('.')
12-
return
32+
function! aceforeverd#keymap#browse#try_open(go_upstream = v:false) abort
33+
let l:pattern = ''
34+
let l:fmt = ''
35+
" default pattern: quotes with inside {owner}/{repo}, support multiple matches in the given string
36+
if !a:go_upstream
37+
let l:pattern = get(g:browse_uri_pattern, &filetype, get(g:browse_uri_pattern, '*', ''))
38+
let l:fmt = get(g:browse_uri_fmt, &filetype, get(g:browse_uri_fmt, '*', ''))
39+
else
40+
let l:pattern = get(g:browse_upstream_pattern, &filetype, get(g:browse_upstream_pattern, '*', ''))
41+
let l:fmt = get(g:browse_upstream_fmt, &filetype, get(g:browse_upstream_fmt, '*', ''))
1342
endif
1443

15-
call aceforeverd#keymap#browse#open(l:uri_list)
16-
endfunction
17-
18-
function! aceforeverd#keymap#browse#get_uri() abort
19-
" default pattern: quotes with inside {owner}/{repo}, support multiple matches in the given string
20-
let l:pattern = get(b:, 'uri_pattern', '')
2144
if l:pattern == ''
2245
echomsg 'no url patern specified for filetype ' . &filetype
23-
return []
46+
return
2447
endif
2548

26-
let l:fmt = get(g:browse_fmt_pattern, &filetype, get(g:browse_fmt_pattern, '*', ''))
2749
if l:fmt == ''
2850
echomsg 'no fmt patern specified for filetype ' . &filetype
29-
return []
51+
return
3052
endif
3153

3254
let l:matched_list = []
3355
call substitute(getline('.'), l:pattern, '\=add(l:matched_list, printf(l:fmt, submatch(0)))', 'g')
34-
return l:matched_list
56+
57+
call aceforeverd#keymap#browse#open(l:matched_list)
3558
endfunction
3659

3760
" TODO: a option to open all plugins once

autoload/aceforeverd/keymap/essential.vim

+1-13
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,8 @@ function! aceforeverd#keymap#essential#setup() abort
5555
nnoremap <M-k> <C-y>k
5656
5757
" go to plugin url
58-
5958
nnoremap gs :call aceforeverd#keymap#browse#try_open()<CR>
60-
augroup gp_goto_plugin
61-
autocmd!
62-
autocmd FileType vim,lua,tmux let b:uri_pattern = '[''"]\zs[^''"/ ]\+\/[^''"/ ]\+\ze[''"]'
63-
autocmd FileType yaml let b:uri_pattern = 'uses:\s\+\zs[^@]\+\/[^@]\+\ze'
64-
65-
" ^domain/sub[/sub ...]
66-
" since we want remove the optional version suffix (v\d+) in the url, it's important to use '{-}' to match subpath as few as possible,
67-
" then the suffix removed selection (if exists) if preferred in regex engine
68-
autocmd FileType gosum let b:uri_pattern = '\v^\zs[[:alnum:]_-]+(\.([[:alnum:]_-])+)+(\/[[:alnum:]_\.-]+){-}(\ze\/v\d+|\ze)\s+v.*'
69-
autocmd FileType gomod let b:uri_pattern = '\v\zs[[:alnum:]_-]+(\.([[:alnum:]_-])+)+(\/[[:alnum:]_\.-]+){-}(\ze\/v\d+|\ze)\s+v.*'
70-
autocmd FileType go let b:uri_pattern = '\v"\zs([[:alnum:]\._-]+)(\/([[:alnum:]\._-])+)*\ze"'
71-
augroup END
59+
nnoremap <space>gs :call aceforeverd#keymap#browse#try_open(v:true)<CR>
7260
7361
nnoremap <silent> zS :<c-u>call aceforeverd#util#syn_query()<cr>
7462
nnoremap <silent> zV :<c-u>call aceforeverd#util#syn_query_verbose()<cr>

0 commit comments

Comments
 (0)