1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-04-14 23:49:19 +08:00

Improve .clang file support (#3847)

close #3842
This commit is contained in:
Wang Shidong 2020-09-29 00:35:18 +08:00 committed by GitHub
parent 93e99def45
commit 5d9be7f19d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 203 additions and 63 deletions

View File

@ -10,49 +10,89 @@
""
" @section lang#c, layer-lang-c
" @parentsection layers
" This layer provides C family language code completion and syntax checking.
" Requires clang.
"
" Configuration for `tweekmonster/deoplete-clang2`:
"
" 1. Set the compile flags:
"
" `let g:deoplete#sources#clang#flags = ['-Iwhatever', ...]`
"
" 2. Set the path to the clang executable:
"
" `let g:deoplete#sources#clang#executable = '/usr/bin/clang'
"
" 3. `g:deoplete#sources#clang#autofill_neomake` is a boolean that tells this
" plugin to fill in the `g:neomake_<filetype>_clang_maker` variable with the
" clang executable path and flags. You will still need to enable it with
" `g:neomake_<filetype>_enabled_make=['clang']`.
"
" 4. Set the standards for each language:
" `g:deoplete#sources#clang#std` is a dict containing the standards you want
" to use. It's not used if you already have `-std=whatever` in your flags. The
" defaults are:
" This layer is for c/cpp development, disabled by default, to enable this
" layer, add following snippet to your SpaceVim configuration file.
" >
" {
" 'c': 'c11',
" 'cpp': 'c++1z',
" 'objc': 'c11',
" 'objcpp': 'c++1z',
" }
" [[layers]]
" name = 'lang#c'
" <
"
" @subsection Layer options
"
" `clang_executable`: Set the path to the clang executable, by default, it is
" `clang`.
"
" `enable_clang_syntax_highlight`: Enable/Disable clang based syntax
" highlighting. By default it is disabled.
"
" `libclang_path`: The libclang shared object (dynamic library) file path.
" By default it is empty
"
" `clang_std`: This is a dictionary for setting std for c/cpp. The default
" valuable is :
" >
" 'c' : 'c11',
" 'cpp' : 'c++1z',
" 'objc' : 'c11',
" 'objcpp': 'c++1z',
" <
"
" `clang_flag`: You should be able to just paste most of your compile
" flags in there.
"
" Here is an example how to use above options:
" >
" [[layers]]
" name = "lang#c"
" clang_executable = "/usr/bin/clang"
" clang_flag = ['-I/user/include']
" [layer.clang_std]
" c = "c11"
" cpp = "c++1z"
" objc = "c11"
" objcpp = "c++1z"
" <
"
" Instead of using `clang_flag` options, You can also create a `.clang` file
" in the root directory of your project. SpaceVim will load the options
" defined in `.clang` file. For example:
" >
" -std=c11
" -I/home/test
" <
" Note: If `.clang` file contains std configuration, it will override
" `clang_std` layer option.
"
" @subsection Key bindings
" >
" Mode Key Function
" ---------------------------------------------
" normal SPC l r run current file
" <
"
" This layer also provides REPL support for c, the key bindings are:
" >
" Key Function
" ---------------------------------------------
" SPC l s i Start a inferior REPL process
" SPC l s b send whole buffer
" SPC l s l send current line
" SPC l s s send selection text
" <
" 5. `g:deoplete#sources#clang#preproc_max_lines` sets the
" maximum number of lines to search for an #ifdef or #endif
" line. #ifdef lines are discarded to get completions within
" conditional preprocessor blocks. The default is 50,
" setting it to 0 disables this feature.
"
if exists('s:clang_executable')
finish
else
let s:clang_executable = 'clang'
let s:clang_flag = []
let s:clang_std = {
\ 'c' : 'c11',
\ 'cpp': 'c++1z',
\ 'objc': 'c11',
\ 'objcpp': 'c++1z',
\ }
endif
let s:SYSTEM = SpaceVim#api#import('system')
let s:CPT = SpaceVim#api#import('vim#compatible')
@ -105,7 +145,7 @@ function! SpaceVim#layers#lang#c#config() abort
let runner1 = {
\ 'exe' : 'gcc',
\ 'targetopt' : '-o',
\ 'opt' : ['-xc', '-'],
\ 'opt' : ['-std=' . s:clang_std.c] + s:clang_flag + ['-xc', '-'],
\ 'usestdin' : 1,
\ }
call SpaceVim#plugins#runner#reg_runner('c', [runner1, '#TEMP#'])
@ -113,7 +153,7 @@ function! SpaceVim#layers#lang#c#config() abort
let runner2 = {
\ 'exe' : 'g++',
\ 'targetopt' : '-o',
\ 'opt' : ['-xc++', '-'],
\ 'opt' : ['-std=' . s:clang_std.cpp] + s:clang_flag + ['-xc', '-'],
\ 'usestdin' : 1,
\ }
call SpaceVim#plugins#runner#reg_runner('cpp', [runner2, '#TEMP#'])
@ -193,6 +233,8 @@ function! SpaceVim#layers#lang#c#set_variable(var) abort
endif
endif
let s:clang_flag = get(a:var, 'clang_flag', s:clang_flag)
let s:enable_clang_syntax = get(a:var, 'enable_clang_syntax_highlight', s:enable_clang_syntax)
endfunction
@ -246,6 +288,7 @@ function! s:update_clang_flag() abort
call s:update_checkers_argv(argvs, ['c', 'cpp'])
call s:update_autocomplete_argv(argvs, ['c', 'cpp'])
call s:update_neoinclude(argvs, ['c', 'cpp'])
call s:update_runner(argvs, ['c', 'cpp'])
endif
endfunction
@ -286,6 +329,40 @@ function! s:update_autocomplete_argv(argv, fts) abort
endfunction
function! s:has_std(argv) abort
for line in a:argv
if line =~# '^-std='
return 1
endif
endfor
endfunction
function! s:update_runner(argv, fts) abort
if s:has_std(a:argv)
let default_std = 1
else
let default_std = 0
endif
if index(a:fts, 'c') !=# -1
let runner1 = {
\ 'exe' : 'gcc',
\ 'targetopt' : '-o',
\ 'opt' : a:argv + (default_std ? [] : ['-std=' . s:clang_std.c]) + s:clang_flag + ['-xc', '-'],
\ 'usestdin' : 1,
\ }
call SpaceVim#plugins#runner#reg_runner('c', [runner1, '#TEMP#'])
endif
if index(a:fts, 'cpp') !=# -1
let runner2 = {
\ 'exe' : 'g++',
\ 'targetopt' : '-o',
\ 'opt' : a:argv + (default_std ? [] : ['-std=' . s:clang_std.cpp]) + s:clang_flag + ['-xc++', '-'],
\ 'usestdin' : 1,
\ }
call SpaceVim#plugins#runner#reg_runner('cpp', [runner2, '#TEMP#'])
endif
endfunction
function! s:update_neoinclude(argv, fts) abort
if s:SYSTEM.isLinux
let path = '.,/usr/include,,'

View File

@ -1589,40 +1589,76 @@ This layer also provides REPL support for batch, the key bindings are:
==============================================================================
LANG#C *SpaceVim-layer-lang-c*
This layer provides C family language code completion and syntax checking.
Requires clang.
This layer is for c/cpp development, disabled by default, to enable this
layer, add following snippet to your SpaceVim configuration file.
>
[[layers]]
name = 'lang#c'
<
Configuration for `tweekmonster/deoplete-clang2`:
LAYER OPTIONS
1. Set the compile flags:
`clang_executable`: Set the path to the clang executable, by default, it is
`clang`.
`let g:deoplete#sources#clang#flags = ['-Iwhatever', ...]`
`enable_clang_syntax_highlight`: Enable/Disable clang based syntax
highlighting. By default it is disabled.
2. Set the path to the clang executable:
`libclang_path`: The libclang shared object (dynamic library) file path. By
default it is empty
`let g:deoplete#sources#clang#executable = '/usr/bin/clang'
`clang_std`: This is a dictionary for setting std for c/cpp. The default
valuable is :
>
'c' : 'c11',
'cpp' : 'c++1z',
'objc' : 'c11',
'objcpp': 'c++1z',
<
3. `g:deoplete#sources#clang#autofill_neomake` is a boolean that tells this
plugin to fill in the `g:neomake_<filetype>_clang_maker` variable with the
clang executable path and flags. You will still need to enable it with
`g:neomake_<filetype>_enabled_make=['clang']`.
`clang_flag`: You should be able to just paste most of your compile flags in
there.
4. Set the standards for each language: `g:deoplete#sources#clang#std` is a
dict containing the standards you want to use. It's not used if you
already have `-std=whatever` in your flags. The defaults are:
Here is an example how to use above options:
>
[[layers]]
name = "lang#c"
clang_executable = "/usr/bin/clang"
clang_flag = ['-I/user/include']
[layer.clang_std]
c = "c11"
cpp = "c++1z"
objc = "c11"
objcpp = "c++1z"
<
Instead of using `clang_flag` options, You can also create a `.clang` file in
the root directory of your project. SpaceVim will load the options defined in
`.clang` file. For example:
>
-std=c11
-I/home/test
<
Note: If `.clang` file contains std configuration, it will override
`clang_std` layer option.
KEY BINDINGS
>
{
'c': 'c11',
'cpp': 'c++1z',
'objc': 'c11',
'objcpp': 'c++1z',
}
Mode Key Function
---------------------------------------------
normal SPC l r run current file
<
This layer also provides REPL support for c, the key bindings are:
>
Key Function
---------------------------------------------
SPC l s i Start a inferior REPL process
SPC l s b send whole buffer
SPC l s l send current line
SPC l s s send selection text
<
5. `g:deoplete#sources#clang#preproc_max_lines` sets the maximum number of
lines to search for an #ifdef or #endif line. #ifdef lines are discarded
to get completions within conditional preprocessor blocks. The default is
50, setting it to 0 disables this feature.
==============================================================================

View File

@ -64,7 +64,7 @@ lang: zh
- `clang_flag`
通常,在项目根目录新建一个 `.clang` 文件,可以将编译参数逐行写入。也可以使用一 List 值来初始化该选项。
可以使用一 List 值来初始化该选项。
例如:`clang_flag = ["-Iwhatever"]`
以下为一个完整的 `lang#c` 模块载入示例:
@ -80,6 +80,20 @@ lang: zh
objcpp = "c++1z"
```
除此之外,也在项目根目录新建一个 `.clang` 文件,可以将编译参数逐行写入。
SpaceVim 将会自动读取 `.clang` 文件内的参数。
例如:
```
-std=c11
-I/home/test
```
需要注意的是,若 `.clang` 文件中包含了`std`选项,将会覆盖掉模块选项
`clang_std` 的值。
## 快捷键
| 快捷键 | 功能描述 |

View File

@ -61,9 +61,10 @@ A dict containing the standards you want to use. The default is:
}
```
- `clang_flag`
- `clang_flag` (list)
Create a `.clang` file at your project root. You should be able to just paste most of your compile flags in there. You can also use a list ['-Iwhatever', ...] when loading this layer.
You should be able to just paste most of your compile flags in there.
You can also use a list ['-Iwhatever', ...] when loading this layer.
Here is an example how to use above options:
@ -71,6 +72,7 @@ Here is an example how to use above options:
[[layers]]
name = "lang#c"
clang_executable = "/usr/bin/clang"
clang_flag = ['-I/user/include']
[layer.clang_std]
c = "c11"
cpp = "c++1z"
@ -78,6 +80,17 @@ Here is an example how to use above options:
objcpp = "c++1z"
```
Instead of using `clang_flag` options, You can also create a `.clang` file
in the root directory of your project. SpaceVim will load the options
defined in `.clang` file. For example:
```
-std=c11
-I/home/test
```
Note: If `.clang` file contains std configuration, it will override
`clang_std` layer option.
## Key bindings