Skip to content

Commit

Permalink
Add commands GhcModOpenHaddockVismode and GhcModEchoUrlVismode
Browse files Browse the repository at this point in the history
for looking up symbol passed as visual mode selection.
  • Loading branch information
carlohamalainen committed Jul 25, 2016
1 parent 1d192d1 commit a3f00c4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
11 changes: 10 additions & 1 deletion after/ftplugin/haskell/ghcmod.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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', <bang>0)
command! -buffer -nargs=0 -bang GhcModCheckAndLintAsync call ghcmod#command#check_and_lint_async(<bang>0)
command! -buffer -nargs=0 -bang GhcModExpand call ghcmod#command#expand(<bang>0)
command! -buffer -nargs=? -bang GhcModOpenDoc call ghcmod#command#opendoc(<q-args>, <bang>0)
command! -buffer -nargs=? -bang GhcModDocUrl call ghcmod#command#echo_doc_url(<q-args>, <bang>0)
command! -buffer -nargs=? -bang GhcModOpenHaddockVismode call ghcmod#command#opendoc(<q-args>, <bang>0, 1)
command! -buffer -nargs=? -bang GhcModEchoUrlVismode call ghcmod#command#echo_doc_url(<q-args>, <bang>0, 1)

let b:undo_ftplugin .= join(map([
\ 'GhcModType',
\ 'GhcModTypeInsert',
Expand All @@ -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'

Expand Down
21 changes: 21 additions & 0 deletions autoload/ghcmod.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
64 changes: 64 additions & 0 deletions autoload/ghcmod/command.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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:

0 comments on commit a3f00c4

Please sign in to comment.