Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically continue leading dashes in Haskell comments on text wrap with Vim

I am editing my Haskell source files with Vim and have set the 'textwidth' option to automatically wrap over-length lines. So my ~/.vimrc look like:

set tw=78

Now when I write a line that is longer than 78 chars the last word gets automatically taken to the next line. This is very useful on writing comments in source files. The nice thing is that within comments the leading comment symbols are automatically inserted at the beginning of each line when the last line was too long and was automatically wrapped. But this works just for C-style comments but unfortunately not for Haskell leading dashes.

So, for example in a JS file

/**
 * This is a very long comment which is longer than 78 chars, yeah really 
 * longer than 78 chars...

gets wrapped nicely as well as

// This is a very long comment which is longer than 78 chars, yeah really 
// longer than 78 chars...

So now it would be nice if the same happens in Haskell comments, like

------------------------------------------------------------------------------
-- | This is a very long module description which is longer than 78 chars,
--   yeah really longer than 78 chars...

Does anybody know if and how this is possible? Where is this behavior specified? Does :set formatexpr or :set formatoptions help me there?

Thanks in advance for your help!

=<< Johannes

/edit: btw I use vim 7.3.x

like image 700
JHannes Avatar asked May 23 '12 12:05

JHannes


2 Answers

You should check the formatoptions setting; for me, it has the value croql. c means that comments are continued on the next line. See :help 'formatoptions'

:set formatoptions=croql
like image 181
daniel kullmann Avatar answered Sep 28 '22 08:09

daniel kullmann


I'm not a vim user (and haskell-mode for emacs usually just "does the right thing," gracefully adapting to a certain indentation style), but the following seemed to work for my quick test:

:set comments=:--\ ,sr:{-,m:\ ,ex:-}

This does not handle -- | specifically, but I've actually never seen something in Haskell indented like this:

-- | Bla Bla
--   continues right under the Bla

It is far more common afaik to indent a comment like this:

-- | Usually a one-line short description.
--
-- Continued like this, with a longer description.

Or, this variant:

-- | Bla bla slightly longer than one line description
-- bla bla.
--
-- Bla bla additional details.

If you want a long running body of text describing your declaration, you can start it off as a multi-line comment instead:

{-|
The definition of 'myfunc', Act I, Chapter 1. Bla bla bla bla
bla bla...
-}
like image 27
dflemstr Avatar answered Sep 28 '22 08:09

dflemstr