Skip to content

Commit c73e2c8

Browse files
committed
Use nvim api for creating REPL instead of vim.cmd
1 parent 1fbc9a5 commit c73e2c8

1 file changed

Lines changed: 29 additions & 28 deletions

File tree

lua/pyrepl/core.lua

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@ local theme = require("pyrepl.theme")
1010
local group = vim.api.nvim_create_augroup("PyreplCore", { clear = true })
1111

1212
---Create window according to current config.
13+
---@param buf integer
1314
---@return integer
14-
local function open_scratch_win()
15+
local function open_scratch_win(buf)
1516
local split_horizontal = config.get_state().split_horizontal
1617
local split_ratio = config.get_state().split_ratio
1718

19+
local win_config = {
20+
win = -1,
21+
style = "minimal",
22+
}
23+
1824
if split_horizontal then
19-
local height = math.floor(vim.o.lines * split_ratio)
20-
vim.cmd("botright " .. height .. "split")
25+
win_config.height = math.floor(vim.o.lines * split_ratio)
26+
win_config.split = "bottom"
2127
else
22-
local width = math.floor(vim.o.columns * split_ratio)
23-
vim.cmd("botright " .. width .. "vsplit")
28+
win_config.width = math.floor(vim.o.columns * split_ratio)
29+
win_config.split = "right"
2430
end
2531

26-
vim.api.nvim_win_set_config(0, { style = "minimal" })
27-
return vim.api.nvim_get_current_win()
32+
return vim.api.nvim_open_win(buf, false, win_config)
2833
end
2934

3035
local function setup_buf_autocmds()
@@ -86,10 +91,7 @@ local function open_hidden_repl()
8691
return
8792
end
8893

89-
local win = vim.api.nvim_get_current_win()
90-
state.win = open_scratch_win()
91-
vim.api.nvim_win_set_buf(state.win, state.buf)
92-
vim.api.nvim_set_current_win(win)
94+
state.win = open_scratch_win(state.buf)
9395
setup_win_autocmds()
9496
M.scroll_repl()
9597
end
@@ -109,12 +111,9 @@ local function open_new_repl(kernel)
109111
local style_treesitter = config.get_state().style_treesitter
110112

111113
local buf = vim.api.nvim_create_buf(false, true)
114+
local win = open_scratch_win(buf)
112115
vim.bo[buf].bufhidden = "hide"
113116

114-
local current_win = vim.api.nvim_get_current_win()
115-
local win = open_scratch_win()
116-
vim.api.nvim_win_set_buf(win, buf)
117-
118117
local cmd = {
119118
python_path,
120119
console_path,
@@ -134,18 +133,21 @@ local function open_new_repl(kernel)
134133
end
135134
end
136135

137-
local chan = vim.fn.jobstart(cmd, {
138-
term = true,
139-
pty = true,
140-
env = vim.tbl_extend(
141-
"force",
142-
vim.env,
143-
{ NVIM = nvim_socket, PYDEVD_DISABLE_FILE_VALIDATION = 1 }
144-
),
145-
on_exit = function()
146-
vim.defer_fn(M.close_repl, 200)
147-
end,
148-
})
136+
local chan = 0
137+
vim.api.nvim_buf_call(buf, function()
138+
chan = vim.fn.jobstart(cmd, {
139+
term = true,
140+
pty = true,
141+
env = vim.tbl_extend(
142+
"force",
143+
vim.env,
144+
{ NVIM = nvim_socket, PYDEVD_DISABLE_FILE_VALIDATION = 1 }
145+
),
146+
on_exit = function()
147+
vim.defer_fn(M.close_repl, 200)
148+
end,
149+
})
150+
end)
149151

150152
if chan == 0 or chan == -1 then
151153
error(config.get_message_prefix() .. "failed to start REPL, try `:PyreplInstall`", 0)
@@ -162,7 +164,6 @@ local function open_new_repl(kernel)
162164
setup_buf_autocmds()
163165
setup_win_autocmds()
164166
vim.api.nvim_buf_set_name(buf, string.format("kernel: %s", kernel))
165-
vim.api.nvim_set_current_win(current_win)
166167
M.scroll_repl()
167168
end
168169

0 commit comments

Comments
 (0)