1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 22:50:06 +08:00

website: Use Vim as a C/C++ IDE (#2814)

This commit is contained in:
Wang Shidong 2019-06-01 13:30:10 +08:00 committed by GitHub
parent b14c2d1e39
commit 4192fdcc6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 218 additions and 2 deletions

View File

@ -25,6 +25,8 @@
"docs/_posts/2019-02-18-use-vim-as-a-ruby-ide.md": {"alternate": "docs/_posts/2019-02-17-use-vim-as-a-ruby-ide.md"},
"docs/_posts/2019-05-08-use-vim-as-a-php-ide.md": {"alternate": "docs/_posts/2019-05-09-use-vim-as-a-php-ide.md"},
"docs/_posts/2019-05-09-use-vim-as-a-php-ide.md": {"alternate": "docs/_posts/2019-05-08-use-vim-as-a-php-ide.md"},
"docs/_posts/2019-05-10-use-vim-as-a-c-cpp-ide.md": {"alternate": "docs/_posts/2019-05-11-use-vim-as-a-c-cpp-ide.md"},
"docs/_posts/2019-05-11-use-vim-as-a-c-cpp-ide.md": {"alternate": "docs/_posts/2019-05-10-use-vim-as-a-c-cpp-ide.md"},
"docs/_posts/2018-09-27-use-vim-as-ide.md": {"alternate": "docs/_posts/2018-09-28-use-vim-as-ide.md"},
"docs/_posts/2018-01-23-grep-on-the-fly-in-spacevim.md": {"alternate": "docs/_posts/2018-01-31-grep-on-the-fly-in-spacevim.md"},
"docs/_posts/2018-01-31-grep-on-the-fly-in-spacevim.md": {"alternate": "docs/_posts/2018-01-23-grep-on-the-fly-in-spacevim.md"},

View File

@ -113,6 +113,11 @@ function! SpaceVim#layers#lang#c#config() abort
\ 'usestdin' : 1,
\ }
call SpaceVim#plugins#runner#reg_runner('cpp', [runner2, '#TEMP#'])
if !empty(s:c_repl_command)
call SpaceVim#plugins#repl#reg('c', s:c_repl_command)
else
call SpaceVim#plugins#repl#reg('c', 'igcc')
endif
call SpaceVim#mapping#space#regesit_lang_mappings('cpp', funcref('s:language_specified_mappings'))
call SpaceVim#plugins#projectmanager#reg_callback(funcref('s:update_clang_flag'))
if executable('clang')
@ -138,6 +143,8 @@ endfunction
let s:enable_clang_syntax = 0
let s:c_repl_command = ''
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
@ -149,7 +156,7 @@ function! SpaceVim#layers#lang#c#set_variable(var) abort
let g:asyncomplete_clang_executable = a:var.clang_executable
endif
endif
let s:c_repl_command = get(a:var, 'repl_command', '')
if has_key(a:var, 'libclang_path')
if has('nvim')
if s:CPT.has('python3') && SpaceVim#util#haspy3lib('clang')
@ -185,6 +192,19 @@ function! s:language_specified_mappings() abort
call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 'f'],
\ 'call SpaceVim#lsp#references()', 'references', 1)
endif
let g:_spacevim_mappings_space.l.s = {'name' : '+Send'}
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 'i'],
\ 'call SpaceVim#plugins#repl#start("c")',
\ 'start REPL process', 1)
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 'l'],
\ 'call SpaceVim#plugins#repl#send("line")',
\ 'send line and keep code buffer focused', 1)
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 'b'],
\ 'call SpaceVim#plugins#repl#send("buffer")',
\ 'send buffer and keep code buffer focused', 1)
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 's'],
\ 'call SpaceVim#plugins#repl#send("selection")',
\ 'send selection and keep code buffer focused', 1)
endfunction

View File

