|
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 = { |
2 | 10 | \ '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', |
3 | 28 | \ 'gomod': 'https://%s',
|
4 | 29 | \ 'gosum': 'https://%s',
|
5 |
| - \ '*': 'https://github.com/%s', |
6 | 30 | \ }
|
7 | 31 |
|
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, '*', '')) |
13 | 42 | endif
|
14 | 43 |
|
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', '') |
21 | 44 | if l:pattern == ''
|
22 | 45 | echomsg 'no url patern specified for filetype ' . &filetype
|
23 |
| - return [] |
| 46 | + return |
24 | 47 | endif
|
25 | 48 |
|
26 |
| - let l:fmt = get(g:browse_fmt_pattern, &filetype, get(g:browse_fmt_pattern, '*', '')) |
27 | 49 | if l:fmt == ''
|
28 | 50 | echomsg 'no fmt patern specified for filetype ' . &filetype
|
29 |
| - return [] |
| 51 | + return |
30 | 52 | endif
|
31 | 53 |
|
32 | 54 | let l:matched_list = []
|
33 | 55 | 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) |
35 | 58 | endfunction
|
36 | 59 |
|
37 | 60 | " TODO: a option to open all plugins once
|
|
0 commit comments