Skip to content

Commit f80ff67

Browse files
committed
Merged open devdocs improvement
1 parent 3939edd commit f80ff67

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Set global variable to `1` to enable or `0` to disable. Ex:
7575
| g:vim_svelte_plugin_has_init_indent | Initially indent one tab inside `style/script` tags. | 1 |
7676
| g:vim_svelte_plugin_use_foldexpr | Enable builtin `foldexpr` foldmethod. | 0 |
7777
| g:vim_svelte_plugin_debug | Echo debug messages in `messages` list. Useful to debug if unexpected indents occur. | 0 |
78+
| g:vim_svelte_plugin_open_devdocs | Key mapping to search documentation on https://devdocs.io for the word under the cursor or selected | `<F1>` |
7879

7980
\*: Vim may be slow if the feature is enabled. Find a balance between syntax highlight and speed. By the way, custom syntax can be added in `~/.vim/syntax` or `$VIM/vimfiles/syntax`.
8081

ftplugin/svelte.vim

+37-7
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,42 @@ if executable('npx') && !empty(globpath(&runtimepath, 'compiler/svelte-check.vim
2929
compiler svelte-check
3030
let b:undo_ftplugin .= " | compiler make"
3131
endif
32-
if exists(':Open') == 2
33-
" let &keywordprg = ':Open https://devdocs.io/\#q='..&filetype
34-
nnoremap <buffer> <expr> <F1> '<cmd>Open https://devdocs.io/\#q='..&filetype..' '..expand('<cword>')..'<CR>'
35-
let b:undo_ftplugin .= " | exe 'nunmap <buffer> <F1>'"
36-
if exists('*getregion')
37-
vnoremap <buffer> <expr> <F1> '<cmd>Open https://devdocs.io/\#q='..&filetype..' '..join(getregion(getpos('v'), getpos('.')))..'<CR>'
38-
let b:undo_ftplugin .= " | exe 'vunmap <buffer> <F1>'"
32+
33+
if !exists(':Open')
34+
function! s:Open(arg)
35+
let cmd = ''
36+
if has('mac')
37+
let cmd = 'open'
38+
endif
39+
if has('linux')
40+
let cmd = 'xdg-open'
41+
endif
42+
if has('win32')
43+
let cmd = 'start'
44+
endif
45+
46+
execute '!'.cmd.' "'.a:arg.'"'
47+
endfunction
48+
49+
command -nargs=1 Open call <SID>Open(<q-args>)
50+
endif
51+
52+
function! s:OpenDevDocs() range
53+
if mode() =~# "[vV\<C-v>]" && exists('*getregion')
54+
let text = join(getregion(getpos('v'), getpos('.')))
55+
else
56+
let text = expand('<cword>')
3957
endif
58+
59+
" Remove special symbols
60+
let clean_text = substitute(text, '[#@$%^&*(){}[\]<>]', '', 'g')
61+
62+
let filetype = &filetype
63+
execute 'Open https://devdocs.io?q='.filetype.' '.clean_text
64+
endfunction
65+
66+
let s:open_devdocs = svelte#GetConfig('open_devdocs', '<F1>')
67+
if !empty(s:open_devdocs)
68+
execute 'noremap <buffer> '.s:open_devdocs.' <Cmd>call <SID>OpenDevDocs()<CR>'
69+
let b:undo_ftplugin .= " | exe 'unmap <buffer> ".s:open_devdocs."'"
4070
endif

0 commit comments

Comments
 (0)