From a3f00c4e629b34d17bb5522a9bb4c093b70f6de6 Mon Sep 17 00:00:00 2001 From: Carlo Hamalainen Date: Mon, 25 Jul 2016 20:53:08 +0800 Subject: [PATCH] Add commands GhcModOpenHaddockVismode and GhcModEchoUrlVismode for looking up symbol passed as visual mode selection. --- after/ftplugin/haskell/ghcmod.vim | 11 +++++- autoload/ghcmod.vim | 21 ++++++++++ autoload/ghcmod/command.vim | 64 +++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/after/ftplugin/haskell/ghcmod.vim b/after/ftplugin/haskell/ghcmod.vim index 5492baa..113248a 100644 --- a/after/ftplugin/haskell/ghcmod.vim +++ b/after/ftplugin/haskell/ghcmod.vim @@ -59,6 +59,11 @@ command! -buffer -nargs=0 -bang GhcModCheckAsync call ghcmod#command#async_make( command! -buffer -nargs=0 -bang GhcModLintAsync call ghcmod#command#async_make('lint', 0) command! -buffer -nargs=0 -bang GhcModCheckAndLintAsync call ghcmod#command#check_and_lint_async(0) command! -buffer -nargs=0 -bang GhcModExpand call ghcmod#command#expand(0) +command! -buffer -nargs=? -bang GhcModOpenDoc call ghcmod#command#opendoc(, 0) +command! -buffer -nargs=? -bang GhcModDocUrl call ghcmod#command#echo_doc_url(, 0) +command! -buffer -nargs=? -bang GhcModOpenHaddockVismode call ghcmod#command#opendoc(, 0, 1) +command! -buffer -nargs=? -bang GhcModEchoUrlVismode call ghcmod#command#echo_doc_url(, 0, 1) + let b:undo_ftplugin .= join(map([ \ 'GhcModType', \ 'GhcModTypeInsert', @@ -72,7 +77,11 @@ let b:undo_ftplugin .= join(map([ \ 'GhcModCheckAsync', \ 'GhcModLintAsync', \ 'GhcModCheckAndLintAsync', - \ 'GhcModExpand' + \ 'GhcModExpand', + \ 'GhcModOpenDoc', + \ 'GhcModDocUrl', + \ 'GhcModOpenHaddockVismode', + \ 'GhcModEchoUrlVismode', \ ], '"delcommand " . v:val'), ' | ') let b:undo_ftplugin .= ' | unlet b:did_ftplugin_ghcmod' diff --git a/autoload/ghcmod.vim b/autoload/ghcmod.vim index 145c614..390636e 100644 --- a/autoload/ghcmod.vim +++ b/autoload/ghcmod.vim @@ -22,6 +22,27 @@ function! ghcmod#info(fexp, path, ...) "{{{ return s:remove_dummy_prefix(l:output) endfunction "}}} +function! ghcmod#get_visual_selection() + " http://stackoverflow.com/a/6271254 + let [lnum1, col1] = getpos("'<")[1:2] + let [lnum2, col2] = getpos("'>")[1:2] + let lines = getline(lnum1, lnum2) + let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)] + let lines[0] = lines[0][col1 - 1:] + return join(lines, "\n") +endfunction + +function! ghcmod#get_doc_url(path, module, fexp, line, col) "{{{ + let l:cmd = ghcmod#build_command(['imported-from', a:path, a:line, a:col, a:fexp]) + let l:output = ghcmod#system(l:cmd) + let l:lines = split(l:output, '\n') + let l:lastline = l:lines[-1] + + if l:lastline =~ "^file.*" + return l:lastline + endif +endfunction "}}} + function! ghcmod#split(line, col, path, ...) "{{{ " `ghc-mod split` is available since v5.0.0. let l:cmd = ghcmod#build_command(['split', a:path, a:line, a:col]) diff --git a/autoload/ghcmod/command.vim b/autoload/ghcmod/command.vim index d8541a7..19bfe7d 100644 --- a/autoload/ghcmod/command.vim +++ b/autoload/ghcmod/command.vim @@ -271,4 +271,68 @@ function! s:open_quickfix() "{{{ endif endfunction "}}} +function! ghcmod#command#opendoc(fexp, force, vismode) "{{{ + let l:path = s:buffer_path(a:force) + if empty(l:path) + return + endif + let l:fexp = a:fexp + if empty(l:fexp) + if a:vismode + let l:fexp = ghcmod#get_visual_selection() + else + let l:fexp = ghcmod#getHaskellIdentifier() + endif + end + + let l:line = line('.') + let l:col = col('.') + + let l:doc_url = ghcmod#get_doc_url(l:path, ghcmod#detect_module(), l:fexp, l:line, l:col) + + if l:doc_url =~ '^file' + if exists('g:ghcmod_browser') + execute 'silent !' . g:ghcmod_browser . ' ' . l:doc_url . ' >& /dev/null &' + execute ':redraw!' + else + if has("win") + echo 'Error, not implemented. Go here: ' . l:doc_url + endif + + if has("unix") + if system('uname')=~'Darwin' + " Redirect output to /dev/null? + execute "silent !open " . l:doc_url + else + execute "silent !xdg-open " . l:doc_url . ' >& /dev/null' + endif + + execute ':redraw!' + endif + endif + else + echo l:doc_url + endif +endfunction "}}} + +function! ghcmod#command#echo_doc_url(fexp, force, vismode) "{{{ + let l:path = s:buffer_path(a:force) + if empty(l:path) + return + endif + let l:fexp = a:fexp + if empty(l:fexp) + if a:vismode + let l:fexp = ghcmod#get_visual_selection() + else + let l:fexp = ghcmod#getHaskellIdentifier() + endif + end + + let l:line = line('.') + let l:col = col('.') + + echo ghcmod#get_doc_url(l:path, ghcmod#detect_module(), l:fexp, l:line, l:col) +endfunction "}}} + " vim: set ts=2 sw=2 et fdm=marker: