mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-01-23 10:30:05 +08:00
Add lsp support for java (#2619)
This commit is contained in:
parent
efc9d28cf2
commit
be6a8786e9
2
.gitignore
vendored
2
.gitignore
vendored
@ -31,3 +31,5 @@ build/
|
||||
.coverage.covimerage
|
||||
*.swp
|
||||
.gtm/
|
||||
.metadata/
|
||||
jdt.ls-java-project/
|
||||
|
@ -395,6 +395,9 @@ function! SpaceVim#layers#core#statusline#get(...) abort
|
||||
\ . '%#SpaceVim_statusline_c_SpaceVim_statusline_z#' . s:lsep
|
||||
endif
|
||||
return st
|
||||
elseif &buftype ==# 'nofile' && bufname('%') ==# '__LanguageClient__'
|
||||
return '%#SpaceVim_statusline_a# LanguageClient %#SpaceVim_statusline_a_SpaceVim_statusline_b#' . s:lsep
|
||||
\ . '%#SpaceVim_statusline_b# ' . &filetype . ' %#SpaceVim_statusline_b_SpaceVim_statusline_c#' . s:lsep
|
||||
elseif &filetype ==# 'SpaceVimLayerManager'
|
||||
return '%#SpaceVim_statusline_a#' . s:winnr(1) . '%#SpaceVim_statusline_a_SpaceVim_statusline_b#' . s:lsep
|
||||
\ . '%#SpaceVim_statusline_b# LayerManager %#SpaceVim_statusline_b_SpaceVim_statusline_c#' . s:lsep
|
||||
|
@ -96,8 +96,11 @@ function! SpaceVim#layers#lang#java#config() abort
|
||||
call add(g:spacevim_project_rooter_patterns, 'pom.xml')
|
||||
augroup SpaceVim_lang_java
|
||||
au!
|
||||
autocmd FileType java setlocal omnifunc=javacomplete#Complete
|
||||
autocmd FileType java call s:java_mappings()
|
||||
if !SpaceVim#layers#lsp#check_filetype('java')
|
||||
" omnifunc will be used only when no java lsp support
|
||||
autocmd FileType java setlocal omnifunc=javacomplete#Complete
|
||||
call SpaceVim#mapping#gd#add('java', function('s:go_to_def'))
|
||||
endif
|
||||
autocmd FileType jsp call JspFileTypeInit()
|
||||
augroup END
|
||||
let g:neoformat_enabled_java = ['googlefmt']
|
||||
@ -114,6 +117,23 @@ endfunction
|
||||
|
||||
function! s:language_specified_mappings() abort
|
||||
|
||||
let g:_spacevim_mappings_space.l = {'name' : '+Language Specified'}
|
||||
if g:spacevim_enable_insert_leader
|
||||
inoremap <silent> <buffer> <leader>UU <esc>bgUwea
|
||||
inoremap <silent> <buffer> <leader>uu <esc>bguwea
|
||||
inoremap <silent> <buffer> <leader>ua <esc>bgulea
|
||||
inoremap <silent> <buffer> <leader>Ua <esc>bgUlea
|
||||
endif
|
||||
nmap <silent><buffer> <F4> <Plug>(JavaComplete-Imports-Add)
|
||||
imap <silent><buffer> <F4> <Plug>(JavaComplete-Imports-Add)
|
||||
|
||||
imap <silent><buffer> <C-j>I <Plug>(JavaComplete-Imports-AddMissing)
|
||||
imap <silent><buffer> <C-j>R <Plug>(JavaComplete-Imports-RemoveUnused)
|
||||
imap <silent><buffer> <C-j>i <Plug>(JavaComplete-Imports-AddSmart)
|
||||
imap <silent><buffer> <C-j>s <Plug>(JavaComplete-Generate-AccessorSetter)
|
||||
imap <silent><buffer> <C-j>g <Plug>(JavaComplete-Generate-AccessorGetter)
|
||||
imap <silent><buffer> <C-j>a <Plug>(JavaComplete-Generate-AccessorSetterGetter)
|
||||
imap <silent><buffer> <C-j>jM <Plug>(JavaComplete-Generate-AbstractMethods)
|
||||
" Import key bindings
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l','I'],
|
||||
\ '<Plug>(JavaComplete-Imports-AddMissing)',
|
||||
@ -197,27 +217,23 @@ function! s:language_specified_mappings() abort
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 's'],
|
||||
\ 'call SpaceVim#plugins#repl#send("selection")',
|
||||
\ 'send selection and keep code buffer focused', 1)
|
||||
|
||||
if SpaceVim#layers#lsp#check_filetype('java')
|
||||
nnoremap <silent><buffer> K :call SpaceVim#lsp#show_doc()<CR>
|
||||
|
||||
call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 'd'],
|
||||
\ 'call SpaceVim#lsp#show_doc()', 'show_document', 1)
|
||||
call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 'e'],
|
||||
\ 'call SpaceVim#lsp#rename()', 'rename symbol', 1)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:java_mappings() abort
|
||||
let g:_spacevim_mappings_space.l = {'name' : '+Language Specified'}
|
||||
if g:spacevim_enable_insert_leader
|
||||
inoremap <silent> <buffer> <leader>UU <esc>bgUwea
|
||||
inoremap <silent> <buffer> <leader>uu <esc>bguwea
|
||||
inoremap <silent> <buffer> <leader>ua <esc>bgulea
|
||||
inoremap <silent> <buffer> <leader>Ua <esc>bgUlea
|
||||
endif
|
||||
nmap <silent><buffer> <F4> <Plug>(JavaComplete-Imports-Add)
|
||||
imap <silent><buffer> <F4> <Plug>(JavaComplete-Imports-Add)
|
||||
|
||||
imap <silent><buffer> <C-j>I <Plug>(JavaComplete-Imports-AddMissing)
|
||||
imap <silent><buffer> <C-j>R <Plug>(JavaComplete-Imports-RemoveUnused)
|
||||
imap <silent><buffer> <C-j>i <Plug>(JavaComplete-Imports-AddSmart)
|
||||
imap <silent><buffer> <C-j>s <Plug>(JavaComplete-Generate-AccessorSetter)
|
||||
imap <silent><buffer> <C-j>g <Plug>(JavaComplete-Generate-AccessorGetter)
|
||||
imap <silent><buffer> <C-j>a <Plug>(JavaComplete-Generate-AccessorSetterGetter)
|
||||
imap <silent><buffer> <C-j>jM <Plug>(JavaComplete-Generate-AbstractMethods)
|
||||
endfunction
|
||||
|
||||
function! s:go_to_def() abort
|
||||
call SpaceVim#lsp#go_to_def()
|
||||
endfunction
|
||||
|
||||
function! s:execCMD(cmd) abort
|
||||
|
@ -445,10 +445,6 @@ endfunction
|
||||
|
||||
let s:previewd_bufnrs = []
|
||||
|
||||
function! Test() abort
|
||||
return s:previewd_bufnrs
|
||||
endfunction
|
||||
|
||||
" @vimlint(EVL103, 1, a:timer)
|
||||
function! s:preview_timer(timer) abort
|
||||
for id in filter(s:previewd_bufnrs, 'bufexists(v:val) && buflisted(v:val)')
|
||||
|
@ -10,13 +10,17 @@ commentsID: "Use Vim as a Java IDE"
|
||||
|
||||
# [Blogs](../blog/) >> Use Vim as a Java IDE
|
||||
|
||||
This is a general guide for using SpaceVim as a Java IDE, including layer configuration and usage.
|
||||
This is a general guide for using SpaceVim as a Java IDE, including layer configuration and usage.
|
||||
Each of the following sections will be covered:
|
||||
|
||||
<!-- vim-markdown-toc GFM -->
|
||||
|
||||
- [Installation](#installation)
|
||||
- [Language server](#language-server)
|
||||
- [Code completion](#code-completion)
|
||||
- [Code outline](#code-outline)
|
||||
- [Rename symbol](#rename-symbol)
|
||||
- [Javadoc hovers](#javadoc-hovers)
|
||||
- [Syntax lint](#syntax-lint)
|
||||
- [Import packages](#import-packages)
|
||||
- [Jump to test file](#jump-to-test-file)
|
||||
@ -26,7 +30,7 @@ Each of the following sections will be covered:
|
||||
|
||||
<!-- vim-markdown-toc -->
|
||||
|
||||
## Installation
|
||||
### Installation
|
||||
|
||||
SpaceVim is a Vim and neovim configuration, so you need to install vim or neovim,
|
||||
here are two guides for installing neovim and vim8 with `+python3` feature.
|
||||
@ -41,14 +45,78 @@ Press `SPC f v d` to open SpaceVim configuration file, and add following section
|
||||
name = "lang#java"
|
||||
```
|
||||
|
||||
## Code completion
|
||||
### Language server
|
||||
|
||||
To enable language server protocol support, you may need to enable lsp layer.
|
||||
|
||||
```toml
|
||||
[[layers]]
|
||||
name = "lsp"
|
||||
filetypes = [
|
||||
"java"
|
||||
]
|
||||
[layers.override_cmd]
|
||||
java = [
|
||||
"java",
|
||||
"-Declipse.application=org.eclipse.jdt.ls.core.id1",
|
||||
"-Dosgi.bundles.defaultStartLevel=4",
|
||||
"-Declipse.product=org.eclipse.jdt.ls.core.product",
|
||||
"-Dlog.protocol=true",
|
||||
"-Dlog.level=NONE",
|
||||
"-noverify",
|
||||
"-Xmx1G",
|
||||
"-jar",
|
||||
"D:\\dev\\jdt-language-server-latest\\plugins\\org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar",
|
||||
"-configuration",
|
||||
"D:\\dev\\jdt-language-server-latest\\config_win",
|
||||
"-data",
|
||||
"C:\\Users\\Administrator\\.cache\\javalsp"
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
You need to replace `D:\dev\jdt-language-server-latest\plugins\org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar` with the actual name of the org.eclipse.equinox.launcher jar
|
||||
|
||||
The configuration flag can point to either:
|
||||
|
||||
- `config_win`, for Windows
|
||||
- `config_mac`, for MacOS
|
||||
- `config_linux`, for Linux
|
||||
|
||||
The data flag value should be the absolute path to the working directory of the server.
|
||||
This should be different from the path of the user's project files (which is sent during the initialize handshake).
|
||||
|
||||
### Code completion
|
||||
|
||||
javacomplete2 which has been included in `lang#java` layer provides omnifunc for java file and deoplete source.
|
||||
with this plugin and `autocomplete` layer, the completion popup menu will be opened automatically。
|
||||
|
||||
![code complete](https://user-images.githubusercontent.com/13142418/46297202-ba0ab980-c5ce-11e8-81a0-4a4a85bc98a5.png)
|
||||
|
||||
## Syntax lint
|
||||
### Code outline
|
||||
|
||||
The default outline plugin is tagbar, and the key binding is `F2`. This key binding will open an outline sidebar on the left.
|
||||
|
||||
![java outline](https://user-images.githubusercontent.com/13142418/53250502-7c313d80-36f5-11e9-8fa2-8437ecf57a78.png)
|
||||
|
||||
To fuzzy find outline in current buffer, you need to enable a fuzzy find layer, for example denite layer,
|
||||
then press `Leader f o`:
|
||||
|
||||
![java fuzzy outline](https://user-images.githubusercontent.com/13142418/53250728-f1047780-36f5-11e9-923d-0b34568f9566.gif)
|
||||
|
||||
### Rename symbol
|
||||
|
||||
After enable lsp layer for java, you can use `SPC l e` to rename symbol under the cursor:
|
||||
|
||||
![rename java symblo](https://user-images.githubusercontent.com/13142418/53250190-da115580-36f4-11e9-9590-bf945fa8dcc0.gif)
|
||||
|
||||
### Javadoc hovers
|
||||
|
||||
The default key binding to get doc of cursor symbol is `SPC l d` or `K`:
|
||||
|
||||
![javadoc](https://user-images.githubusercontent.com/13142418/53255520-bf44de00-3700-11e9-9f47-50bc50ed6e83.gif)
|
||||
|
||||
### Syntax lint
|
||||
|
||||
`checkers` layer provides asynchronous linting feature, this layer use [neomake](https://github.com/neomake/neomake) by default.
|
||||
neomake support maven, gradle and eclipse project. it will generate classpath automatically for these project.
|
||||
@ -62,14 +130,14 @@ within above picture, we can see the checkers layer provides following feature:
|
||||
- show numbers of errors and warnings on statusline
|
||||
- show cursor error and warning information below current line
|
||||
|
||||
## Import packages
|
||||
### Import packages
|
||||
|
||||
There are two kind features for importing packages, import packages automatically and manually. SpaceVim will import the packages after selecting the class name on popmenu.
|
||||
Also, you can use key binding `<F4>` to import the class at the cursor point. If there are more than one class, a menu will be shown below current windows.
|
||||
|
||||
![import class](https://user-images.githubusercontent.com/13142418/46298485-c04e6500-c5d1-11e8-96f3-01d84f9fe237.png)
|
||||
|
||||
## Jump to test file
|
||||
### Jump to test file
|
||||
|
||||
SpaceVim use vim-project to manager the files in a project, you can add a `.projections.json` to the root of your project with following content:
|
||||
|
||||
@ -85,22 +153,23 @@ with this configuration, you can jump between the source code and test file via
|
||||
![jump-test](https://user-images.githubusercontent.com/13142418/46322905-12b57300-c61e-11e8-81a2-53c69d10140f.gif)
|
||||
|
||||
|
||||
## running code
|
||||
### running code
|
||||
|
||||
Base on JavaUnite, you can use `SPC l r c` to run current function or use `SPC l r m` to run the main function of current Class.
|
||||
|
||||
![run-main](https://user-images.githubusercontent.com/13142418/46323137-61174180-c61f-11e8-94df-61b6998b8907.gif)
|
||||
|
||||
|
||||
## Code formatting
|
||||
### Code formatting
|
||||
|
||||
For formatting java code, you also nEed have [uncrustify](https://github.com/uncrustify/uncrustify) or [astyle](http://astyle.sourceforge.net/) in your PATH.
|
||||
BTW, the google's [java formatter](https://github.com/google/google-java-format) also works well with neoformat.
|
||||
|
||||
![format-java](https://user-images.githubusercontent.com/13142418/46323426-ccadde80-c620-11e8-9726-d99025f3bf76.gif)
|
||||
|
||||
## REPL
|
||||
### REPL
|
||||
|
||||
you need to install jdk9 which provide a build-in tools `jshell`, and SpaceVim use the `jshell` as default inferior REPL process:
|
||||
|
||||
![REPl-JAVA](https://user-images.githubusercontent.com/13142418/34159605-758461ba-e48f-11e7-873c-fc358ce59a42.gif)
|
||||
|
||||
|
@ -16,7 +16,11 @@ commentsID: "使用 Vim 搭建 Java 开发环境"
|
||||
<!-- vim-markdown-toc GFM -->
|
||||
|
||||
- [启用模块](#启用模块)
|
||||
- [语言服务器](#语言服务器)
|
||||
- [代码补全](#代码补全)
|
||||
- [语法树](#语法树)
|
||||
- [重命名光标符号](#重命名光标符号)
|
||||
- [光标符号文档查询](#光标符号文档查询)
|
||||
- [语法检查](#语法检查)
|
||||
- [导包](#导包)
|
||||
- [跳转测试文件](#跳转测试文件)
|
||||
@ -41,12 +45,84 @@ SpaceVim 初次安装时默认并未启用相关语言模块。首先需要启
|
||||
`lang#java` 模块主要采用插件 vim-javacomplete2,该插件可以自动读取工程配置文件,获取当前项目的 classpath,
|
||||
目前支持的项目包括 maven、gradle 以及 eclipse 下的配置文件。
|
||||
|
||||
|
||||
### 语言服务器
|
||||
|
||||
To enable language server protocol support, you may need to enable lsp layer.
|
||||
若需要启动 Java 语言服务支持,可以启用 lsp 模块,以下配置示例使用 eclipse.jdt.ls:
|
||||
|
||||
```toml
|
||||
[[layers]]
|
||||
name = "lsp"
|
||||
filetypes = [
|
||||
"java"
|
||||
]
|
||||
[layers.override_cmd]
|
||||
java = [
|
||||
"java",
|
||||
"-Declipse.application=org.eclipse.jdt.ls.core.id1",
|
||||
"-Dosgi.bundles.defaultStartLevel=4",
|
||||
"-Declipse.product=org.eclipse.jdt.ls.core.product",
|
||||
"-Dlog.protocol=true",
|
||||
"-Dlog.level=NONE",
|
||||
"-noverify",
|
||||
"-Xmx1G",
|
||||
"-jar",
|
||||
"D:\\dev\\jdt-language-server-latest\\plugins\\org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar",
|
||||
"-configuration",
|
||||
"D:\\dev\\jdt-language-server-latest\\config_win",
|
||||
"-data",
|
||||
"C:\\Users\\Administrator\\.cache\\javalsp"
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
需要将配置中 `D:\dev\jdt-language-server-latest\plugins\org.eclipse.equinox.launcher_1.5.200.v20180922-1751.jar`
|
||||
改为 org.eclipse.equinox.launcher jar 文件的实际路径。
|
||||
|
||||
根据当前系统,选择对应的配置文件:
|
||||
|
||||
- `config_win`, Windows 系统
|
||||
- `config_mac`, MacOS 系统
|
||||
- `config_linux`, Linux 系统
|
||||
|
||||
`-data` 选项指定语言服务器的运行绝对路径。这应该不同于用户项目文件的路径。
|
||||
|
||||
### 代码补全
|
||||
|
||||
vim-javacomplete2 为 java 项目提供了很好的代码补全功能,配合 autocomplete 模块,可以在编辑代码时实时补全代码,并且可以模糊匹配。
|
||||
|
||||
![code complete](https://user-images.githubusercontent.com/13142418/46297202-ba0ab980-c5ce-11e8-81a0-4a4a85bc98a5.png)
|
||||
|
||||
### 语法树
|
||||
|
||||
默认的语法树插件是 tagbar,快捷键为 `F2`。这一快捷键将会在左侧打开一个语法树侧栏,如下图所示:
|
||||
|
||||
![java outline](https://user-images.githubusercontent.com/13142418/53250502-7c313d80-36f5-11e9-8fa2-8437ecf57a78.png)
|
||||
|
||||
如果需要使用模糊搜索快速调到当前文件中的某个函数,首先需要载入一个模糊搜索的模块,比如 denite 模块:
|
||||
|
||||
```toml
|
||||
[[layers]]
|
||||
name = "denite"
|
||||
```
|
||||
|
||||
之后使用快捷键 `Leader f o` 就可以打开模糊搜索窗口,效果图如下:
|
||||
|
||||
![java fuzzy outline](https://user-images.githubusercontent.com/13142418/53250728-f1047780-36f5-11e9-923d-0b34568f9566.gif)
|
||||
|
||||
### 重命名光标符号
|
||||
|
||||
当启用了 lsp 模块,并配置好 Java 语言服务器后,可以使用快捷键 `SPC l e` 对光标下的符号进行重命名:
|
||||
|
||||
![rename java symblo](https://user-images.githubusercontent.com/13142418/53250190-da115580-36f4-11e9-9590-bf945fa8dcc0.gif)
|
||||
|
||||
### 光标符号文档查询
|
||||
|
||||
同样的,这一功能也依赖 lsp 模块,默认的快捷键为 `SPC l d` 或者 `K`:
|
||||
|
||||
![javadoc](https://user-images.githubusercontent.com/13142418/53255520-bf44de00-3700-11e9-9f47-50bc50ed6e83.gif)
|
||||
|
||||
### 语法检查
|
||||
|
||||
`checkers` 模块为 SpaceVim 提供了异步语法检查功能,该模块主要包括插件 [neomake](https://github.com/neomake/neomake)。
|
||||
|
Loading…
Reference in New Issue
Block a user