perf: don't make local copies of global `vim` module

pull/14/head
Iron-E 3 years ago
parent afdfadf365
commit b4419002a2
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22

@ -515,17 +515,16 @@ PROMPTS *libmodal-examples-prompt
Using a callback `function`: >
local libmodal = require('libmodal')
local cmd = vim.api.nvim_command
local commandList = {'new', 'close', 'last'}
function fooMode()
local userInput = vim.api.nvim_get_var 'fooModeInput'
local userInput = vim.g.fooModeInput
if userInput == 'new' then
cmd 'tabnew'
vim.api.nvim_command 'tabnew'
elseif userInput == 'close' then
cmd 'tabclose'
vim.api.nvim_command 'tabclose'
elseif userInput == 'last' then
cmd 'tablast'
vim.api.nvim_command 'tablast'
end
end
@ -642,7 +641,6 @@ then reset it upon exit. Example:
<
Lua: ~
>
local api = vim.api
local libmodal = require('libmodal')
function fooMode()

@ -1,11 +1,10 @@
-- Imports
local cmd = vim.api.nvim_command
local libmodal = require 'libmodal'
-- A function which will split the window both horizontally and vertically.
local function _split_twice()
cmd 'split'
cmd 'vsplit'
vim.api.nvim_command 'split'
vim.api.nvim_command 'vsplit'
end
-- Register key combos for splitting windows and then closing windows

@ -1,6 +1,5 @@
-- Imports
local libmodal = require 'libmodal'
local cmd = vim.api.nvim_command
-- The list of commands. Providing this will allow for autocomplete.
local commandList = {'new', 'close', 'last'}
@ -9,11 +8,11 @@ local commandList = {'new', 'close', 'last'}
function FooMode()
local userInput = vim.g.fooModeInput
if userInput == 'new' then
cmd 'tabnew'
vim.api.nvim_command 'tabnew'
elseif userInput == 'close' then
cmd 'tabclose'
vim.api.nvim_command 'tabclose'
elseif userInput == 'last' then
cmd 'tablast'
vim.api.nvim_command 'tablast'
end
end

@ -1,8 +1,4 @@
--[[
/*
* IMPORTS
*/
--]]
--[[/* IMPORTS */]]
local classes = require('libmodal/src/classes')
local globals = require('libmodal/src/globals')
@ -10,24 +6,17 @@ local ParseTable = require('libmodal/src/collections/ParseTable')
local utils = require('libmodal/src/utils')
local Vars = require('libmodal/src/Vars')
local api = vim.api
local go = vim.go
--[[
/*
* MODULE
*/
--]]
--[[/* MODULE */]]
local Mode = {TYPE = 'libmodal-mode'}
local _HELP = '?'
local _TIMEOUT = {
CHAR = 'ø',
LEN = go.timeoutlen,
SEND = function(self) api.nvim_feedkeys(self.CHAR, 'nt', false) end
LEN = vim.go.timeoutlen,
SEND = function(self) vim.api.nvim_feedkeys(self.CHAR, 'nt', false) end
}
_TIMEOUT.NR = string.byte(_TIMEOUT.CHAR)
_TIMEOUT.CHAR_NUMBER = string.byte(_TIMEOUT.CHAR)
--[[
/*
@ -60,7 +49,7 @@ classes = nil
-----------------------------------------------------------
function _metaMode._commandTableExecute(instruction)
if type(instruction) == globals.TYPE_FUNC then instruction()
else api.nvim_command(instruction) end
else vim.api.nvim_command(instruction) end
end
-----------------------------------------------
@ -209,7 +198,7 @@ function _metaMode:_inputLoop()
local userInput = utils.api.nvim_input()
-- Return if there was a timeout event.
if userInput == _TIMEOUT.NR then
if userInput == _TIMEOUT.CHAR_NUMBER then
return true
end

@ -4,14 +4,7 @@ local globals = require('libmodal/src/globals')
local utils = require('libmodal/src/utils')
local Vars = require('libmodal/src/Vars')
local api = vim.api
local fn = vim.fn
--[[
/*
* MODULE
*/
--]]
--[[/* MODULE */]]
local Prompt = {TYPE = 'libmodal-prompt'}
@ -51,7 +44,7 @@ function _metaPrompt:_executeInstruction(userInput)
if type(to_execute) == globals.TYPE_FUNC then
to_execute()
else
api.nvim_command(instruction[userInput])
vim.api.nvim_command(instruction[userInput])
end
elseif userInput == _HELP then -- The user did not define a 'help' command, so use the default.
self._help:show()
@ -83,13 +76,13 @@ function _metaPrompt:_inputLoop()
local userInput = ''
-- echo the highlighting
api.nvim_command('echohl ' .. self.indicator.hl)
vim.api.nvim_command('echohl ' .. self.indicator.hl)
-- set the user input variable
if self._completions then userInput =
fn['libmodal#_inputWith'](self.indicator.str, self._completions)
vim.fn['libmodal#_inputWith'](self.indicator.str, self._completions)
else userInput =
fn.input(self.indicator)
vim.fn.input(self.indicator)
end
-- determine what to do with the input

