dotfiles/symlinks/config/nvim/lua/esensar/vim_rest_console_extensions...

69 lines
1.9 KiB
Lua
Raw Normal View History

-------------------------------------------------------------------------------
-- - Vim REST Console extensions library -
-------------------------------------------------------------------------------
local function get_vim_rest_home_dir()
2022-04-28 13:53:54 +00:00
vim.fn.mkdir(vim.env.NVIMHOME .. "/vim-rest-console", "p")
return vim.env.NVIMHOME .. "/vim-rest-console"
end
-- Opens up a new tab if current buffer is not empty
local function new_tab_if_needed()
2022-04-29 07:46:45 +00:00
if vim.api.nvim_buf_get_name(0) ~= "" then
2022-04-28 13:53:54 +00:00
-- Current buffer is not empty, open up a new tab
vim.cmd("tabnew")
end
end
local function open_rest_console(file)
2022-04-28 13:53:54 +00:00
new_tab_if_needed()
local ending = ".rest"
if file:sub(-#ending) ~= ending then
file = file .. ".rest"
end
vim.cmd("e " .. file)
end
local function open_cached_rest_console(name)
2022-04-28 13:53:54 +00:00
local dir = get_vim_rest_home_dir()
open_rest_console(dir .. "/" .. name)
end
-------------------------------------------------------------------------------
-- - Public API -
-------------------------------------------------------------------------------
local M = {}
-- Opens us a scratch rest console (not saved)
function M.open_scratch_rest_console()
2022-04-28 13:53:54 +00:00
new_tab_if_needed()
vim.cmd("set ft=rest")
end
-- Opens up a rest console which can be saved -- cached by name
2022-04-29 07:46:45 +00:00
function M.open_cached_rest_console(args)
local name = args[0] or args[1]
if not name then
name = require("esensar.common.projects").get_project_id()
2022-04-28 13:53:54 +00:00
end
open_cached_rest_console(name)
end
-- Opens up a rest console which can be saved -- cached by name
function M.open_named_cached_rest_console(name)
name = require("esensar.common.projects").get_project_id() .. name
2022-04-28 13:53:54 +00:00
open_cached_rest_console(name)
end
-- Opens up a rest console based on local file path
2022-04-29 07:46:45 +00:00
function M.open_local_rest_console(args)
local file = args[0] or args[1]
if not file then
2022-04-28 13:53:54 +00:00
file = "default"
end
open_rest_console(file)
end
return M