Skip to content

Commit 5ff9a59

Browse files
feat: integrate tree-sitter injections for known filetypes
## Details Discussed here: #141 When using this plugin with multiple filetypes it would be nice if the tree-sitter injection logic could be offloaded from the user and be handled by the plugin. Especially when the way markdown would be injected is likely to be the same for anyone that wants the plugin to work within the filetype. Turns out this is fairly straightforward using the vim.treesitter.query.set API, and specifying the injections query name. The only slight complication is that in order to be able to extend the existing injections (instead of overwriting them) we need a way to get the current injection query as a string. The only way I found to do that is to pull the files associated with injections then read and append them together. All other ways to get the query return different kinds of objects that would need to be serialized back into a string. I have made this part of the configuration users can modify with the top level `injections` field. Currently a value is only specified for the gitcommit filetype. Injections are only processed if this plugin is meant to run on that filetype, meaning additional configurations can be added without impacting users who do not use this plugin on multiple file types. Users are free to: - modify the queries used - disable the injection, by setting enabled to false - add entirely new queries that are coupled to this plugin rather than needing to live in their global queries configuration
1 parent 8fdfc32 commit 5ff9a59

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ require('render-markdown').setup({
208208
log_level = 'error',
209209
-- Filetypes this plugin will run on
210210
file_types = { 'markdown' },
211+
-- Out of the box language injections for known filetypes that allow markdown to be
212+
-- interpreted in specified locations, see :h treesitter-language-injections
213+
-- Set enabled to false in order to disable
214+
injections = {
215+
gitcommit = {
216+
enabled = true,
217+
query = [[
218+
((message) @injection.content
219+
(#set! injection.combined)
220+
(#set! injection.include-children)
221+
(#set! injection.language "markdown"))
222+
]],
223+
},
224+
},
211225
-- Vim modes that will show a rendered view of the markdown file
212226
-- All other modes will be uneffected by this plugin
213227
render_modes = { 'n', 'c' },

doc/render-markdown.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@ Full Default Configuration ~
237237
log_level = 'error',
238238
-- Filetypes this plugin will run on
239239
file_types = { 'markdown' },
240+
-- Out of the box language injections for known filetypes that allow markdown to be
241+
-- interpreted in specified locations, see :h treesitter-language-injections
242+
-- Set enabled to false in order to disable
243+
injections = {
244+
gitcommit = {
245+
enabled = true,
246+
query = [[
247+
((message) @injection.content
248+
(#set! injection.combined)
249+
(#set! injection.include-children)
250+
(#set! injection.language "markdown"))
251+
]],
252+
},
253+
},
240254
-- Vim modes that will show a rendered view of the markdown file
241255
-- All other modes will be uneffected by this plugin
242256
render_modes = { 'n', 'c' },
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---@class render.md.TreeSitter
2+
local M = {}
3+
4+
---@param language string
5+
---@param injection render.md.Injection?
6+
function M.inject(language, injection)
7+
if injection == nil or not injection.enabled then
8+
return
9+
end
10+
11+
local query = ''
12+
local files = vim.treesitter.query.get_files(language, 'injections')
13+
for _, file in ipairs(files) do
14+
local f = io.open(file, 'r')
15+
if f ~= nil then
16+
query = query .. f:read('*all') .. '\n'
17+
f:close()
18+
end
19+
end
20+
query = query .. injection.query
21+
pcall(vim.treesitter.query.set, language, 'injections', query)
22+
end
23+
24+
return M

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local M = {}
55

66
---@private
77
---@type string
8-
M.version = '6.2.8'
8+
M.version = '6.2.9'
99

1010
function M.check()
1111
vim.health.start('render-markdown.nvim [version]')

lua/render-markdown/init.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ local M = {}
1818
---@field public top_pad? integer
1919
---@field public bottom_pad? integer
2020

21+
---@class (exact) render.md.UserInjection
22+
---@field public enabled? boolean
23+
---@field public query? string
24+
2125
---@class (exact) render.md.UserWindowOption
2226
---@field public default? number|string|boolean
2327
---@field public rendered? number|string|boolean
@@ -175,6 +179,7 @@ local M = {}
175179
---@field public inline_query? string
176180
---@field public log_level? render.md.config.LogLevel
177181
---@field public file_types? string[]
182+
---@field public injections? table<string, render.md.UserInjection>
178183
---@field public acknowledge_conflicts? boolean
179184
---@field public latex? render.md.UserLatex
180185
---@field public overrides? render.md.UserConfigOverrides
@@ -252,6 +257,20 @@ M.default_config = {
252257
log_level = 'error',
253258
-- Filetypes this plugin will run on
254259
file_types = { 'markdown' },
260+
-- Out of the box language injections for known filetypes that allow markdown to be
261+
-- interpreted in specified locations, see :h treesitter-language-injections
262+
-- Set enabled to false in order to disable
263+
injections = {
264+
gitcommit = {
265+
enabled = true,
266+
query = [[
267+
((message) @injection.content
268+
(#set! injection.combined)
269+
(#set! injection.include-children)
270+
(#set! injection.language "markdown"))
271+
]],
272+
},
273+
},
255274
-- Vim modes that will show a rendered view of the markdown file
256275
-- All other modes will be uneffected by this plugin
257276
render_modes = { 'n', 'c' },

lua/render-markdown/state.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local presets = require('render-markdown.presets')
2+
local treesitter = require('render-markdown.core.treesitter')
23
local util = require('render-markdown.core.util')
34

45
---@type table<integer, render.md.BufferConfig>
@@ -44,6 +45,9 @@ function M.setup(default_config, user_config)
4445
M.markdown_quote_query = vim.treesitter.query.parse('markdown', config.markdown_quote_query)
4546
M.inline_query = vim.treesitter.query.parse('markdown_inline', config.inline_query)
4647
end)
48+
for _, language in ipairs(M.file_types) do
49+
treesitter.inject(language, config.injections[language])
50+
end
4751
end
4852

4953
function M.invalidate_cache()
@@ -420,6 +424,7 @@ function M.validate()
420424
inline_query = { config.inline_query, 'string' },
421425
log_level = one_of(config.log_level, { 'debug', 'error' }, {}, false),
422426
file_types = string_array(config.file_types, false),
427+
injections = { config.injections, 'table' },
423428
acknowledge_conflicts = { config.acknowledge_conflicts, 'boolean' },
424429
latex = { config.latex, 'table' },
425430
overrides = { config.overrides, 'table' },
@@ -428,6 +433,14 @@ function M.validate()
428433

429434
validate_buffer_config('render-markdown', config, false)
430435

436+
local injections = config.injections
437+
for name, injection in pairs(injections) do
438+
append_errors('render-markdown.injections.' .. name, injection, {
439+
enabled = { injection.enabled, 'boolean' },
440+
query = { injection.query, 'string' },
441+
})
442+
end
443+
431444
local latex = config.latex
432445
append_errors('render-markdown.latex', latex, {
433446
enabled = { latex.enabled, 'boolean' },

lua/render-markdown/types.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
---@field public top_pad integer
88
---@field public bottom_pad integer
99

10+
---@class (exact) render.md.Injection
11+
---@field public enabled boolean
12+
---@field public query string
13+
1014
---@class (exact) render.md.WindowOption
1115
---@field public default number|string|boolean
1216
---@field public rendered number|string|boolean
@@ -147,6 +151,7 @@
147151
---@field public inline_query string
148152
---@field public log_level render.md.config.LogLevel
149153
---@field public file_types string[]
154+
---@field public injections table<string, render.md.Injection>
150155
---@field public acknowledge_conflicts boolean
151156
---@field public latex render.md.Latex
152157
---@field public overrides render.md.ConfigOverrides

0 commit comments

Comments
 (0)