Skip to content

Commit

Permalink
another go to upstream map
Browse files Browse the repository at this point in the history
  • Loading branch information
aceforeverd committed Aug 25, 2024
1 parent 2c5fd59 commit c6fe045
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
1 change: 1 addition & 0 deletions autoload/aceforeverd/cmd/essential.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ endfunction

function! aceforeverd#cmd#essential#setup() abort
command! -nargs=? Apache silent call <SID>apache(<args>)
command! -bang -nargs=0 Gdep call aceforeverd#keymap#browse#try_open(<bang>0)
endfunction
57 changes: 40 additions & 17 deletions autoload/aceforeverd/keymap/browse.vim
Original file line number Diff line number Diff line change
@@ -1,37 +1,60 @@
let g:browse_fmt_pattern = {
" Default to <quote>username/repo<quote>, for filetype vim,lua,tmux
let g:browse_uri_pattern = {
\ '*': '[''"]\zs[^''"/ ]\+\/[^''"/ ]\+\ze[''"]',
\ 'yaml': 'uses:\s\+\zs[^@]\+\/[^@]\+\ze',
\ 'gomod': '\v\zs([[:alnum:]\._-]+)(\/([[:alnum:]\._-])+)*\ze\s+v.*',
\ 'gosum': '\v^\zs([[:alnum:]\._-]+)(\/([[:alnum:]\._-])+)*\ze\s+v.*',
\ 'go': '\v"\zs([[:alnum:]\._-]+)(\/([[:alnum:]\._-])+)*\ze"',
\ }
let g:browse_uri_fmt = {
\ 'go': 'https://pkg.go.dev/%s',
\ 'gomod': 'https://pkg.go.dev/%s',
\ 'gosum': 'https://pkg.go.dev/%s',
\ '*': 'https://github.com/%s',
\ }

" For gomod/gosum filetype: ^domain/sub[/sub ...]
" 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,
" then the suffix removed selection (if exists) if preferred in regex engine.
" It is not perfect when dealing with upstream string 'github.com/uname/repo/subpath', this path never exists.
let g:browse_upstream_pattern = {
\ '*': '\v[''"]\zs[^''"/ ]+\/[^''"/ ]+\ze[''"]',
\ 'gosum': '\v^\zs[[:alnum:]_-]+(\.([[:alnum:]_-])+)+(\/[[:alnum:]_\.-]+){-}(\ze\/v\d+|\ze)\s+v.*',
\ 'gomod': '\v\zs[[:alnum:]_-]+(\.([[:alnum:]_-])+)+(\/[[:alnum:]_\.-]+){-}(\ze\/v\d+|\ze)\s+v.*',
\ }
let g:browse_upstream_fmt = {
\ '*': 'https://github.com/%s',
\ 'go': 'https://%s',
\ 'gomod': 'https://%s',
\ 'gosum': 'https://%s',
\ '*': 'https://github.com/%s',
\ }

function! aceforeverd#keymap#browse#try_open() abort
let l:uri_list = aceforeverd#keymap#browse#get_uri()
if empty(l:uri_list)
echomsg 'no plugin uri found here, line ' . line('.')
return
function! aceforeverd#keymap#browse#try_open(go_upstream = v:false) abort
let l:pattern = ''
let l:fmt = ''
" default pattern: quotes with inside {owner}/{repo}, support multiple matches in the given string
if !a:go_upstream
let l:pattern = get(g:browse_uri_pattern, &filetype, get(g:browse_uri_pattern, '*', ''))
let l:fmt = get(g:browse_uri_fmt, &filetype, get(g:browse_uri_fmt, '*', ''))
else
let l:pattern = get(g:browse_upstream_pattern, &filetype, get(g:browse_upstream_pattern, '*', ''))
let l:fmt = get(g:browse_upstream_fmt, &filetype, get(g:browse_upstream_fmt, '*', ''))
endif

call aceforeverd#keymap#browse#open(l:uri_list)
endfunction

function! aceforeverd#keymap#browse#get_uri() abort
" default pattern: quotes with inside {owner}/{repo}, support multiple matches in the given string
let l:pattern = get(b:, 'uri_pattern', '')
if l:pattern == ''
echomsg 'no url patern specified for filetype ' . &filetype
return []
return
endif

let l:fmt = get(g:browse_fmt_pattern, &filetype, get(g:browse_fmt_pattern, '*', ''))
if l:fmt == ''
echomsg 'no fmt patern specified for filetype ' . &filetype
return []
return
endif

let l:matched_list = []
call substitute(getline('.'), l:pattern, '\=add(l:matched_list, printf(l:fmt, submatch(0)))', 'g')
return l:matched_list

call aceforeverd#keymap#browse#open(l:matched_list)
endfunction

" TODO: a option to open all plugins once
Expand Down
14 changes: 1 addition & 13 deletions autoload/aceforeverd/keymap/essential.vim
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,8 @@ function! aceforeverd#keymap#essential#setup() abort
nnoremap <M-k> <C-y>k
" go to plugin url

nnoremap gs :call aceforeverd#keymap#browse#try_open()<CR>
augroup gp_goto_plugin
autocmd!
autocmd FileType vim,lua,tmux let b:uri_pattern = '[''"]\zs[^''"/ ]\+\/[^''"/ ]\+\ze[''"]'
autocmd FileType yaml let b:uri_pattern = 'uses:\s\+\zs[^@]\+\/[^@]\+\ze'

" ^domain/sub[/sub ...]
" 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,
" then the suffix removed selection (if exists) if preferred in regex engine
autocmd FileType gosum let b:uri_pattern = '\v^\zs[[:alnum:]_-]+(\.([[:alnum:]_-])+)+(\/[[:alnum:]_\.-]+){-}(\ze\/v\d+|\ze)\s+v.*'
autocmd FileType gomod let b:uri_pattern = '\v\zs[[:alnum:]_-]+(\.([[:alnum:]_-])+)+(\/[[:alnum:]_\.-]+){-}(\ze\/v\d+|\ze)\s+v.*'
autocmd FileType go let b:uri_pattern = '\v"\zs([[:alnum:]\._-]+)(\/([[:alnum:]\._-])+)*\ze"'
augroup END
nnoremap <space>gs :call aceforeverd#keymap#browse#try_open(v:true)<CR>
nnoremap <silent> zS :<c-u>call aceforeverd#util#syn_query()<cr>
nnoremap <silent> zV :<c-u>call aceforeverd#util#syn_query_verbose()<cr>
Expand Down

0 comments on commit c6fe045

Please sign in to comment.