Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I fix Groovy indenting in Vim?

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.

like image 529
DuckPuppy Avatar asked Jul 08 '15 20:07

DuckPuppy


2 Answers

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.

like image 128
mestia Avatar answered Nov 14 '22 17:11

mestia


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.

like image 41
FDinoff Avatar answered Nov 14 '22 17:11

FDinoff