feat: show field names

master
Thomas Vigouroux 4 years ago
parent 443fa6d279
commit ccf1f62466

@ -4,21 +4,31 @@ local api = vim.api
local M = {}
function M.print_node(node, results, options)
local options = options or {}
local level = options.level or 0
local indent_char = options.indent_char or ' '
local type = node:type()
local start_row, start_col, end_row, end_col = node:range()
local function print_tree(root, results, indent)
local results = results or { lines = {}, nodes = {} }
table.insert(results.lines, string.rep(indent_char, level) .. string.format("%s [%d, %d] - [%d, %d])", type, start_col, start_row, end_col, end_row))
table.insert(results.nodes, node)
local node_count = node:named_child_count()
for i = 0, node:named_child_count() - 1, 1 do
M.print_node(node:named_child(i), results, vim.tbl_extend("force", options, { level = level + 1 }))
local indentation = indent or ""
for node, field in root:iter_children() do
if node:named() then
local line
if field then
line = string.format("%s%s: %s [%d, %d] - [%d, %d]",
indentation,
field,
node:type(),
node:range())
else
line = string.format("%s%s [%d, %d] - [%d, %d]",
indentation,
node:type(),
node:range())
end
table.insert(results.lines, line)
table.insert(results.nodes, node)
print_tree(node, results, indentation .. " ")
end
end
return results
@ -30,7 +40,7 @@ function M.print(bufnr, lang)
if not parser then return end
return M.print_node(parser:parse():root())
return print_tree(parser:parse():root())
end
return M

@ -5,9 +5,11 @@ endif
syn match nodeType "[a-zA-Z_]\+"
syn match nodeNumber "\d\+"
syn match nodeOp "[,\-\)]\+"
syn match nodeTag "\k\+:"
hi def link nodeType Identifier
hi def link nodeNumber Number
hi def link nodeOp Operator
hi def link nodeTag Tag
let b:current_syntax = 'tsplayground'

Loading…
Cancel
Save