mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-15 19:45:46 +08:00
Add lua system
api & test (#4392)
This commit is contained in:
parent
3b455c1b7f
commit
66f3306acb
2
.github/workflows/check.yml
vendored
2
.github/workflows/check.yml
vendored
@ -49,7 +49,7 @@ jobs:
|
|||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install ninja-build gettext libtool libtool-bin autoconf automake cmake g++ pkg-config unzip curl
|
sudo apt-get install ninja-build gettext libtool libtool-bin autoconf automake cmake g++ pkg-config unzip curl lua5.2 liblua5.2-dev
|
||||||
- name: Install ${{ matrix.vimbin }} ${{ matrix.vimtag }}
|
- name: Install ${{ matrix.vimbin }} ${{ matrix.vimtag }}
|
||||||
run: |
|
run: |
|
||||||
.ci/install/linux.sh $VIM_BIN $VIM_TAG
|
.ci/install/linux.sh $VIM_BIN $VIM_TAG
|
||||||
|
@ -40,7 +40,7 @@ function! s:name() abort
|
|||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
let s:system['name'] = s:name()
|
let s:system['name'] = function('s:name')
|
||||||
|
|
||||||
function! s:isDarwin() abort
|
function! s:isDarwin() abort
|
||||||
if exists('s:is_darwin')
|
if exists('s:is_darwin')
|
||||||
|
@ -345,6 +345,17 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
" this function is only for test
|
||||||
|
function! s:self.luaeval(expr) abort
|
||||||
|
let rst = luaeval(a:expr)
|
||||||
|
if type(rst) ==# 5
|
||||||
|
return float2nr(rst)
|
||||||
|
else
|
||||||
|
return rst
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function! SpaceVim#api#vim#compatible#get() abort
|
function! SpaceVim#api#vim#compatible#get() abort
|
||||||
return deepcopy(s:self)
|
return deepcopy(s:self)
|
||||||
|
@ -76,7 +76,12 @@ function! SpaceVim#default#options() abort
|
|||||||
set backup
|
set backup
|
||||||
set undofile
|
set undofile
|
||||||
set undolevels=1000
|
set undolevels=1000
|
||||||
let g:data_dir = g:spacevim_data_dir.'/SpaceVim/'
|
" Neovim 0.5 changed the undofile format
|
||||||
|
if has('nvim-0.5.0')
|
||||||
|
let g:data_dir = g:spacevim_data_dir.'/SpaceVim/'
|
||||||
|
else
|
||||||
|
let g:data_dir = g:spacevim_data_dir.'/SpaceVim/old/'
|
||||||
|
endif
|
||||||
let g:backup_dir = g:data_dir . 'backup//'
|
let g:backup_dir = g:data_dir . 'backup//'
|
||||||
let g:swap_dir = g:data_dir . 'swap//'
|
let g:swap_dir = g:data_dir . 'swap//'
|
||||||
let g:undo_dir = g:data_dir . 'undofile//'
|
let g:undo_dir = g:data_dir . 'undofile//'
|
||||||
|
@ -5,17 +5,55 @@ description: "system API provides some basic functions and values for current os
|
|||||||
|
|
||||||
# [Available APIs](../) >> system
|
# [Available APIs](../) >> system
|
||||||
|
|
||||||
## values
|
<!-- vim-markdown-toc GFM -->
|
||||||
|
|
||||||
|
- [Intro](#intro)
|
||||||
|
- [Valuables](#valuables)
|
||||||
|
- [Functions](#functions)
|
||||||
|
- [Usage](#usage)
|
||||||
|
|
||||||
|
<!-- vim-markdown-toc -->
|
||||||
|
|
||||||
|
## Intro
|
||||||
|
|
||||||
|
The `system` provides basic functions for os detection.
|
||||||
|
|
||||||
|
## Valuables
|
||||||
|
|
||||||
| name | values | description |
|
| name | values | description |
|
||||||
| --------- | :----: | -------------------------- |
|
| --------- | :----: | -------------------------- |
|
||||||
| isWindows | 0 or 1 | check if the os is windows |
|
| isWindows | 0 or 1 | check if the os is windows |
|
||||||
| isLinux | 0 or 1 | check if the os is linux |
|
| isLinux | 0 or 1 | check if the os is linux |
|
||||||
| isOSX | 0 or 1 | check if the os is OSX |
|
| isOSX | 0 or 1 | check if the os is OSX |
|
||||||
| isDarwin | 0 or 1 | check if the os is Darwin |
|
|
||||||
|
|
||||||
## functions
|
## Functions
|
||||||
|
|
||||||
| name | description |
|
| name | description |
|
||||||
| ---------- | -------------------------------------- |
|
| ------------ | ---------------------------------------- |
|
||||||
| fileformat | return the icon of current file format |
|
| fileformat() | return the icon of current file format |
|
||||||
|
| isDarwin() | return 0 or 1, check if the os is Darwin |
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
This api can be used in both vim script and lua script.
|
||||||
|
|
||||||
|
**vim script:**
|
||||||
|
|
||||||
|
```vim
|
||||||
|
let s:system = SpaceVim#api#import('system')
|
||||||
|
|
||||||
|
" check the if current os is Windows.
|
||||||
|
if s:system.isWindows
|
||||||
|
echom "OS is Windows"
|
||||||
|
endif
|
||||||
|
```
|
||||||
|
|
||||||
|
**lua script:**
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local sys = require('spacevim.api').import('system')
|
||||||
|
|
||||||
|
if sys.isWindows == 1 then
|
||||||
|
print('this is windows os!')
|
||||||
|
end
|
||||||
|
```
|
||||||
|
@ -9,8 +9,9 @@ lang: zh
|
|||||||
<!-- vim-markdown-toc GFM -->
|
<!-- vim-markdown-toc GFM -->
|
||||||
|
|
||||||
- [简介](#简介)
|
- [简介](#简介)
|
||||||
- [values](#values)
|
- [变量](#变量)
|
||||||
- [functions](#functions)
|
- [函数](#函数)
|
||||||
|
- [基本使用](#基本使用)
|
||||||
|
|
||||||
<!-- vim-markdown-toc -->
|
<!-- vim-markdown-toc -->
|
||||||
|
|
||||||
@ -18,7 +19,7 @@ lang: zh
|
|||||||
|
|
||||||
system 函数提供了系统相关函数,包括判断当前系统平台,文件格式等函数。
|
system 函数提供了系统相关函数,包括判断当前系统平台,文件格式等函数。
|
||||||
|
|
||||||
## values
|
## 变量
|
||||||
|
|
||||||
| names | values | descriptions |
|
| names | values | descriptions |
|
||||||
| --------- | ------ | -------------------------- |
|
| --------- | ------ | -------------------------- |
|
||||||
@ -27,8 +28,34 @@ system 函数提供了系统相关函数,包括判断当前系统平台,文
|
|||||||
| isOSX | 0 or 1 | check if the os is OSX |
|
| isOSX | 0 or 1 | check if the os is OSX |
|
||||||
| isDarwin | 0 or 1 | check if the os is Darwin |
|
| isDarwin | 0 or 1 | check if the os is Darwin |
|
||||||
|
|
||||||
## functions
|
## 函数
|
||||||
|
|
||||||
| names | descriptions |
|
| name | description |
|
||||||
| ---------- | -------------------------------------- |
|
| ------------ | ---------------------------------------- |
|
||||||
| fileformat | return the icon of current file format |
|
| fileformat() | return the icon of current file format |
|
||||||
|
| isDarwin() | return 0 or 1, check if the os is Darwin |
|
||||||
|
|
||||||
|
## 基本使用
|
||||||
|
|
||||||
|
这一个函数接口提供了两种版本可供使用,Vim 脚本 和 Lua 脚本:
|
||||||
|
|
||||||
|
**vim script:**
|
||||||
|
|
||||||
|
```vim
|
||||||
|
let s:system = SpaceVim#api#import('system')
|
||||||
|
|
||||||
|
" check the if current os is Windows.
|
||||||
|
if s:system.isWindows
|
||||||
|
echom "OS is Windows"
|
||||||
|
endif
|
||||||
|
```
|
||||||
|
|
||||||
|
**lua script:**
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local sys = require('spacevim.api').import('system')
|
||||||
|
|
||||||
|
if sys.isWindows == 1 then
|
||||||
|
print('this is windows os!')
|
||||||
|
end
|
||||||
|
```
|
||||||
|
@ -14,11 +14,63 @@ function M.bootstrap()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.eval(l)
|
function M.eval(l)
|
||||||
if vim['api'] ~= nil then
|
if vim.api ~= nil then
|
||||||
return vim.eval(l)
|
|
||||||
else
|
|
||||||
return vim.api.nvim_eval(l)
|
return vim.api.nvim_eval(l)
|
||||||
|
else
|
||||||
|
return vim.eval(l)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- there is no want to call viml function in old vim and neovim
|
||||||
|
|
||||||
|
function M.call(funcname, ...)
|
||||||
|
if vim.call ~= nil then
|
||||||
|
return vim.call(funcname, ...)
|
||||||
|
else
|
||||||
|
if vim.api ~= nil then
|
||||||
|
return vim.api.nvim_call_function(funcname, {...})
|
||||||
|
else
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- this is for Vim and old neovim
|
||||||
|
M.fn = setmetatable({}, {
|
||||||
|
__index = function(t, key)
|
||||||
|
local _fn
|
||||||
|
if vim.api ~= nil and vim.api[key] ~= nil then
|
||||||
|
_fn = function()
|
||||||
|
error(string.format("Tried to call API function with vim.fn: use vim.api.%s instead", key))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
_fn = function(...)
|
||||||
|
return M.call(key, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
t[key] = _fn
|
||||||
|
return _fn
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
-- This is for vim and old neovim to use vim.o
|
||||||
|
M.vim_options = setmetatable({}, {
|
||||||
|
__index = function(t, key)
|
||||||
|
local _fn
|
||||||
|
if vim.api ~= nil then
|
||||||
|
-- for neovim
|
||||||
|
return vim.api.nvim_get_option(key)
|
||||||
|
else
|
||||||
|
-- for vim
|
||||||
|
_fn = M.eval('&' .. key)
|
||||||
|
end
|
||||||
|
t[key] = _fn
|
||||||
|
return _fn
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
-- this function is only for vim
|
||||||
|
function M.has(feature)
|
||||||
|
return M.eval('float2nr(has("' .. feature .. '"))')
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
94
lua/spacevim/api/system.lua
Normal file
94
lua/spacevim/api/system.lua
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
local has = nil
|
||||||
|
local fn = nil
|
||||||
|
local vim_options = nil
|
||||||
|
|
||||||
|
if vim.o ~= nil then
|
||||||
|
vim_options = vim.o
|
||||||
|
else
|
||||||
|
vim_options = require('spacevim').vim_options
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
if vim.api == nil then
|
||||||
|
has = require('spacevim').has
|
||||||
|
else
|
||||||
|
if vim.fn ~= nil then
|
||||||
|
has = vim.fn.has
|
||||||
|
else
|
||||||
|
has = require('spacevim').has
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if vim.fn == nil then
|
||||||
|
fn = require('spacevim').fn
|
||||||
|
else
|
||||||
|
fn = vim.fn
|
||||||
|
end
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
if has('win16') ==1 or has('win32') == 1 or has('win64') == 1 then
|
||||||
|
M.isWindows = 1
|
||||||
|
else
|
||||||
|
M.isWindows = 0
|
||||||
|
end
|
||||||
|
if has('unix') == 1 and has('macunix') == 0 and has('win32unix') == 0 then
|
||||||
|
M.isLinux = 1
|
||||||
|
else
|
||||||
|
M.isLinux = 0
|
||||||
|
end
|
||||||
|
M.isOSX = has('macunix')
|
||||||
|
|
||||||
|
function M.name()
|
||||||
|
if M.isLinux == 1 then
|
||||||
|
return 'linux'
|
||||||
|
elseif M.isWindows == 1 then
|
||||||
|
if has('win32unix') == 1 then
|
||||||
|
return 'cygwin'
|
||||||
|
else
|
||||||
|
return 'windows'
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return 'mac'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local is_darwin = nil
|
||||||
|
function M.isDarwin()
|
||||||
|
if is_darwin ~= nil then
|
||||||
|
return is_darwin
|
||||||
|
end
|
||||||
|
if has('macunix') == 1 then
|
||||||
|
is_darwin = 1
|
||||||
|
return is_darwin
|
||||||
|
end
|
||||||
|
if has('unix') ~= 1 then
|
||||||
|
is_darwin = 0
|
||||||
|
return is_darwin
|
||||||
|
end
|
||||||
|
if fn.system('uname -s') == "Darwin\n" then
|
||||||
|
is_darwin = 1
|
||||||
|
else
|
||||||
|
is_darwin = 0
|
||||||
|
end
|
||||||
|
return is_darwin
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.fileformat()
|
||||||
|
local fileformat = ''
|
||||||
|
if vim_options.fileformat == 'dos' then
|
||||||
|
fileformat = ''
|
||||||
|
elseif vim_options.fileformat == 'unix' then
|
||||||
|
if M.isDarwin() == 1 then
|
||||||
|
fileformat = ''
|
||||||
|
else
|
||||||
|
fileformat = ''
|
||||||
|
end
|
||||||
|
elseif vim_options.fileformat == 'mac' then
|
||||||
|
fileformat = ''
|
||||||
|
end
|
||||||
|
return fileformat
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
|
|
16
test/lua/api/system.vader
Normal file
16
test/lua/api/system.vader
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
Execute ( lua api system ):
|
||||||
|
let api_system = SpaceVim#api#import('system')
|
||||||
|
let cmp = SpaceVim#api#import('vim#compatible')
|
||||||
|
Log 'test system.isWindows'
|
||||||
|
AssertEqual cmp.luaeval('require("spacevim.api").import("system").isWindows'), api_system.isWindows
|
||||||
|
Log 'test system.isLinux'
|
||||||
|
AssertEqual cmp.luaeval('require("spacevim.api").import("system").isLinux'), api_system.isLinux
|
||||||
|
Log 'test system.isOSX'
|
||||||
|
AssertEqual cmp.luaeval('require("spacevim.api").import("system").isOSX'), api_system.isOSX
|
||||||
|
Log 'test system.name()'
|
||||||
|
AssertEqual cmp.luaeval('require("spacevim.api").import("system").name()'), api_system.name()
|
||||||
|
Log 'test system.isDarwin()'
|
||||||
|
AssertEqual cmp.luaeval('require("spacevim.api").import("system").isDarwin()'), api_system.isDarwin()
|
||||||
|
Log 'test system.fileformat()'
|
||||||
|
AssertEqual cmp.luaeval('require("spacevim.api").import("system").fileformat()'), api_system.fileformat()
|
||||||
|
unlet api_system
|
Loading…
x
Reference in New Issue
Block a user