Skip to content

fix: get correct list of ft and cmd from lazy function #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

carloscalla
Copy link
Contributor

@carloscalla carloscalla commented Jun 1, 2025

The current approach of getting the ft and cmd values from lazy.nvim only consider the last spec definition of the lazy.nvim configuration.

If the plugin spec is defined in 2 places it will create metatables and it will just get the ft defined in the last definition.

Example:

Spec definition run first:

  {
    'MeanderingProgrammer/render-markdown.nvim',
    dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-tree/nvim-web-devicons' },
    ft = { 'markdown', 'norg', 'rmd', 'org', 'codecompanion' },
    opts = {
      file_types = { 'markdown', 'norg', 'rmd', 'org', 'codecompanion' },
      completions = {
        blink = {
          enabled = true,
        },
      },
    },
  },

Second spec definition somewhere else in your config, ran last:

  {
    'MeanderingProgrammer/render-markdown.nvim',
    ft = { 'Avante' },
    opts = {
      file_types = { 'Avante' },
    },
    opts_extend = {
      'file_types',
    },
  },

This creates a structure like this after inspecing the plugin in the Lazy UI:

  dependencies = { "nvim-treesitter", "nvim-web-devicons" },
  dir = "/Users/carlos.calla/.local/share/nvim/lazy/render-markdown.nvim",
  lazy = true,
  name = "render-markdown.nvim",
  url = "https://github.com/MeanderingProgrammer/render-markdown.nvim.git",
  <metatable> = {
    __index = { "MeanderingProgrammer/render-markdown.nvim",
      ft = { "Avante" },
      opts = {
        file_types = { "Avante" }
      },  
      opts_extend = { "file_types" },
      <metatable> = {
        __index = { "MeanderingProgrammer/render-markdown.nvim",
          dependencies = { "nvim-treesitter/nvim-treesitter", "nvim-tree/nvim-web-devicons" },
          ft = { "markdown", "norg", "rmd", "org", "codecompanion" },
          opts = {
            completions = {
              blink = {
                enabled = true
              }
            },
            file_types = { "markdown", "norg", "rmd", "org", "codecompanion" }
          }
        } 
      }   
    }     
  }       
} 

However we can use the final value list of ft or cmd that lazy resolves:

    cache = {
      ft_list = { "markdown", "norg", "rmd", "org", "codecompanion", "Avante" }
    },

Lazy always resolves ft and cmd to a list and this values function from lazy.core.plugin already returns an empty table {} when no values are set.

Note: these values are set when lazy.nvim loads and resolves them, inside the values method it sets the cache if it's not set but it will be set by the time the plugin runs. If this repo don't want to use this method because it sets cache if not set, then I could try to find a way to query plugin._.cache[key] directly. Nevertheless I think this PR implementation is safe.

@MeanderingProgrammer
Copy link
Owner

This is great, thanks for the PR!

One minor note for anyone using this is you need to remove any usage of file_types in opts.

The lazy values are only used as a fallback if the user values (the ones provided through opts) are empty.

So your configurations should look something like:

  -- spec 1
  {
    'MeanderingProgrammer/render-markdown.nvim',
    dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-tree/nvim-web-devicons' },
    ft = { 'markdown', 'norg', 'rmd', 'org', 'codecompanion' },
    -- an example setting some additional options, this part is completely optional
    opts = {
      completions = { blink = { enabled = true } },
    },
  },
  ...
  -- spec 2
  {
    'MeanderingProgrammer/render-markdown.nvim',
    ft = { 'Avante' },
  },

This will now correctly resolve to the full list of file types: { 'markdown', 'norg', 'rmd', 'org', 'codecompanion', 'Avante' }

@MeanderingProgrammer MeanderingProgrammer merged commit af1a7f0 into MeanderingProgrammer:main Jun 2, 2025
@carloscalla
Copy link
Contributor Author

@MeanderingProgrammer thanks for the reply. That's correct, fyle_types is not needed now that ft are correctly set.
My bad, I should have removed them in the example of the PR description to avoid confusion. I had that prop set as I realized this bug and I had to explicitly set the file_types while working on the solution.

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.

2 participants