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

Improve lua support (#2470)

This commit is contained in:
Wang Shidong 2019-01-20 20:40:58 +08:00 committed by GitHub
parent 02e6d9d3b0
commit 3acb74eb55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 183 additions and 11 deletions

View File

@ -21,20 +21,14 @@
function! SpaceVim#layers#lang#lua#plugins() abort
let plugins = []
" Improved Lua 5.3 syntax and indentation support for Vim
call add(plugins, ['tbastos/vim-lua', {'on_ft' : 'lua'}])
call add(plugins, ['wsdjeg/vim-lua', {'on_ft' : 'lua'}])
call add(plugins, ['WolfgangMehner/lua-support', {'on_ft' : 'lua'}])
call add(plugins, ['SpaceVim/vim-luacomplete', {'on_ft' : 'lua', 'if' : has('lua')}])
return plugins
endfunction
let s:lua_repl_command = ''
function! SpaceVim#layers#lang#lua#config() abort
if has('lua')
augroup spacevim_lua
autocmd FileType lua setlocal omnifunc=luacomplete#complete
augroup END
endif
call SpaceVim#mapping#space#regesit_lang_mappings('lua', function('s:language_specified_mappings'))
let luaexe = filter(['lua53', 'lua52', 'lua51'], 'executable(v:val)')
@ -43,8 +37,23 @@ function! SpaceVim#layers#lang#lua#config() abort
else
call SpaceVim#plugins#runner#reg_runner('lua', 'lua %s')
endif
let g:neomake_lua_enabled_makers = ['luac']
let luacexe = filter(['luac53', 'luac52', 'luac51'], 'executable(v:val)')
if !empty(luacexe)
let g:neomake_lua_luac_maker = {
\ 'exe': luacexe[0],
\ 'args': ['-p'],
\ 'errorformat': '%*\f: %#%f:%l: %m',
\ }
else
let g:neomake_lua_luac_maker = {
\ 'exe': 'luac',
\ 'args': ['-p'],
\ 'errorformat': '%*\f: %#%f:%l: %m',
\ }
endif
if !empty(s:lua_repl_command)
call SpaceVim#plugins#repl#reg('lua',s:lua_repl_command)
call SpaceVim#plugins#repl#reg('lua',s:lua_repl_command)
else
if executable('luap')
call SpaceVim#plugins#repl#reg('lua', 'luap')

View File

@ -17,7 +17,7 @@ Each of the following sections will be covered:
- [Enable language layer](#enable-language-layer)
- [Code completion](#code-completion)
- [Syntax lint](#syntax-lint)
- [Syntax linting](#syntax-linting)
- [Import packages](#import-packages)
- [Jump to test file](#jump-to-test-file)
- [running code](#running-code)
@ -50,8 +50,6 @@ The completion menu will be opened as you type.
1. [neomake](https://github.com/neomake/neomake) - Asynchronous linting and make framework for Neovim/Vim
The javac maker in neomake supports maven projects, gradle projects or eclipse projects. You can also set the classpath.
### Import packages
When edit python file, you can import the package automatically, remove unused package and format package list.

View File

@ -0,0 +1,82 @@
---
title: "Use Vim as a Lua IDE"
categories: [tutorials, blog]
images: https://user-images.githubusercontent.com/13142418/51436347-3502f780-1cc6-11e9-9ae1-02e1dfa1e165.png
excerpt: "A general guide for using SpaceVim as Lua IDE, including layer configuration, requiems installation and usage."
type: BlogPosting
comments: true
commentsID: "Use Vim as a Lua IDE"
---
# [Blogs](../blog/) >> Use Vim as a Lua IDE
This is a general guide for using SpaceVim as a lua 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)
- [Syntax linting](#syntax-linting)
- [Jump to test file](#jump-to-test-file)
- [running code](#running-code)
- [Code formatting](#code-formatting)
<!-- vim-markdown-toc -->
### Enable language layer
To add lua language support in SpaceVim, you need to enable the `lang#lua` layer. Press `SPC f v d` to open
SpaceVim configuration file, and add following configuration:
```toml
[[layers]]
name = "lang#lua"
```
for more info, you can read the [lang#lua](../layers/lang/lua/) layer documentation.
### Code completion
`lang#lua` layer will load the vim-lua plugin automatically, unless overriden in your `init.toml`.
The completion menu will be opened as you type.
![lua](https://user-images.githubusercontent.com/13142418/51436347-3502f780-1cc6-11e9-9ae1-02e1dfa1e165.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 luac asynchronously.
![luac](https://user-images.githubusercontent.com/13142418/51438866-b8cfda80-1cec-11e9-8645-b43fc6481e42.png)
### Jump to test file
SpaceVim use built-in plugin to manager the files in a project, you can add a `.projections.json` to the root of your project with following content:
```json
{
"src/*.lua": {"alternate": "test/{}.lua"},
"test/*.lua": {"alternate": "src/{}.lua"}
}
```
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 windows
will be openen, the output of the script will be shown in this windows.
It is running asynchronously, and will not block your vim.
![luarunner](https://user-images.githubusercontent.com/13142418/51438907-76f36400-1ced-11e9-8838-441965a22ce9.png)
### 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 luaformatter.
```sh
luarocks install formatter
```

View File

@ -0,0 +1,83 @@
---
title: "使用 Vim 搭建 Lua 开发环境"
categories: [tutorials_cn, blog_cn]
images: https://user-images.githubusercontent.com/13142418/51436347-3502f780-1cc6-11e9-9ae1-02e1dfa1e165.png
excerpt: "这篇文章主要介绍如何使用 SpaceVim 搭建 Lua 的开发环境,简介 lang#lua 模块所支持的功能特性以及使用技巧"
permalink: /cn/:title/
lang: cn
type: BlogPosting
comments: true
commentsID: "使用 Vim 搭建 Lua 开发环境"
---
# [Blogs](../blog/) >> 使用 Vim 搭建 Lua 开发环境
SpaceVim 是一个模块化的 Vim IDE针对 lua 这一语言的支持主要依靠 `lang#lua` 模块以及与之相关的其他模块。
的这篇文章主要介绍如何使用 SpaceVim 搭建 lua 的开发环境,侧重介绍跟 lua 开发相关使用技巧。
在阅读这篇文章之前,可以先阅读《[使用 Vim 搭建基础的开发环境](../use-vim-as-ide/)》,对语言相关以外的功能有一个大致的了解。
<!-- vim-markdown-toc GFM -->
- [安装模块](#安装模块)
- [代码自动补全](#代码自动补全)
- [语法检查](#语法检查)
- [工程文件跳转](#工程文件跳转)
- [快速运行](#快速运行)
- [代码格式化](#代码格式化)
<!-- vim-markdown-toc -->
### 安装模块
SpaceVim 初次安装时默认并未启用相关语言模块。首先需要启用
`lang#lua` 模块, 通过快捷键 `SPC f v d` 打开配置文件,添加:
```toml
[[layers]]
name = "lang#lua"
```
启用 `lang#lua` 模块后,在打开 lua 文件时,就可以使用语言专属快捷键,这些快捷键都是以 `SPC l` 为前缀的。
### 代码自动补全
`autocomplete` 模块为 SpaceVim 提供了自动补全功能,
该模块会根据当前环境自动在多种补全引擎之间选择合适的,
默认的补全引擎有deoplete、neocomplete、ycm、asyncomplete 以及 neocomplcache。
几种自动补全引擎当中,要数 deoplete 的体验效果最好。
![lua](https://user-images.githubusercontent.com/13142418/51436347-3502f780-1cc6-11e9-9ae1-02e1dfa1e165.png)
### 语法检查
`checkers` 模块为 SpaceVim 提供了语法检查的功能,该模块默认已经载入。该模块默认使用 [neomake](https://github.com/neomake/neomake)
这一异步语法检查工具。对于 luac 的支持,是通过异步调用 luac 命令来完成的。
![luac](https://user-images.githubusercontent.com/13142418/51438866-b8cfda80-1cec-11e9-8645-b43fc6481e42.png)
### 工程文件跳转
SpaceVim 自带工程管理插件可以识别项目根目录自动跳转alternate文件。
### 快速运行
在编辑 lua 文件时,可以快速运行当前文件,这个功能有点类似于 vscode 的 code runner 插件,默认的快捷键是 `SPC l r`。按下后,
会在屏幕下方打开一个插件窗口,运行的结果会被展示在窗口内。于此同时,光标并不会跳到该插件窗口,避免影响编辑。在这里需要说明下,
这一功能是根据当前文件的路径调用相对应的 lua 命令。因此,在执行这个快捷键之前,应当先保存一下该文件。
![luarunner](https://user-images.githubusercontent.com/13142418/51438907-76f36400-1ced-11e9-8838-441965a22ce9.png)
### 代码格式化
lua 代码格式化,主要依赖 `format` 模块,同时需要安装相关的后台命令 luaformatter
```toml
[[layers]]
name = "format"
```
安装 luaformatter
```sh
luarocks install formatter
```