-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.lua
More file actions
102 lines (85 loc) · 2.26 KB
/
Copy pathconfig.lua
File metadata and controls
102 lines (85 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
local M = {}
---@type pyrepl.Config
local defaults = {
split_horizontal = false,
split_ratio = 0.5,
style = "default",
style_integration = true,
image_max_history = 10,
image_width_ratio = 0.5,
image_height_ratio = 0.5,
image_provider = "placeholders",
cell_pattern = "^# %%%%.*$",
python_path = "python",
preferred_kernel = "python3",
jupytext_hook = true,
auto_install = false,
}
local image_provider_cache
local message_prefix = "[pyrepl] "
---@type pyrepl.Config
local state = vim.deepcopy(defaults)
---@param num any
---@param min number
---@param max number
---@param fallback number
---@return number
local function clip_number(num, min, max, fallback)
num = tonumber(num)
if not num then
return fallback
end
if num < min then
return min
end
if num > max then
return max
end
return num
end
---@return pyrepl.Image
function M.get_image_provider()
if not image_provider_cache then
local ok, provider = pcall(require, "pyrepl.providers." .. state.image_provider)
if ok then
image_provider_cache = provider
else
image_provider_cache = require("pyrepl.providers." .. defaults.image_provider)
end
end
return image_provider_cache
end
---@return string
function M.get_message_prefix()
return message_prefix
end
---@return pyrepl.Config
function M.get_state()
return state
end
---Get the effective cell pattern for the current buffer.
---@return string
function M.get_cell_pattern()
local pattern = state.cell_pattern
if type(pattern) == "function" then
return pattern()
end
return pattern
end
---@param opts? pyrepl.ConfigOpts
function M.update_state(opts)
state = vim.tbl_deep_extend("force", state, opts or {})
local to_clip = {
{ "split_ratio", 0.1, 0.9 },
{ "image_width_ratio", 0.1, 0.9 },
{ "image_height_ratio", 0.1, 0.9 },
{ "image_max_history", 2, 100 },
}
for _, args in ipairs(to_clip) do
local key, min, max = args[1], args[2], args[3]
state[key] = clip_number(state[key], min, max, defaults[key] --[[@as number]])
end
-- reload image provider after config update
image_provider_cache = nil
end
return M