11

I am trying to use Vim for some Groovy code at work. It doesn't seem to handle autoindents well when semicolons are not used. I've found a few questions related to Ruby and JavaScript with the same issues, but none of the fixes I find work for me. I do have filetype plugin indent on in my .vimrc, and do not set cindent, autoindent, or smartindent. I've tried running setlocal nocindent nosmartindent in a Groovy buffer and reindenting the file with ggVG= just in case there's a plugin setting those behind the scenes, and it still always gets it wrong.

For example, I get this on a small sample (from a personal libGDX sandbox app I'm writing in Groovy)

@Override
    void render () {
        Gdx.gl.glClearColor(0.75f, 0.75f, 0.75f, 1)
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
            batch.begin()
            batch.draw(img, 0, 0)
            font.draw(batch, "Testing", 300, 400)
            batch.end()
    }

Notice the extra indents after the annotation, the opening brace, and the first line of the function. My .vimrc is fairly complex, but I can post a link to my dotfiles if anyone thinks that will help.

DuckPuppy
  • 1,148
  • 1
  • 10
  • 21
  • Most likely you just need to update vim. What version of vim are you running? – FDinoff Jul 08 '15 at 20:22
  • Vim 7.3.547 on Raspbian (on my Raspberry Pi), 7.4.591 on Cygwin (compiled myself), 7.3 with unknown patch level on OS X Yosemite, and MacVim 7.4.712 all show the same issue. – DuckPuppy Jul 09 '15 at 12:48
  • Also, I have tested this by running `vim -u NONE -U NONE ` on each of those versions and can see the same behavior, so I'm fairly certain it's not a plugin or one of my .vimrc settings causing this. – DuckPuppy Jul 09 '15 at 12:58
  • Is the sample the only thing in your file? I can't replicate it with vim 7.4.712. `void` and `}` are flush with the annotation in the first column of the file. – FDinoff Jul 09 '15 at 15:39
  • It is in a class definition with one other function (which also has the same issue - including annotation). If I just put that snippet into a buffer and indent it, I do see that `void` and `}` are flush with the annotation, but the contents of the method are still improperly indented. Do you have the other problem with all lines after the first inside the method being indented one extra level? – DuckPuppy Jul 09 '15 at 16:46
  • Yes. (Sorry didn't realize that was part of the problem). Let me look to see what vim is doing. – FDinoff Jul 09 '15 at 16:53

2 Answers2

14

Vim doesn't come with a indent script for groovy. So it tries to use the standard indentation rules which are based on C. These rules use semicolons to determine if a line is ended which is why you get line continuation indents on the second line of the function.

Since vim doesn't include the indent script you can use the groovyindent plugin. Placed in ~/.vim/indent. You also need to run dos2unix on groovy.vim since it contains the wrong line endings.

FDinoff
  • 28,493
  • 5
  • 67
  • 88
  • That was it, and I feel a bit silly now that I didn't find that earlier. I use NeoBundle to manage my Vim plugins. Anyone that wants to do the same for the groovyindent plugin can use this snippet to install it... this will take care of running dos2unix on the file during the installation: https://gist.github.com/duckpuppy/cf5931c7abbf5389f7b9 – DuckPuppy Jul 10 '15 at 16:31
  • 5
    There is a unix version of groovyindent now: https://github.com/vim-scripts/groovyindent-unix – cmcginty Apr 22 '16 at 04:12
  • @cmcginty Great, and on linux, could simply do: `mkdir ~/.vim/indent/; curl https://raw.githubusercontent.com/vim-scripts/groovyindent-unix/master/indent/groovy.vim > ~/.vim/indent/groovy.vim`, then re-open a new vim tab, it will take effect. – user218867 Sep 06 '18 at 00:03
  • It doesn't seem to recognize and indent maps, is there a tweak for that? – rboy Sep 11 '19 at 22:48
0

Since groovy looks very much like simplified perl, for my little groovy (actually nextflow DSL) coding, setting filetype=perl followed by gg=G worked quite nicely.

mestia
  • 130
  • 5