Skip to content

Commit

Permalink
feature: Allow signs to be disabled per component
Browse files Browse the repository at this point in the history
## Details

Request: MeanderingProgrammer#64

Currently the controls that exist around sign column rendering are at
a buffer level. Meaning either all signs are rendered for a particular
buffer or none of them are.

There are 2 components that can render into the sign column, headings
& code blocks.

Add a 'sign' field to both of these which can be toggled on / off.

If the 'sign' field is set to false then do not attempt to render any signs
for that component. These component level settings are checked first, before
any of the buffer level ones, meaning they have the highest presedence.

Other minor changes made:

- Split heading icon & background to 2 different extmarks
- Have each handler set 'render.md.Handler' as a parent class, this
  fixes the diagnostic error so no longer needs to be disabled
- Make logger a class, missed this when adding everywhere else
- Add more methods to logger
- Have all other methods call into logger.error / logger.debug to avoid
  checking same condition in multiple places
- Add UserWindowOption type to avoid type errors when only default or
  rendered is set, align with health check and use number or string
  instead of any
  • Loading branch information
MeanderingProgrammer committed Jul 15, 2024
1 parent 3578523 commit 9b771cc
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 67 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ require('render-markdown').setup({
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- Added to the sign column if enabled
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
Expand All @@ -199,10 +201,12 @@ require('render-markdown').setup({
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon to sign column and icon + name above code blocks
-- language: adds language icon to sign column if enabled and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Determins how the top / bottom of code block are rendered:
Expand Down Expand Up @@ -383,12 +387,14 @@ require('render-markdown').setup({
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- Added to the sign column if enabled
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
Expand All @@ -415,10 +421,12 @@ require('render-markdown').setup({
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon to sign column and icon + name above code blocks
-- language: adds language icon to sign column if enabled and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Determins how the top / bottom of code block are rendered:
Expand Down
18 changes: 13 additions & 5 deletions doc/render-markdown.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*render-markdown.txt* For 0.10.0 Last change: 2024 July 13
*render-markdown.txt* For 0.10.0 Last change: 2024 July 15

==============================================================================
Table of Contents *render-markdown-table-of-contents*
Expand Down Expand Up @@ -212,12 +212,14 @@ Full Default Configuration ~
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- Added to the sign column if enabled
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
Expand All @@ -237,10 +239,12 @@ Full Default Configuration ~
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon to sign column and icon + name above code blocks
-- language: adds language icon to sign column if enabled and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Determins how the top / bottom of code block are rendered:
Expand Down Expand Up @@ -420,12 +424,14 @@ HEADINGS *render-markdown-setup-headings*
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- Added to the sign column if enabled
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
Expand Down Expand Up @@ -453,10 +459,12 @@ CODE BLOCKS *render-markdown-setup-code-blocks*
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon to sign column and icon + name above code blocks
-- language: adds language icon to sign column if enabled and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Determins how the top / bottom of code block are rendered:
Expand Down
2 changes: 1 addition & 1 deletion lua/render-markdown/handler/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local cache = {
expressions = {},
}

---@class render.md.handler.Latex
---@class render.md.handler.Latex: render.md.Handler
local M = {}

---@param namespace integer
Expand Down
43 changes: 20 additions & 23 deletions lua/render-markdown/handler/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local str = require('render-markdown.str')
local ts = require('render-markdown.ts')
local util = require('render-markdown.util')

---@class render.md.handler.Markdown
---@class render.md.handler.Markdown: render.md.Handler
local M = {}

---@param namespace integer
Expand Down Expand Up @@ -42,15 +42,13 @@ M.render = function(namespace, root, buf)
if nested_capture == 'quote_marker' then
M.render_quote_marker(namespace, buf, nested_info, info)
else
-- Should only get here if user provides custom capture, currently unhandled
logger.error('Unhandled markdown quote capture: ' .. nested_capture)
logger.unhandled_capture('markdown quote', nested_capture)
end
end
elseif capture == 'table' then
M.render_table(namespace, buf, info)
else
-- Should only get here if user provides custom capture, currently unhandled
logger.error('Unhandled markdown capture: ' .. capture)
logger.unhandled_capture('markdown', capture)
end
end
end
Expand All @@ -74,7 +72,6 @@ M.render_heading = function(namespace, buf, info)
-- is added to account for the space after the last `#` but before the heading title,
-- and concealed text is subtracted since that space is not usable
local padding = level + 1 - ts.concealed(buf, info) - str.width(icon)

if padding < 0 then
-- Requires inline extmarks to place when there is not enough space available
if util.has_10 then
Expand All @@ -85,25 +82,25 @@ M.render_heading = function(namespace, buf, info)
virt_text_pos = 'inline',
conceal = '',
})
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, 0, {
end_row = info.end_row + 1,
end_col = 0,
hl_group = background,
hl_eol = true,
})
end
else
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, 0, {
end_row = info.end_row + 1,
end_col = 0,
hl_group = background,
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, info.start_col, {
end_row = info.end_row,
end_col = info.end_col,
virt_text = { { str.pad(icon, padding), { foreground, background } } },
virt_text_pos = 'overlay',
hl_eol = true,
})
end
vim.api.nvim_buf_set_extmark(buf, namespace, info.start_row, 0, {
end_row = info.end_row + 1,
end_col = 0,
hl_group = background,
hl_eol = true,
})

M.render_sign(namespace, buf, info, list.cycle(heading.signs, level), foreground)
if heading.sign then
M.render_sign(namespace, buf, info, list.cycle(heading.signs, level), foreground)
end
end

---@private
Expand Down Expand Up @@ -188,7 +185,9 @@ M.render_language = function(namespace, buf, info, code_block)
if icon == nil or icon_highlight == nil then
return false
end
M.render_sign(namespace, buf, info, icon, icon_highlight)
if code.sign then
M.render_sign(namespace, buf, info, icon, icon_highlight)
end
-- Requires inline extmarks
if not util.has_10 then
return false
Expand Down Expand Up @@ -351,8 +350,7 @@ M.render_table = function(namespace, buf, info)
end
M.render_table_row(namespace, buf, row, pipe_table.row)
else
-- Should only get here if markdown introduces more row types, currently unhandled
logger.error('Unhandled markdown row type: ' .. row.type)
logger.unhandled_type('markdown', 'row', row.type)
end
end
if pipe_table.style == 'full' then
Expand Down Expand Up @@ -411,8 +409,7 @@ M.render_table_row = function(namespace, buf, row, highlight)
end
end
else
-- Should only get here if markdown introduces more cell types, currently unhandled
logger.error('Unhandled markdown cell type: ' .. cell.type)
logger.unhandled_type('markdown', 'cell', cell.type)
end
end
elseif pipe_table.cell == 'overlay' then
Expand Down
5 changes: 2 additions & 3 deletions lua/render-markdown/handler/markdown_inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local str = require('render-markdown.str')
local ts = require('render-markdown.ts')
local util = require('render-markdown.util')

---@class render.md.handler.MarkdownInline
---@class render.md.handler.MarkdownInline: render.md.Handler
local M = {}

---@param namespace integer
Expand All @@ -25,8 +25,7 @@ M.render = function(namespace, root, buf)
elseif capture == 'link' then
M.render_link(namespace, buf, info)
else
-- Should only get here if user provides custom capture, currently unhandled
logger.error('Unhandled inline capture: ' .. capture)
logger.unhandled_capture('inline', capture)
end
end
end
Expand Down
18 changes: 12 additions & 6 deletions lua/render-markdown/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ local M = {}
---@field public render fun(namespace: integer, root: TSNode, buf: integer)
---@field public extends? boolean

---@class render.md.WindowOption
---@field public default any
---@field public rendered any
---@class render.md.UserWindowOption
---@field public default? number|string
---@field public rendered? number|string

---@class render.md.UserSign
---@field public enabled? boolean
Expand Down Expand Up @@ -58,6 +58,7 @@ local M = {}

---@class render.md.UserCode
---@field public enabled? boolean
---@field public sign? boolean
---@field public style? 'full'|'normal'|'language'|'none'
---@field public border? 'thin'|'thick'
---@field public above? string
Expand All @@ -66,6 +67,7 @@ local M = {}

---@class render.md.UserHeading
---@field public enabled? boolean
---@field public sign? boolean
---@field public icons? string[]
---@field public signs? string[]
---@field public backgrounds? string[]
Expand Down Expand Up @@ -101,7 +103,7 @@ local M = {}
---@field public callout? table<string, render.md.UserCustomComponent>
---@field public link? render.md.UserLink
---@field public sign? render.md.UserSign
---@field public win_options? table<string, render.md.WindowOption>
---@field public win_options? table<string, render.md.UserWindowOption>
---@field public custom_handlers? table<string, render.md.Handler>

---@type render.md.Config
Expand Down Expand Up @@ -179,12 +181,14 @@ M.default_config = {
heading = {
-- Turn on / off heading icon & background rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Replaces '#+' of 'atx_h._marker'
-- The number of '#' in the heading determines the 'level'
-- The 'level' is used to index into the array using a cycle
-- The result is left padded with spaces to hide any additional '#'
icons = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
-- Added to the sign column
-- Added to the sign column if enabled
-- The 'level' is used to index into the array using a cycle
signs = { '󰫎 ' },
-- The 'level' is used to index into the array using a clamp
Expand All @@ -204,10 +208,12 @@ M.default_config = {
code = {
-- Turn on / off code block & inline code rendering
enabled = true,
-- Turn on / off any sign column related rendering
sign = true,
-- Determines how code blocks & inline code are rendered:
-- none: disables all rendering
-- normal: adds highlight group to code blocks & inline code
-- language: adds language icon to sign column and icon + name above code blocks
-- language: adds language icon to sign column if enabled and icon + name above code blocks
-- full: normal + language
style = 'full',
-- Determins how the top / bottom of code block are rendered:
Expand Down
40 changes: 27 additions & 13 deletions lua/render-markdown/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ log.flush = function()
log.reset()
end

---@class render.md.Logger
local M = {}

M.start = function()
Expand All @@ -67,26 +68,39 @@ M.debug = function(message)
end
end

---@param capture string
---@param info render.md.NodeInfo
M.debug_node_info = function(capture, info)
if vim.tbl_contains({ 'debug' }, state.config.log_level) then
log.add('debug', {
capture = capture,
text = info.text,
rows = { info.start_row, info.end_row },
cols = { info.start_col, info.end_col },
})
end
end

---@param message any
M.error = function(message)
if vim.tbl_contains({ 'debug', 'error' }, state.config.log_level) then
log.add('error', message)
end
end

---@param capture string
---@param info render.md.NodeInfo
M.debug_node_info = function(capture, info)
M.debug({
capture = capture,
text = info.text,
rows = { info.start_row, info.end_row },
cols = { info.start_col, info.end_col },
})
end

---Encountered if user provides custom capture
---@param group string
---@param capture string
M.unhandled_capture = function(group, capture)
M.error(string.format('Unhandled %s capture: %s', group, capture))
end

---Encountered if new type is seen for a particular group
---@param language string
---@param group string
---@param value string
M.unhandled_type = function(language, group, value)
M.error(string.format('Unhandled %s %s type: %s', language, group, value))
end

M.flush = function()
log.flush()
end
Expand Down
Loading

0 comments on commit 9b771cc

Please sign in to comment.