Vim: Indent code for parenthesis just like it does for braces

193 views Asked by At

I want to somehow indent my java code within parenthesis just like it'd be within braces. Here a dummy example of how it could look like:

public String hello(
    String really,
    String looong,
    String listOf,
    Optional<String> params
) {
    return params.map((p) -> "Do something with the value " + p).orElse("world");
}

I was trying to solve it using cinoptions, but seems like there is no such an option. I don't want to end up writing custom identexpr for java.

1

There are 1 answers

0
Alexey Balchunas On BEST ANSWER

Well, since I couldn't find a simple solution for that, I've overridden the indentexpr for java by putting the following code to ~/.vim/after/indent/java.vim:

setlocal indentexpr=GetMyJavaIndent()
function GetMyJavaIndent()
    " When the line starts with a ), try aligning it with the matching (
    if getline(v:lnum) =~ '^\s*)'
        call cursor(v:lnum, 1)
        silent normal! %
        let lnum = line('.')
        if lnum < v:lnum
            return indent(lnum)
        endif
    endif
    return GetJavaIndent()
endfunction