From 1c33d30eb24600bf90cc90197c53635d34a66eb3 Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Mon, 25 Feb 2019 11:18:21 -0600 Subject: [PATCH] Add guide for using vim as ruby ide (#2603) --- .projections.json | 2 + autoload/SpaceVim/custom.vim | 4 +- autoload/SpaceVim/layers.vim | 2 +- autoload/SpaceVim/layers/lang/markdown.vim | 6 + autoload/SpaceVim/layers/lang/ruby.vim | 18 +++ autoload/SpaceVim/layers/lsp.vim | 1 + .../2019-02-17-use-vim-as-a-ruby-ide.md | 128 ++++++++++++++++++ .../2019-02-18-use-vim-as-a-ruby-ide.md | 101 ++++++++++++++ docs/cn/layers/language-server-protocol.md | 7 + docs/layers/language-server-protocol.md | 7 + 10 files changed, 273 insertions(+), 3 deletions(-) create mode 100644 docs/_posts/2019-02-17-use-vim-as-a-ruby-ide.md create mode 100644 docs/_posts/2019-02-18-use-vim-as-a-ruby-ide.md diff --git a/.projections.json b/.projections.json index 59b719032..f1ef21dfa 100644 --- a/.projections.json +++ b/.projections.json @@ -19,6 +19,8 @@ "docs/_posts/2018-09-28-use-vim-as-a-python-ide.md": {"alternate": "docs/_posts/2018-09-27-use-vim-as-a-python-ide.md"}, "docs/_posts/2019-02-11-use-vim-as-a-perl-ide.md": {"alternate": "docs/_posts/2019-02-12-use-vim-as-a-perl-ide.md"}, "docs/_posts/2019-02-12-use-vim-as-a-perl-ide.md": {"alternate": "docs/_posts/2019-02-11-use-vim-as-a-perl-ide.md"}, + "docs/_posts/2019-02-17-use-vim-as-a-ruby-ide.md": {"alternate": "docs/_posts/2019-02-18-use-vim-as-a-ruby-ide.md"}, + "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/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/custom.vim b/autoload/SpaceVim/custom.vim index 68d05cad2..7a3e54d44 100644 --- a/autoload/SpaceVim/custom.vim +++ b/autoload/SpaceVim/custom.vim @@ -135,7 +135,7 @@ function! SpaceVim#custom#load() abort " if file .SpaceVim.d/init.toml exist if filereadable('.SpaceVim.d/init.toml') let g:_spacevim_config_path = s:CMP.resolve(fnamemodify('.SpaceVim.d/init.toml', ':p')) - let &rtp = fnamemodify('.SpaceVim.d', ':p:h') . ',' . &rtp + let &rtp = s:FILE.unify_path(s:CMP.resolve(fnamemodify('.SpaceVim.d', ':p:h'))) . ',' . &rtp let local_conf = g:_spacevim_config_path call SpaceVim#logger#info('find local conf: ' . local_conf) let local_conf_cache = s:path_to_fname(local_conf) @@ -155,7 +155,7 @@ function! SpaceVim#custom#load() abort endif elseif filereadable('.SpaceVim.d/init.vim') let g:_spacevim_config_path = fnamemodify('.SpaceVim.d/init.vim', ':p') - let &rtp = fnamemodify('.SpaceVim.d', ':p:h') . ',' . &rtp + let &rtp = s:FILE.unify_path(s:CMP.resolve(fnamemodify('.SpaceVim.d', ':p:h'))) . ',' . &rtp let local_conf = g:_spacevim_config_path call SpaceVim#logger#info('find local conf: ' . local_conf) exe 'source .SpaceVim.d/init.vim' diff --git a/autoload/SpaceVim/layers.vim b/autoload/SpaceVim/layers.vim index 97bb40908..53d5eb3ee 100644 --- a/autoload/SpaceVim/layers.vim +++ b/autoload/SpaceVim/layers.vim @@ -90,7 +90,7 @@ function! s:find_layers() abort endfunction function! SpaceVim#layers#get() abort - return s:enabled_layers + return deepcopy(s:enabled_layers) endfunction function! SpaceVim#layers#isLoaded(layer) abort diff --git a/autoload/SpaceVim/layers/lang/markdown.vim b/autoload/SpaceVim/layers/lang/markdown.vim index 9e20a193d..62c44aaab 100644 --- a/autoload/SpaceVim/layers/lang/markdown.vim +++ b/autoload/SpaceVim/layers/lang/markdown.vim @@ -31,6 +31,12 @@ function! SpaceVim#layers#lang#markdown#plugins() abort endfunction function! SpaceVim#layers#lang#markdown#config() abort + " do not highlight markdown error + let g:markdown_hi_error = 0 + " the fenced languages based on loaded language layer + let g:markdown_fenced_languages = [] + let g:markdown_nested_languages = map(filter(SpaceVim#layers#get(), + \ 'v:val =~# "^lang#" && v:val !=# "lang#markdown" && v:val !=# "lang#vim"'), 'v:val[5:]') let g:vmt_list_item_char = s:md_listItemChar let g:markdown_minlines = 100 let g:markdown_syntax_conceal = 0 diff --git a/autoload/SpaceVim/layers/lang/ruby.vim b/autoload/SpaceVim/layers/lang/ruby.vim index 48a09f94b..da1d3b6b3 100644 --- a/autoload/SpaceVim/layers/lang/ruby.vim +++ b/autoload/SpaceVim/layers/lang/ruby.vim @@ -16,6 +16,7 @@ let s:ruby_repl_command = '' function! SpaceVim#layers#lang#ruby#config() abort call SpaceVim#plugins#runner#reg_runner('ruby', 'ruby %s') + call SpaceVim#mapping#gd#add('ruby', function('s:go_to_def')) call SpaceVim#mapping#space#regesit_lang_mappings('ruby', function('s:language_specified_mappings')) if !empty(s:ruby_repl_command) call SpaceVim#plugins#repl#reg('ruby',s:ruby_repl_command) @@ -30,6 +31,14 @@ endfunction function! s:language_specified_mappings() abort call SpaceVim#mapping#space#langSPC('nmap', ['l', 'r'], 'call SpaceVim#plugins#runner#open()', 'execute current file', 1) + if SpaceVim#layers#lsp#check_filetype('ruby') + nnoremap K :call SpaceVim#lsp#show_doc() + + call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 'd'], + \ 'call SpaceVim#lsp#show_doc()', 'show_document', 1) + call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 'e'], + \ 'call SpaceVim#lsp#rename()', 'rename symbol', 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("ruby")', @@ -47,4 +56,13 @@ function! s:language_specified_mappings() abort call SpaceVim#mapping#space#langSPC('nmap', ['l','c', 'f'], \ 'Neoformat rubocop', \ 'Runs RuboCop on the currently visited file', 1) + let g:neomake_ruby_rubylint_remove_invalid_entries = 1 +endfunction + +function! s:go_to_def() abort + if !SpaceVim#layers#lsp#check_filetype('ruby') + normal! gd + else + call SpaceVim#lsp#go_to_def() + endif endfunction diff --git a/autoload/SpaceVim/layers/lsp.vim b/autoload/SpaceVim/layers/lsp.vim index 0b978f40e..f09497631 100644 --- a/autoload/SpaceVim/layers/lsp.vim +++ b/autoload/SpaceVim/layers/lsp.vim @@ -100,6 +100,7 @@ let s:lsp_servers = { \ 'rust' : ['rustup', 'run', 'nightly', 'rls'], \ 'sh' : ['bash-language-server', 'start'], \ 'typescript' : ['typescript-language-server', '--stdio'], + \ 'ruby' : ['solargraph.BAT', 'stdio'], \ 'vue' : ['vls'] \ } diff --git a/docs/_posts/2019-02-17-use-vim-as-a-ruby-ide.md b/docs/_posts/2019-02-17-use-vim-as-a-ruby-ide.md new file mode 100644 index 000000000..afdf7f533 --- /dev/null +++ b/docs/_posts/2019-02-17-use-vim-as-a-ruby-ide.md @@ -0,0 +1,128 @@ +--- +title: "使用 Vim 搭建 Ruby 开发环境" +categories: [tutorials_cn, blog_cn] +image: https://user-images.githubusercontent.com/13142418/53355518-20202080-3964-11e9-92f3-476060f2761e.png +excerpt: "这篇文章主要介绍如何使用 SpaceVim 搭建 Ruby 的开发环境,简介 lang#ruby 模块所支持的功能特性以及使用技巧" +permalink: /cn/:title/ +lang: cn +type: BlogPosting +comments: true +commentsID: "使用 Vim 搭建 Ruby 开发环境" +--- + +# [Blogs](../blog/) >> 使用 Vim 搭建 Ruby 开发环境 + +SpaceVim 是一个模块化的 Vim IDE,针对 Ruby 这一语言的支持主要依靠 `lang#ruby` 模块以及与之相关的其它模块。 +的这篇文章主要介绍如何使用 SpaceVim 搭建 Ruby 的开发环境,侧重介绍跟 Ruby 开发相关使用技巧。 +在阅读这篇文章之前,可以先阅读《[使用 Vim 搭建基础的开发环境](../use-vim-as-ide/)》,对语言相关以外的功能有一个大致的了解。 + + + +- [安装模块](#安装模块) +- [代码自动补全](#代码自动补全) +- [语法检查](#语法检查) +- [工程文件跳转](#工程文件跳转) +- [快速运行](#快速运行) +- [代码格式化](#代码格式化) +- [交互式编程](#交互式编程) + + + +### 安装模块 + +SpaceVim 初次安装时默认并未启用相关语言模块。首先需要启用 +`lang#ruby` 模块,通过快捷键 `SPC f v d` 打开配置文件,添加如下片断: + +```toml +[[layers]] + name = "lang#ruby" +``` + +启用 `lang#ruby` 模块后,在打开 Ruby 文件时,就可以使用语言专属快捷键,这些快捷键都是以 `SPC l` 为前缀的。 + +### 代码自动补全 + +`autocomplete` 模块为 SpaceVim 提供了自动补全功能,目前针对 Ruby 而言,比较好的补全方案是配合使用 lsp 模块: + +```toml +[[layers]] + name = "lsp" +``` + +lsp 模块默认使用 `["solargraph", "stdio"]` 作为 Ruby 的语言服务器后台命令,首先需要安装 solargraph: + +```sh +gem install solargraph +``` + +在配置文件中添加如下内容即可为 ruby 启用语言服务器: + +```toml +[[layers]] + name = "lsp" + filetypes = [ + "ruby" + ] + [layers.override_cmd] + ruby = ["solargraph", "stdio"] +``` + +### 语法检查 + +`checkers` 模块为 SpaceVim 提供了语法检查的功能,该模块默认已经载入。该模块默认使用 [neomake](https://github.com/neomake/neomake) +这一异步语法检查工具。对于 Ruby 的支持,是通过异步调用 rubocop 命令来完成的。 + +使用 gem 安装 rubocop 命令: + +```sh +gem install rubocop +``` + +![rubylint](https://user-images.githubusercontent.com/13142418/53347011-32459300-3953-11e9-9ca2-3e07f832db5a.png) + +### 工程文件跳转 + +SpaceVim 自带工程管理插件,可以识别项目根目录,自动跳转 alternate 文件。需要在项目根目录添加工程文件 `.project_alt.json`: + +```json +{ + "src/*.rb": {"alternate": "test/{}.rb"}, + "test/*.rb": {"alternate": "src/{}.rb"} +} +``` + +通过以上的配置,就可以使用命令 `:A` 在源文件和测试文件之间进行跳转。 + +### 快速运行 + +在编辑 Ruby 文件时,可以快速运行当前文件,这个功能有点类似于 vscode 的 code runner 插件,默认的快捷键是 `SPC l r` 。按下后, +会在屏幕下方打开一个插件窗口,运行的结果会被展示在窗口内。于此同时,光标并不会跳到该插件窗口,避免影响编辑。在这里需要说明下, +这一功能是根据当前 buffer 内容调用 Ruby 命令。因此,在执行这个快捷键之前,不一定要保存该文件。 + +![rubyrunner](https://user-images.githubusercontent.com/13142418/53300165-6b600380-387e-11e9-852f-f8766300ece1.gif) + +### 代码格式化 + +Ruby 代码格式化,主要依赖 `format` 模块,同时需要安装相关的后台命令 rufo,默认快捷键为 `SPC b f` : + +```toml +[[layers]] + name = "format" +``` + +使用 gem 安装 rufo: + +```sh +gem install rufo +``` + +![formatruby](https://user-images.githubusercontent.com/13142418/53301042-3c02c400-3889-11e9-9918-430ad6a7f08f.gif) + +### 交互式编程 + +在编辑 Ruby 文件时,可通过快捷键 `SPC l s i` 启动 `irb` 交互窗口, +之后使用快捷键将代码发送至解释器。默认快捷键都以 `SPC l s` 为前缀。 + +![rubyrepl](https://user-images.githubusercontent.com/13142418/53347455-1098db80-3954-11e9-87c3-13a027ec88f6.gif) + + diff --git a/docs/_posts/2019-02-18-use-vim-as-a-ruby-ide.md b/docs/_posts/2019-02-18-use-vim-as-a-ruby-ide.md new file mode 100644 index 000000000..e848061a7 --- /dev/null +++ b/docs/_posts/2019-02-18-use-vim-as-a-ruby-ide.md @@ -0,0 +1,101 @@ +--- +title: "Use Vim as a Ruby IDE" +categories: [tutorials, blog] +image: https://user-images.githubusercontent.com/13142418/53355518-20202080-3964-11e9-92f3-476060f2761e.png +excerpt: "A general guide for using SpaceVim as Ruby IDE, including layer configuration, requiems installation and usage." +type: BlogPosting +comments: true +commentsID: "Use Vim as a Ruby IDE" +--- + +# [Blogs](../blog/) >> Use Vim as a Ruby IDE + +This is a general guide for using SpaceVim as a Ruby IDE, including layer configuration and usage. +Each of the following sections will be covered: + + + + +- [Enable language layer](#enable-language-layer) +- [Code completion](#code-completion) +- [Syntax linting](#syntax-linting) +- [Jump to test file](#jump-to-test-file) +- [running code](#running-code) +- [Code formatting](#code-formatting) +- [REPL support](#repl-support) + + + +### Enable language layer + +To add Ruby language support in SpaceVim, you need to enable the `lang#ruby` layer. Press `SPC f v d` to open +SpaceVim configuration file, and add the following snippet: + +```toml +[[layers]] + name = "lang#ruby" +``` + +For more info, you can read the [lang#ruby](../layers/lang/ruby/) layer documentation. + +### Code completion + +`lang#ruby` layer will load the Ruby plugin automatically, unless it's overriden in your `init.toml`. +The completion menu will be opened as you type. + +![perlcomplete](https://user-images.githubusercontent.com/13142418/52611209-54550500-2ebf-11e9-9b9f-f697a0db52a3.png) + +### Syntax linting + +The checkers layer is enabled by default. This layer provides asynchronous syntax linting via [neomake](https://github.com/neomake/neomake). +It will run rubocop asynchronously. + +Install rubocop via gem: + +```sh +gem install rubocop +``` + +![rubylint](https://user-images.githubusercontent.com/13142418/53347011-32459300-3953-11e9-9ca2-3e07f832db5a.png) + +### Jump to test file + +SpaceVim use built-in plugin to manager the files in a project, you can add a `.project_alt.json` to the root of your project with the following content: + +```json +{ + "src/*.rb": {"alternate": "test/{}.rb"}, + "test/*.rb": {"alternate": "src/{}.rb"} +} +``` + +With this configuration, you can jump between the source code and test file via command `:A` + +### running code + +To run current script, you can press `SPC l r`, and a split window +will be openen, the output of the script will be shown in this window. +It is running asynchronously, and will not block your Vim. + +![rubyrunner](https://user-images.githubusercontent.com/13142418/53300165-6b600380-387e-11e9-852f-f8766300ece1.gif) + +### Code formatting + +The format layer is also enabled by default. With this layer you can use key binding `SPC b f` to format current buffer. +Before using this feature, please install rufo: + +```sh +gem install rufo +``` + +![formatruby](https://user-images.githubusercontent.com/13142418/53301042-3c02c400-3889-11e9-9918-430ad6a7f08f.gif) + +### REPL support + +Start a `irb` 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. + +![rubyrepl](https://user-images.githubusercontent.com/13142418/53347455-1098db80-3954-11e9-87c3-13a027ec88f6.gif) + + diff --git a/docs/cn/layers/language-server-protocol.md b/docs/cn/layers/language-server-protocol.md index 32de4cd34..b5619da9c 100644 --- a/docs/cn/layers/language-server-protocol.md +++ b/docs/cn/layers/language-server-protocol.md @@ -106,6 +106,12 @@ npm install vue-language-server -g npm install -g vscode-css-languageserver-bin ``` +**ruby:** + +```sh +gem install solargraph +``` + ## 模块配置 为指定模块启用语言服务器支持,需要在载入模块时,指定 `filetypes` 选项: @@ -137,6 +143,7 @@ npm install -g vscode-css-languageserver-bin | `php` | `['php', 'path/to/bin/php-language-server.php']` | | `purescript` | `['purescript-language-server', '--stdio']` | | `python` | `['pyls']` | +| `ruby` | `['solargraph', 'stdio']` | `rust` | `['rustup', 'run', 'nightly', 'rls']` | | `sh` | `['bash-language-server', 'start']` | | `typescript` | `['typescript-language-server', '--stdio']` | diff --git a/docs/layers/language-server-protocol.md b/docs/layers/language-server-protocol.md index 9aec8d282..aba7f2871 100644 --- a/docs/layers/language-server-protocol.md +++ b/docs/layers/language-server-protocol.md @@ -119,6 +119,12 @@ npm install vue-language-server -g npm install -g vscode-css-languageserver-bin ``` +**ruby:** + +```sh +gem install solargraph +``` + ## Configuration To enable lsp support for a specified filetype, you may need to load this layer with `filtypes` option, for example: @@ -150,6 +156,7 @@ default language server commands: | `php` | `['php', 'path/to/bin/php-language-server.php']` | | `purescript` | `['purescript-language-server', '--stdio']` | | `python` | `['pyls']` | +| `ruby` | `['solargraph', 'stdio']` | `rust` | `['rustup', 'run', 'nightly', 'rls']` | | `sh` | `['bash-language-server', 'start']` | | `typescript` | `['typescript-language-server', '--stdio']` |