Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell and Vim: Proper Indentation

Tags:

Search for "vim haskell indent" on SO. There are lot of answers for how to configure Vim for Haskell indentation. None of them really "work". They don't provide code as is recommended by the Haskell indentation wiki page. For example, alignment of statements in a do or let block, the = and | of a data type, etc.

Does a Vim solution exist that generates code like the wiki?

like image 321
Ana Avatar asked Dec 24 '11 00:12

Ana


People also ask

Does indentation matter Haskell?

Explicit characters in place of indentation Even though the consensus among Haskell programmers is that meaningful indentation leads to better-looking code, understanding how to convert from one style to the other can help understand the indentation rules.

Is Haskell indentation sensitive?

No, Haskell indentation is not like Python. Haskell is not about indentation levels, it's all about making things line up with other things.

How do I indent in vim?

To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . )

What is auto indent vim?

Vim has four methods of indentation, namely: Autoindent – this method uses indent from the previous line for the file type you are editing. smartindent – smartindent works similarly to autoindent but recognizes the syntax for some languages such as C language.


1 Answers

This might not be the answer your are looking for, but there is a way you can follow the indentation wiki guide and be compatible with most editors.

For example, do-blocks

Instead of

myFunc x = do y <- bar
              return $ x + y

You can indent it like this

myFunx x = do
    y <- bar
    return $ x + y

This is explicitly mentioned as an acceptable alternative in the indentation wiki.

In the same way, you can format data types

data FooBar
    = Foo
    | Bar
    | Asdf

Guards

myFunc x
    | x < 0     = 0
    | otherwise = x

Where-clauses

myFunc x = x + y + c where
    y = x + 5
    c = x * y

And so on...

I personally started to use this kind of style because, like you said, no editor could reliable indent the code otherwise. This works better in all editors, as the indentation is always a multiple of four (or whatever else you pick for your base indentation level). As I used this style, I also started to prefer this consistent indentation level visually, so I wouldn't go back at this point even if editors got smarter.

like image 175
shang Avatar answered Sep 22 '22 20:09

shang