Skip to content

Commit 0fbef12

Browse files
feat(commands): :Rest open
1 parent 47beb74 commit 0fbef12

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

lua/rest-nvim/commands.lua

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,28 @@ local function ui() return require("rest-nvim.ui.result") end
4646
local function config() return require("rest-nvim.config") end
4747
-- stylua: ignore end
4848

49+
---Open window based on command mods and return new window identifier
50+
---@param opts table
51+
---@return integer winnr
52+
local function split_open_cmd(opts)
53+
local is_split = opts.smods.vertical or opts.smods.horizontal
54+
local is_tab = opts.smods.tab ~= -1
55+
if is_split or is_tab then
56+
vim.cmd(opts.mods .. " split")
57+
end
58+
return vim.api.nvim_get_current_win()
59+
end
60+
4961
---@type table<string, RestCmd>
5062
local rest_command_tbl = {
63+
open = {
64+
impl = function (_, opts)
65+
local winnr = split_open_cmd(opts)
66+
ui().enter(winnr)
67+
end
68+
},
5169
run = {
52-
impl = function(args, _opts)
70+
impl = function(args, _)
5371
if vim.bo.filetype ~= "http" or vim.b.__rest_no_http_file then
5472
vim.notify("`:Rest run` can be only called from http file", vim.log.levels.ERROR)
5573
return
@@ -62,8 +80,11 @@ local rest_command_tbl = {
6280
return
6381
end
6482
ui().clear()
65-
-- TODO: open result window here (use `:horizontal`)
66-
ui().open_ui()
83+
if not ui().is_open() then
84+
vim.cmd.wincmd("v")
85+
ui().enter(0)
86+
vim.cmd.wincmd("p")
87+
end
6788
request().run()
6889
end,
6990
---@return string[]
@@ -85,28 +106,19 @@ local rest_command_tbl = {
85106
},
86107
logs = {
87108
impl = function(_, opts)
88-
local is_split = opts.smods.vertiacal or opts.smods.horizontal
89-
local is_tab = opts.smods.tab ~= -1
90-
if is_split or is_tab then
91-
---@diagnostic disable-next-line: invisible
92-
vim.cmd(opts.mods .. " split " .. logger().get_logfile())
93-
else
109+
local winnr = split_open_cmd(opts)
110+
vim.api.nvim_win_call(winnr, function ()
94111
---@diagnostic disable-next-line: invisible
95112
vim.cmd.edit(logger().get_logfile())
96-
end
113+
end)
97114
end,
98115
},
99116
cookies = {
100117
impl = function(_, opts)
101-
local is_split = opts.smods.vertiacal or opts.smods.horizontal
102-
local is_tab = opts.smods.tab ~= -1
103-
if is_split or is_tab then
104-
---@diagnostic disable-next-line: invisible
105-
vim.cmd(opts.mods .. " split " .. config().cookies.path)
106-
else
107-
---@diagnostic disable-next-line: invisible
118+
local winnr = split_open_cmd(opts)
119+
vim.api.nvim_win_call(winnr, function ()
108120
vim.cmd.edit(config().cookies.path)
109-
end
121+
end)
110122
end,
111123
},
112124
env = {

lua/rest-nvim/ui/result.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,18 @@ vim.api.nvim_set_hl(0, "RestPaneTitle", {
194194
underline = true,
195195
})
196196

197-
---@param horizontal? boolean
198-
function ui.open_ui(horizontal)
197+
---@return boolean
198+
function ui.is_open()
199199
local winnr = vim.iter(vim.api.nvim_tabpage_list_wins(0)):find(function(id)
200200
local buf = vim.api.nvim_win_get_buf(id)
201201
return vim.b[buf].__pane_group == group.name
202202
end)
203-
if not winnr then
204-
vim.cmd.wincmd(horizontal and "s" or "v")
205-
group:enter(0)
206-
vim.cmd.wincmd("p")
207-
end
203+
return winnr ~= nil
204+
end
205+
206+
---@param winnr integer
207+
function ui.enter(winnr)
208+
group:enter(winnr)
208209
end
209210

210211
function ui.clear()

0 commit comments

Comments
 (0)