From c6fe0455f7f21a390589145fec05a6b7e3307881 Mon Sep 17 00:00:00 2001 From: aceforeverd Date: Sun, 25 Aug 2024 20:22:17 +0800 Subject: [PATCH] another go to upstream map --- autoload/aceforeverd/cmd/essential.vim | 1 + autoload/aceforeverd/keymap/browse.vim | 57 ++++++++++++++++------- autoload/aceforeverd/keymap/essential.vim | 14 +----- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/autoload/aceforeverd/cmd/essential.vim b/autoload/aceforeverd/cmd/essential.vim index 7984b2d..96a2a6b 100644 --- a/autoload/aceforeverd/cmd/essential.vim +++ b/autoload/aceforeverd/cmd/essential.vim @@ -9,4 +9,5 @@ endfunction function! aceforeverd#cmd#essential#setup() abort command! -nargs=? Apache silent call apache() + command! -bang -nargs=0 Gdep call aceforeverd#keymap#browse#try_open(0) endfunction diff --git a/autoload/aceforeverd/keymap/browse.vim b/autoload/aceforeverd/keymap/browse.vim index 1c2ebe6..b0ed3d3 100644 --- a/autoload/aceforeverd/keymap/browse.vim +++ b/autoload/aceforeverd/keymap/browse.vim @@ -1,37 +1,60 @@ -let g:browse_fmt_pattern = { +" Default to username/repo, 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 diff --git a/autoload/aceforeverd/keymap/essential.vim b/autoload/aceforeverd/keymap/essential.vim index b8d9ee4..b97347b 100644 --- a/autoload/aceforeverd/keymap/essential.vim +++ b/autoload/aceforeverd/keymap/essential.vim @@ -55,20 +55,8 @@ function! aceforeverd#keymap#essential#setup() abort nnoremap k " go to plugin url - nnoremap gs :call aceforeverd#keymap#browse#try_open() - 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 gs :call aceforeverd#keymap#browse#try_open(v:true) nnoremap zS :call aceforeverd#util#syn_query() nnoremap zV :call aceforeverd#util#syn_query_verbose()