Add basic extensions for vim-rest-console

pull/2/head
Ensar Sarajčić 2021-10-04 14:22:54 +02:00
parent 797cce9ef2
commit f56683ae0f
2 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1,54 @@
-------------------------------------------------------------------------------
-- - Vim REST Console extensions library -
-------------------------------------------------------------------------------
local function get_vim_rest_home_dir()
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()
if vim.api.nvim_buf_get_name('%') ~= '' then
-- Current buffer is not empty, open up a new tab
vim.cmd('tabnew')
end
end
local function open_rest_console(file)
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)
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()
new_tab_if_needed()
vim.cmd('set ft=rest')
end
-- Opens up a rest console which can be saved -- cached by name
function M.open_cached_rest_console(name)
open_cached_rest_console(name)
end
-- Opens up a rest console based on local file path
function M.open_rest_console(file)
open_rest_console(file)
end
return M

View File

@ -2,4 +2,7 @@
-- - Vim REST Console setup and extra commands -
-------------------------------------------------------------------------------
vim.cmd[[command! -nargs=0 RestConsole :tabnew <bar> :set ft=rest]]
vim.cmd[[command! -nargs=0 ScratchRestConsole :lua require('vim_rest_console_extensions').open_scratch_rest_console()]]
vim.cmd[[command! -nargs=1 RestConsole :lua require('vim_rest_console_extensions').open_cached_rest_console(<f-args>)]]
vim.cmd[[command! -nargs=1 RestConsoleLocal :lua require('vim_rest_console_extensions').open_local_rest_console(<f-args>)]]
vim.cmd[[command! -nargs=1 RestConsoleCached :lua require('vim_rest_console_extensions').open_cached_rest_console(<f-args>)]]