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