Skip to content

Commit 4645c18

Browse files
fix: handle more lazy loading issues
## Details Related issues: - #309 - #315 - #317 Okay this is getting convoluted to handle the differences in lazy loading between `lazy.nvim` and `vim-plug`. The issues are as follows: - `lazy.nvim` & Lazy Loaded: Attempting to attach to current buffer at the start will not pickup user configuration (called from plugin directory before setup) so may not listen to all needed events. This means we should not attach to current buffer and rely on the `FileType` auto command. - `vim-plug` & Lazy Loaded: The initial load swallows the `FileType` event for the current buffer (`lazy.nvim` seems to trigger the event) so first buffer will be ignored. This means we cannot rely on the `FileType` auto command and need to attach to the current buffer. To fix this we need to be aware of the plugin manager being used so we can do the right thing, which sucks, but oh well. The solution expands on our existing logic to hook into `lazy.nvim` configuration for this plugin. When the user has NOT configured lazy loading (via filetypes nor commands) we attempt to attach to the current buffer. This means if they have we will skip attaching and will rely on the `FileType` event. This solves the `lazy.nvim` problem of not using the updated user configuration. This does mean when not lazy loading with `lazy.nvim` we will attempt to attach to the current buffer. This will initially fail since the filetype will not be set for the buffer but will succeed for the buffer later once the `FileType` event is triggered. For `vim-plug` we don't have any hooks so no matter what it will look like we are NOT lazy loading. This means we will attempt to attach to the current buffer fixing the issue of lazy loading with `vim-plug`. Have also added a check that buffer is valid in the `should_attach` function. The issue related to this should be fixed from the other updates but having it in place seems like a good idea in either case.
1 parent b9c98ff commit 4645c18

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

Diff for: doc/render-markdown.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2025 January 31
1+
*render-markdown.txt* For 0.10.0 Last change: 2025 February 01
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

Diff for: lua/render-markdown/core/util.lua

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ local M = {}
33

44
M.has_10 = vim.fn.has('nvim-0.10') == 1
55

6-
---@param name string
6+
---@param key 'ft'|'cmd'
77
---@return string[]
8-
function M.lazy_file_types(name)
8+
function M.lazy(key)
99
-- https://github.com/folke/lazydev.nvim/blob/main/lua/lazydev/pkg.lua -> get_plugin_path
1010
if type(package.loaded.lazy) ~= 'table' then
1111
return {}
@@ -14,15 +14,16 @@ function M.lazy_file_types(name)
1414
if not ok then
1515
return {}
1616
end
17+
local name = 'render-markdown.nvim'
1718
local plugin = lazy_config.spec.plugins[name]
1819
if plugin == nil then
1920
return {}
2021
end
21-
local file_types = plugin.ft
22-
if type(file_types) == 'table' then
23-
return file_types
24-
elseif type(file_types) == 'string' then
25-
return { file_types }
22+
local values = plugin[key]
23+
if type(values) == 'table' then
24+
return values
25+
elseif type(values) == 'string' then
26+
return { values }
2627
else
2728
return {}
2829
end

Diff for: lua/render-markdown/health.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '7.8.13'
8+
M.version = '7.8.14'
99

1010
function M.check()
1111
M.start('version')

Diff for: lua/render-markdown/manager.lua

+13-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ M.group = vim.api.nvim_create_augroup('RenderMarkdown', { clear = true })
1515
---Should only be called from plugin directory
1616
function M.setup()
1717
-- Lazy Loading: ignores current buffer as FileType event already executed
18-
M.schedule_attach()
18+
if #util.lazy('ft') == 0 and #util.lazy('cmd') == 0 then
19+
M.attach_current()
20+
end
1921
-- Attempt to attach to all buffers, cannot use pattern to support plugin directory
2022
vim.api.nvim_create_autocmd('FileType', {
2123
group = M.group,
@@ -43,7 +45,9 @@ end
4345
---@param enabled boolean
4446
function M.set_all(enabled)
4547
-- Lazy Loading: all previously opened buffers have been ignored
46-
M.schedule_attach()
48+
if #util.lazy('cmd') > 0 then
49+
M.attach_current()
50+
end
4751
state.enabled = enabled
4852
for _, buf in ipairs(buffers) do
4953
ui.update(buf, vim.fn.bufwinid(buf), 'UserCommand', true)
@@ -57,11 +61,8 @@ function M.is_attached(buf)
5761
end
5862

5963
---@private
60-
function M.schedule_attach()
61-
local buf = util.current('buf')
62-
vim.schedule(function()
63-
M.attach(buf)
64-
end)
64+
function M.attach_current()
65+
M.attach(util.current('buf'))
6566
end
6667

6768
---@private
@@ -109,6 +110,11 @@ function M.should_attach(buf)
109110
return false
110111
end
111112

113+
if not vim.api.nvim_buf_is_valid(buf) then
114+
log.buf('info', 'attach', buf, 'skip', 'invalid')
115+
return false
116+
end
117+
112118
local file_type, file_types = util.get('buf', buf, 'filetype'), state.file_types
113119
if not vim.tbl_contains(file_types, file_type) then
114120
local reason = string.format('%s /∈ %s', file_type, vim.inspect(file_types))

Diff for: lua/render-markdown/state.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function M.setup(default_config, user_config)
3333
end
3434
-- Use lazy.nvim file type configuration if available and no user value is specified
3535
if user_config.file_types == nil then
36-
local lazy_file_types = util.lazy_file_types('render-markdown.nvim')
36+
local lazy_file_types = util.lazy('ft')
3737
if #lazy_file_types > 0 then
3838
config.file_types = lazy_file_types
3939
end

0 commit comments

Comments
 (0)