I've installed Haskell on my Mac using Homebrew, that is brew install ghc haskell-platform
.
I'm looking for a way to generate a ctags file of the standard Haskell Platform libraries (modules) so I could browse the source while coding in Vim. I specifically need Prelude
and the other most popular modules, like Data.List
and such.
I am aware that the source is available on the web via Hoogle, but It'll be easier for me to jump-to-source whenever I need to, for learning purposes.
tags
file somewhere and tell Vim to read from it. I also understand there's no need to re-generate the tags
file, since these modules are pretty much static and don't get updated very often.1) and 2) were answered by permeakra in comments. I'll try to cover 3) by describing setup similar to the one I'm using. First simple solution for base libraries, then more generic solution for whatever Haskell source package in general.
As a prerequisites we will need a tool which generates tags file for Haskell:
cabal install hothasktags
Instead of hothasktags
you might use your favourite one. See for example https://github.com/bitc/lushtags page which enumerates some of these.
Then we need to have sources for base libraries available. Here I'm using the ones from GitHub:
cd /space/haskell/sources/ # tweak to your personal taste
git clone https://github.com/ghc/packages-base.git
Optionally we might switch to particular branch. E.g.:
git checkout ghc-7.4
Run git branch -a
to see all possibilities.
Now let's generate tags for the base libraries (I do not have Mac available and thus have to assume the command works there or you are able to tweak it appropriately):
cd packages-base
export LC_ALL=C # needed for case-sensitive searching
find -type f | egrep \.hs$\|\.lhs$ | xargs -Ii hothasktags i | sort > tags
(Note about sort: My Vim complains when I do not use the sort. For LC_ALL
explanation see for example this blog post)
Now we need to let the Vim know about the tags we generated. The easiest way is probably to put the following line into your $HOME/.vimrc
:
autocmd FileType haskell setlocal tags+=/space/haskell/sources/packages-base/tags
This way the tags for base libraries will be set for each Haskell file we open. If this is not desirable we can put following Vim command into .vimrc
:
autocmd FileType haskell command! SetGHCTags
\ setlocal tags+=/space/haskell/sources/packages-base/tags
and call :SetGHCTags
on demand.
For more generic solution which works with all Haskell sources packages we can use the following function (put into .vimrc
or into Vim file dedicated to Haskell filetype):
" Add 'tags' of the given package to the current tag stack. The package sources
" must be available in "/space/haskell/sources/<package>" and the tags must be
" generated for it.
fun! s:SetHaskellTags(pathInHaskellSrcDir) "{{{
let tagFile = "/space/haskell/sources/" . a:pathInHaskellSrcDir . "/tags"
if filereadable(tagFile)
exe "setlocal tags+=" . tagFile
else
echoerr "File does not exist or is not readable: " . tagFile
endif
endfunction "}}}
command! -nargs=1 SetHaskellTags call <SID>SetHaskellTags(<args>)
Utilizing it for example for Shelly.hs
library:
cd /space/haskell/sources/
git clone https://github.com/yesodweb/Shelly.hs.git
cd Shelly.hs
regenerate-haskell-tags # [1]
In Vim just call:
:SetHaskellTags "Shelly.hs"
There is space for improvement - SetHaskellTags
could generate tags if not exist, or could even fetch the sources, configurable Haskell source code storage, directory completion, etc. But works good enough for me now. So at least sharing the solution I have. Will come back here if I get to some of these improvement done.
[1]: It's better to store regenerate-haskell-tags
in your $PAHT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With