feat: treesitter support for json

This commit is contained in:
Steven Arcangeli 2021-11-20 13:32:12 -08:00
parent 2c0ef0f7b3
commit fdcadb73a8
5 changed files with 55 additions and 2 deletions

View file

@ -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
View file

@ -0,0 +1,4 @@
(pair
key: (string
(string_content) @name)
value: (object) @type) @location

View file

@ -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' }"

View 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)

View file

@ -0,0 +1,9 @@
{
"key1": 1,
"obj1": {},
"obj2": {
"obj3": {
"key2": 2
}
}
}