Skip to content

Commit 67bdd9b

Browse files
feature: auto-setup using plugin directory
## Details Request: #79 This allows users to use this plugin without calling the setup function. In this case the default configuration will be used. In the original request the suggestion was to use ftplugin, however since the filetypes this plugin runs on are configurable this is not possible, hence the usage of the plugin directory instead. This change should have no impact to existing user while adding functionality to avoid the setup paradigm if desired. As a consequence of other changes that needed to be made the concepts of configuration and initialization are much better separated instead of being coupled. In order to support this some minor structural changes needed to be made: - Color and Manager modules as well as User Command are initialed inside plugin directory instead of setup to avoid creating multiple copies. - FileType autocommand can no longer use a pattern directly since it is not known until after user calls setup. Instead the callback now runs for every event and we filter out events related to other filetypes. - Creating user command in plugin directory it makes sense to move available commands into a separate API module. Use a metatable to allow users to call the API methods without changing require logic. - Minimal init for unit tests now needs to be ordered and call runtime method for this plugin. This mimics what a plugin manager with dependencies would do. Other minor changes: - Add limitation of not running in Telescope Preview - Add todo to remove gifs from being inside the repo - Some unrelated comments added to keep track of things
1 parent 9d13bfc commit 67bdd9b

File tree

9 files changed

+99
-65
lines changed

9 files changed

+99
-65
lines changed

Diff for: doc/limitations.md

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
`LaTeX` formula evaluations are placed above text rather than overlayed.
88

9+
## Does Not Run in Telescope Preview
10+
11+
[ISSUE #98](https://github.com/MeanderingProgrammer/markdown.nvim/issues/98)
12+
13+
Telescope has a special way of previewing files that does not work like a
14+
standard buffer: [info](https://github.com/nvim-telescope/telescope.nvim?tab=readme-ov-file#previewers)
15+
16+
Due to this the events this plugin relies on to attach to and render buffers
17+
do not get triggered.
18+
919
# Resolved Limitations
1020

1121
## Telescope Opening File

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: 2024 July 25
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 July 26
22

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

Diff for: doc/todo.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
same hardware should still be useful.
77
- Look into showing marks when switching buffers, currently remains hidden
88
- Figure out how to display the many configuration options & impact
9+
- Remove demos from repository, store elsewhere

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local manager = require('render-markdown.manager')
2+
local state = require('render-markdown.state')
3+
4+
---@class render.md.Api
5+
local M = {}
6+
7+
function M.enable()
8+
manager.set_all(true)
9+
end
10+
11+
function M.disable()
12+
manager.set_all(false)
13+
end
14+
15+
function M.toggle()
16+
manager.set_all(not state.enabled)
17+
end
18+
19+
return M

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

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ M.colors = {
5555
Error = 'DiagnosticError',
5656
}
5757

58+
---Should only be called from plugin directory
5859
function M.setup()
5960
for name, link in pairs(M.colors) do
6061
vim.api.nvim_set_hl(0, M.prefix .. name, { link = link, default = true })

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

+7-53
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
local colors = require('render-markdown.colors')
2-
local manager = require('render-markdown.manager')
31
local state = require('render-markdown.state')
42

5-
---@class render.md.Init
3+
---@class render.md.Init: render.md.Api
64
local M = {}
75

86
---@class render.md.Mark
@@ -454,55 +452,11 @@ function M.setup(opts)
454452
state.inline_query = vim.treesitter.query.parse('markdown_inline', state.config.inline_query)
455453
state.inline_link_query = vim.treesitter.query.parse('markdown_inline', state.config.inline_link_query)
456454
end)
457-
458-
colors.setup()
459-
manager.setup()
460-
461-
vim.api.nvim_create_user_command('RenderMarkdown', M.command, {
462-
nargs = '*',
463-
desc = 'markdown.nvim commands',
464-
complete = function(_, cmdline)
465-
if cmdline:find('RenderMarkdown%s+%S+%s+.*') then
466-
return {}
467-
elseif cmdline:find('RenderMarkdown%s+') then
468-
return { 'enable', 'disable', 'toggle' }
469-
else
470-
return {}
471-
end
472-
end,
473-
})
474-
end
475-
476-
---@param opts any
477-
function M.command(opts)
478-
local args = opts.fargs
479-
if #args == 0 then
480-
M.enable()
481-
elseif #args == 1 then
482-
if args[1] == 'enable' then
483-
M.enable()
484-
elseif args[1] == 'disable' then
485-
M.disable()
486-
elseif args[1] == 'toggle' then
487-
M.toggle()
488-
else
489-
vim.notify('markdown.nvim: unexpected command: ' .. args[1], vim.log.levels.ERROR)
490-
end
491-
else
492-
vim.notify('markdown.nvim: unexpected # arguments: ' .. #args, vim.log.levels.ERROR)
493-
end
494-
end
495-
496-
function M.enable()
497-
manager.set_all(true)
498-
end
499-
500-
function M.disable()
501-
manager.set_all(false)
502-
end
503-
504-
function M.toggle()
505-
manager.set_all(not state.enabled)
506455
end
507456

508-
return M
457+
return setmetatable(M, {
458+
__index = function(_, key)
459+
-- Allows API methods to be accessed from top level
460+
return require('render-markdown.api')[key]
461+
end,
462+
})

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ local M = {}
1717
---@type integer
1818
M.group = vim.api.nvim_create_augroup('RenderMarkdown', { clear = true })
1919

20+
---Should only be called from plugin directory
2021
function M.setup()
21-
-- Attach to buffers based on matching filetype, this will add additional events
22+
-- Attempt to attach to all buffers, cannot use pattern to support plugin directory
2223
vim.api.nvim_create_autocmd({ 'FileType' }, {
2324
group = M.group,
24-
pattern = state.config.file_types,
2525
callback = function(event)
2626
M.attach(event.buf)
2727
end,
@@ -70,6 +70,7 @@ function M.attach(buf)
7070
ui.schedule_refresh(buf, true)
7171
end,
7272
})
73+
-- Use information specific to this event to determine whether to render or not
7374
vim.api.nvim_create_autocmd({ 'ModeChanged' }, {
7475
group = M.group,
7576
buffer = buf,
@@ -80,6 +81,8 @@ function M.attach(buf)
8081
-- Only need to re-render if render state is changing. I.e. going from normal mode to
8182
-- command mode with the default config, both are rendered, so no point re-rendering
8283
if prev_rendered ~= should_render then
84+
-- Since we do not listen to changes that happen while the user is in insert mode
85+
-- we must assume changes were made and re-parse the buffer
8386
ui.schedule_refresh(buf, true)
8487
end
8588
end,
@@ -89,7 +92,7 @@ function M.attach(buf)
8992
group = M.group,
9093
buffer = buf,
9194
callback = function()
92-
-- Moving cursor should not result in text change, skip parsing
95+
-- Moving cursor should not result in modifications to buffer so can avoid re-parsing
9396
ui.schedule_refresh(buf, false)
9497
end,
9598
})

Diff for: plugin/render-markdown.lua

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
if vim.g.loaded_render_markdown then
2+
return
3+
end
4+
vim.g.loaded_render_markdown = true
5+
6+
require('render-markdown').setup({})
7+
require('render-markdown.colors').setup()
8+
require('render-markdown.manager').setup()
9+
10+
vim.api.nvim_create_user_command('RenderMarkdown', function(opts)
11+
local args = opts.fargs
12+
if #args == 0 then
13+
require('render-markdown.api').enable()
14+
elseif #args == 1 then
15+
if args[1] == 'enable' then
16+
require('render-markdown.api').enable()
17+
elseif args[1] == 'disable' then
18+
require('render-markdown.api').disable()
19+
elseif args[1] == 'toggle' then
20+
require('render-markdown.api').toggle()
21+
else
22+
vim.notify('markdown.nvim: unexpected command: ' .. args[1], vim.log.levels.ERROR)
23+
end
24+
else
25+
vim.notify('markdown.nvim: unexpected # arguments: ' .. #args, vim.log.levels.ERROR)
26+
end
27+
end, {
28+
nargs = '*',
29+
desc = 'markdown.nvim commands',
30+
complete = function(_, cmdline)
31+
if cmdline:find('RenderMarkdown%s+%S+%s+.*') then
32+
return {}
33+
elseif cmdline:find('RenderMarkdown%s+') then
34+
return { 'enable', 'disable', 'toggle' }
35+
else
36+
return {}
37+
end
38+
end,
39+
})

Diff for: tests/minimal.lua

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---@param path_name string
2-
---@param plugin_name string?
3-
local function source_plugin(path_name, plugin_name)
2+
local function source_plugin(path_name)
43
local data_path = vim.fn.stdpath('data')
54
assert(type(data_path) == 'string')
65
local plugin_path = vim.fs.find(path_name, { path = data_path })
76
vim.opt.rtp:prepend(unpack(plugin_path))
8-
if plugin_name ~= nil then
9-
vim.cmd.runtime('plugin/' .. plugin_name)
10-
end
7+
end
8+
9+
---@param plugin_name string
10+
local function add_runtime(plugin_name)
11+
vim.cmd.runtime('plugin/' .. plugin_name)
1112
end
1213

1314
---@param required_parsers string[]
@@ -21,10 +22,16 @@ local function ensure_installed(required_parsers)
2122
end
2223
end
2324

25+
-- Source dependencies first
26+
source_plugin('nvim-treesitter')
27+
add_runtime('nvim-treesitter.lua')
28+
source_plugin('mini.nvim')
29+
-- Now we can safely source this plugin
2430
vim.opt.rtp:prepend('.')
25-
source_plugin('plenary.nvim', 'plenary.vim')
26-
source_plugin('nvim-treesitter', 'nvim-treesitter.lua')
27-
source_plugin('mini.nvim', nil)
31+
add_runtime('render-markdown.lua')
32+
-- Used for unit testing, not an actual dependency of this plugin
33+
source_plugin('plenary.nvim')
34+
add_runtime('plenary.vim')
2835

2936
-- https://github.com/ThePrimeagen/refactoring.nvim/blob/master/scripts/minimal.vim
3037
ensure_installed({ 'markdown', 'markdown_inline', 'latex' })

0 commit comments

Comments
 (0)