mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-03 02:30:03 +08:00
Update post for C/C++ (#3862)
This commit is contained in:
parent
bc99556215
commit
23244fdb20
@ -6,6 +6,7 @@
|
|||||||
" License: GPLv3
|
" License: GPLv3
|
||||||
"=============================================================================
|
"=============================================================================
|
||||||
|
|
||||||
|
" Layer doc {{{
|
||||||
|
|
||||||
""
|
""
|
||||||
" @section lang#c, layer-lang-c
|
" @section lang#c, layer-lang-c
|
||||||
@ -80,8 +81,9 @@
|
|||||||
" SPC l s s send selection text
|
" SPC l s s send selection text
|
||||||
" <
|
" <
|
||||||
"
|
"
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" Init layer options {{{
|
||||||
if exists('s:clang_executable')
|
if exists('s:clang_executable')
|
||||||
finish
|
finish
|
||||||
else
|
else
|
||||||
@ -93,11 +95,18 @@ else
|
|||||||
\ 'objc': 'c11',
|
\ 'objc': 'c11',
|
||||||
\ 'objcpp': 'c++1z',
|
\ 'objcpp': 'c++1z',
|
||||||
\ }
|
\ }
|
||||||
|
let s:highlight_cmd = ''
|
||||||
|
let s:enable_clang_syntax = 0
|
||||||
|
let s:c_repl_command = ''
|
||||||
endif
|
endif
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" Load the APIs{{{
|
||||||
let s:SYSTEM = SpaceVim#api#import('system')
|
let s:SYSTEM = SpaceVim#api#import('system')
|
||||||
let s:CPT = SpaceVim#api#import('vim#compatible')
|
let s:CPT = SpaceVim#api#import('vim#compatible')
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" plugins {{{
|
||||||
function! SpaceVim#layers#lang#c#plugins() abort
|
function! SpaceVim#layers#lang#c#plugins() abort
|
||||||
let plugins = []
|
let plugins = []
|
||||||
if !SpaceVim#layers#lsp#check_filetype('c') && !SpaceVim#layers#lsp#check_filetype('cpp')
|
if !SpaceVim#layers#lsp#check_filetype('c') && !SpaceVim#layers#lsp#check_filetype('cpp')
|
||||||
@ -135,7 +144,9 @@ function! SpaceVim#layers#lang#c#plugins() abort
|
|||||||
endif
|
endif
|
||||||
return plugins
|
return plugins
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" config {{{
|
||||||
function! SpaceVim#layers#lang#c#config() abort
|
function! SpaceVim#layers#lang#c#config() abort
|
||||||
call SpaceVim#mapping#gd#add('c',
|
call SpaceVim#mapping#gd#add('c',
|
||||||
\ function('s:go_to_def'))
|
\ function('s:go_to_def'))
|
||||||
@ -190,20 +201,18 @@ function! SpaceVim#layers#lang#c#config() abort
|
|||||||
let s:highlight_cmd = 'ClighterEnable'
|
let s:highlight_cmd = 'ClighterEnable'
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
let s:highlight_cmd = ''
|
" local function: highlight {{{
|
||||||
|
|
||||||
function! s:highlight() abort
|
function! s:highlight() abort
|
||||||
try
|
try
|
||||||
exe s:highlight_cmd
|
exe s:highlight_cmd
|
||||||
catch
|
catch
|
||||||
endtry
|
endtry
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
let s:enable_clang_syntax = 0
|
" set_variable {{{
|
||||||
|
|
||||||
let s:c_repl_command = ''
|
|
||||||
|
|
||||||
function! SpaceVim#layers#lang#c#set_variable(var) abort
|
function! SpaceVim#layers#lang#c#set_variable(var) abort
|
||||||
if has_key(a:var, 'clang_executable')
|
if has_key(a:var, 'clang_executable')
|
||||||
let g:completor_clang_binary = a:var.clang_executable
|
let g:completor_clang_binary = a:var.clang_executable
|
||||||
@ -239,7 +248,9 @@ function! SpaceVim#layers#lang#c#set_variable(var) abort
|
|||||||
|
|
||||||
call extend(s:clang_std, get(a:var, 'clang_std', {}))
|
call extend(s:clang_std, get(a:var, 'clang_std', {}))
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" local function: language_specified_mappings {{{
|
||||||
function! s:language_specified_mappings() abort
|
function! s:language_specified_mappings() abort
|
||||||
|
|
||||||
call SpaceVim#mapping#space#langSPC('nmap', ['l','r'],
|
call SpaceVim#mapping#space#langSPC('nmap', ['l','r'],
|
||||||
@ -282,8 +293,9 @@ function! s:language_specified_mappings() abort
|
|||||||
\ 'call SpaceVim#plugins#repl#send("selection")',
|
\ 'call SpaceVim#plugins#repl#send("selection")',
|
||||||
\ 'send selection and keep code buffer focused', 1)
|
\ 'send selection and keep code buffer focused', 1)
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" local function: update_clang_flag {{{
|
||||||
function! s:update_clang_flag() abort
|
function! s:update_clang_flag() abort
|
||||||
if filereadable('.clang')
|
if filereadable('.clang')
|
||||||
let argvs = readfile('.clang')
|
let argvs = readfile('.clang')
|
||||||
@ -293,7 +305,9 @@ function! s:update_clang_flag() abort
|
|||||||
call s:update_runner(argvs, ['c', 'cpp'])
|
call s:update_runner(argvs, ['c', 'cpp'])
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" local function: update_checkers_argv {{{
|
||||||
if g:spacevim_enable_neomake && g:spacevim_enable_ale == 0
|
if g:spacevim_enable_neomake && g:spacevim_enable_ale == 0
|
||||||
function! s:update_checkers_argv(argv, fts) abort
|
function! s:update_checkers_argv(argv, fts) abort
|
||||||
for ft in a:fts
|
for ft in a:fts
|
||||||
@ -326,11 +340,15 @@ else
|
|||||||
|
|
||||||
endfunction
|
endfunction
|
||||||
endif
|
endif
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" local function: update_autocomplete_argv {{{
|
||||||
function! s:update_autocomplete_argv(argv, fts) abort
|
function! s:update_autocomplete_argv(argv, fts) abort
|
||||||
|
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" local function: has_std {{{
|
||||||
function! s:has_std(argv) abort
|
function! s:has_std(argv) abort
|
||||||
for line in a:argv
|
for line in a:argv
|
||||||
if line =~# '^-std='
|
if line =~# '^-std='
|
||||||
@ -338,7 +356,9 @@ function! s:has_std(argv) abort
|
|||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" local function: update_runner {{{
|
||||||
function! s:update_runner(argv, fts) abort
|
function! s:update_runner(argv, fts) abort
|
||||||
if s:has_std(a:argv)
|
if s:has_std(a:argv)
|
||||||
let default_std = 1
|
let default_std = 1
|
||||||
@ -364,7 +384,9 @@ function! s:update_runner(argv, fts) abort
|
|||||||
call SpaceVim#plugins#runner#reg_runner('cpp', [runner2, '#TEMP#'])
|
call SpaceVim#plugins#runner#reg_runner('cpp', [runner2, '#TEMP#'])
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" local function: update_neoinclude {{{
|
||||||
function! s:update_neoinclude(argv, fts) abort
|
function! s:update_neoinclude(argv, fts) abort
|
||||||
if s:SYSTEM.isLinux
|
if s:SYSTEM.isLinux
|
||||||
let path = '.,/usr/include,,'
|
let path = '.,/usr/include,,'
|
||||||
@ -378,7 +400,9 @@ function! s:update_neoinclude(argv, fts) abort
|
|||||||
endfor
|
endfor
|
||||||
let b:neoinclude_paths = path
|
let b:neoinclude_paths = path
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
|
||||||
|
" local function: go_to_def {{{
|
||||||
function! s:go_to_def() abort
|
function! s:go_to_def() abort
|
||||||
if !SpaceVim#layers#lsp#check_filetype(&ft)
|
if !SpaceVim#layers#lsp#check_filetype(&ft)
|
||||||
execute "norm! g\<c-]>"
|
execute "norm! g\<c-]>"
|
||||||
@ -386,3 +410,4 @@ function! s:go_to_def() abort
|
|||||||
call SpaceVim#lsp#go_to_def()
|
call SpaceVim#lsp#go_to_def()
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
" }}}
|
||||||
|
@ -1625,7 +1625,7 @@ Here is an example how to use above options:
|
|||||||
name = "lang#c"
|
name = "lang#c"
|
||||||
clang_executable = "/usr/bin/clang"
|
clang_executable = "/usr/bin/clang"
|
||||||
clang_flag = ['-I/user/include']
|
clang_flag = ['-I/user/include']
|
||||||
[layer.clang_std]
|
[layers.clang_std]
|
||||||
c = "c11"
|
c = "c11"
|
||||||
cpp = "c++1z"
|
cpp = "c++1z"
|
||||||
objc = "c11"
|
objc = "c11"
|
||||||
@ -1660,6 +1660,7 @@ This layer also provides REPL support for c, the key bindings are:
|
|||||||
SPC l s s send selection text
|
SPC l s s send selection text
|
||||||
<
|
<
|
||||||
|
|
||||||
|
}}}
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
LANG#CHAPEL *SpaceVim-layer-lang-chapel*
|
LANG#CHAPEL *SpaceVim-layer-lang-chapel*
|
||||||
|
@ -16,9 +16,11 @@ Each of the following sections will be covered:
|
|||||||
<!-- vim-markdown-toc GFM -->
|
<!-- vim-markdown-toc GFM -->
|
||||||
|
|
||||||
- [Enable language layer](#enable-language-layer)
|
- [Enable language layer](#enable-language-layer)
|
||||||
|
- [Syntax highlighting](#syntax-highlighting)
|
||||||
- [code completion](#code-completion)
|
- [code completion](#code-completion)
|
||||||
- [alternate file jumping](#alternate-file-jumping)
|
- [alternate file jumping](#alternate-file-jumping)
|
||||||
- [code running](#code-running)
|
- [code running](#code-running)
|
||||||
|
- [Syntax lint](#syntax-lint)
|
||||||
- [code format](#code-format)
|
- [code format](#code-format)
|
||||||
- [REPL support](#repl-support)
|
- [REPL support](#repl-support)
|
||||||
|
|
||||||
@ -36,6 +38,19 @@ SpaceVim configuration file, and add following configuration:
|
|||||||
|
|
||||||
for more info, you can read the [lang#c](../layers/lang/c/) layer documentation.
|
for more info, you can read the [lang#c](../layers/lang/c/) layer documentation.
|
||||||
|
|
||||||
|
### Syntax highlighting
|
||||||
|
|
||||||
|
The basic syntax highlighting is based on regular expression, If you want `clang` based
|
||||||
|
syntax highlighting. Enable `enable_clang_syntax_highlight` layer option:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[layers]]
|
||||||
|
name = 'lang#c'
|
||||||
|
enable_clang_syntax_highlight = true
|
||||||
|
```
|
||||||
|
|
||||||
|
This option requires `+python` or `+python3` enabled and `libclang` has been installed.
|
||||||
|
|
||||||
### code completion
|
### code completion
|
||||||
|
|
||||||
By default the autocomplete layer has been enabled, so after loading `lang#c` layer, the code completion
|
By default the autocomplete layer has been enabled, so after loading `lang#c` layer, the code completion
|
||||||
@ -66,6 +81,29 @@ And the stdout will be shown on a runner buffer.
|
|||||||
|
|
||||||
![c-cpp-runner](https://user-images.githubusercontent.com/13142418/58743787-db2bee80-846a-11e9-9b19-17202ac542c9.png)
|
![c-cpp-runner](https://user-images.githubusercontent.com/13142418/58743787-db2bee80-846a-11e9-9b19-17202ac542c9.png)
|
||||||
|
|
||||||
|
The default runner compile option is defineded in `clang_std` and `clang_flag` option.
|
||||||
|
If you want to use `c11`, you can change `clang_std` option to:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[layers]]
|
||||||
|
name = "lang#c"
|
||||||
|
[layer.clang_std]
|
||||||
|
cpp = "c11"
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also create a `.clang` file in the root directory of you project. Within this
|
||||||
|
file, all compile option should be defineded in it. for example:
|
||||||
|
|
||||||
|
```
|
||||||
|
-I/home/test
|
||||||
|
-I/user/std/include
|
||||||
|
```
|
||||||
|
|
||||||
|
### Syntax lint
|
||||||
|
|
||||||
|
The [checker](../layers/checkers/) layer provides syntax checking for many programming languages.
|
||||||
|
Including C/C++, and the default plugin is [neomake](https://github.com/neomake/neomake). The default
|
||||||
|
lint for C/C++ is `gcc`/`g++`. These commands also read configuration in `.clang` file.
|
||||||
|
|
||||||
### code format
|
### code format
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ SpaceVim 是一个模块化的 Vim IDE,针对 C/C++ 语言的支持主要依
|
|||||||
<!-- vim-markdown-toc GFM -->
|
<!-- vim-markdown-toc GFM -->
|
||||||
|
|
||||||
- [安装模块](#安装模块)
|
- [安装模块](#安装模块)
|
||||||
|
- [语法高亮](#语法高亮)
|
||||||
- [代码自动补全](#代码自动补全)
|
- [代码自动补全](#代码自动补全)
|
||||||
- [语法检查](#语法检查)
|
- [语法检查](#语法检查)
|
||||||
- [工程文件跳转](#工程文件跳转)
|
- [工程文件跳转](#工程文件跳转)
|
||||||
@ -40,6 +41,19 @@ SpaceVim 初次安装时默认并未启用相关语言模块。首先需要启
|
|||||||
|
|
||||||
启用 `lang#c` 模块后,在打开 C/C++ 文件时,就可以使用语言专属快捷键,这些快捷键都是以 `SPC l` 为前缀的。
|
启用 `lang#c` 模块后,在打开 C/C++ 文件时,就可以使用语言专属快捷键,这些快捷键都是以 `SPC l` 为前缀的。
|
||||||
|
|
||||||
|
### 语法高亮
|
||||||
|
|
||||||
|
基础的语法高亮是基于正则表达式的,如果需要基于 `clang` 解析的语法高亮,
|
||||||
|
可以启用 `enable_clang_syntax_highlight` 这一模块选项:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[layers]]
|
||||||
|
name = 'lang#c'
|
||||||
|
enable_clang_syntax_highlight = true
|
||||||
|
```
|
||||||
|
|
||||||
|
这一功能需要 Vim/Neovim 支持 `+python` 或者 `+python3`,并且安装了 `libclang`。
|
||||||
|
|
||||||
### 代码自动补全
|
### 代码自动补全
|
||||||
|
|
||||||
`autocomplete` 模块为 SpaceVim 提供了自动补全功能,目前针对 C/C++ 而言,比较好的补全方案是配合使用 lsp 模块:
|
`autocomplete` 模块为 SpaceVim 提供了自动补全功能,目前针对 C/C++ 而言,比较好的补全方案是配合使用 lsp 模块:
|
||||||
@ -99,6 +113,24 @@ C/C++ 代码格式化,主要依赖 `format` 模块,快捷键为 `SPC b f`,
|
|||||||
|
|
||||||
![c-cpp-runner](https://user-images.githubusercontent.com/13142418/58743787-db2bee80-846a-11e9-9b19-17202ac542c9.png)
|
![c-cpp-runner](https://user-images.githubusercontent.com/13142418/58743787-db2bee80-846a-11e9-9b19-17202ac542c9.png)
|
||||||
|
|
||||||
|
默认的代码快速运行命令会读取模块选项 `clang_std` 和 `clang_flag`。
|
||||||
|
例如,如果需要使用 `c11`,可以设置 `clang_std` 选项如下:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[layers]]
|
||||||
|
name = "lang#c"
|
||||||
|
[layer.clang_std]
|
||||||
|
cpp = "c11"
|
||||||
|
```
|
||||||
|
|
||||||
|
与此同时,你也可以在项目根目录新建一个 `.clang` 文件,在其内逐行写入 C/C++ 编译的参数,
|
||||||
|
比如:
|
||||||
|
|
||||||
|
```
|
||||||
|
-I/home/test
|
||||||
|
-I/user/std/include
|
||||||
|
```
|
||||||
|
|
||||||
### 交互式编程
|
### 交互式编程
|
||||||
|
|
||||||
在编辑 C/C++ 文件时,可通过快捷键 `SPC l s i` 启动 `php -a` 交互窗口,
|
在编辑 C/C++ 文件时,可通过快捷键 `SPC l s i` 启动 `php -a` 交互窗口,
|
||||||
|
Loading…
Reference in New Issue
Block a user