1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 17:30:04 +08:00

Update post for C/C++ (#3862)

This commit is contained in:
Wang Shidong 2020-10-04 19:06:29 +08:00 committed by GitHub
parent bc99556215
commit 23244fdb20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 10 deletions

View File

@ -6,6 +6,7 @@
" License: GPLv3
"=============================================================================
" Layer doc {{{
""
" @section lang#c, layer-lang-c
@ -80,8 +81,9 @@
" SPC l s s send selection text
" <
"
" }}}
" Init layer options {{{
if exists('s:clang_executable')
finish
else
@ -93,11 +95,18 @@ else
\ 'objc': 'c11',
\ 'objcpp': 'c++1z',
\ }
let s:highlight_cmd = ''
let s:enable_clang_syntax = 0
let s:c_repl_command = ''
endif
" }}}
" Load the APIs{{{
let s:SYSTEM = SpaceVim#api#import('system')
let s:CPT = SpaceVim#api#import('vim#compatible')
" }}}
" plugins {{{
function! SpaceVim#layers#lang#c#plugins() abort
let plugins = []
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
return plugins
endfunction
" }}}
" config {{{
function! SpaceVim#layers#lang#c#config() abort
call SpaceVim#mapping#gd#add('c',
\ function('s:go_to_def'))
@ -190,20 +201,18 @@ function! SpaceVim#layers#lang#c#config() abort
let s:highlight_cmd = 'ClighterEnable'
endif
endfunction
" }}}
let s:highlight_cmd = ''
" local function: highlight {{{
function! s:highlight() abort
try
exe s:highlight_cmd
catch
endtry
endfunction
" }}}
let s:enable_clang_syntax = 0
let s:c_repl_command = ''
" set_variable {{{
function! SpaceVim#layers#lang#c#set_variable(var) abort
if has_key(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', {}))
endfunction
" }}}
" local function: language_specified_mappings {{{
function! s:language_specified_mappings() abort
call SpaceVim#mapping#space#langSPC('nmap', ['l','r'],
@ -282,8 +293,9 @@ function! s:language_specified_mappings() abort
\ 'call SpaceVim#plugins#repl#send("selection")',
\ 'send selection and keep code buffer focused', 1)
endfunction
" }}}
" local function: update_clang_flag {{{
function! s:update_clang_flag() abort
if filereadable('.clang')
let argvs = readfile('.clang')
@ -293,7 +305,9 @@ function! s:update_clang_flag() abort
call s:update_runner(argvs, ['c', 'cpp'])
endif
endfunction
" }}}
" local function: update_checkers_argv {{{
if g:spacevim_enable_neomake && g:spacevim_enable_ale == 0
function! s:update_checkers_argv(argv, fts) abort
for ft in a:fts
@ -326,11 +340,15 @@ else
endfunction
endif
" }}}
" local function: update_autocomplete_argv {{{
function! s:update_autocomplete_argv(argv, fts) abort
endfunction
" }}}
" local function: has_std {{{
function! s:has_std(argv) abort
for line in a:argv
if line =~# '^-std='
@ -338,7 +356,9 @@ function! s:has_std(argv) abort
endif
endfor
endfunction
" }}}
" local function: update_runner {{{
function! s:update_runner(argv, fts) abort
if s:has_std(a:argv)
let default_std = 1
@ -364,7 +384,9 @@ function! s:update_runner(argv, fts) abort
call SpaceVim#plugins#runner#reg_runner('cpp', [runner2, '#TEMP#'])
endif
endfunction
" }}}
" local function: update_neoinclude {{{
function! s:update_neoinclude(argv, fts) abort
if s:SYSTEM.isLinux
let path = '.,/usr/include,,'
@ -378,7 +400,9 @@ function! s:update_neoinclude(argv, fts) abort
endfor
let b:neoinclude_paths = path
endfunction
" }}}
" local function: go_to_def {{{
function! s:go_to_def() abort
if !SpaceVim#layers#lsp#check_filetype(&ft)
execute "norm! g\<c-]>"
@ -386,3 +410,4 @@ function! s:go_to_def() abort
call SpaceVim#lsp#go_to_def()
endif
endfunction
" }}}

View File

@ -1625,7 +1625,7 @@ Here is an example how to use above options:
name = "lang#c"
clang_executable = "/usr/bin/clang"
clang_flag = ['-I/user/include']
[layer.clang_std]
[layers.clang_std]
c = "c11"
cpp = "c++1z"
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
<
}}}
==============================================================================
LANG#CHAPEL *SpaceVim-layer-lang-chapel*

View File

@ -16,9 +16,11 @@ Each of the following sections will be covered:
<!-- vim-markdown-toc GFM -->
- [Enable language layer](#enable-language-layer)
- [Syntax highlighting](#syntax-highlighting)
- [code completion](#code-completion)
- [alternate file jumping](#alternate-file-jumping)
- [code running](#code-running)
- [Syntax lint](#syntax-lint)
- [code format](#code-format)
- [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.
### 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
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)
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

View File

@ -19,6 +19,7 @@ SpaceVim 是一个模块化的 Vim IDE针对 C/C++ 语言的支持主要依
<!-- vim-markdown-toc GFM -->
- [安装模块](#安装模块)
- [语法高亮](#语法高亮)
- [代码自动补全](#代码自动补全)
- [语法检查](#语法检查)
- [工程文件跳转](#工程文件跳转)
@ -40,6 +41,19 @@ SpaceVim 初次安装时默认并未启用相关语言模块。首先需要启
启用 `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 模块:
@ -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)
默认的代码快速运行命令会读取模块选项 `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` 交互窗口,