DEV Community

loading...

Vim as Java IDE

mafflerbach profile image mafflerbach ・2 min read

Yes I know. Vim is not an IDE, it is a text editor. And the original use case of vim is not to be a full blown IDE. But the plugin possibilities and plugin Landscape is so universal, that people invest the time and wrote language server integrations. nvim, a fork of vim, includes the language server support out of the box.

But what are the expectation of an IDE.
What comes in my mind is: Autocompletion, managing imports, go to definition/implementation/references formatting and debugging as well. Syntax highlighting as well of course.

I think, the most known Plugin for auto completions and language server integration is coc-vim. But it has the drawback, that it implements an own plugin manager and ecosystem, where you have to install the language-servers which you want to use. The coc plugin which I have installed are:

"coc-java": ">=1.4.12",
"coc-java-debug": ">=0.1.4",
Enter fullscreen mode Exit fullscreen mode

To install:

:CocInstall coc-java
:CocInstall coc-java-debug
Enter fullscreen mode Exit fullscreen mode

It covers auto-completion, import management and the "go to" stuff to navigate trough your code structure.

The debug part is covered by vimspector and with the coc-java-debug extension. I don't want to go in detail how to use both extensions because I don't use the standard mappings, but if you are following the readme then Vim will be near to an IDE.

In Addition: to run my tests or start the debug process, I have some key bindings which make it easier.


" start the mvn build in a tmux pane with debug flag an the current buffer as filter parameter 
" [vimux](https://github.com/benmills/vimux) is a plugin for tmux user to interact better between tmux and vim
" I just use it to kick of my maven build in debugmode with the actual Testclass
noremap <leader>ds :VimuxRunCommand "mvn -Dmaven.surefire.debug  -Dtest=".expand("%:t:r")." test --offline"<CR>

" runs mvn test with the current buffer name a filter parameter.  
noremap <leader>rs :call RunMvnThisTest(expand("%:t:r"))<CR>

" runs all tests, but catch the output and open a new buffer with the result of the mvn build
noremap <leader>rS :call RunMvnTest()<CR>

Enter fullscreen mode Exit fullscreen mode

You can see the implementation of the vim function here.
When I am starting the Debug mode, I am using

" after starting in debug mode, the build will stuck and waiting for the attaching debugger. 
map <leader>at :CocCommand java.debug.vimspector.start<CR>
Enter fullscreen mode Exit fullscreen mode

to attach to the debugger and switch to the vimpector tab.

But you have to remember that you need in your project root a .vimspector.json with following content:

{
  "adapters": {
    "java-debug-server": {
      "name": "vscode-java",
      "port": "${AdapterPort}"
    }
  },
  "configurations": {
    "Java Attach": {
      "adapter": "java-debug-server",
      "configuration": {
        "request": "attach",
        "host": "127.0.0.1",
        "port": "5005"
      },
      "breakpoints": {
        "exception": {
          "caught": "N",
          "uncaught": "N"
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide