1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 02:10:05 +08:00

Improve java layer (#3954)

This commit is contained in:
Wang Shidong 2020-11-16 22:31:00 +08:00 committed by GitHub
parent e114730e71
commit 322c56a74d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 234 additions and 96 deletions

View File

@ -195,6 +195,7 @@ let g:spacevim_windows_leader = 's'
""
" Enable/Disable spacevim's insert mode leader, default is enable
" This options has been deprecated.
let g:spacevim_enable_insert_leader = 1
""

View File

@ -155,7 +155,7 @@ endfunction
function! SpaceVim#default#keyBindings() abort
if g:spacevim_enable_insert_leader
inoremap <silent> <Leader><Tab> <C-r>=MyLeaderTabfunc()<CR>
" inoremap <silent> <Leader><Tab> <C-r>=MyLeaderTabfunc()<CR>
endif
" yank and paste

View File

@ -9,16 +9,36 @@
""
" @section lang#java, layer-lang-java
" @parentsection layers
" This layer is for Java development.
" This layer is for java development, disabled by default, to enable this
" layer, add following snippet to your SpaceVim configuration file.
" >
" [[layers]]
" name = 'lang#java'
" <
" @subsection Layer options
"
" 1. `format_on_save`: Enable/disabled code formatting when saving current file.
" Disabled by default.
" 2. `java_fomatter_jar`: Set the full path of google's java formatter jar.
" 3. `java_file_head`: The default file header for new java file.
" by default it is:
" >
" [[layers]]
" name = 'lang#java'
" java_file_head = [
" '/**',
" ' * @author : `fnamemodify(expand("~"), ":t")`',
" ' * @created : `strftime("%Y-%m-%d")`',
" '**/',
" ''
" ]
" <
" @subsection Mappings
" >
" Import key bindings:
"
" Mode Key Function
" -------------------------------------------------------------
" normal <F4> import class under cursor
" insert <F4> import class under cursor
" normal SPC l I import missing classes
" normal SPC l R remove unused imports
" normal SPC l i smart import class under cursor
@ -88,6 +108,23 @@
" and set 'g:spacevim_layer_lang_java_formatter' to the path of the jar.
if exists('s:java_fomatter_jar')
finish
endif
let s:java_fomatter_jar = ''
let s:format_on_save = 0
let s:java_file_head = [
\ '/**',
\ ' * @author : `fnamemodify(expand("~"), ":t")`',
\ ' * @created : `strftime("%Y-%m-%d")`',
\ '**/',
\ ''
\ ]
let s:java_interpreter = 'java'
function! SpaceVim#layers#lang#java#plugins() abort
let plugins = [
\ ['wsdjeg/vim-dict', { 'on_ft' : 'java'}],
@ -120,34 +157,41 @@ function! SpaceVim#layers#lang#java#config() abort
let g:neoformat_enabled_java = get(g:, 'neoformat_enabled_java', ['googlefmt'])
let g:neoformat_java_googlefmt = {
\ 'exe': 'java',
\ 'args': ['-jar', get(g:,'spacevim_layer_lang_java_formatter', ''), '-'],
\ 'args': ['-jar', s:java_fomatter_jar, '-'],
\ 'stdin': 1,
\ }
try
let g:neoformat_enabled_java += neoformat#formatters#java#enabled()
catch
endtry
" Format on save
if s:format_on_save
call SpaceVim#layers#format#add_filetype({
\ 'filetype' : 'java',
\ 'enable' : 1,
\ })
endif
call SpaceVim#layers#edit#add_ft_head_tamplate('java', s:java_file_head)
endfunction
function! s:JspFileTypeInit() abort
setlocal omnifunc=javacomplete#Complete
inoremap . <c-r>=OnmiConfigForJsp()<cr>
nnoremap <F4> :JCimportAdd<cr>
inoremap <F4> <esc>:JCimportAddI<cr>
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)
" we have removed all insert key bindings which use leader as prefix.
" because when use leader in insert mode key bindings. vim will wait for
" next key after insert \ in insert mode.
" 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
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)
@ -199,12 +243,6 @@ function! s:language_specified_mappings() abort
\ '<Plug>(JavaComplete-Generate-NewClass)',
\ 'Generate NewClass in current Package', 0)
" Jump
let g:_spacevim_mappings_space.l.j = {'name' : '+Jump'}
call SpaceVim#mapping#space#langSPC('nnoremap', ['l','j', 'a'], 'call call('
\ . string(function('s:jump_to_alternate')) . ', [])',
\ 'jump to alternate file', 1)
" execute
let g:_spacevim_mappings_space.l.r = {'name' : '+Run'}
" run main method
@ -212,11 +250,6 @@ function! s:language_specified_mappings() abort
call SpaceVim#mapping#space#langSPC('nmap', ['l','r', 'c'], 'JavaUnitExec', 'Run current method', 1)
call SpaceVim#mapping#space#langSPC('nmap', ['l','r', 'a'], 'JavaUnitTestAll', 'Run all test methods', 1)
" debug
let g:_spacevim_mappings_space.l.d = {'name' : '+Debug'}
call SpaceVim#mapping#space#langSPC('nmap', ['l','d', 's'], ':VBGstartJDB', 'start jdb', 0)
call SpaceVim#mapping#space#langSPC('nmap', ['l','d', 't'], 'VBGtoggleBreakpointThisLine', 'toggle breakpoint at this line', 1)
" maven
let g:_spacevim_mappings_space.l.m = {'name' : '+Maven'}
call SpaceVim#mapping#space#langSPC('nnoremap', ['l','m', 'i'], 'call call('
@ -290,12 +323,17 @@ function! s:execCMD(cmd) abort
call javaunit#util#ExecCMD(a:cmd)
endfunction
function! s:jump_to_alternate() abort
try
A
catch /^Vim\%((\a\+)\)\=:E464/
echom 'no alternate file'
endtry
function! SpaceVim#layers#lang#java#set_variable(var) abort
let s:format_on_save = get(a:var,
\ 'format_on_save',
\ s:format_on_save)
let s:java_file_head = get(a:var,
\ 'java_file_head',
\ s:java_file_head)
let s:java_interpreter = get(a:var,
\ 'java_interpreter',
\ s:java_interpreter
\ )
endfunction
" vim:set et sw=2 cc=80:

