Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where the plugin fails to enable features when lazy-loaded using ft = "markdown" in lazy.nvim. The fix detects if setup is called while already in a markdown buffer and immediately enables all configured features instead of waiting for the next FileType autocmd.
- Adds immediate feature enablement when setup() is called in an already-open markdown buffer
- Handles the lazy-loading scenario where the FileType event has already fired before setup() executes
| if vim.tbl_contains(M.config.filetypes, vim.bo.filetype) then | ||
| -- Trigger enable for all loaded modules (same as the autocmd callback) | ||
| if M.list then | ||
| M.list.enable() | ||
| end | ||
| if M.format then | ||
| M.format.enable() | ||
| end | ||
| if M.headers then | ||
| M.headers.enable() | ||
| end | ||
| if M.links then | ||
| M.links.enable() | ||
| end | ||
| if M.images then | ||
| M.images.enable() | ||
| end | ||
| if M.quotes then | ||
| M.quotes.enable() | ||
| end | ||
| if M.callouts then | ||
| M.callouts.enable() | ||
| end | ||
| if M.table then | ||
| -- Set up buffer-local table keymaps | ||
| require("markdown-plus.table.keymaps").setup_buffer_keymaps(M.config.table or M.table.config) | ||
| end | ||
| end |
There was a problem hiding this comment.
The logic for enabling modules is duplicated between lines 163-190 and the autocmd callback in setup_autocmds() (lines 201-228). This violates the DRY principle and makes maintenance harder.
Recommendation: Extract this logic into a separate helper function (e.g., enable_features_for_buffer()) that can be called from both places:
---Enable all loaded modules for the current buffer
---@return nil
local function enable_features_for_buffer()
if M.list then
M.list.enable()
end
if M.format then
M.format.enable()
end
if M.headers then
M.headers.enable()
end
if M.links then
M.links.enable()
end
if M.images then
M.images.enable()
end
if M.quotes then
M.quotes.enable()
end
if M.callouts then
M.callouts.enable()
end
if M.table then
require("markdown-plus.table.keymaps").setup_buffer_keymaps(M.config.table or M.table.config)
end
endThen call this function from both locations (line 164 and in the autocmd callback).
| -- If we're already in a markdown buffer, enable features immediately | ||
| -- This handles the case where setup() is called via lazy-loading after FileType event | ||
| if vim.tbl_contains(M.config.filetypes, vim.bo.filetype) then | ||
| -- Trigger enable for all loaded modules (same as the autocmd callback) | ||
| if M.list then | ||
| M.list.enable() | ||
| end | ||
| if M.format then | ||
| M.format.enable() | ||
| end | ||
| if M.headers then | ||
| M.headers.enable() | ||
| end | ||
| if M.links then | ||
| M.links.enable() | ||
| end | ||
| if M.images then | ||
| M.images.enable() | ||
| end | ||
| if M.quotes then | ||
| M.quotes.enable() | ||
| end | ||
| if M.callouts then | ||
| M.callouts.enable() | ||
| end | ||
| if M.table then | ||
| -- Set up buffer-local table keymaps | ||
| require("markdown-plus.table.keymaps").setup_buffer_keymaps(M.config.table or M.table.config) | ||
| end | ||
| end |
There was a problem hiding this comment.
This new lazy-loading behavior lacks test coverage. According to the custom coding guidelines, all new features must have accompanying tests, and the project aims to maintain minimum 80% coverage.
Recommendation: Add tests in spec/markdown-plus/config_spec.lua to verify:
- When
setup()is called with a markdown buffer already open, features are enabled immediately - When
setup()is called without a markdown buffer, features are not enabled until FileType autocmd fires - Features work correctly in both scenarios (lazy-loaded vs. autocmd-triggered)
Description
Fix a bug where using the basic setup is not actually loading the plugin config.
Type of Change
Related Issues
Fixes #127
Testing
Checklist