Autoformat

master
Santos Gallegos 3 years ago
parent cb9af32e45
commit 13d597ca10
No known key found for this signature in database
GPG Key ID: 811173C303342045

@ -1,31 +1,31 @@
local parsers = require 'nvim-treesitter.parsers'
local parsers = require "nvim-treesitter.parsers"
local M = {}
function M.init()
require "nvim-treesitter".define_modules {
require("nvim-treesitter").define_modules {
playground = {
module_path = "nvim-treesitter-playground.internal",
updatetime = 25,
persist_queries = false,
keybindings = {
toggle_query_editor = 'o',
toggle_hl_groups = 'i',
toggle_injected_languages = 't',
toggle_anonymous_nodes = 'a',
toggle_language_display = 'I',
focus_language = 'f',
unfocus_language = 'F',
update = 'R',
goto_node = '<cr>',
show_help = '?',
toggle_query_editor = "o",
toggle_hl_groups = "i",
toggle_injected_languages = "t",
toggle_anonymous_nodes = "a",
toggle_language_display = "I",
focus_language = "f",
unfocus_language = "F",
update = "R",
goto_node = "<cr>",
show_help = "?",
},
},
query_linter = {
module_path = "nvim-treesitter-playground.query_linter",
use_virtual_text = true,
lint_events = {"BufWrite", "CursorHold"},
lint_events = { "BufWrite", "CursorHold" },
is_supported = function(lang)
return lang == "query" and parsers.has_parser("query")
return lang == "query" and parsers.has_parser "query"
end,
},
}

@ -1,4 +1,4 @@
local highlighter = require("vim.treesitter.highlighter")
local highlighter = require "vim.treesitter.highlighter"
local ts_utils = require "nvim-treesitter.ts_utils"
local M = {}
@ -10,23 +10,31 @@ function M.get_treesitter_hl()
local self = highlighter.active[buf]
if not self then return {} end
if not self then
return {}
end
local matches = {}
self.tree:for_each_tree(function(tstree, tree)
if not tstree then return end
if not tstree then
return
end
local root = tstree:root()
local root_start_row, _, root_end_row, _ = root:range()
-- Only worry about trees within the line range
if root_start_row > row or root_end_row < row then return end
if root_start_row > row or root_end_row < row then
return
end
local query = self:get_query(tree:lang())
-- Some injected languages may not have highlight queries.
if not query:query() then return end
if not query:query() then
return
end
local iter = query:query():iter_captures(root, self.bufnr, row, row + 1)
@ -45,14 +53,13 @@ function M.get_treesitter_hl()
end
end
end
end, true)
return matches
end
function M.get_syntax_hl()
local line = vim.fn.line(".")
local col = vim.fn.col(".")
local line = vim.fn.line "."
local col = vim.fn.col "."
local matches = {}
for _, i1 in ipairs(vim.fn.synstack(line, col)) do
local i2 = vim.fn.synIDtrans(i1)
@ -74,11 +81,14 @@ function M.show_hl_captures()
table.insert(lines, "# Syntax")
matches = M.get_syntax_hl()
end
if #matches == 0 then table.insert(lines, "* No highlight groups found") end
if #matches == 0 then
table.insert(lines, "* No highlight groups found")
end
table.insert(lines, "")
for _, line in ipairs(matches) do table.insert(lines, line) end
vim.lsp.util.open_floating_preview(lines, "markdown",
{ border = "single", pad_left = 4, pad_right = 4 })
for _, line in ipairs(matches) do
table.insert(lines, line)
end
vim.lsp.util.open_floating_preview(lines, "markdown", { border = "single", pad_left = 4, pad_right = 4 })
end
return M

@ -1,11 +1,11 @@
local parsers = require 'nvim-treesitter.parsers'
local configs = require 'nvim-treesitter.configs'
local ts_utils = require 'nvim-treesitter.ts_utils'
local printer = require 'nvim-treesitter-playground.printer'
local utils = require 'nvim-treesitter-playground.utils'
local ts_query = require 'nvim-treesitter.query'
local pl_query = require 'nvim-treesitter-playground.query'
local Promise = require 'nvim-treesitter-playground.promise'
local parsers = require "nvim-treesitter.parsers"
local configs = require "nvim-treesitter.configs"
local ts_utils = require "nvim-treesitter.ts_utils"
local printer = require "nvim-treesitter-playground.printer"
local utils = require "nvim-treesitter-playground.utils"
local ts_query = require "nvim-treesitter.query"
local pl_query = require "nvim-treesitter-playground.query"
local Promise = require "nvim-treesitter-playground.promise"
local api = vim.api
local luv = vim.loop
@ -29,18 +29,18 @@ M._entries = setmetatable({}, {
suppress_injected_languages = false,
include_language = false,
include_hl_groups = false,
focused_language_tree = nil
focused_language_tree = nil,
}
rawset(tbl, key, entry)
end
return entry
end
end,
})
local query_buf_var_name = 'TSPlaygroundForBuf'
local playground_ns = api.nvim_create_namespace('nvim-treesitter-playground')
local query_hl_ns = api.nvim_create_namespace('nvim-treesitter-playground-query')
local query_buf_var_name = "TSPlaygroundForBuf"
local playground_ns = api.nvim_create_namespace "nvim-treesitter-playground"
local query_hl_ns = api.nvim_create_namespace "nvim-treesitter-playground-query"
local function get_node_at_cursor(options)
options = options or {}
@ -50,13 +50,15 @@ local function get_node_at_cursor(options)
local root_lang_tree = parsers.get_parser()
-- This can happen in some scenarios... best not assume.
if not root_lang_tree then return end
if not root_lang_tree then
return
end
local owning_lang_tree = root_lang_tree:language_for_range({lnum - 1, col, lnum - 1, col})
local owning_lang_tree = root_lang_tree:language_for_range { lnum - 1, col, lnum - 1, col }
local result
for _, tree in ipairs(owning_lang_tree:trees()) do
local range = {lnum - 1, col, lnum - 1, col}
local range = { lnum - 1, col, lnum - 1, col }
if utils.node_contains(tree:root(), range) then
if include_anonymous then
@ -65,13 +67,17 @@ local function get_node_at_cursor(options)
result = tree:root():named_descendant_for_range(unpack(range))
end
if result then return result end
if result then
return result
end
end
end
end
local function focus_buf(bufnr)
if not bufnr then return end
if not bufnr then
return
end
local windows = vim.fn.win_findbuf(bufnr)
@ -81,7 +87,9 @@ local function focus_buf(bufnr)
end
local function close_buf_windows(bufnr)
if not bufnr then return end
if not bufnr then
return
end
utils.for_each_buf_window(bufnr, function(window)
api.nvim_win_close(window, true)
@ -89,7 +97,9 @@ local function close_buf_windows(bufnr)
end
local function close_buf(bufnr)
if not bufnr then return end
if not bufnr then
return
end
close_buf_windows(bufnr)
@ -113,7 +123,7 @@ local function is_buf_visible(bufnr)
end
local function get_update_time()
local config = configs.get_module 'playground'
local config = configs.get_module "playground"
return config and config.updatetime or 25
end
@ -145,7 +155,7 @@ local function make_entry_toggle(property, options)
if node_at_cursor then
for lnum, node_entry in ipairs(node_entries) do
if node_entry.node:id() == node_at_cursor:id() then
cursor_pos = {lnum, cursor_pos[2]}
cursor_pos = { lnum, cursor_pos[2] }
end
end
end
@ -163,26 +173,52 @@ local function setup_buf(for_buf)
local buf = api.nvim_create_buf(false, false)
api.nvim_buf_set_option(buf, 'buftype', 'nofile')
api.nvim_buf_set_option(buf, 'swapfile', false)
api.nvim_buf_set_option(buf, 'buflisted', false)
api.nvim_buf_set_option(buf, 'filetype', 'tsplayground')
api.nvim_buf_set_option(buf, "buftype", "nofile")
api.nvim_buf_set_option(buf, "swapfile", false)
api.nvim_buf_set_option(buf, "buflisted", false)
api.nvim_buf_set_option(buf, "filetype", "tsplayground")
api.nvim_buf_set_var(buf, query_buf_var_name, for_buf)
vim.cmd(string.format('augroup TreesitterPlayground_%d', buf))
vim.cmd 'au!'
vim.cmd(string.format([[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.highlight_node(%d)]], buf, for_buf))
vim.cmd(string.format([[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_highlights(%d)]], buf, for_buf))
vim.cmd(string.format([[autocmd BufWinEnter <buffer=%d> lua require'nvim-treesitter-playground.internal'.update(%d)]], buf, for_buf))
vim.cmd 'augroup END'
local config = configs.get_module("playground")
vim.cmd(string.format("augroup TreesitterPlayground_%d", buf))
vim.cmd "au!"
vim.cmd(
string.format(
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.highlight_node(%d)]],
buf,
for_buf
)
)
vim.cmd(
string.format(
[[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_highlights(%d)]],
buf,
for_buf
)
)
vim.cmd(
string.format(
[[autocmd BufWinEnter <buffer=%d> lua require'nvim-treesitter-playground.internal'.update(%d)]],
buf,
for_buf
)
)
vim.cmd "augroup END"
local config = configs.get_module "playground"
for func, mapping in pairs(config.keybindings) do
api.nvim_buf_set_keymap(buf, 'n', mapping, string.format(':lua require "nvim-treesitter-playground.internal".%s(%d)<CR>', func, for_buf), { silent = true })
api.nvim_buf_set_keymap(
buf,
"n",
mapping,
string.format(':lua require "nvim-treesitter-playground.internal".%s(%d)<CR>', func, for_buf),
{ silent = true }
)
end
api.nvim_buf_attach(buf, false, {
on_detach = function() clear_entry(for_buf) end
on_detach = function()
clear_entry(for_buf)
end,
})
return buf
@ -201,7 +237,9 @@ local function resolve_lang_tree(bufnr)
end
end)
if found then return found end
if found then
return found
end
end
end
@ -212,20 +250,34 @@ local function setup_query_editor(bufnr)
local buf = api.nvim_create_buf(false, false)
api.nvim_buf_set_option(buf, 'buftype', 'nofile')
api.nvim_buf_set_option(buf, 'swapfile', false)
api.nvim_buf_set_option(buf, 'buflisted', false)
api.nvim_buf_set_option(buf, 'filetype', 'query')
api.nvim_buf_set_option(buf, "buftype", "nofile")
api.nvim_buf_set_option(buf, "swapfile", false)
api.nvim_buf_set_option(buf, "buflisted", false)
api.nvim_buf_set_option(buf, "filetype", "query")
api.nvim_buf_set_var(buf, query_buf_var_name, bufnr)
vim.cmd(string.format([[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.on_query_cursor_move(%d)]], buf, bufnr))
api.nvim_buf_set_keymap(buf, 'n', 'R', string.format(':lua require "nvim-treesitter-playground.internal".update_query(%d, %d)<CR>', bufnr, buf), { silent = true })
vim.cmd(
string.format(
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.on_query_cursor_move(%d)]],
buf,
bufnr
)
)
api.nvim_buf_set_keymap(
buf,
"n",
"R",
string.format(':lua require "nvim-treesitter-playground.internal".update_query(%d, %d)<CR>', bufnr, buf),
{ silent = true }
)
api.nvim_buf_attach(buf, false, {
on_lines = utils.debounce(function() M.update_query(bufnr, buf) end, 1000)
on_lines = utils.debounce(function()
M.update_query(bufnr, buf)
end, 1000),
})
local config = configs.get_module 'playground'
local config = configs.get_module "playground"
if config.persist_queries then
M.read_saved_query(bufnr):then_(vim.schedule_wrap(function(lines)
@ -239,11 +291,11 @@ local function setup_query_editor(bufnr)
end
local function get_cache_path()
return vim.fn.stdpath('cache') .. '/nvim_treesitter_playground'
return vim.fn.stdpath "cache" .. "/nvim_treesitter_playground"
end
local function get_filename(bufnr)
return vim.fn.fnamemodify(vim.fn.bufname(bufnr), ':t')
return vim.fn.fnamemodify(vim.fn.bufname(bufnr), ":t")
end
function M.save_query_file(bufnr, query)
@ -251,33 +303,53 @@ function M.save_query_file(bufnr, query)
local filename = get_filename(bufnr)
fs_stat(cache_path)
:catch(function() return fs_mkdir(cache_path, 493) end)
:then_(function() return fs_open(cache_path .. '/' .. filename .. '~', 'w', 493) end)
:catch(function()
return fs_mkdir(cache_path, 493)
end)
:then_(function()
return fs_open(cache_path .. "/" .. filename .. "~", "w", 493)
end)
:then_(function(fd)
return fs_write(fd, query, -1):then_(function() return fd end)
return fs_write(fd, query, -1):then_(function()
return fd
end)
end)
:then_(function(fd)
return fs_close(fd)
end)
:catch(function(err)
print(err)
end)
:then_(function(fd) return fs_close(fd) end)
:catch(function(err) print(err) end)
end
function M.read_saved_query(bufnr)
local cache_path = get_cache_path()
local filename = get_filename(bufnr)
local query_path = cache_path .. '/' .. filename .. '~'
local query_path = cache_path .. "/" .. filename .. "~"
return fs_open(query_path, 'r', 438)
:then_(function(fd) return fs_fstat(fd)
:then_(function(stat) return fs_read(fd, stat.size, 0) end)
:then_(function(data) return fs_close(fd)
:then_(function() return vim.split(data, '\n') end) end)
return fs_open(query_path, "r", 438)
:then_(function(fd)
return fs_fstat(fd)
:then_(function(stat)
return fs_read(fd, stat.size, 0)
end)
:then_(function(data)
return fs_close(fd):then_(function()
return vim.split(data, "\n")
end)
end)
end)
:catch(function()
return {}
end)
:catch(function() return {} end)
end
function M.focus_language(bufnr)
local node_entry = M.get_current_entry(bufnr)
if not node_entry then return end
if not node_entry then
return
end
M.update(bufnr, node_entry.language_tree)
end
@ -293,9 +365,13 @@ function M.highlight_playground_nodes(bufnr, nodes)
local display_buf = entry.display_bufnr
local lines = {}
local count = 0
local node_map = utils.to_lookup_table(nodes, function(node) return node:id() end)
local node_map = utils.to_lookup_table(nodes, function(node)
return node:id()
end)
if not results or not display_buf then return end
if not results or not display_buf then
return
end
for line, result in ipairs(results) do
if node_map[result.node:id()] then
@ -312,7 +388,7 @@ function M.highlight_playground_nodes(bufnr, nodes)
local buf_lines = api.nvim_buf_get_lines(display_buf, lnum - 1, lnum, false)
if buf_lines[1] then
vim.api.nvim_buf_add_highlight(display_buf, playground_ns, 'TSPlaygroundFocus', lnum - 1, 0, -1)
vim.api.nvim_buf_add_highlight(display_buf, playground_ns, "TSPlaygroundFocus", lnum - 1, 0, -1)
end
end
@ -325,11 +401,15 @@ function M.highlight_playground_node_from_buffer(bufnr)
local entry = M._entries[bufnr]
local display_buf = entry.display_bufnr
if not display_buf then return end
if not display_buf then
return
end
local node_at_point = get_node_at_cursor({ include_anonymous = entry.include_anonymous_nodes })
local node_at_point = get_node_at_cursor { include_anonymous = entry.include_anonymous_nodes }
if not node_at_point then return end
if not node_at_point then
return
end
local lnums = M.highlight_playground_nodes(bufnr, { node_at_point })
@ -360,7 +440,9 @@ function M.highlight_node(bufnr)
local node = M.get_current_node(bufnr)
if not node then return end
if not node then
return
end
local start_row, start_col, _ = node:start()
@ -373,7 +455,7 @@ end
function M.highlight_nodes(bufnr, nodes)
for _, node in ipairs(nodes) do
ts_utils.highlight_node(node, bufnr, playground_ns, 'TSPlaygroundFocus')
ts_utils.highlight_node(node, bufnr, playground_ns, "TSPlaygroundFocus")
end
end
@ -395,12 +477,12 @@ function M.goto_node(bufnr)
end
function M.update_query(bufnr, query_bufnr)
local query = table.concat(api.nvim_buf_get_lines(query_bufnr, 0, -1, false), '\n')
local query = table.concat(api.nvim_buf_get_lines(query_bufnr, 0, -1, false), "\n")
local matches = pl_query.parse(bufnr, query, M._entries[bufnr].focused_language_tree)
local capture_by_color = {}
local index = 1
local config = configs.get_module 'playground'
local config = configs.get_module "playground"
if config.persist_queries then
M.save_query_file(bufnr, query)
@ -411,13 +493,13 @@ function M.update_query(bufnr, query_bufnr)
M.clear_highlights(query_bufnr, query_hl_ns)
M.clear_highlights(bufnr, query_hl_ns)
for capture_match in ts_query.iter_group_results(query_bufnr, 'captures') do
for capture_match in ts_query.iter_group_results(query_bufnr, "captures") do
table.insert(M._entries[bufnr].captures, capture_match.capture)
local capture = ts_utils.get_node_text(capture_match.capture.name.node)[1]
if not capture_by_color[capture] then
capture_by_color[capture] = 'TSPlaygroundCapture' .. index
capture_by_color[capture] = "TSPlaygroundCapture" .. index
index = index + 1
end
@ -443,7 +525,9 @@ function M.highlight_matched_query_nodes_from_capture(bufnr, capture)
local query_results = M._entries[bufnr].query_results
local display_buf = M._entries[bufnr].display_bufnr
if not query_results then return end
if not query_results then
return
end
local nodes_to_highlight = {}
@ -461,13 +545,15 @@ function M.highlight_matched_query_nodes_from_capture(bufnr, capture)
end
function M.on_query_cursor_move(bufnr)
local node_at_point = get_node_at_cursor({ include_anonymous = false })
local node_at_point = get_node_at_cursor { include_anonymous = false }
local captures = M._entries[bufnr].captures
M.clear_highlights(bufnr)
M.clear_highlights(M._entries[bufnr].display_bufnr)
if not node_at_point or not captures then return end
if not node_at_point or not captures then
return
end
for _, capture in ipairs(captures) do
local _, _, capture_start = capture.def.node:start()
@ -484,7 +570,9 @@ function M.on_query_cursor_move(bufnr)
end
function M.clear_highlights(bufnr, namespace)
if not bufnr then return end
if not bufnr then
return
end
namespace = namespace or playground_ns
@ -516,8 +604,8 @@ function M.toggle_query_editor(bufnr)
vim.cmd "split"
vim.cmd(string.format("buffer %d", query_buf))
api.nvim_win_set_option(0, 'spell', false)
api.nvim_win_set_option(0, 'number', true)
api.nvim_win_set_option(0, "spell", false)
api.nvim_win_set_option(0, "number", true)
api.nvim_set_current_win(current_win)
end
@ -533,10 +621,10 @@ function M.open(bufnr)
vim.cmd "vsplit"
vim.cmd(string.format("buffer %d", display_buf))
api.nvim_win_set_option(0, 'spell', false)
api.nvim_win_set_option(0, 'number', false)
api.nvim_win_set_option(0, 'relativenumber', false)
api.nvim_win_set_option(0, 'cursorline', false)
api.nvim_win_set_option(0, "spell", false)
api.nvim_win_set_option(0, "number", false)
api.nvim_win_set_option(0, "relativenumber", false)
api.nvim_win_set_option(0, "cursorline", false)
api.nvim_set_current_win(current_window)
@ -565,7 +653,7 @@ end
M.toggle_anonymous_nodes = make_entry_toggle("include_anonymous_nodes", { reprocess = true })
M.toggle_injected_languages = make_entry_toggle("suppress_injected_languages", { reprocess = true })
M.toggle_hl_groups = make_entry_toggle("include_hl_groups", { reprocess = true })
M.toggle_language_display = make_entry_toggle("include_language")
M.toggle_language_display = make_entry_toggle "include_language"
function M.update(bufnr, lang_tree)
bufnr = bufnr or api.nvim_get_current_buf()
@ -575,14 +663,16 @@ function M.update(bufnr, lang_tree)
local display_buf = entry.display_bufnr
-- Don't bother updating if the playground isn't shown
if not display_buf or not is_buf_visible(display_buf) then return end
if not display_buf or not is_buf_visible(display_buf) then
return
end
entry.focused_language_tree = lang_tree
local results = printer.process(bufnr, lang_tree, {
include_anonymous_nodes = entry.include_anonymous_nodes,
suppress_injected_languages = entry.suppress_injected_languages,
include_hl_groups = entry.include_hl_groups
include_hl_groups = entry.include_hl_groups,
})
M._entries[bufnr].results = results
@ -596,7 +686,9 @@ function M.render(bufnr)
local display_buf = entry.display_bufnr
-- Don't bother updating if the playground isn't shown
if not display_buf or not is_buf_visible(display_buf) then return end
if not display_buf or not is_buf_visible(display_buf) then
return
end
api.nvim_buf_set_lines(display_buf, 0, -1, false, printer.print_entries(entry.results))
@ -619,11 +711,13 @@ end
function M.show_help()
local function filter(item, path)
if path[#path] == vim.inspect.METATABLE then return end
if path[#path] == vim.inspect.METATABLE then
return
end
return item
end
print("Current keybindings:")
print(vim.inspect(configs.get_module('playground').keybindings, {process=filter}))
print "Current keybindings:"
print(vim.inspect(configs.get_module("playground").keybindings, { process = filter }))
end
function M.get_entries()
@ -634,20 +728,32 @@ function M.attach(bufnr)
api.nvim_buf_attach(bufnr, true, {
on_lines = vim.schedule_wrap(utils.debounce(function()
M.update(bufnr)
end, get_update_time))
end, get_update_time)),
})
vim.cmd(string.format('augroup TreesitterPlayground_%d', bufnr))
vim.cmd 'au!'
vim.cmd(string.format([[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'._highlight_playground_node_debounced(%d)]], bufnr, bufnr))
vim.cmd(string.format([[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_playground_highlights(%d)]], bufnr, bufnr))
vim.cmd 'augroup END'
vim.cmd(string.format("augroup TreesitterPlayground_%d", bufnr))
vim.cmd "au!"
vim.cmd(
string.format(
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'._highlight_playground_node_debounced(%d)]],
bufnr,
bufnr
)
)
vim.cmd(
string.format(
[[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_playground_highlights(%d)]],
bufnr,
bufnr
)
)
vim.cmd "augroup END"
end
function M.detach(bufnr)
clear_entry(bufnr)
vim.cmd(string.format('autocmd! TreesitterPlayground_%d CursorMoved', bufnr))
vim.cmd(string.format('autocmd! TreesitterPlayground_%d BufLeave', bufnr))
vim.cmd(string.format("autocmd! TreesitterPlayground_%d CursorMoved", bufnr))
vim.cmd(string.format("autocmd! TreesitterPlayground_%d BufLeave", bufnr))
end
return M

@ -1,11 +1,11 @@
local parsers = require 'nvim-treesitter.parsers'
local utils = require 'nvim-treesitter-playground.utils'
local parsers = require "nvim-treesitter.parsers"
local utils = require "nvim-treesitter-playground.utils"
local api = vim.api
local M = {}
local treesitter_namespace = api.nvim_get_namespaces()["treesitter/highlighter"]
local virt_text_id = api.nvim_create_namespace('TSPlaygroundHlGroups')
local lang_virt_text_id = api.nvim_create_namespace('TSPlaygroundLangGroups')
local virt_text_id = api.nvim_create_namespace "TSPlaygroundHlGroups"
local lang_virt_text_id = api.nvim_create_namespace "TSPlaygroundLangGroups"
local function get_extmarks(bufnr, start, end_)
return api.nvim_buf_get_extmarks(bufnr, treesitter_namespace, start, end_, { details = true })
@ -13,7 +13,7 @@ end
local function get_hl_group_for_node(bufnr, node)
local start_row, start_col, end_row, end_col = node:range()
local extmarks = get_extmarks(bufnr, {start_row, start_col}, {end_row, end_col})
local extmarks = get_extmarks(bufnr, { start_row, start_col }, { end_row, end_col })
local groups = {}
if #extmarks > 0 then
@ -36,10 +36,7 @@ local function flatten_node(root, results, level, language_tree, options)
node = node,
field = field,
language_tree = language_tree,
hl_groups = options.include_hl_groups
and options.bufnr
and get_hl_group_for_node(options.bufnr, node)
or {}
hl_groups = options.include_hl_groups and options.bufnr and get_hl_group_for_node(options.bufnr, node) or {},
}
table.insert(results, node_entry)
@ -60,7 +57,7 @@ local function flatten_lang_tree(lang_tree, results, options)
local head_entry_index = nil
for i, node_entry in ipairs(results) do
local is_contained = utils.node_contains(node_entry.node, {root:range()})
local is_contained = utils.node_contains(node_entry.node, { root:range() })
if is_contained then
if not head_entry then
@ -107,7 +104,9 @@ function M.process(bufnr, lang_tree, options)
lang_tree = lang_tree or parsers.get_parser(bufnr)
options.bufnr = options.bufnr or bufnr
if not lang_tree then return {} end
if not lang_tree then
return {}
end
return flatten_lang_tree(lang_tree, nil, options)
end
@ -125,16 +124,9 @@ function M.print_entry(node_entry)
end
if field then
line = string.format("%s%s: %s [%d, %d] - [%d, %d]",
indent,
field,
node_name,
node:range())
line = string.format("%s%s: %s [%d, %d] - [%d, %d]", indent, field, node_name, node:range())
else
line = string.format("%s%s [%d, %d] - [%d, %d]",
indent,
node_name,
node:range())
line = string.format("%s%s [%d, %d] - [%d, %d]", indent, node_name, node:range())
end
return line
@ -161,7 +153,7 @@ function M.print_hl_groups(bufnr, node_entries)
str = string.sub(str, 0, -3)
end
table.insert(groups, {str, hl_group})
table.insert(groups, { str, hl_group })
end
api.nvim_buf_set_virtual_text(bufnr, virt_text_id, i, groups, {})
@ -170,7 +162,7 @@ end
function M.print_language(bufnr, node_entries)
for i, node_entry in ipairs(node_entries) do
api.nvim_buf_set_virtual_text(bufnr, lang_virt_text_id, i - 1, {{node_entry.language_tree:lang()}}, {})
api.nvim_buf_set_virtual_text(bufnr, lang_virt_text_id, i - 1, { { node_entry.language_tree:lang() } }, {})
end
end

@ -3,10 +3,12 @@ local luv = vim.loop
function M.promisify(fn)
return function(...)
local args = {...}
local args = { ... }
return M.new(function(resolve, reject)
table.insert(args, function(err, v)
if err then return reject(err) end
if err then
return reject(err)
end
resolve(v)
end)
@ -35,16 +37,24 @@ function M.new(sink)
is_errored = false,
cbs = {},
err_cbs = {},
}, { __index = M })
}, {
__index = M,
})
p._resolve = function(v) p:_set_result(v, false) end
p._reject = function(err) p:_set_result(err, true) end
p._resolve = function(v)
p:_set_result(v, false)
end
p._reject = function(err)
p:_set_result(err, true)
end
local success, err = pcall(function()
sink(p._resolve, p._reject)
end)
if not success then p._reject(err) end
if not success then
p._reject(err)
end
return p
end
@ -58,9 +68,13 @@ function M:then_(on_success, on_error)
return resolve(result)
end
local success, res = pcall(function() resolve(on_success(result)) end)
local success, res = pcall(function()
resolve(on_success(result))
end)
if not success then reject(res) end
if not success then
reject(res)
end
return res
end)
@ -70,9 +84,13 @@ function M:then_(on_success, on_error)
return reject(result)
end
local success, res = pcall(function() resolve(on_error(result)) end)
local success, res = pcall(function()
resolve(on_error(result))
end)
if not success then reject(res) end
if not success then
reject(res)
end
return res
end)
@ -128,7 +146,7 @@ function M:_set_result(result, errored)
end
function M.is_promise(v)
return type(v) == 'table' and type(v.then_) == 'function'
return type(v) == "table" and type(v.then_) == "function"
end
return M

@ -1,6 +1,6 @@
local ts_query = require 'nvim-treesitter.query'
local parsers = require 'nvim-treesitter.parsers'
local locals = require 'nvim-treesitter.locals'
local ts_query = require "nvim-treesitter.query"
local parsers = require "nvim-treesitter.parsers"
local locals = require "nvim-treesitter.locals"
local M = {}
@ -11,7 +11,9 @@ function M.parse(bufnr, query, lang_tree)
return vim.treesitter.parse_query(lang_tree:lang(), query)
end)
if not success then return {} end
if not success then
return {}
end
local results = {}

