Top Banner
Haskell Code Tools
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Haskell code tools

Haskell Code Tools

Page 2: Haskell code tools

Types offer more than just safety

● Haskell provides structure to assist source navigation tools

● Often overlooked. Most people I pair with don’t use this stuff.

● Only danger: you get addicted to it.

Page 3: Haskell code tools

Things we’ll do

● Find Code● Find Problems● Find Docs● Autocomplete● Format Code

First the tools themselves, then editor support

Page 4: Haskell code tools

Find Code

Where is a symbol defined?

Create a ctags index with

hasktags

Page 5: Haskell code tools

Hasktags Experiments

● Demonstrate it with “-o -”● Try the options, and notice the problem of

“close implementations”● Combine with grep for primitive lookup

Page 6: Haskell code tools

Find Code

ALL THE SYMBOLS!

codex

“the xargs of cabal”It requires some configuration(Stack support in progress)

Page 7: Haskell code tools

Codex

● Inspect the configuration file, ~/.codex● Watch it at work by swapping command

○ tagsCmd: echo '$SOURCES' >> '$TAGS'● Considerations of full paths

○ Perhaps helps the editor jump to correct file○ But makes tags file break if dir moved

Page 8: Haskell code tools

Find Code

Callers of a symbol (the opposite direction)

hscope

More accurate than ack, since hscope is aware of syntax.

Page 9: Haskell code tools

hscope?

Page 10: Haskell code tools

hscope

● First build an index with -b○ git ls-files | grep '\.l\?hs$' | xargs

hscope -b "$@"● Perhaps codex would be nice here, but

would need a new config file● Then we can use -3 to find callers● Other features too, in a way subsumes

hasktags, but want vim ctags support

Page 11: Haskell code tools

Find Docs

Local hoogle

Good for editor integration

Let’s explore the hoogle database(s)

Page 12: Haskell code tools

Hoogle

● Location of db is compiled into the binary● Override when installing

○ cabal install hoogle \ --datadir=/foo/bar

● Two basic ways to populate db○ hoogle data○ hoogle data all

● The second one builds everything!

Page 13: Haskell code tools

Getting the Hoogle db right

● If you include too many packages your searches can yield false positives

● And too few is of course bad.● Default “hoogle data” installs 42 of the most

popular packages● And “hoogle data all” drags in all of hackage● ...What about tailored per project dbs?

Page 14: Haskell code tools

Hoobuddy

● To examine a project and build a custom hoogle db you can use hoobuddy

● hoobuddy fetch <project.cabal>● Creates default.hoo in the current

directory which is the first place hoogle looks● Have to compile with same db location as

hoogle

Page 15: Haskell code tools

Alfred Shortcuts

● A mac thing● Hackage

○ https://hackage.haskell.org/packages/search?terms={query}

● Stackage Hoogle○ http://www.stackage.org/lts-2.17/hoogle?q={query}

● Also Hackage-Fu extension for Chrome

Page 16: Haskell code tools

Find problems

● Quick feedback with ghc-mod● ghc-mod check filename● Checks the file without creating any output

Page 17: Haskell code tools

Find (potential) problems

● hlint file-or-directory● Like having a Haskell teacher sit over your

shoulder● Ignore warnings of your choice with HLint.hsimport "hint" HLint.Defaultignore "Eta reduce"

● Enable in continuous integration

Page 18: Haskell code tools

Autocomplete

● The fuel for in-editor suggestions● Find the materials with ghc-mod● ghc-mod list to see visible modules● ghc-mod browse

○ options to show types● ghc-mod lang to see extensions● ghc-mod type easier to demo in an editor

Page 19: Haskell code tools

Auto formatting

● Many dislike Haskell’s touchy indentation● Use stylish-haskell for a quick touchup

○ Cleans up import statements and data records○ demo with icdiff○ Hard-coded config path, try -v to see

● Or try the big (slow) guns, hindent○ Choose from “styles”○ Layout algorithms inspired by actual Haskellers

● Git commit hook to end all arguments

Page 20: Haskell code tools

demo

● Try PostgREST/Auth.hs● gq on imports● find uses of e.g. setRole● where is something

defined?● type inspect cs function● hlint after removal● check correctness

● Autocomplete from sandbox

● tt for ghc-mod list

● hoogle search and info

Page 21: Haskell code tools

Try it and Contribute Back

http:// github.com / begriffs / haskell-vim-now

● One-line installer● Backs up your configuration● Installs necessary tools to your system