View File

@ -826,7 +826,8 @@ this feature, or you can set to another char.
<
*g:spacevim_enable_insert_leader*
Enable/Disable spacevim's insert mode leader, default is enable
Enable/Disable spacevim's insert mode leader, default is enable This options
has been deprecated.
*g:spacevim_data_dir*
Set the cache directory of SpaceVim. Default is `$XDG_CACHE_HOME` or if not
@ -2375,8 +2376,31 @@ This layer also provides REPL support for janet, the key bindings are:
==============================================================================
LANG#JAVA *SpaceVim-layer-lang-java*
This layer is for Java development.
This layer is for java development, disabled by default, to enable this layer,
add following snippet to your SpaceVim configuration file.
>
[[layers]]
name = 'lang#java'
<
LAYER OPTIONS
1. `format_on_save`: Enable/disabled code formatting when saving current
file. Disabled by default.
2. `java_fomatter_jar`: Set the full path of google's java formatter jar.
3. `java_file_head`: The default file header for new java file. by default
it is:
>
[[layers]]
name = 'lang#java'
java_file_head = [
'/**',
' * @author : `fnamemodify(expand("~"), ":t")`',
' * @created : `strftime("%Y-%m-%d")`',
'**/',
''
]
<
MAPPINGS
>
@ -2384,8 +2408,6 @@ MAPPINGS
Mode Key Function
-------------------------------------------------------------
normal <F4> import class under cursor
insert <F4> import class under cursor
normal SPC l I import missing classes
normal SPC l R remove unused imports
normal SPC l i smart import class under cursor

View File

