Skip to content

Commit 78a2eb7

Browse files
feat: support getting language icon by extension
## Details Request: #233 When rendering an icon for a code block ideally we could handle both the language name `python` as well as the extension `py`. Currently only the former works while the latter results in a default icon from the icon provider if configured. To handle this resolve the input language to a filetype using `vim.filetype.match` with a filename. The filename is generated by using a random name, in this case `a`, and appending the language as the extension. If the match is nil then assume the input language is the filetype. For the python example this means: - python: vim.filetype.match({ filename = 'a.python' }) -> nil or python -> python - py: vim.filetype.match({ filename = 'a.py' }) -> python or py -> python This approach is taken from the `nvim-treesitter` directive `set-lang-from-info-string`: https://github.com/nvim-treesitter/nvim-treesitter/blob/master/lua/nvim-treesitter/query_predicates.lua, aligning icon resolution with treesitter highlighting should make things feel consistent. If syntax highlighting works we can probably get an icon, if there is no syntax highlighting then we probably can't. Other change, update troubleshooting docs to validate parse tree.
1 parent d69a885 commit 78a2eb7

File tree

7 files changed

+83
-46
lines changed

7 files changed

+83
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- disabling background for code now keeps border [#220](https://github.com/MeanderingProgrammer/render-markdown.nvim/issues/220)
88
[bee16b2](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/bee16b21bf47b64ceda8c9bb281d4b576d329c0f)
99
- table support for all conceal levels [3da1bfc](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/3da1bfc4bd3a13fee57551f0b705ebcf2614d7a2)
10+
- roll own type validation to remove vim.validate [d69a885](https://github.com/MeanderingProgrammer/render-markdown.nvim/commit/d69a885e1bf21cb329d2eafe56fd80b6de627216)
1011

1112
### Bug Fixes
1213

doc/troubleshooting.md

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,52 +31,85 @@ the `filetype` of the current buffer and make sure it is in that list:
3131
:lua vim.print(vim.bo.filetype)
3232
```
3333

34-
## Generating debug logs
34+
## Validate Parse Tree
3535

36-
If all else fails hopefully the logs can provide some insight. This plugin
37-
ships with logging, however it only includes errors by default.
36+
Create a new `markdown` file locally with the following content:
3837

39-
To help debug your issue you'll need to go through the following steps:
38+
```text
39+
# Heading
4040
41-
1. Update the log level to `debug`
42-
2. Create a test file
43-
3. Generate logs from the test file
44-
4. Provide the logs in the issue
41+
- Item
4542
46-
### Update the log level to `debug`
43+
> [!NOTE]
44+
> A note
4745
48-
Change the plugin configuration to:
46+
- [x] Checked
47+
```
4948

50-
```lua
51-
require('render-markdown').setup({
52-
log_level = 'debug',
53-
})
49+
Run `:InspectTree` which should output the following:
50+
51+
```text
52+
(document ; [0, 0] - [8, 0]
53+
(section ; [0, 0] - [8, 0]
54+
(atx_heading ; [0, 0] - [1, 0]
55+
(atx_h1_marker) ; [0, 0] - [0, 1]
56+
heading_content: (inline ; [0, 2] - [0, 9]
57+
(inline))) ; [0, 2] - [0, 9]
58+
(list ; [2, 0] - [4, 0]
59+
(list_item ; [2, 0] - [4, 0]
60+
(list_marker_minus) ; [2, 0] - [2, 2]
61+
(paragraph ; [2, 2] - [3, 0]
62+
(inline ; [2, 2] - [2, 6]
63+
(inline)) ; [2, 2] - [2, 6]
64+
(block_continuation)))) ; [3, 0] - [3, 0]
65+
(block_quote ; [4, 0] - [6, 0]
66+
(block_quote_marker) ; [4, 0] - [4, 2]
67+
(paragraph ; [4, 2] - [6, 0]
68+
(inline ; [4, 2] - [5, 8]
69+
(inline ; [4, 2] - [5, 8]
70+
(shortcut_link ; [4, 2] - [4, 9]
71+
(link_text))) ; [4, 3] - [4, 8]
72+
(block_continuation)))) ; [5, 0] - [5, 2]
73+
(list ; [7, 0] - [8, 0]
74+
(list_item ; [7, 0] - [8, 0]
75+
(list_marker_minus) ; [7, 0] - [7, 2]
76+
(task_list_marker_checked) ; [7, 2] - [7, 5]
77+
(paragraph ; [7, 6] - [8, 0]
78+
(inline ; [7, 6] - [7, 13]
79+
(inline))))))) ; [7, 6] - [7, 13]
5480
```
5581

56-
### Create a test file
82+
If this is not what you see you likely need to update `nvim-treesitter` and your
83+
treesitter parsers.
5784

58-
Create a new `markdown` file locally with the following content:
85+
## Generate Debug Logs
5986

60-
```text
61-
# Heading
87+
If all else fails hopefully the logs can provide some insight. This plugin
88+
ships with logging, however it only includes errors by default.
6289

63-
- Item
64-
- Nested
90+
To help debug your issue you'll need to go through the following steps:
6591

66-
> [!NOTE]
67-
> A note
92+
### 1) Create a Test File
6893

69-
- [ ] Unchecked
70-
- [x] Checked
94+
Use the same file from [Validate Parse Tree](#validate-parse-tree).
95+
96+
### 2) Update Log Level
97+
98+
Change plugin configuration to output `debug` logs:
99+
100+
```lua
101+
require('render-markdown').setup({
102+
log_level = 'debug',
103+
})
71104
```
72105

73-
### Generate logs from the test file
106+
### 3) Generate Logs
74107

75-
To do this restart Neovim and open the `markdown` file from the previous step.
108+
To do this restart Neovim and open the `markdown` file from the first step.
76109

77-
This should trigger the render function by default, then close Neovim.
110+
This should trigger the rendering logic, then close Neovim.
78111

79-
### Provide the logs in the issue
112+
### 4) Provide Logs in Issue
80113

81114
Logs are written to a file typically located at: `~/.local/state/nvim/render-markdown.log`.
82115

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.5.4'
7+
M.version = '7.5.5'
88

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

lua/render-markdown/lib/icons.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ local M = {}
77
---@param language string
88
---@return string?, string?
99
function M.get(language)
10+
-- Handle input possibly being an extension rather than a language name
11+
local file_type = vim.filetype.match({ filename = 'a.' .. language }) or language
12+
1013
if has_mini_icons then
1114
---@diagnostic disable-next-line: return-type-mismatch
12-
return mini_icons.get('filetype', language)
15+
return mini_icons.get('filetype', file_type)
1316
elseif has_devicons then
14-
return devicons.get_icon_by_filetype(language)
17+
return devicons.get_icon_by_filetype(file_type)
1518
else
1619
return nil, nil
1720
end

tests/code_spec.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('code.md', function()
4242

4343
vim.list_extend(expected, {
4444
util.bullet(row:increment(2), 0, 1),
45-
util.code_language(row:increment(2), 2, 'lua'),
45+
util.code_language(row:increment(2), 2, 'py'),
4646
})
4747
for _ = 1, 2 do
4848
table.insert(expected, util.code_row(row:increment(), 2))
@@ -81,9 +81,9 @@ describe('code.md', function()
8181
' 8',
8282
' 9 ● Nested code',
8383
' 10',
84-
'󰢱 11 󰢱 lua',
85-
" 12 print('hello')",
86-
" 13 print('world')",
84+
'󰌠 11 󰌠 py',
85+
' 12 print("hello")',
86+
' 13 print("world")',
8787
' 14 ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀',
8888
' 15',
8989
' 16 ● Nested code with blank',
@@ -125,7 +125,7 @@ describe('code.md', function()
125125
local width_2 = 20
126126
vim.list_extend(expected, {
127127
util.bullet(row:increment(2), 0, 1),
128-
util.code_language(row:increment(2), 2, 'lua', width_2),
128+
util.code_language(row:increment(2), 2, 'py', width_2),
129129
})
130130
for _ = 1, 2 do
131131
vim.list_extend(expected, {
@@ -171,9 +171,9 @@ describe('code.md', function()
171171
' 8',
172172
' 9 ● Nested code',
173173
' 10',
174-
'󰢱 11 󰢱 lua',
175-
" 12 print('hello')",
176-
" 13 print('world')",
174+
'󰌠 11 󰌠 py',
175+
' 12 print("hello")',
176+
' 13 print("world")',
177177
' 14 ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀',
178178
' 15',
179179
' 16 ● Nested code with blank',

tests/data/code.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ fn main() {
88

99
- Nested code
1010

11-
```lua
12-
print('hello')
13-
print('world')
11+
```py
12+
print("hello")
13+
print("world")
1414
```
1515

1616
- Nested code with blank

tests/util.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,17 @@ end
225225

226226
---@param row integer
227227
---@param col integer
228-
---@param name 'python'|'lua'|'rust'
228+
---@param name 'python'|'py'|'rust'|'rs'|'lua'
229229
---@param win_col? integer
230230
---@return render.md.MarkInfo[]
231231
function M.code_language(row, col, name, win_col)
232232
local icon, highlight
233-
if name == 'python' then
233+
if name == 'python' or name == 'py' then
234234
icon, highlight = '󰌠 ', 'MiniIconsYellow'
235+
elseif name == 'rust' or name == 'rs' then
236+
icon, highlight = '󱘗 ', 'MiniIconsOrange'
235237
elseif name == 'lua' then
236238
icon, highlight = '󰢱 ', 'MiniIconsAzure'
237-
elseif name == 'rust' then
238-
icon, highlight = '󱘗 ', 'MiniIconsOrange'
239239
end
240240

241241
---@type render.md.MarkInfo

0 commit comments

Comments
 (0)