-
Notifications
You must be signed in to change notification settings - Fork 35
/
document.lua
209 lines (182 loc) · 6.87 KB
/
document.lua
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
---@diagnostic disable: duplicate-doc-field
local utils = require("image/utils")
local resolve_absolute_path = function(document_file_path, image_path)
if string.sub(image_path, 1, 1) == "/" then return image_path end
if string.sub(image_path, 1, 1) == "~" then return vim.fn.fnamemodify(image_path, ":p") end
local document_dir = vim.fn.fnamemodify(document_file_path, ":h")
local absolute_image_path = document_dir .. "/" .. image_path
absolute_image_path = vim.fn.fnamemodify(absolute_image_path, ":p")
return absolute_image_path
end
local is_remote_url = function(url)
return string.sub(url, 1, 7) == "http://" or string.sub(url, 1, 8) == "https://"
end
---@param ctx IntegrationContext
---@param filetype string
---@return boolean
local has_valid_filetype = function(ctx, filetype)
return vim.tbl_contains(ctx.options.filetypes or {}, filetype)
end
---@class DocumentIntegrationConfig
---@field name string
---@field query_buffer_images fun(buffer: number): { node: any, range: { start_row: number, start_col: number, end_row: number, end_col: number }, url: string }[]
---@field default_options? DocumentIntegrationOptions
---@field debug? boolean
---@param config DocumentIntegrationConfig
local create_document_integration = function(config)
local trace = function(...)
if config.debug then utils.log("[" .. config.name .. "]", ...) end
end
local render = vim.schedule_wrap(
---@param ctx IntegrationContext
function(ctx)
local windows = utils.window.get_windows({
normal = true,
with_masks = ctx.state.options.window_overlap_clear_enabled,
ignore_masking_filetypes = ctx.state.options.window_overlap_clear_ft_ignore,
})
local image_queue = {}
for _, window in ipairs(windows) do
if has_valid_filetype(ctx, window.buffer_filetype) then
local matches = config.query_buffer_images(window.buffer)
local previous_images = ctx.api.get_images({
window = window.id,
buffer = window.buffer,
})
local new_image_ids = {}
local file_path = vim.api.nvim_buf_get_name(window.buffer)
local cursor_row = vim.api.nvim_win_get_cursor(window.id)[1] - 1 -- 0-indexed row
for _, match in ipairs(matches) do
local id = string.format("%d:%d:%d:%s", window.id, window.buffer, match.range.start_row, match.url)
if ctx.options.only_render_image_at_cursor and match.range.start_row ~= cursor_row then goto continue end
local to_render = {
id = id,
match = match,
window = window,
file_path = file_path,
}
table.insert(image_queue, to_render)
table.insert(new_image_ids, id)
::continue::
end
-- clear old images
for _, image in ipairs(previous_images) do
if not vim.tbl_contains(new_image_ids, image.id) then image:clear() end
end
end
end
-- render images from queue
for _, item in ipairs(image_queue) do
local render_image = function(image)
image:render({
x = item.match.range.start_col,
y = item.match.range.start_row + 1,
})
end
if is_remote_url(item.match.url) then
if ctx.options.download_remote_images then
ctx.api.from_url(
item.match.url,
{ id = item.id, window = item.window.id, buffer = item.window.buffer, with_virtual_padding = true },
function(image)
if not image then return end
render_image(image)
end
)
end
else
local path = resolve_absolute_path(item.file_path, item.match.url)
local ok, image = pcall(ctx.api.from_file, path, {
id = item.id,
window = item.window.id,
buffer = item.window.buffer,
with_virtual_padding = true,
})
if ok then render_image(image) end
end
end
end
)
local text_change_watched_buffers = {}
local setup_text_change_watcher = function(ctx, buffer)
if vim.tbl_contains(text_change_watched_buffers, buffer) then return end
vim.api.nvim_buf_attach(buffer, false, {
on_lines = function()
vim.schedule(function()
render(ctx)
end)
end,
})
table.insert(text_change_watched_buffers, buffer)
end
---@type fun(ctx: IntegrationContext)
local setup_autocommands = function(ctx)
local group_name = ("image.nvim:%s"):format(config.name)
local group = vim.api.nvim_create_augroup(group_name, { clear = true })
-- watch for window changes
vim.api.nvim_create_autocmd({ "WinNew", "BufWinEnter" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
render(ctx)
end,
})
-- watch for text changes
vim.api.nvim_create_autocmd({ "BufAdd", "BufNew", "BufNewFile" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
setup_text_change_watcher(ctx, args.buf)
render(ctx)
end,
})
if has_valid_filetype(ctx, vim.bo.filetype) then setup_text_change_watcher(ctx, vim.api.nvim_get_current_buf()) end
if ctx.options.only_render_image_at_cursor then
vim.api.nvim_create_autocmd({ "CursorMoved" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
render(ctx)
end,
})
end
if ctx.options.clear_in_insert_mode then
vim.api.nvim_create_autocmd({ "InsertEnter" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
local current_window = vim.api.nvim_get_current_win()
local images = ctx.api.get_images({ window = current_window })
for _, image in ipairs(images) do
image:clear()
end
end,
})
vim.api.nvim_create_autocmd({ "InsertLeave" }, {
group = group,
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
render(ctx)
end,
})
end
end
---@type fun(api: API, options: IntegrationOptions, state: State)
local setup = function(api, options, state)
---@diagnostic disable-next-line: missing-fields
local opts = vim.tbl_deep_extend("force", config.default_options or {}, options or {})
local context = {
api = api,
options = opts,
state = state,
}
vim.schedule(function()
setup_autocommands(context)
render(context)
end)
end
return { setup = setup }
end
return {
create_document_integration = create_document_integration,
}