@ -10,12 +10,14 @@ 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 tutorial introduces you to SpaceVim as a Java environment,
by using the `lang#java` layer, you make SpaceVim into a great lightweight Java IDE.
Each of the following sections will be covered:
<!-- vim-markdown-toc GFM -->
- [Installation](#installation)
- [Enable language layer](#enable-language-layer)
- [Language server](#language-server)
- [Code completion](#code-completion)
- [Code outline](#code-outline)
@ -24,22 +26,24 @@ Each of the following sections will be covered:
- [Syntax lint](#syntax-lint)
- [Import packages](#import-packages)
- [Jump to test file](#jump-to-test-file)
- [running code](#running-code)
- [Code running](#code-running)
- [Code formatting](#code-formatting)
- [REPL](#repl)
<!-- vim-markdown-toc -->
### Installation
This tutorial is not intended to teach you Java itself.
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.
following the [quick start guide](../quick-start-guide/) to install SpaceVim.
If you have any problems,
feel free to join the [SpaceVim gitter chatting room](https://gitter.im/SpaceVim/SpaceVim)
for general discussion.
SpaceVim do not enable language layer by default, so you need to enable `lang#java` layer.
### Enable language layer
`lang#java` layer is not loaded by default in SpaceVim, to use SpaceVim for java,
you need to enable this layer in SpaceVim configuration file.
Press `SPC f v d` to open SpaceVim configuration file, and add following section:
```toml
[[layers]]
name = "lang#java"
@ -74,7 +78,6 @@ To enable language server protocol support, you may need to enable lsp layer.
]
```
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:
@ -106,7 +109,8 @@ then press `Leader f o`:
### Rename symbol
After enable lsp layer for java, you can use `SPC l e` to rename symbol under the cursor:
To rename java symbol, you need to enable `lsp` layer for java. The default key binding
for rename symbol under the cursor is `SPC l e`.
![rename java symblo](https://user-images.githubusercontent.com/13142418/53250190-da115580-36f4-11e9-9590-bf945fa8dcc0.gif)
@ -132,8 +136,10 @@ within above picture, we can see the checkers layer provides following feature:
### 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.
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 `SPC l i` 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)
@ -143,8 +149,10 @@ SpaceVim use vim-project to manager the files in a project, you can add a `.proj
```json
{
"src/main/java/*.java": {"alternate": "src/test/java/{dirname}/Test{basename}.java"},
"src/test/java/**/Test*.java": {"alternate": "src/main/java/{}.java"}
"src/main/java/*.java": {
"alternate": "src/test/java/{dirname}/Test{basename}.java"
},
"src/test/java/**/Test*.java": { "alternate": "src/main/java/{}.java" }
}
```
@ -152,18 +160,25 @@ 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
### Code running
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
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.
Code formatting is provided by `format` layer, which is loaded by default.
The default format engine is `neoformat`, it will run google's [java formatter](https://github.com/google/google-java-format)
asynchronously. The key binding for formatting current file is `SPC b f`.
To use this feature, you need to download the google's java formatter jar, and set the
path of this jar file in layer option.
```toml
[[layers]]
name = 'lang#java'
java_fomatter_jar = 'path/to/google-java-format.jar'
```
![format-java](https://user-images.githubusercontent.com/13142418/46323426-ccadde80-c620-11e8-9726-d99025f3bf76.gif)
@ -172,4 +187,3 @@ BTW, the google's [java formatter](https://github.com/google/google-java-format)
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)

View File

@ -11,12 +11,12 @@ lang: zh
- [模块描述](#模块描述)
- [功能特性](#功能特性)
- [启用模块](#启用模块)
- [模块选项](#模块选项)
- [快捷键](#快捷键)
- [导包相关快捷键](#导包相关快捷键)
- [代码生成相关快捷键](#代码生成相关快捷键)
- [代码格式化](#代码格式化)
- [Maven](#maven)
- [Jump](#jump)
- [交互式编程](#交互式编程)
<!-- vim-markdown-toc -->
@ -43,13 +43,40 @@ lang: zh
name = "lang#java"
```
## 模块选项
- `format_on_save`: 启用/禁用保存文件时自动格式化,默认的值为 `false`
可以使用如下配置启用该功能:
```toml
[[layers]]
name = 'lang#java'
format_on_save = true
```
- `java_fomatter_jar`: 设置 [谷歌格式化工具](https://github.com/google/google-java-format) 的具体路径。
```toml
[[layers]]
name = 'lang#java'
java_fomatter_jar = 'path/to/google-java-format.jar'
```
- `java_file_head`: 默认的文件头,当新建 Java 文件时自动加入。
```toml
[[layers]]
name = 'lang#java'
java_file_head = [
'/**',
' * @author : `fnamemodify(expand("~"), ":t")`',
' * @created : `strftime("%Y-%m-%d")`',
'**/',
''
]
```
## 快捷键
### 导包相关快捷键
| 模式 | 快捷键 | 按键描述 |
| ------------- | --------- | ------------------ |
| Insert/Normal | `<F4>` | 导入光标下的类 |
| Normal | `SPC l I` | 导入所有缺失的类 |
| Normal | `SPC l R` | 删除多余的导包 |
| Normal | `SPC l i` | 智能导入光标下的类 |
@ -78,12 +105,13 @@ lang: zh
默认的代码格式化快捷键是 `SPC b f`,该快捷键由 `format` 模块定义,同时也可以通过 `g =` 来对齐整个文档。
为了使 format 模块支持 Java 文件,需要安装 uncrustify 或者下载 [google's formater jar](https://github.com/google/google-java-format)。
同时,需要设置 SpaceVim 选项`layer_lang_java_formatter`
为了使 format 模块支持 Java 文件,需要安装 [google's formater jar](https://github.com/google/google-java-format)。
同时,需要设置模块选项`java_fomatter_jar`
```toml
[options]
layer_lang_java_formatter = "path/to/google-java-format.jar"
[[layers]]
name = 'lang#java'
java_fomatter_jar = 'path/to/google-java-format.jar'
```
### Maven
@ -97,12 +125,6 @@ lang: zh
| `SPC l m R` | Run one maven goal |
| `SPC l m t` | Run maven test |
### Jump
| 快捷键 | 描述 |
| ----------- | ---------------------- |
| `SPC l j a` | jump to alternate file |
### 交互式编程
启动 `jshell` 交互进程,快捷键为: `SPC l s i`

View File

@ -10,12 +10,14 @@ description: "This layer is for Java development. All the features such as code
- [Description](#description)
- [Feature](#feature)
- [Install](#install)
- [Layer options](#layer-options)
- [Key bindings](#key-bindings)
- [Import key bindings](#import-key-bindings)
- [Generate key bindings](#generate-key-bindings)
- [Code formatting](#code-formatting)
- [Maven](#maven)
- [Jump](#jump)
- [Maven support](#maven-support)
- [Gradle support](#gradle-support)
- [Code runner](#code-runner)
- [Inferior REPL process](#inferior-repl-process)
<!-- vim-markdown-toc -->
@ -27,10 +29,11 @@ This layer is for Java development.
## Feature
- code completion: `autocomplete` layer
- code formatting
- code formatting: `format` layer
- refactoring
- syntax checking: `checkers` layer
- REPL(need java8's jshell)
- REPL(requires `jshell`)
- code runner
- debug: check out the `debug` layer
## Install
@ -42,13 +45,40 @@ To use this configuration layer, update custom configuration file with:
name = "lang#java"
```
## Layer options
- `format_on_save`: Enable/disabled code formatting when saving current file.
The default value is `false`. To enable this feature:
```toml
[[layers]]
name = 'lang#java'
format_on_save = true
```
- `java_fomatter_jar`: Set the full path of [google's formater jar](https://github.com/google/google-java-format).
```toml
[[layers]]
name = 'lang#java'
java_fomatter_jar = 'path/to/google-java-format.jar'
```
- `java_file_head`: The default file header for new java file. by default it is:
```toml
[[layers]]
name = 'lang#java'
java_file_head = [
'/**',
' * @author : `fnamemodify(expand("~"), ":t")`',
' * @created : `strftime("%Y-%m-%d")`',
'**/',
''
]
```
## Key bindings
### Import key bindings
| Key Bindings | Descriptions |
| -------------------- | ------------------------------- |
| `<F4>` (Insert/Normal) | Import class under cursor |
| ------------------- | ------------------------------- |
| `SPC l I` | Import missing classes |
| `SPC l R` | Remove unused classes |
| `SPC l i` | smart import class under cursor |
@ -75,14 +105,14 @@ To use this configuration layer, update custom configuration file with:
### Code formatting
The default key bindings for format current buffer is `SPC b f`. And this key binding is defined in [format layer](<>). You can also use `g=` to indent current buffer.
The default formater of java language is [google's formater jar](https://github.com/google/google-java-format).
You need to download the jar and set the `java_fomatter_jar` layer option.
To make neoformat support Java file, you should install uncrustify.
Or download [google's formater jar](https://github.com/google/google-java-format)
and add `let g:spacevim_layer_lang_java_formatter = 'path/to/google-java-format.jar'`
to SpaceVim custom configuration file.
The default key bindings for format current buffer is `SPC b f`.
And this key binding is defined in [`format`](../layers/format/) layer.
You can also use `g=` to indent current buffer.
### Maven
### Maven support
| Key Bindings | Descriptions |
| ------------ | ------------------------------ |
@ -93,11 +123,22 @@ to SpaceVim custom configuration file.
| `SPC l m R` | Run one maven goal |
| `SPC l m t` | Run maven test |
### Jump
### Gradle support
| Key Bindings | Descriptions |
| ------------ | ---------------------- |
| `SPC l j a` | jump to alternate file |
| ------------ | ------------------ |
| `SPC l g b` | gradle build |
| `SPC l g B` | gradle clean build |
| `SPC l g r` | gradle run |
| `SPC l g t` | gradle test |
### Code runner
| Key bindings | Descriptions |
| ------------ | ------------------------------- |
| `SPC l r m` | run main method of current file |
| `SPC l r m` | run current method |
| `SPC l r t` | run all test methods |
### Inferior REPL process