From fb7393d0f54de31652cd0b2c7af88fb53bbab901 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Mon, 13 May 2024 14:22:41 -0500 Subject: [PATCH] test: improve treesitter setup --- tests/minimal_init.lua | 63 +++++++++++++++++++++------- tests/specs/close_slash_tag_spec.lua | 4 -- tests/specs/closetag_spec.lua | 4 -- tests/specs/renametag_spec.lua | 8 ---- tests/utils/helpers.lua | 19 --------- 5 files changed, 47 insertions(+), 51 deletions(-) diff --git a/tests/minimal_init.lua b/tests/minimal_init.lua index 25f7522..5adae58 100644 --- a/tests/minimal_init.lua +++ b/tests/minimal_init.lua @@ -44,6 +44,41 @@ function M.load_plugin(plugin_name, plugin_clone_args) print(("[LOAD PLUGIN] Loaded plugin '%s'"):format(plugin_name)) end +function M.setup_treesitter() + print("[TREESITTER] Setting up nvim-treesitter") + local parser_cfgs = require("nvim-treesitter.parsers").get_parser_configs() + for parser_name, parser_cfg in pairs({ + rescript = { + install_info = { + url = "https://github.com/rescript-lang/tree-sitter-rescript", + branch = "main", + files = { "src/parser.c" }, + generate_requires_npm = false, + requires_generate_from_grammar = true, + use_makefile = true, + }, + }, + }) do + parser_cfgs[parser_name] = parser_cfg + end + require("nvim-treesitter.configs").setup({ + sync_install = true, + ensure_installed = { + "html", + "javascript", + "typescript", + "svelte", + "vue", + "tsx", + "php", + "glimmer", + "rescript", + "embedded_template", + }, + }) + print("[TREESITTER] Done setting up nvim-treesitter") +end + ---Do the initial setup. Downloads plugins, ensures the minimal init does not pollute the filesystem by keeping ---everything self contained to the CWD of the minimal init file. Run prior to running tests, reproducing issues, etc. ---@param plugins? MinPlugins @@ -59,28 +94,21 @@ function M.setup(plugins) "config", "state", } + local clean = (vim.env.TEST_CLEANUP and vim.env.TEST_CLEANUP:lower() or true) + if clean then + vim.fn.delete(xdg_root:get(), "rf") + elseif clean == "false" or clean == "0" then + print("[CLEANUP]: `TEST_CLEANUP` was disabled, not cleaning " .. xdg_root:get()) + end for _, std_path in pairs(std_paths) do local xdg_str = "XDG_" .. std_path:upper() .. "_HOME" local xdg_path = xdg_root:push(std_path):get() - print(("[SETUP] Set vim.env.%-3s -> %s"):format(xdg_str, xdg_path)) + print(("[SETUP] Set vim.env.%s -> %s"):format(xdg_str, xdg_path)) vim.env[xdg_str] = xdg_path ---@diagnostic disable-next-line: param-type-mismatch - vim.fn.mkdir(vim.fn.stdpath(std_path), "p") + vim.fn.mkdir(xdg_path, "p") end - -- Ignore cleanups if specified in the environment - -- NOTE: Cleanup the xdg cache on exit so new runs of the minimal init doesn't share any previous state, e.g. shada - vim.api.nvim_create_autocmd("VimLeave", { - callback = function() - if vim.env.TEST_NO_CLEANUP and vim.env.TEST_NO_CLEANUP:lower() == "true" then - print("[CLEANUP]: `TEST_NO_CLEANUP` was specified, not removing: " .. xdg_root) - else - print("[CLEANUP]: `TEST_NO_CLEANUP` not specified, removing " .. xdg_root) - vim.fn.delete(xdg_root:get(), "rf") - end - end, - }) - -- Empty the package path so we use only the plugins specified vim.opt.packpath = {} @@ -91,6 +119,9 @@ function M.setup(plugins) end end + -- Setup nvim-treesitter + M.setup_treesitter() + -- Ensure `nvim-ts-autotag` is registed on the runtimepath and set it up utils.rtp_register_ts_autotag() require("nvim-ts-autotag").setup({ @@ -99,6 +130,7 @@ function M.setup(plugins) enable_close = true, enable_close_on_slash = true, }) + print("[SETUP] Finished setting up minimal init") end @@ -107,5 +139,4 @@ M.setup({ ["popup.nvim"] = "https://github.com/nvim-lua/popup.nvim", ["nvim-treesitter"] = "https://github.com/nvim-treesitter/nvim-treesitter", ["playground"] = "https://github.com/nvim-treesitter/playground", - ["nvim-treesitter-rescript"] = "https://github.com/nkrkv/nvim-treesitter-rescript", }) diff --git a/tests/specs/close_slash_tag_spec.lua b/tests/specs/close_slash_tag_spec.lua index 9fa6b66..17cd315 100644 --- a/tests/specs/close_slash_tag_spec.lua +++ b/tests/specs/close_slash_tag_spec.lua @@ -1,9 +1,5 @@ local helpers = require("tests.utils.helpers") -helpers.setup_nvim_treesitter({ - highlight = { enable = true }, -}) - local data = { { name = "1 html close tag after inputting /", diff --git a/tests/specs/closetag_spec.lua b/tests/specs/closetag_spec.lua index 3212d45..1b954b9 100644 --- a/tests/specs/closetag_spec.lua +++ b/tests/specs/closetag_spec.lua @@ -1,9 +1,5 @@ local helpers = require("tests.utils.helpers") -helpers.setup_nvim_treesitter({ - highlight = { enable = true }, -}) - local data = { { name = "1 html close tag", diff --git a/tests/specs/renametag_spec.lua b/tests/specs/renametag_spec.lua index fc06da5..924fc76 100644 --- a/tests/specs/renametag_spec.lua +++ b/tests/specs/renametag_spec.lua @@ -1,12 +1,4 @@ local helpers = require("tests.utils.helpers") -helpers.setup_nvim_treesitter({ - - highlight = { - use_languagetree = false, - enable = true, - }, - fold = { enable = false }, -}) local data = { { diff --git a/tests/utils/helpers.lua b/tests/utils/helpers.lua index fdb173a..d86591e 100644 --- a/tests/utils/helpers.lua +++ b/tests/utils/helpers.lua @@ -35,25 +35,6 @@ M.feed = function(text, num) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(result, true, false, true), "x", true) end -M.setup_nvim_treesitter = function(opts) - opts = vim.tbl_deep_extend("keep", opts or {}, { - sync_install = true, - ensure_installed = { - "html", - "javascript", - "typescript", - "svelte", - "vue", - "tsx", - "php", - "glimmer", - "rescript", - "embedded_template", - }, - }) - require("nvim-treesitter.configs").setup(opts) -end - M.Test_filter = function(data) local run_data = {} for _, value in pairs(data) do