feat: treesitter supports markdown setext_heading (#276)

This commit is contained in:
Jaehwang Jung 2023-08-07 09:53:36 +09:00 committed by GitHub
parent 2a6498f4b5
commit de460a4a29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 11 deletions

View file

@ -113,7 +113,7 @@ M.markdown = {
get_parent = function(stack, match, node)
local level_node = (utils.get_at_path(match, "level") or {}).node
-- Parse the level out of e.g. atx_h1_marker
local level = tonumber(string.sub(level_node:type(), 6, 6)) - 1
local level = tonumber(string.match(level_node:type(), "%d")) - 1
for i = #stack, 1, -1 do
if stack[i].item.level < level or stack[i].node == node then
return stack[i].item, stack[i].node, level

View file

@ -3,3 +3,9 @@
heading_content: (_) @name
(#set! "kind" "Interface")
) @type
(setext_heading
heading_content: (_) @name
(#set! "kind" "Interface")
[(setext_h1_underline) (setext_h2_underline)] @level
) @type

View file

@ -1,7 +1,7 @@
local util = require("tests.test_util")
describe("markdown", function()
local symbols = {
local symbols_all = {
{
kind = "Interface",
name = "Title 1",
@ -28,8 +28,8 @@ describe("markdown", function()
level = 0,
lnum = 5,
col = 0,
end_lnum = 15,
end_col = 8,
end_lnum = 17,
end_col = 0,
children = {
{
kind = "Interface",
@ -37,8 +37,8 @@ describe("markdown", function()
level = 2,
lnum = 7,
col = 0,
end_lnum = 15,
end_col = 8,
end_lnum = 17,
end_col = 0,
children = {
{
kind = "Interface",
@ -46,8 +46,81 @@ describe("markdown", function()
level = 3,
lnum = 13,
col = 0,
end_lnum = 15,
end_col = 8,
end_lnum = 17,
end_col = 0,
},
},
},
},
},
{
kind = "Interface",
name = "Title 6",
level = 0,
lnum = 18,
col = 0,
end_lnum = 27,
end_col = 3,
children = {
{
kind = "Interface",
name = "Title 7",
level = 1,
lnum = 21,
col = 0,
end_lnum = 27,
end_col = 3,
},
},
},
}
local symbols_atx = {
{
kind = "Interface",
name = "Title 1",
level = 0,
lnum = 1,
col = 0,
end_lnum = 4,
end_col = 0,
children = {
{
kind = "Interface",
name = "Title 2",
level = 1,
lnum = 3,
col = 0,
end_lnum = 4,
end_col = 0,
},
},
},
{
kind = "Interface",
name = "Title 3",
level = 0,
lnum = 5,
col = 0,
end_lnum = 27,
end_col = 3,
children = {
{
kind = "Interface",
name = "Title 4",
level = 2,
lnum = 7,
col = 0,
end_lnum = 27,
end_col = 3,
children = {
{
kind = "Interface",
name = "Title 5",
level = 3,
lnum = 13,
col = 0,
end_lnum = 27,
end_col = 3,
},
},
},
@ -55,9 +128,9 @@ describe("markdown", function()
},
}
it("treesitter parses all symbols correctly", function()
util.test_file_symbols("treesitter", "./tests/treesitter/markdown_test.md", symbols)
util.test_file_symbols("treesitter", "./tests/treesitter/markdown_test.md", symbols_all)
end)
it("custom backend parses all symbols correctly", function()
util.test_file_symbols("markdown", "./tests/treesitter/markdown_test.md", symbols)
it("custom backend parses atx symbols correctly", function()
util.test_file_symbols("markdown", "./tests/treesitter/markdown_test.md", symbols_atx)
end)
end)

View file

@ -13,3 +13,15 @@
#### Title 5
contents
Title 6
=======
Title 7
-------
```
Nope
----
```