Skip to content

Commit cb9a5e2

Browse files
feat: integrate with lazy.nvim filetype config to avoid duplicate lists
## Details Currently if a user wants to run this plugin on multiple filetypes and have `lazy.nvim` load this plugin based on filetype the list has to be put in 2 places: 1. In the `lazy.nvim` `ft` option so the plugin manager knows when to lazy load this plugin 2. In this plugins `file_types` option so this plugin knows what buffers to run on This is a little redundant and can by improved by reading the user's plugin configuration from `lazy.core.config`. To avoid potentially breaking any users we do 2 things: 1. Skip reading the `lazy.nvim` filetypes if the user has configured a value for `file_types`. User config always takes precedence. As an implementation detail since we support loading via the `plugin` directory initially the `file_types` will be empty and we will try to read the `lazy.nvim` values. However the subsequent call to `setup` by the user will override these values. 2. Attempt to call the `lazy.nvim` modules as safely as possible to avoid breaking the plugin on any internal changes and to support any non `lazy.nvim` users. Copied the usage of `package.loaded.lazy` from `lazydev` and safely call require on `lazy.core.config` by using a `pcall`. After pulling this change users of `lazy.nvim` who configure the `ft` option can safely remove the duplicate `file_types` value from their plugin configuration.
1 parent d8be437 commit cb9a5e2

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ Plugin to improve viewing Markdown files in Neovim
1717
- Contained: runs entirely inside Neovim with no external windows
1818
- Configurable: all components, padding, icons, and colors can be modified
1919
- File type agnostic: can render `markdown` injected into any file
20+
- Automatically runs on lazy load file types defined in `lazy.nvim` `ft`
2021
- Injections: can directly manipulate treesitter to add logical `markdown` sections
21-
- Mode based rendering: changes between `rendered` and `raw` view based on mode
22+
- Modal rendering: changes between `rendered` and `raw` view based on mode
2223
- Anti-conceal: hides virtual text added by this plugin on cursor line
2324
- Window options: changes option values between `rendered` and `raw` view
2425
- Large files: only renders visisble range, can be entirely disabled based on size

benches/readme_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local util = require('benches.util')
44

55
describe('README.md', function()
66
it('default', function()
7-
local base_marks = 48
7+
local base_marks = 50
88
util.less_than(util.setup('README.md'), 50)
99
util.num_marks(base_marks)
1010

doc/render-markdown.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ Plugin to improve viewing Markdown files in Neovim
4242
- Contained: runs entirely inside Neovim with no external windows
4343
- Configurable: all components, padding, icons, and colors can be modified
4444
- File type agnostic: can render `markdown` injected into any file
45+
- Automatically runs on lazy load file types defined in `lazy.nvim` `ft`
4546
- Injections: can directly manipulate treesitter to add logical `markdown` sections
46-
- Mode based rendering: changes between `rendered` and `raw` view based on mode
47+
- Modal rendering: changes between `rendered` and `raw` view based on mode
4748
- Anti-conceal: hides virtual text added by this plugin on cursor line
4849
- Window options: changes option values between `rendered` and `raw` view
4950
- Large files: only renders visisble range, can be entirely disabled based on size

lua/render-markdown/core/util.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ local M = {}
33

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

6+
---@param name string
7+
---@return string[]
8+
function M.lazy_file_types(name)
9+
-- https://github.com/folke/lazydev.nvim/blob/main/lua/lazydev/pkg.lua -> get_plugin_path
10+
if type(package.loaded.lazy) ~= 'table' then
11+
return {}
12+
end
13+
local ok, lazy_config = pcall(require, 'lazy.core.config')
14+
if not ok then
15+
return {}
16+
end
17+
local plugin = lazy_config.spec.plugins[name]
18+
if plugin == nil then
19+
return {}
20+
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 }
26+
else
27+
return {}
28+
end
29+
end
30+
631
---@param buf integer
732
---@param win integer
833
---@return boolean

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local state = require('render-markdown.state')
44
local M = {}
55

66
---@private
7-
M.version = '7.1.7'
7+
M.version = '7.1.8'
88

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

lua/render-markdown/state.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ function M.setup(default_config, user_config)
3232
config.code.position = 'right'
3333
config.checkbox.position = 'overlay'
3434
end
35+
-- Use lazy.nvim file type configuration if available and no user value is specified
36+
if user_config.file_types == nil then
37+
local lazy_file_types = util.lazy_file_types('render-markdown.nvim')
38+
if #lazy_file_types > 0 then
39+
config.file_types = lazy_file_types
40+
end
41+
end
3542

3643
M.config = config
3744
M.enabled = config.enabled

0 commit comments

Comments
 (0)