@ -1,27 +1,14 @@
--[[
/*
* IMPORTS
*/
--]]
local api = vim.api
local go = vim.go
--[[
/*
* MODULE
*/
--]]
--[[/* MODULE */]]
local Popup = require('libmodal/src/classes').new(
'libmodal-popup',
{config = {
anchor = 'SW',
col = go.columns - 1,
col = vim.go.columns - 1,
focusable = false,
height = 1,
relative = 'editor',
row = go.lines - go.cmdheight - 1,
row = vim.go.lines - vim.go.cmdheight - 1,
style = 'minimal',
width = 1
}}
@ -40,7 +27,7 @@ local Popup = require('libmodal/src/classes').new(
]]
----------------------------
local function valid(window)
return window and api.nvim_win_is_valid(window)
return window and vim.api.nvim_win_is_valid(window)
end
--[[
@ -62,7 +49,7 @@ local _metaPopup = require('libmodal/src/classes').new(Popup.TYPE)
-------------------------------------
function _metaPopup:close(keepBuffer)
if valid(self.window) then
api.nvim_win_close(self.window, false)
vim.api.nvim_win_close(self.window, false)
end
self.window = nil
@ -83,11 +70,11 @@ function _metaPopup:open(config)
if not config then config = Popup.config end
if valid(self.window) then
config = vim.tbl_extend('keep', config, api.nvim_win_get_config(self.window))
config = vim.tbl_extend('keep', config, vim.api.nvim_win_get_config(self.window))
self:close(true)
end
self.window = api.nvim_open_win(self.buffer, false, config)
self.window = vim.api.nvim_open_win(self.buffer, false, config)
end
---------------------------------------
@ -114,22 +101,18 @@ function _metaPopup:refresh(inputBytes)
self._inputChars = chars
end
api.nvim_buf_set_lines(self.buffer, 0, 1, true, {
vim.api.nvim_buf_set_lines(self.buffer, 0, 1, true, {
table.concat(self._inputChars)
})
if not valid(self.window) or api.nvim_win_get_tabpage(self.window) ~= api.nvim_get_current_tabpage() then
if not valid(self.window) or vim.api.nvim_win_get_tabpage(self.window) ~= vim.api.nvim_get_current_tabpage() then
self:open()
end
api.nvim_win_set_width(self.window, #self._inputChars)
vim.api.nvim_win_set_width(self.window, #self._inputChars)
end
--[[
/*
* CLASS `Popup`
*/
--]]
--[[/* CLASS `Popup` */]]
--------------------
--[[ SUMMARY:
@ -140,11 +123,9 @@ end
]]
--------------------
function Popup.new(config)
local buf = api.nvim_create_buf(false, true)
local self = setmetatable(
{
buffer = buf,
buffer = vim.api.nvim_create_buf(false, true),
_inputChars = {},
},
_metaPopup
@ -155,10 +136,6 @@ function Popup.new(config)
return self
end
--[[
/*
* PUBLICIZE `Popup`
*/
--]]
--[[/* PUBLICIZE `Popup` */]]
return Popup

@ -1,6 +1,5 @@
--[[/* IMPORTS */]]
local go = vim.go
local api = require('libmodal/src/utils/api')
--[[
@ -25,8 +24,8 @@ local _metaWindowState = require('libmodal/src/classes').new(WindowState.TYPE)
]]
-----------------------------------
function _metaWindowState:restore()
go.winheight = self.height
go.winwidth = self.width
vim.go.winheight = self.height
vim.go.winwidth = self.width
api.nvim_redraw()
end
@ -47,8 +46,8 @@ end
function WindowState.new()
return setmetatable(
{
height = go.winheight,
width = go.winwidth,
height = vim.go.winheight,
width = vim.go.winwidth,
},
_metaWindowState
)

@ -1,9 +1,7 @@
--[[/* IMPORTS */]]
local fn = vim.fn
local globals = require('libmodal/src/globals')
local HighlightSegment = require('libmodal/src/Indicator/HighlightSegment')
local vim_api = vim.api
--[[/* MODULE */]]
@ -19,17 +17,17 @@ function api.mode_exit(exit_char)
end
-- Exit the prompt by sending an escape key.
vim_api.nvim_feedkeys(exit_char, 'nt', false)
vim.api.nvim_feedkeys(exit_char, 'nt', false)
end
--- Make vim ring the visual/audio bell, if it is enabled.
function api.nvim_bell()
vim_api.nvim_command('normal '..string.char(27)) -- escape char
vim.api.nvim_command('normal '..string.char(27)) -- escape char
end
--- Gets one character of user input, as a number.
function api.nvim_input()
return fn.getchar()
return vim.fn.getchar()
end
--------------------------
@ -39,7 +37,7 @@ end
]]
--------------------------
function api.nvim_redraw()
vim_api.nvim_command 'mode'
vim.api.nvim_command 'mode'
end
---------------------------------
@ -65,9 +63,9 @@ function api.nvim_lecho(hlTables)
lecho_template[2] = tostring(hlTable.hl)
lecho_template[4] = tostring(hlTable.str)
vim_api.nvim_command(table.concat(lecho_template))
vim.api.nvim_command(table.concat(lecho_template))
end
vim_api.nvim_command 'echohl None'
vim.api.nvim_command 'echohl None'
end
--------------------------------------
@ -84,7 +82,7 @@ function api.nvim_show_err(title, msg)
HighlightSegment.new('Title', tostring(title)..'\n'),
HighlightSegment.new('Error', tostring(msg)),
})
fn.getchar()
vim.fn.getchar()
end
return api

@ -1,7 +1,3 @@
--[[/* IMPORTS */]]
local v = vim.v
--[[/* MODULE */]]
local utils = {}
@ -9,11 +5,7 @@ utils.api = require('libmodal/src/utils/api')
utils.Help = require('libmodal/src/utils/Help')
utils.WindowState = require('libmodal/src/utils/WindowState')
--[[
/*
* FUNCTIONS
*/
--]]
--[[/* FUNCTIONS */]]
--- Show an error from `pcall()`.
--- @param pcall_err string the error generated by `pcall()`.
@ -22,14 +14,10 @@ function utils.show_error(pcall_err)
utils.api.nvim_show_err(
require('libmodal/src/globals').DEFAULT_ERROR_TITLE,
v.throwpoint..'\n'..v.exception..'\n'..pcall_err
vim.v.throwpoint..'\n'..vim.v.exception..'\n'..pcall_err
)
end
--[[
/*
* PUBLICIZE MODULE
*/
--]]
--[[/* PUBLICIZE MODULE */]]
return utils

@ -1,9 +1,7 @@
local g = vim.g
if vim.g.loaded_libmodal then return end
vim.g.loaded_libmodal = true
if g.loaded_libmodal then return end
g.loaded_libmodal = true
g.libmodalTimeouts = g.libmodalTimeouts or vim.go.timeout
vim.g.libmodalTimeouts = vim.g.libmodalTimeouts or vim.go.timeout
-- The default highlight groups (for colors) are specified below.
-- Change these default colors by defining or linking the corresponding highlight group.

Loading…
Cancel
Save