diff --git a/autoload/aceforeverd/keymap/browse.vim b/autoload/aceforeverd/keymap/browse.vim index c7a6d24..1c2ebe6 100644 --- a/autoload/aceforeverd/keymap/browse.vim +++ b/autoload/aceforeverd/keymap/browse.vim @@ -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() @@ -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 @@ -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 diff --git a/autoload/aceforeverd/keymap/essential.vim b/autoload/aceforeverd/keymap/essential.vim index 51bec96..b8d9ee4 100644 --- a/autoload/aceforeverd/keymap/essential.vim +++ b/autoload/aceforeverd/keymap/essential.vim @@ -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 zS :call aceforeverd#util#syn_query() diff --git a/lua/aceforeverd/keymap/plugin_browse.lua b/lua/aceforeverd/keymap/plugin_browse.lua index 98c2098..cd9ea99 100644 --- a/lua/aceforeverd/keymap/plugin_browse.lua +++ b/lua/aceforeverd/keymap/plugin_browse.lua @@ -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