2

When editing an Haskell file, I want to configure a comment format {- -} such that:

{- <cr>
 - <cr>
 - -}

But it doesn't work as expected (but almost there):

{- <cr>
- <cr>
- -}

notice that after hitting enter, the - doesn't align.

What option should I use to make it aligned?

2

The behavior of the comments is controled by the commentsoptions (see :h format-comments)

I think that your options are not properly configured, if you use the following command:

:set comments=s1:{-,m:-,e:-}

Your problem should be solved.

Lets decompose the options:

  • s is for the start of three-piece comments
  • 1 defines the offset to add to the middle and end part of the comment
  • :{- defines the comment string of the first part of the three-piece comment.

  • m is for the middle part of the three-piece comments

  • e is (surprisingly) for the end of the three-piece comments.

This is a very minimal version of this option, please see the help topic linked at the beggining of the post to see all the magic you can do.

Originally my comments option was set to s1fl:{-,mb:-,ex:-},:--

  • your setting still doesn't make the -s aligned. – Jason Hu Aug 17 '15 at 14:23
  • Hm that's pretty weird it works on my setup... I'll check if something else might interfere – statox Aug 17 '15 at 14:25
  • @HuStmpHrrr: Is cindent enabled on your system? (you can check with :set cindent? it will output cindentor nocindent) If it is enabled try disabling it with :set nocindent and see if it solves the problem. – statox Aug 17 '15 at 14:39
  • cindent is not on indeed. but why would i need cindent on in haskell anyway? will cindent corrupt the indent config set up by other plugins? – Jason Hu Aug 17 '15 at 15:04
  • Not you don't need it but when enabled it can mess your config, as I don't know your configuration I was just trying to find what could go wrong. – statox Aug 17 '15 at 15:06
0

Ok. figured it out by looking into haskell.vim, I found ad slight modification of indentexpr would work.

check it out.

first,

$ cd ~/.vim/bundle/haskell-vim/
$ vi indent/haskell.vim

then find the definition of GetHaskellIndent function.

I add following lines right after the handling of -- comment, therefore the function becomes:

function! GetHaskellIndent()
  let l:prevline = getline(v:lnum - 1)

  if l:prevline =~ '^\s*--'
    return match(l:prevline, '\S')
  endif

  if synIDattr(synID(line("."), col("."), 1), "name") == 'hsBlockComment'
      for l:c in range(v:lnum - 1, 0, -1)
          let l:bline = getline(l:c)
          if l:bline =~ '{-'
              return 1 + match(l:bline, '{-')
      endfor
      return 1
  endif

starting from if synIDattr(synID(line("."), ... is my code.

basically what it means is check whether the current position is in block comment syntax, if so, get in the for loop and find the first occurrence of {-, then return the indent as deep as the - in {-.

notice that, my haskell environment is heavily customized. you should config according to yours. though my indent is setup to use haskell.vim, my syntax highlight is setup to use vim2hs. so it's better to look into the syntax highlight file first to find out what syntax element you should look at(in vim2hs's case, it should be 'hsBlockComment', in haskell.vim it should be 'haskellBlockComment').

after having this code, I have following effect:

{-
 -
 -
 - -}

for comments in deep indent:

      {-
       -
       -
       -
       -
       - -}

works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.