haskell-vim icon indicating copy to clipboard operation
haskell-vim copied to clipboard

Bad indentation on space

Open osa1 opened this issue 9 years ago • 21 comments

I'm collecting some problems with the indentation here, with examples.


  • Enter a comment. Make sure the comment ends with character .. E.g. something like -- | This is a function.
  • Move the the next line. -- will be automatically added. Remove those.
  • Type a symbol and then space. E.g. fun. The line will be indented. Final code will look like:
-- | This is a function.
  fun  -- this shouldn't be indented

In this code:

data Test = Test
  { x :: {-# UNPACK #-} !Int
  , y :: Int
  }

I want to unpack y too, so I move the cursor right before Int and type {-#. At this point when I add a space the line gets indented like this:

data Test = Test
  { x :: {-# UNPACK #-} !Int
         , y :: {-# _Int
  }

(_ indicates the cursor)

The line isn't indented back after the pragma is completely added:

data Test = Test
  { x :: {-# UNPACK #-} !Int
         , y :: {-# UNPACK #-} !Int
  }

Suppose I have this:

main :: IO ()
main = do
      -- blah blah blah
    line1
    line2
    line3

I want to comment-out line1 and line2, so I move my cursor to the beginning of line1 and switch to vertical insert mode, selecting the l column of line1 and line2, switching to insert mode with I and typing --. Once I insert that last space, things get indented and I get this:

main :: IO ()
main = do
      -- blah blah blah
      -- line1
    li-- line2
    line3

This is a bit hard to explain, so I recorded it here https://asciinema.org/a/5h44shozwdzmla82t6a8nqvn2


(more will come here)

osa1 avatar Aug 05 '16 10:08 osa1

I'm planning to collect examples where the indenter does things one does not expect in this thread. Additionally I'm going to push fixes to the branch indentation. Indenting is hard and very brittle, I'm thankful for edge cases where things fall apart since I can only check if it's behaving correctly for the code I write.

raichoo avatar Aug 05 '16 14:08 raichoo

(I haven't tried the indentation branch yet, but I added one more example here because it looked quite different than the first example)

EDIT: Added one more example.

osa1 avatar Aug 06 '16 14:08 osa1

Thanks a lot, it looks like I didn't handle the pragmas and block comments correctly when checking for nesting. I've pushed another fix on the indentation branch that should deal with these. Please give them a try and tell me if that fixes the issue for this case.

raichoo avatar Aug 11 '16 10:08 raichoo

I'm afraid I don't understand the comment indentation example. You start with a somewhat unusual example:

main :: IO ()
main = do
      -- blah blah blah <- why is this indented at all?
      -- line1
    li-- line2
    line3

The indenter aligns by looking at the previous line is you indent that it will use that line to do the indentation. Can you describe what kind of effect you would like the have under which circumstances?

raichoo avatar Aug 11 '16 11:08 raichoo

I just tried indentation branch and it works great, thanks!

About the unusual example: In the code base I'm working on we have all kinds of crazy coding styles and things are very inconsistent. We don't have exactly that function, but we have similarly indented comment blocks followed by de-indented code blocks and haskell-vim tries to indent the code blocks in these cases.

But I agree that it's unusual and maybe we can just ignore that.

osa1 avatar Aug 11 '16 15:08 osa1

No problem. Thanks for bringing these issues to my attention. If you find anything or have an idea how we can support more unusual indentation styles without breaking things and without diminishing convenience just let me know. I will leave this issue open for a while just in case you find something else that bothers you :)

raichoo avatar Aug 11 '16 15:08 raichoo

By the way, these changes are now on master.

raichoo avatar Aug 11 '16 17:08 raichoo

I ran into a similar case:

foo =
  { field = bar "("
  }

... insert a comma directly below the {...

foo =
  { field = bar "("
                 ,
  }

... and it jumps forward, beneath the (.

Is this also covered by the changes you made in indendation?

mitchellwrosen avatar Aug 22 '16 19:08 mitchellwrosen

I have pushed a change onto the indentation branch that should deal with this. Can you check if this fixes your problem and maybe take a shallow look if something else breaks because of this?

raichoo avatar Aug 23 '16 07:08 raichoo

Sure, but I'm (embarrassingly) not exactly sure how to test the changes. I use vundle to manage my vim packages and I don't believe there's any support for pointing at a particular branch/commit hash. It just downloads master/HEAD.

mitchellwrosen avatar Aug 23 '16 18:08 mitchellwrosen

Doesn't vundle keep git repos of the plugins? If so you could simply fetch the branch and check it out manually.

raichoo avatar Aug 23 '16 18:08 raichoo

Ah, it does. Looks good, thanks! I'll report any other weird behavior I come across.

mitchellwrosen avatar Aug 23 '16 18:08 mitchellwrosen

I have merged this into master.

raichoo avatar Aug 26 '16 07:08 raichoo

Another annoying one:

foobarlongname = [
    blah
  , what
  ,█
   ^
   Cursor here

Press space...

foobarlongname = [
    blah
  , what
                 , █
                   ^
                   Cursor here

Also, it doesn't seem to go away with g:haskell_indent_disable=1. Is that intended?

mitchellwrosen avatar Feb 02 '17 16:02 mitchellwrosen

Oh, that's weird... I manually went into my plugin folder and git pull'd, now it works fine. Not sure why :PlugInstall wasn't updating properly.

mitchellwrosen avatar Feb 02 '17 16:02 mitchellwrosen

This is intended behavior since I'm using comma-first and align them with the enclosing delimiter.

raichoo avatar Feb 02 '17 17:02 raichoo

I'm having issues with indentation as well. Given the following code block:

    [ (NS spotify spotify (className =? "Spotify") doFullFloat ) ]

If I go to the position before the closing ] and do a linebreak and insert a comma I end up with the following code (_ is the cursor):

    [ (NS spotify spotify (className =? "Spotify") doFullFloat )
    ,_]

Adding a space would now result in the following code:

    [ (NS spotify spotify (className =? "Spotify") doFullFloat ) 
      , ]

Any spaces added anywhere on the line would try and match up the comma with the opening parenthesis on the first line and not the opening bracket which (in my opinion) is the correct and expected behavior.

xintron avatar Feb 15 '17 14:02 xintron

This is indeed an interesting specimen… I have to take a closer look at that.

raichoo avatar Feb 15 '17 17:02 raichoo

@xintron I have pushed a fix with 40011132d4ffae60e7a5db04faef1a198a822ad9 but I'm not quite sure why this happens, there seems to be some odd edge case regarding synstack() which I somewhat fail to wrap my head around.

raichoo avatar Feb 15 '17 22:02 raichoo

I'm currently building a test suite for these cases so hopefully this will make the indentation more robust.

raichoo avatar Feb 26 '17 12:02 raichoo

Issue #97 has an additional case where indentation doesn't seem to be working quite right.

cdepillabout avatar Sep 29 '17 14:09 cdepillabout