diff --git a/.projections.json b/.projections.json index 04afdf6dc..d416caa7f 100644 --- a/.projections.json +++ b/.projections.json @@ -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"}, diff --git a/autoload/SpaceVim/layers/lang/c.vim b/autoload/SpaceVim/layers/lang/c.vim index 2d8af5508..6b9117bdd 100644 --- a/autoload/SpaceVim/layers/lang/c.vim +++ b/autoload/SpaceVim/layers/lang/c.vim @@ -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') @@ -125,7 +130,7 @@ function! SpaceVim#layers#lang#c#config() abort autocmd! if s:enable_clang_syntax if has('nvim') && SpaceVim#util#haspy3lib('clang') - auto FileType c,cpp ChromaticaStart + auto FileType c,cpp ChromaticaStart " else Clamp will start when detect c, cpp file elseif !has('job') " Clighter8 will start when detect c, cpp file @@ -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 diff --git a/docs/_posts/2019-05-10-use-vim-as-a-c-cpp-ide.md b/docs/_posts/2019-05-10-use-vim-as-a-c-cpp-ide.md new file mode 100644 index 000000000..48a243cce --- /dev/null +++ b/docs/_posts/2019-05-10-use-vim-as-a-c-cpp-ide.md @@ -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: + + + +- [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) + + + +### 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) diff --git a/docs/_posts/2019-05-11-use-vim-as-a-c-cpp-ide.md b/docs/_posts/2019-05-11-use-vim-as-a-c-cpp-ide.md new file mode 100644 index 000000000..7f15ad602 --- /dev/null +++ b/docs/_posts/2019-05-11-use-vim-as-a-c-cpp-ide.md @@ -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/)》,对语言相关以外的功能有一个大致的了解。 + + + +- [安装模块](#安装模块) +- [代码自动补全](#代码自动补全) +- [语法检查](#语法检查) +- [工程文件跳转](#工程文件跳转) +- [代码格式化](#代码格式化) +- [快速运行](#快速运行) +- [交互式编程](#交互式编程) + + + +### 安装模块 + +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)