@ -5,16 +5,16 @@ local ts_utils = require "nvim-treesitter.ts_utils"
local utils = require "nvim-treesitter.utils"
local configs = require "nvim-treesitter.configs"
local hl_namespace = api.nvim_create_namespace("nvim-playground-lints")
local hl_namespace = api.nvim_create_namespace "nvim-playground-lints"
local ERROR_HL = "TSQueryLinterError"
local MAGIC_NODE_NAMES = {"_", "ERROR"}
local playground_module = require 'nvim-treesitter-playground.internal'
local MAGIC_NODE_NAMES = { "_", "ERROR" }
local playground_module = require "nvim-treesitter-playground.internal"
local M = {}
M.lints = {}
M.use_virtual_text = true
M.lint_events = {"BufWrite", "CursorHold"}
M.lint_events = { "BufWrite", "CursorHold" }
local function lint_node(node, buf, error_type, complete_message)
if error_type ~= "Invalid Query" then
@ -22,11 +22,11 @@ local function lint_node(node, buf, error_type, complete_message)
end
local node_text = table.concat(ts_utils.get_node_text(node, buf), " ")
local error_text = complete_message or error_type .. ": " .. node_text
local error_range = {node:range()}
local error_range = { node:range() }
if M.use_virtual_text then
api.nvim_buf_set_virtual_text(buf, hl_namespace, error_range[1], {{error_text, ERROR_HL}}, {})
api.nvim_buf_set_virtual_text(buf, hl_namespace, error_range[1], { { error_text, ERROR_HL } }, {})
end
table.insert(M.lints[buf], {type = error_type, range = error_range, message = error_text, node_text = node_text})
table.insert(M.lints[buf], { type = error_type, range = error_range, message = error_text, node_text = node_text })
end
local function table_contains(predicate, table)
@ -98,19 +98,15 @@ function M.lint(query_buf)
local node_type = ts_utils.get_node_text(node)[1]
if anonymous_node then
node_type = node_type:gsub('"(.*)".*$', "%1"):gsub('\\(.)', '%1')
node_type = node_type:gsub('"(.*)".*$', "%1"):gsub("\\(.)", "%1")
end
local is_named = named_node ~= nil
local found =
vim.tbl_contains(MAGIC_NODE_NAMES, node_type) or
table_contains(
function(t)
return node_type == t[1] and is_named == t[2]
end,
parser_info.symbols
)
local found = vim.tbl_contains(MAGIC_NODE_NAMES, node_type)
or table_contains(function(t)
return node_type == t[1] and is_named == t[2]
end, parser_info.symbols)
if not found then
lint_node(node, query_buf, "Invalid Node Type")
@ -138,7 +134,7 @@ end
function M.attach(buf, _)
M.lints[buf] = {}
local config = configs.get_module("query_linter")
local config = configs.get_module "query_linter"
M.use_virtual_text = config.use_virtual_text
M.lint_events = config.lint_events

