neovimhaskell / haskell-vim

Custom Haskell Vimscripts
BSD 2-Clause "Simplified" License
681 stars 84 forks source link

Bad indentation on space #73

Open osa1 opened 8 years ago

osa1 commented 8 years ago

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


-- | 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)

raichoo commented 8 years ago

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.

osa1 commented 8 years ago

(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.

raichoo commented 8 years ago

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 commented 8 years ago

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?

osa1 commented 8 years ago

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.

raichoo commented 8 years ago

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 commented 8 years ago

By the way, these changes are now on master.

mitchellwrosen commented 8 years ago

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?

raichoo commented 8 years ago

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?

mitchellwrosen commented 8 years ago

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.

raichoo commented 8 years ago

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

mitchellwrosen commented 8 years ago

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

raichoo commented 8 years ago

I have merged this into master.

mitchellwrosen commented 7 years ago

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 commented 7 years ago

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.

raichoo commented 7 years ago

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

xintron commented 7 years ago

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.

raichoo commented 7 years ago

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

raichoo commented 7 years ago

@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 commented 7 years ago

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

cdepillabout commented 7 years ago

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