Skip to content

fix(bug): basic lazy-loading setup#128

Merged
YousefHadder merged 1 commit intomainfrom
fix/lazy-loading-basic
Nov 18, 2025
Merged

fix(bug): basic lazy-loading setup#128
YousefHadder merged 1 commit intomainfrom
fix/lazy-loading-basic

Conversation

@YousefHadder
Copy link
Copy Markdown
Owner

Description

Fix a bug where using the basic setup is not actually loading the plugin config.

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Code refactoring
  • Performance improvement

Related Issues

Fixes #127

Testing

  • Tested manually
  • Added/updated tests (if applicable)

Checklist

  • Code follows project style
  • Self-reviewed my code
  • Commented complex logic
  • Updated documentation (if needed)
  • No new warnings generated

Copilot AI review requested due to automatic review settings November 18, 2025 16:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +163 to +190
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
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
end

Then call this function from both locations (line 164 and in the autocmd callback).

Copilot uses AI. Check for mistakes.
Comment on lines +161 to +190
-- 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
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. When setup() is called with a markdown buffer already open, features are enabled immediately
  2. When setup() is called without a markdown buffer, features are not enabled until FileType autocmd fires
  3. Features work correctly in both scenarios (lazy-loaded vs. autocmd-triggered)

Copilot generated this review using guidance from repository custom instructions.
@YousefHadder YousefHadder merged commit 35c977e into main Nov 18, 2025
20 checks passed
@YousefHadder YousefHadder deleted the fix/lazy-loading-basic branch November 18, 2025 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Basic setup without config function does not load correctly

2 participants