Skip to content

Commit

Permalink
feat(plugin browse): support go,gomod,gosum
Browse files Browse the repository at this point in the history
  • Loading branch information
aceforeverd committed Aug 25, 2024
1 parent 0240fa5 commit 2c5fd59
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
16 changes: 14 additions & 2 deletions autoload/aceforeverd/keymap/browse.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
let g:browse_fmt_pattern = {
\ 'go': 'https://pkg.go.dev/%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()
Expand All @@ -17,8 +23,14 @@ function! aceforeverd#keymap#browse#get_uri() abort
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 []
endif

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

Expand All @@ -30,7 +42,7 @@ function! aceforeverd#keymap#browse#open(uris) abort
endif

if type(a:uris) == v:t_string
call aceforeverd#keymap#browse#open_uri('https://github.com/' .. a:uris)
call aceforeverd#keymap#browse#open_uri(a:uris)
elseif len(a:uris) == 1
call aceforeverd#keymap#browse#open(a:uris[0])
else
Expand Down
7 changes: 7 additions & 0 deletions autoload/aceforeverd/keymap/essential.vim
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ function! aceforeverd#keymap#essential#setup() abort
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 <silent> zS :<c-u>call aceforeverd#util#syn_query()<cr>
Expand Down
4 changes: 2 additions & 2 deletions lua/aceforeverd/keymap/plugin_browse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ function M.select_browse_plugin(plugin_list)
vim.ui.select(plugin_list, {
prompt = 'select a plugin',
format_item = function(item)
return 'https://github.com/' .. item
return item
end,
}, function(choice)
if choice ~= nil then
vim.api.nvim_call_function('plugin_browse#open', { choice })
vim.api.nvim_call_function('aceforeverd#keymap#browse#open', { choice })
end
end)
end
Expand Down

0 comments on commit 2c5fd59

Please sign in to comment.