@ -0,0 +1,86 @@
---
title: "Use Vim as a C/C++ IDE"
categories: [tutorials, blog]
image: https://user-images.githubusercontent.com/13142418/58743787-db2bee80-846a-11e9-9b19-17202ac542c9.png
excerpt: "A general guide for using SpaceVim as C/C++ IDE, including layer configuration, requiems installation and usage."
type: BlogPosting
comments: true
commentsID: "Use Vim as a C/C++ IDE"
---
# [Blogs](../blog/) >> Use Vim as a C/C++ IDE
This is a general guide for using SpaceVim as a C/C++ IDE, including layer configuration and usage.
Each of the following sections will be covered:
<!-- vim-markdown-toc GFM -->
- [Enable language layer](#enable-language-layer)
- [code completion](#code-completion)
- [alternate file jumping](#alternate-file-jumping)
- [code running](#code-running)
- [code format](#code-format)
- [REPL support](#repl-support)
<!-- vim-markdown-toc -->
### Enable language layer
To add C/C++ language support in SpaceVim, you need to enable the `lang#c` layer. Press `SPC f v d` to open
SpaceVim configuration file, and add following configuration:
```toml
[[layers]]
name = "lang#c"
```
for more info, you can read the [lang#c](../layers/lang/c/) layer documentation.
### code completion
By default the autocomplete layer has been enabled, so after loading `lang#c` layer, the code completion
for C/C++ language should works well.
### alternate file jumping
To manage the alternate file for a project, you may need to create a `.project_alt.json` file in the root of your
project.
for exmaple, add following content into the `.project_alt.json` file:
```json
{
"*.c": {"alternate": "{}.h"},
"*.h": {"alternate": "{}.c"}
}
```
with this configuration, you can jump between the alternate file via command `:A`
### code running
The default code running key binding is `SPC l r`. It will compile and run current file asynchronously.
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)
### code format
The format layer use neoformat as default tool to format code, it will format current file.
And the default key binding is `SPC b f`.
```toml
[[layers]]
name = "format"
```
### REPL support
Start a `igcc` inferior REPL process with `SPC l s i`. After the REPL process being started, you can
send code to inferior process, all key bindings prefix with `SPC l s`, including sending line, sending selection or even
send whole buffer.
![c_repl](https://user-images.githubusercontent.com/13142418/58744043-28aa5a80-846f-11e9-94c1-e6927696e662.png)

View File

@ -0,0 +1,108 @@
---
title: "使用 Vim 搭建 C/C++ 开发环境"
categories: [tutorials_cn, blog_cn]
image: https://user-images.githubusercontent.com/13142418/58743787-db2bee80-846a-11e9-9b19-17202ac542c9.png
excerpt: "这篇文章主要介绍如何使用 SpaceVim 搭建 C/C++ 的开发环境,简介 lang#c 模块所支持的功能特性以及使用技巧"
permalink: /cn/:title/
lang: cn
type: BlogPosting
comments: true
commentsID: "使用 Vim 搭建 C/C++ 开发环境"
---
# [Blogs](../blog/) >> 使用 Vim 搭建 C/C++ 开发环境
SpaceVim 是一个模块化的 Vim IDE针对 C/C++ 语言的支持主要依靠 `lang#c` 模块以及与之相关的其它模块。
的这篇文章主要介绍如何使用 SpaceVim 搭建 C/C++ 的开发环境,侧重介绍跟 C/C++ 开发相关使用技巧。
在阅读这篇文章之前,可以先阅读《[使用 Vim 搭建基础的开发环境](../use-vim-as-ide/)》,对语言相关以外的功能有一个大致的了解。
<!-- vim-markdown-toc GFM -->
- [安装模块](#安装模块)
- [代码自动补全](#代码自动补全)
- [语法检查](#语法检查)
- [工程文件跳转](#工程文件跳转)
- [代码格式化](#代码格式化)
- [快速运行](#快速运行)
- [交互式编程](#交互式编程)
<!-- vim-markdown-toc -->
### 安装模块
SpaceVim 初次安装时默认并未启用相关语言模块。首先需要启用
`lang#c` 模块,通过快捷键 `SPC f v d` 打开配置文件,添加如下片断:
```toml
[[layers]]
name = "lang#c"
```
启用 `lang#c` 模块后,在打开 C/C++ 文件时,就可以使用语言专属快捷键,这些快捷键都是以 `SPC l` 为前缀的。
### 代码自动补全
`autocomplete` 模块为 SpaceVim 提供了自动补全功能,目前针对 PHP 而言,比较好的补全方案是配合使用 lsp 模块:
```toml
[[layers]]
name = "lsp"
```
lsp 模块默认使用 `clangd` 作为 C/C++ 的语言服务器后台命令。
在配置文件中添加如下内容即可为 C/C++ 启用语言服务器:
```toml
[[layers]]
name = "lsp"
filetypes = [
"c",
"cpp"
]
[layers.override_cmd]
c = ["clangd"]
```
### 语法检查
`checkers` 模块为 SpaceVim 提供了语法检查的功能,该模块默认已经载入。该模块默认使用 [neomake](https://github.com/neomake/neomake)
这一异步语法检查工具。对于 C/C++ 的支持,是通过异步调用 gcc 命令来完成的。
### 工程文件跳转
SpaceVim 自带工程管理插件,可以识别项目根目录,自动跳转 alternate 文件。需要在项目根目录添加工程文件 `.project_alt.json`
```json
{
"*.c": {"alternate": "{}.h"},
"*.h": {"alternate": "{}.c"}
}
```
通过以上的配置,就可以使用命令 `:A` 在源文件和测试文件之间进行跳转。
### 代码格式化
C/C++ 代码格式化,主要依赖 `format` 模块,快捷键为 `SPC b f`,异步执行 `clang-format` 命令:
```toml
[[layers]]
name = "format"
```
### 快速运行
在编辑 C/C++ 文件时,可以快速运行当前文件,默认的快捷键是 `SPC l r` 。按下后,
会在屏幕下方打开一个插件窗口,运行的结果会被展示在窗口内。于此同时,光标并不会跳到该插件窗口,避免影响编辑。在这里需要说明下,
![c-cpp-runner](https://user-images.githubusercontent.com/13142418/58743787-db2bee80-846a-11e9-9b19-17202ac542c9.png)
### 交互式编程
在编辑 C/C++ 文件时,可通过快捷键 `SPC l s i` 启动 `php -a` 交互窗口,
之后使用快捷键将代码发送至解释器。默认快捷键都以 `SPC l s` 为前缀。
![c_repl](https://user-images.githubusercontent.com/13142418/58744043-28aa5a80-846f-11e9-94c1-e6927696e662.png)