Skip to content

Commit 8ee2701

Browse files
feat: allow language icon to be disabled while still showing language name
## Details Request: #376 Adds configuration option `code.language_icon` which works with the existing `code.language_name` option. This option is enabled by default so the behavior is identical to what it was before. This changes the behavior if an icon is not available for a particular language. Rather than adding an empty border (old behavior) we now add a line with just the language name. Since the icon provider previously gave us the highlight to use and it could be missing, we now need a fallback. This is set by the new `code.highlight_fallback` option which by default links to the `Normal` highlight group. To make this work is mostly minor implementation details. Continue processing the language even if the icon returned from the provider is missing. After creating the virtual text line check if it's empty before adding it, this would happen if `language_name = false` and there is no icon available. Other minor differences in behavior, but these only impact users who do NOT have the language details concealed, likely by NOT enabling `nvim-treesitter` highlights + these options: - `position = 'left'`: the language name is added after the icon, rather than being skipped by default, to avoid adding the name users will now have to set `language_name = false`, which will add only the icon - `position = 'left'` + `language_padding > 0`: the padding is now added, previously we would skip adding the padding
1 parent f9badfb commit 8ee2701

File tree

8 files changed

+73
-37
lines changed

8 files changed

+73
-37
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,9 @@ require('render-markdown').setup({
388388
-- Amount of padding to add around the language.
389389
-- If a float < 1 is provided it is treated as a percentage of available window space.
390390
language_pad = 0,
391-
-- Whether to include the language name next to the icon.
391+
-- Whether to include the language icon above code blocks.
392+
language_icon = true,
393+
-- Whether to include the language name above code blocks.
392394
language_name = true,
393395
-- A list of language names for which background highlighting will be disabled.
394396
-- Likely because that language has background highlights itself.
@@ -424,6 +426,8 @@ require('render-markdown').setup({
424426
highlight = 'RenderMarkdownCode',
425427
-- Highlight for language, overrides icon provider value.
426428
highlight_language = nil,
429+
-- Highlight for language, used if icon provider does not have a value.
430+
highlight_fallback = 'RenderMarkdownCodeFallback',
427431
-- Padding to add to the left & right of inline code.
428432
inline_pad = 0,
429433
-- Highlight for inline code.
@@ -933,7 +937,9 @@ require('render-markdown').setup({
933937
-- Amount of padding to add around the language.
934938
-- If a float < 1 is provided it is treated as a percentage of available window space.
935939
language_pad = 0,
936-
-- Whether to include the language name next to the icon.
940+
-- Whether to include the language icon above code blocks.
941+
language_icon = true,
942+
-- Whether to include the language name above code blocks.
937943
language_name = true,
938944
-- A list of language names for which background highlighting will be disabled.
939945
-- Likely because that language has background highlights itself.
@@ -969,6 +975,8 @@ require('render-markdown').setup({
969975
highlight = 'RenderMarkdownCode',
970976
-- Highlight for language, overrides icon provider value.
971977
highlight_language = nil,
978+
-- Highlight for language, used if icon provider does not have a value.
979+
highlight_fallback = 'RenderMarkdownCodeFallback',
972980
-- Padding to add to the left & right of inline code.
973981
inline_pad = 0,
974982
-- Highlight for inline code.
@@ -1403,6 +1411,7 @@ The table below shows all the highlight groups with their default link
14031411
| RenderMarkdownH5Bg | DiffDelete | H5 background line |
14041412
| RenderMarkdownH6Bg | DiffDelete | H6 background line |
14051413
| RenderMarkdownCode | ColorColumn | Code block background |
1414+
| RenderMarkdownCodeFallback | Normal | Fallback for code language |
14061415
| RenderMarkdownCodeInline | RenderMarkdownCode | Inline code background |
14071416
| RenderMarkdownInlineHighlight | RenderMarkdownCodeInline | Inline highlights contents |
14081417
| RenderMarkdownBullet | Normal | List item bullet points |

doc/render-markdown.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ Default Configuration ~
453453
-- Amount of padding to add around the language.
454454
-- If a float < 1 is provided it is treated as a percentage of available window space.
455455
language_pad = 0,
456-
-- Whether to include the language name next to the icon.
456+
-- Whether to include the language icon above code blocks.
457+
language_icon = true,
458+
-- Whether to include the language name above code blocks.
457459
language_name = true,
458460
-- A list of language names for which background highlighting will be disabled.
459461
-- Likely because that language has background highlights itself.
@@ -489,6 +491,8 @@ Default Configuration ~
489491
highlight = 'RenderMarkdownCode',
490492
-- Highlight for language, overrides icon provider value.
491493
highlight_language = nil,
494+
-- Highlight for language, used if icon provider does not have a value.
495+
highlight_fallback = 'RenderMarkdownCodeFallback',
492496
-- Padding to add to the left & right of inline code.
493497
inline_pad = 0,
494498
-- Highlight for inline code.
@@ -992,7 +996,9 @@ Code Block Configuration ~
992996
-- Amount of padding to add around the language.
993997
-- If a float < 1 is provided it is treated as a percentage of available window space.
994998
language_pad = 0,
995-
-- Whether to include the language name next to the icon.
999+
-- Whether to include the language icon above code blocks.
1000+
language_icon = true,
1001+
-- Whether to include the language name above code blocks.
9961002
language_name = true,
9971003
-- A list of language names for which background highlighting will be disabled.
9981004
-- Likely because that language has background highlights itself.
@@ -1028,6 +1034,8 @@ Code Block Configuration ~
10281034
highlight = 'RenderMarkdownCode',
10291035
-- Highlight for language, overrides icon provider value.
10301036
highlight_language = nil,
1037+
-- Highlight for language, used if icon provider does not have a value.
1038+
highlight_fallback = 'RenderMarkdownCodeFallback',
10311039
-- Padding to add to the left & right of inline code.
10321040
inline_pad = 0,
10331041
-- Highlight for inline code.
@@ -1459,6 +1467,9 @@ The table below shows all the highlight groups with their default link
14591467
RenderMarkdownCode ColorColumn Code block
14601468
background
14611469

1470+
RenderMarkdownCodeFallback Normal Fallback for code
1471+
language
1472+
14621473
RenderMarkdownCodeInline RenderMarkdownCode Inline code
14631474
background
14641475

lua/render-markdown/colors.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ M.colors = {
2929
H4Bg = 'DiffDelete',
3030
H5Bg = 'DiffDelete',
3131
H6Bg = 'DiffDelete',
32-
-- General
32+
-- Code
3333
Code = 'ColorColumn',
34+
CodeFallback = 'Normal',
3435
CodeInline = 'RenderMarkdownCode',
36+
-- General
3537
InlineHighlight = 'RenderMarkdownCodeInline',
3638
Bullet = 'Normal',
3739
Quote = '@markup.quote',

lua/render-markdown/health.lua

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

77
---@private
8-
M.version = '8.1.10'
8+
M.version = '8.1.11'
99

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

lua/render-markdown/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ local M = {}
208208
---@field style? render.md.code.Style
209209
---@field position? render.md.code.Position
210210
---@field language_pad? number
211+
---@field language_icon? boolean
211212
---@field language_name? boolean
212213
---@field disable_background? boolean|string[]
213214
---@field width? render.md.code.Width
@@ -220,6 +221,7 @@ local M = {}
220221
---@field below? string
221222
---@field highlight? string
222223
---@field highlight_language? string
224+
---@field highlight_fallback? string
223225
---@field inline_pad? integer
224226
---@field highlight_inline? string
225227

@@ -549,7 +551,9 @@ M.default_config = {
549551
-- Amount of padding to add around the language.
550552
-- If a float < 1 is provided it is treated as a percentage of available window space.
551553
language_pad = 0,
552-
-- Whether to include the language name next to the icon.
554+
-- Whether to include the language icon above code blocks.
555+
language_icon = true,
556+
-- Whether to include the language name above code blocks.
553557
language_name = true,
554558
-- A list of language names for which background highlighting will be disabled.
555559
-- Likely because that language has background highlights itself.
@@ -585,6 +589,8 @@ M.default_config = {
585589
highlight = 'RenderMarkdownCode',
586590
-- Highlight for language, overrides icon provider value.
587591
highlight_language = nil,
592+
-- Highlight for language, used if icon provider does not have a value.
593+
highlight_fallback = 'RenderMarkdownCodeFallback',
588594
-- Padding to add to the left & right of inline code.
589595
inline_pad = 0,
590596
-- Highlight for inline code.

lua/render-markdown/render/code.lua

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ function Render:render()
9999

100100
local icon = self:language()
101101
local start_row, end_row = self.node.start_row, self.node.end_row - 1
102-
self:border(start_row, self.code.above, not icon and self:concealed(self.data.code_node))
103-
self:border(end_row, self.code.below, true)
102+
self:border(start_row, true, not icon and self:concealed(self.data.code_node))
103+
self:border(end_row, false, true)
104104
if background then
105105
self:background(start_row + 1, end_row - 1)
106106
end
@@ -113,6 +113,9 @@ function Render:language()
113113
if not vim.tbl_contains({ 'language', 'full' }, self.code.style) then
114114
return false
115115
end
116+
if not self.code.language_icon and not self.code.language_name then
117+
return false
118+
end
116119

117120
local node, padding = self.data.language_node, self.data.language_padding
118121
if node == nil then
@@ -123,58 +126,61 @@ function Render:language()
123126
if self.code.highlight_language ~= nil then
124127
icon_highlight = self.code.highlight_language
125128
end
126-
if icon == nil or icon_highlight == nil then
127-
return false
128-
end
129129

130130
self:sign(self.code.sign, icon, icon_highlight)
131131

132-
local text, highlight = icon .. ' ', { icon_highlight }
132+
local text = ''
133+
if self.code.language_icon and icon ~= nil then
134+
text = text .. icon .. ' '
135+
end
136+
if self.code.language_name then
137+
text = text .. node.text
138+
end
139+
if #text == 0 then
140+
return false
141+
end
142+
143+
local highlight = { icon_highlight or self.code.highlight_fallback }
133144
if self.code.border ~= 'none' then
134145
table.insert(highlight, self.code.highlight)
135146
end
136147

137148
if self.code.position == 'left' then
138-
if self.code.language_name and self:concealed(node) then
139-
-- Code blocks pick up varying amounts of leading white space depending
140-
-- on the context they are in. This is lumped into the delimiter node
141-
-- and as a result, after concealing, the extmark would be shifted.
142-
local spaces = Str.spaces('start', self.node.text)
143-
text = Str.pad(spaces + padding) .. text .. node.text
149+
text = Str.pad(padding) .. text
150+
if self:concealed(node) then
151+
-- Code blocks can pick up varying amounts of leading white space.
152+
-- This is lumped into the delimiter node and needs to be handled.
153+
text = Str.pad(Str.spaces('start', self.node.text)) .. text
144154
end
145155
return self.marks:add('code_language', node.start_row, node.start_col, {
146156
virt_text = { { text, highlight } },
147157
virt_text_pos = 'inline',
148158
})
149-
elseif self.code.position == 'right' then
150-
if self.code.language_name then
151-
text = text .. node.text
152-
end
153-
local win_col = self.data.max_width - padding
159+
else
160+
local start = self.data.max_width - padding
154161
if self.code.width == 'block' then
155-
win_col = win_col - Str.width(text)
162+
start = start - Str.width(text)
156163
end
157164
return self.marks:add('code_language', node.start_row, 0, {
158165
virt_text = { { text, highlight } },
159-
virt_text_win_col = win_col + self.data.indent,
166+
virt_text_win_col = start + self.data.indent,
160167
})
161-
else
162-
return false
163168
end
164169
end
165170

166171
---@private
167172
---@param row integer
168-
---@param border string
169-
---@param context_hidden boolean
170-
function Render:border(row, border, context_hidden)
173+
---@param above boolean
174+
---@param empty boolean
175+
function Render:border(row, above, empty)
171176
if self.code.border == 'none' then
172177
return
173178
end
174-
local delim_node = self.node:child('fenced_code_block_delimiter', row)
175-
if self.code.border == 'thin' and context_hidden and self:concealed(delim_node) then
176-
local width = self.data.width - self.data.col
177-
local line = { { border:rep(width), colors.bg_to_fg(self.code.highlight) } }
179+
local delim = self.node:child('fenced_code_block_delimiter', row)
180+
local width, highlight = self.data.width - self.data.col, self.code.highlight
181+
local border = above and self.code.above or self.code.below
182+
if self.code.border == 'thin' and empty and self:concealed(delim) then
183+
local line = { { border:rep(width), colors.bg_to_fg(highlight) } }
178184
self.marks:add('code_border', row, self.data.col, {
179185
virt_text = line,
180186
virt_text_pos = 'overlay',

lua/render-markdown/state.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ function M.validate()
188188
end)
189189
:nested('code', function(code)
190190
component_rules(code)
191-
:type({ 'sign', 'language_name' }, 'boolean')
191+
:type({ 'sign', 'language_icon', 'language_name' }, 'boolean')
192192
:type({ 'language_pad', 'left_margin', 'left_pad', 'right_pad', 'min_width' }, 'number')
193193
:type('inline_pad', 'number')
194-
:type({ 'above', 'below', 'highlight', 'highlight_inline' }, 'string')
194+
:type({ 'above', 'below', 'highlight', 'highlight_fallback', 'highlight_inline' }, 'string')
195195
:type('highlight_language', { 'string', 'nil' })
196196
:list('disable_background', 'string', 'boolean')
197197
:one_of('style', { 'full', 'normal', 'language', 'none' })

lua/render-markdown/types.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
---@field style render.md.code.Style
140140
---@field position render.md.code.Position
141141
---@field language_pad number
142+
---@field language_icon boolean
142143
---@field language_name boolean
143144
---@field disable_background boolean|string[]
144145
---@field width render.md.code.Width
@@ -151,6 +152,7 @@
151152
---@field below string
152153
---@field highlight string
153154
---@field highlight_language? string
155+
---@field highlight_fallback string
154156
---@field inline_pad integer
155157
---@field highlight_inline string
156158

0 commit comments

Comments
 (0)