@ -2,19 +2,25 @@ local M = {}
function M.debounce(fn, debounce_time)
local timer = vim.loop.new_timer()
local is_debounce_fn = type(debounce_time) == 'function'
local is_debounce_fn = type(debounce_time) == "function"
return function(...)
timer:stop()
local time = debounce_time
local args = {...}
local args = { ... }
if is_debounce_fn then
time = debounce_time()
end
timer:start(time, 0, vim.schedule_wrap(function() fn(unpack(args)) end))
timer:start(
time,
0,
vim.schedule_wrap(function()
fn(unpack(args))
end)
)
end
end

@ -1,12 +1,12 @@
local query_linter = require 'nvim-treesitter-playground.query_linter'
local tsc = require 'vim.treesitter.query'
local query_linter = require "nvim-treesitter-playground.query_linter"
local tsc = require "vim.treesitter.query"
local M = {}
function M.omnifunc(findstart, base)
if findstart == 1 then
local start = vim.fn.col('.') - 1
local result = vim.fn.matchstrpos(vim.fn.getline('.'):sub(1, start), '\\v(["#\\-]|\\w)*$')
local start = vim.fn.col "." - 1
local result = vim.fn.matchstrpos(vim.fn.getline("."):sub(1, start), '\\v(["#\\-]|\\w)*$')
return result[2]
end
@ -19,28 +19,28 @@ function M.omnifunc(findstart, base)
local items = {}
for _, f in pairs(parser_info.fields) do
if f:find(base, 1, true) == 1 then
table.insert(items, f..':')
table.insert(items, f .. ":")
end
end
for _, p in pairs(tsc.list_predicates()) do
local text = '#'..p
local text = "#" .. p
local found = text:find(base, 1, true)
if found and found <= 2 then -- with or without '#'
table.insert(items, text)
end
text = '#not-'..p
text = "#not-" .. p
found = text:find(base, 1, true)
if found and found <= 2 then -- with or without '#'
table.insert(items, text)
end
end
for _, s in pairs(parser_info.symbols) do
local text = s[2] and s[1] or '"'..vim.fn.escape(s[1], '\\')..'"'
local text = s[2] and s[1] or '"' .. vim.fn.escape(s[1], "\\") .. '"'
if text:find(base, 1, true) == 1 then
table.insert(items, text)
end
end
return {words = items, refresh= 'always'}
return { words = items, refresh = "always" }
else
return -2
end

Loading…
Cancel
Save