aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzbirenbaum <zacharyobirenbaum@gmail.com>2022-04-28 03:34:39 -0400
committerzbirenbaum <zacharyobirenbaum@gmail.com>2022-04-28 03:34:39 -0400
commit137aeab479f9ccba565f7ad3a68fa3fdf31dd038 (patch)
tree95fa9025dece6b2b30fd9e096f43d9f3b4df8cab
parentformat | add stylua.toml (diff)
fil nil error caused by jit, change stylua to two spaces
-rw-r--r--.stylua.toml4
-rw-r--r--lua/nvterm/init.lua157
-rw-r--r--lua/nvterm/terminal.lua128
-rw-r--r--lua/nvterm/termutil.lua68
4 files changed, 179 insertions, 178 deletions
diff --git a/.stylua.toml b/.stylua.toml
index e9bf648..0fd4cb5 100644
--- a/.stylua.toml
+++ b/.stylua.toml
@@ -1,6 +1,6 @@
column_width = 120
line_endings = "Unix"
indent_type = "Spaces"
-indent_width = 3
+indent_width = 2
quote_style = "AutoPreferDouble"
-no_call_parentheses = true
+no_call_parentheses = false
diff --git a/lua/nvterm/init.lua b/lua/nvterm/init.lua
index 726d0d8..c354e6b 100644
--- a/lua/nvterm/init.lua
+++ b/lua/nvterm/init.lua
@@ -1,106 +1,107 @@
local M = {}
local defaults = {
- terminals = {
- list = {},
- type_opts = {
- float = {
- relative = "editor",
- row = 0.3,
- col = 0.25,
- width = 0.5,
- height = 0.4,
- border = "single",
- },
- horizontal = { location = "rightbelow", split_ratio = 0.3 },
- vertical = { location = "rightbelow", split_ratio = 0.5 },
+ terminals = {
+ list = {},
+ type_opts = {
+ float = {
+ relative = "editor",
+ row = 0.3,
+ col = 0.25,
+ width = 0.5,
+ height = 0.4,
+ border = "single",
},
- },
- behavior = {
- close_on_exit = true,
- auto_insert = true,
- },
- mappings = {
- toggle = {
- float = "<A-i>",
- horizontal = "<A-h>",
- vertical = "<A-v>",
- },
- new = {
- horizontal = "<leader>h",
- vertical = "<leader>v",
- },
- },
- enable_new_mappings = false,
+ horizontal = { location = "rightbelow", split_ratio = 0.3 },
+ vertical = { location = "rightbelow", split_ratio = 0.5 },
+ },
+ },
+ behavior = {
+ close_on_exit = true,
+ auto_insert = true,
+ },
+ mappings = {
+ toggle = {
+ float = "<A-i>",
+ horizontal = "<A-h>",
+ vertical = "<A-v>",
+ },
+ new = {
+ horizontal = "<leader>h",
+ vertical = "<leader>v",
+ },
+ },
+ enable_new_mappings = false,
}
local set_behavior = function(behavior)
- if behavior.close_on_exit then
- vim.api.nvim_create_autocmd({ "TermClose" }, {
- callback = function()
- vim.schedule_wrap(vim.api.nvim_input "<CR>")
- end,
- })
- end
+ if behavior.close_on_exit then
+ vim.api.nvim_create_autocmd({ "TermClose" }, {
+ callback = function()
+ vim.schedule_wrap(vim.api.nvim_input("<CR>"))
+ end,
+ })
+ end
- if behavior.auto_insert then
- vim.api.nvim_create_autocmd({ "BufEnter" }, {
- callback = function()
- vim.cmd "startinsert"
- end,
- pattern = "term://*",
- })
+ if behavior.auto_insert then
+ vim.api.nvim_create_autocmd({ "BufEnter" }, {
+ callback = function()
+ vim.cmd("startinsert")
+ end,
+ pattern = "term://*",
+ })
- vim.api.nvim_create_autocmd({ "BufLeave" }, {
- callback = function()
- vim.cmd "stopinsert"
- end,
- pattern = "term://*",
- })
- end
+ vim.api.nvim_create_autocmd({ "BufLeave" }, {
+ callback = function()
+ vim.cmd("stopinsert")
+ end,
+ pattern = "term://*",
+ })
+ end
end
M.create_mappings = function(method, map_table, opts)
- opts = opts or {}
- vim.tbl_deep_extend("force", { noremap = true, silent = true }, opts)
-
- for type, mapping in pairs(map_table) do
- vim.keymap.set({ "n", "t" }, mapping, function()
- require("nvterm.terminal")[method](type)
- end, opts)
- end
+ opts = opts or {}
+ vim.tbl_deep_extend("force", { noremap = true, silent = true }, opts)
+ for type, mapping in pairs(map_table) do
+ vim.keymap.set({ "n", "t" }, mapping, function()
+ require("nvterm.terminal")[method](type)
+ end, opts)
+ end
end
local setup_mappings = function(mappings)
- for method, map_table in pairs(mappings) do
- M.create_mappings(method, map_table)
- end
+ for method, map_table in pairs(mappings) do
+ M.create_mappings(method, map_table)
+ end
end
local map_config_shortcuts = function(config)
- local shortcuts = {
- { config.terminals.type_opts, { "horizontal", "vertical", "float" } },
- { config.mappings, { "toggle", "new" } },
- }
+ local shortcuts = {
+ { config.terminals.type_opts, { "horizontal", "vertical", "float" } },
+ { config.mappings, { "toggle", "new" } },
+ }
- for _, shortcut_map in ipairs(shortcuts) do
- for _, key in ipairs(shortcut_map[2]) do
- shortcut_map[1][key] = vim.tbl_deep_extend("force", shortcut_map[1][key], config[key] or {})
- end
- end
+ for _, shortcut_map in ipairs(shortcuts) do
+ for _, key in ipairs(shortcut_map[2]) do
+ config[key] = config[key] or {}
+ shortcut_map[1][key] = shortcut_map[1][key] or {}
+ shortcut_map[1][key] = vim.tbl_deep_extend("force", shortcut_map[1][key], config[key] or {})
+ end
+ end
- return config
+ return config
end
M.setup = function(config)
- config = config and vim.tbl_deep_extend("force", defaults, config) or defaults
- set_behavior(config.behavior)
+ config = config and vim.tbl_deep_extend("force", defaults, config) or defaults
+ set_behavior(config.behavior)
- config = map_config_shortcuts(config)
- config.mappings.new = config.enable_new_mappings and config.mappings.new or nil
+ config = map_config_shortcuts(config)
+ config.mappings.new = config.enable_new_mappings and config.mappings.new or nil
- setup_mappings(config.mappings)
- require("nvterm.terminal").init(config.terminals)
+ setup_mappings(config.mappings)
+ require("nvterm.terminal").init(config.terminals)
end
return M
diff --git a/lua/nvterm/terminal.lua b/lua/nvterm/terminal.lua
index 1f93e85..ff73119 100644
--- a/lua/nvterm/terminal.lua
+++ b/lua/nvterm/terminal.lua
@@ -1,133 +1,133 @@
-local util = require "nvterm.termutil"
+local util = require("nvterm.termutil")
local a = vim.api
local nvterm = {}
local terminals = {}
local function get_last(list)
- if list then
- return not vim.tbl_isempty(list) and list[#list] or nil
- end
- return terminals[#terminals] or nil
+ if list then
+ return not vim.tbl_isempty(list) and list[#list] or nil
+ end
+ return terminals[#terminals] or nil
end
local function get_type(type)
- return vim.tbl_filter(function(t)
- return t.type == type
- end, terminals.list)
+ return vim.tbl_filter(function(t)
+ return t.type == type
+ end, terminals.list)
end
local function get_still_open()
- return vim.tbl_filter(function(t)
- return t.open == true
- end, terminals.list)
+ return vim.tbl_filter(function(t)
+ return t.open == true
+ end, terminals.list)
end
local function get_last_still_open()
- return get_last(get_still_open())
+ return get_last(get_still_open())
end
local function get_type_last(type)
- return get_last(get_type(type))
+ return get_last(get_type(type))
end
local create_term_window = function(type)
- util.execute_type_cmd(type, terminals)
+ util.execute_type_cmd(type, terminals)
- vim.wo.relativenumber = false
- vim.wo.number = false
+ vim.wo.relativenumber = false
+ vim.wo.number = false
- return a.nvim_get_current_win()
+ return a.nvim_get_current_win()
end
local create_term = function(type)
- local win = create_term_window(type)
- local buf = a.nvim_create_buf(false, true)
+ local win = create_term_window(type)
+ local buf = a.nvim_create_buf(false, true)
- a.nvim_buf_set_option(buf, "filetype", "terminal")
- a.nvim_buf_set_option(buf, "buflisted", false)
- a.nvim_win_set_buf(win, buf)
+ a.nvim_buf_set_option(buf, "filetype", "terminal")
+ a.nvim_buf_set_option(buf, "buflisted", false)
+ a.nvim_win_set_buf(win, buf)
- local job_id = vim.fn.termopen(vim.o.shell)
- local term = { win = win, buf = buf, open = true, type = type, job_id = job_id }
+ local job_id = vim.fn.termopen(vim.o.shell)
+ local term = { win = win, buf = buf, open = true, type = type, job_id = job_id }
- table.insert(terminals.list, term)
- vim.cmd "startinsert"
+ table.insert(terminals.list, term)
+ vim.cmd("startinsert")
- return term
+ return term
end
local ensure_and_send = function(cmd, type)
- terminals = util.verify_terminals(terminals)
- local term = type and get_type_last(type) or get_last_still_open() or nvterm.new "vertical"
+ terminals = util.verify_terminals(terminals)
+ local term = type and get_type_last(type) or get_last_still_open() or nvterm.new("vertical")
- if not term then
- term = nvterm.new "horizontal"
- end
+ if not term then
+ term = nvterm.new("horizontal")
+ end
- a.nvim_chan_send(term.job_id, cmd .. "\n")
+ a.nvim_chan_send(term.job_id, cmd .. "\n")
end
local call_and_restore = function(fn, opts)
- local current_win = a.nvim_get_current_win()
- local mode = a.nvim_get_mode().mode == "i" and "startinsert" or "stopinsert"
+ local current_win = a.nvim_get_current_win()
+ local mode = a.nvim_get_mode().mode == "i" and "startinsert" or "stopinsert"
- fn(unpack(opts))
- a.nvim_set_current_win(current_win)
+ fn(unpack(opts))
+ a.nvim_set_current_win(current_win)
- vim.cmd(mode)
+ vim.cmd(mode)
end
nvterm.send = function(cmd, type)
- if not cmd then
- return
- end
+ if not cmd then
+ return
+ end
- call_and_restore(ensure_and_send, { cmd, type })
+ call_and_restore(ensure_and_send, { cmd, type })
end
nvterm.hide_term = function(term)
- term.open = false
- a.nvim_win_close(term.win, false)
+ term.open = false
+ a.nvim_win_close(term.win, false)
end
nvterm.show_term = function(term)
- term.open = true
- term.win = create_term_window(term.type)
- a.nvim_win_set_buf(term.win, term.buf)
+ term.open = true
+ term.win = create_term_window(term.type)
+ a.nvim_win_set_buf(term.win, term.buf)
- vim.cmd "startinsert"
+ vim.cmd("startinsert")
end
nvterm.hide = function(type)
- local term = type and get_type_last(type) or get_last()
- nvterm.hide_term(term)
+ local term = type and get_type_last(type) or get_last()
+ nvterm.hide_term(term)
end
nvterm.show = function(type)
- local term = type and get_type_last(type) or terminals.last
- nvterm.show_term(term)
+ local term = type and get_type_last(type) or terminals.last
+ nvterm.show_term(term)
end
nvterm.new = function(type)
- local term = create_term(type)
- return term
+ local term = create_term(type)
+ return term
end
nvterm.toggle = function(type)
- terminals = util.verify_terminals(terminals)
- local term = get_type_last(type)
+ terminals = util.verify_terminals(terminals)
+ local term = get_type_last(type)
- if not term then
- term = nvterm.new(type)
- elseif term.open then
- nvterm.hide_term(term)
- else
- nvterm.show_term(term)
- end
+ if not term then
+ term = nvterm.new(type)
+ elseif term.open then
+ nvterm.hide_term(term)
+ else
+ nvterm.show_term(term)
+ end
end
nvterm.init = function(term_config)
- terminals = term_config
+ terminals = term_config
end
return nvterm
diff --git a/lua/nvterm/termutil.lua b/lua/nvterm/termutil.lua
index c6d8614..b104075 100644
--- a/lua/nvterm/termutil.lua
+++ b/lua/nvterm/termutil.lua
@@ -2,59 +2,59 @@ local util = {}
local a = vim.api
util.calc_float_opts = function(opts)
- return {
- relative = "editor",
+ return {
+ relative = "editor",
- width = math.ceil(opts.width * vim.o.columns),
- height = math.ceil(opts.height * vim.o.lines),
+ width = math.ceil(opts.width * vim.o.columns),
+ height = math.ceil(opts.height * vim.o.lines),
- row = math.floor(opts.row * vim.o.lines),
- col = math.floor(opts.col * vim.o.columns),
+ row = math.floor(opts.row * vim.o.lines),
+ col = math.floor(opts.col * vim.o.columns),
- border = opts.border,
- }
+ border = opts.border,
+ }
end
util.get_split_dims = function(type, ratio)
- local type_switch = type == "horizontal"
- local type_func = type_switch and a.nvim_win_get_height or a.nvim_win_get_width
+ local type_switch = type == "horizontal"
+ local type_func = type_switch and a.nvim_win_get_height or a.nvim_win_get_width
- return math.floor(type_func(0) * ratio)
+ return math.floor(type_func(0) * ratio)
end
util.execute_type_cmd = function(type, terminals)
- local opts = terminals.type_opts[type]
+ local opts = terminals.type_opts[type]
- local type_cmds = {
- horizontal = function()
- local dims = util.get_split_dims(type, opts.split_ratio)
- vim.cmd(opts.location .. dims .. " split")
- end,
+ local type_cmds = {
+ horizontal = function()
+ local dims = util.get_split_dims(type, opts.split_ratio)
+ vim.cmd(opts.location .. dims .. " split")
+ end,
- vertical = function()
- local dims = util.get_split_dims(type, opts.split_ratio)
- vim.cmd(opts.location .. dims .. " vsplit")
- end,
+ vertical = function()
+ local dims = util.get_split_dims(type, opts.split_ratio)
+ vim.cmd(opts.location .. dims .. " vsplit")
+ end,
- float = function()
- a.nvim_open_win(0, true, util.calc_float_opts(opts))
- end,
- }
+ float = function()
+ a.nvim_open_win(0, true, util.calc_float_opts(opts))
+ end,
+ }
- type_cmds[type]()
+ type_cmds[type]()
end
util.verify_terminals = function(terminals)
- terminals.list = vim.tbl_filter(function(term)
- return vim.api.nvim_buf_is_valid(term.buf)
- end, terminals.list)
+ terminals.list = vim.tbl_filter(function(term)
+ return vim.api.nvim_buf_is_valid(term.buf)
+ end, terminals.list)
- terminals.list = vim.tbl_map(function(term)
- term.open = vim.api.nvim_win_is_valid(term.win)
- return term
- end, terminals.list)
+ terminals.list = vim.tbl_map(function(term)
+ term.open = vim.api.nvim_win_is_valid(term.win)
+ return term
+ end, terminals.list)
- return terminals
+ return terminals
end
return util