mirror of
https://github.com/stevearc/aerial.nvim
synced 2024-09-16 14:34:08 +02:00
feat: treesitter support for json
This commit is contained in:
parent
2c0ef0f7b3
commit
fdcadb73a8
5 changed files with 55 additions and 2 deletions
|
@ -7,6 +7,9 @@ local utils = require("nvim-treesitter.utils")
|
|||
local M = {}
|
||||
|
||||
local language_kind_map = {
|
||||
json = {
|
||||
object = "Class",
|
||||
},
|
||||
lua = {
|
||||
function_definition = "Function",
|
||||
local_function = "Function",
|
||||
|
@ -56,9 +59,15 @@ M.fetch_symbols_sync = function(timeout)
|
|||
for match in query.iter_group_results(bufnr, "aerial", tree:root(), lang) do
|
||||
local name_node = (utils.get_at_path(match, "name") or {}).node
|
||||
local type_node = (utils.get_at_path(match, "type") or {}).node
|
||||
local loc_node = (utils.get_at_path(match, "location") or {}).node
|
||||
local parent, parent_node, level = get_parent(type_node)
|
||||
if type_node and type_node ~= parent_node then
|
||||
local row, col = type_node:start()
|
||||
local row, col
|
||||
if loc_node then
|
||||
row, col = loc_node:start()
|
||||
else
|
||||
row, col = type_node:start()
|
||||
end
|
||||
local kind = kind_map[type_node:type()]
|
||||
if not kind then
|
||||
vim.api.nvim_err_writeln(
|
||||
|
|
4
queries/json/aerial.scm
Normal file
4
queries/json/aerial.scm
Normal file
|
@ -0,0 +1,4 @@
|
|||
(pair
|
||||
key: (string
|
||||
(string_content) @name)
|
||||
value: (object) @type) @location
|
|
@ -2,4 +2,4 @@
|
|||
set -e
|
||||
|
||||
nvim --headless --noplugin -u tests/init.lua \
|
||||
-c "PlenaryBustedDirectory tests { minimal_init = './tests/init.lua' }"
|
||||
-c "PlenaryBustedDirectory ${1-tests} { minimal_init = './tests/init.lua' }"
|
||||
|
|
31
tests/treesitter/json_spec.lua
Normal file
31
tests/treesitter/json_spec.lua
Normal file
|
@ -0,0 +1,31 @@
|
|||
local util = require("tests.test_util")
|
||||
|
||||
describe("treesitter json", function()
|
||||
it("parses all symbols correctly", function()
|
||||
util.test_file_symbols("./tests/treesitter/json_test.json", {
|
||||
{
|
||||
kind = "Class",
|
||||
name = "obj1",
|
||||
level = 0,
|
||||
lnum = 3,
|
||||
col = 2,
|
||||
},
|
||||
{
|
||||
kind = "Class",
|
||||
name = "obj2",
|
||||
level = 0,
|
||||
lnum = 4,
|
||||
col = 2,
|
||||
children = {
|
||||
{
|
||||
kind = "Class",
|
||||
name = "obj3",
|
||||
level = 1,
|
||||
lnum = 5,
|
||||
col = 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
end)
|
||||
end)
|
9
tests/treesitter/json_test.json
Normal file
9
tests/treesitter/json_test.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"key1": 1,
|
||||
"obj1": {},
|
||||
"obj2": {
|
||||
"obj3": {
|
||||
"key2": 2
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue