Skip to content

Commit

Permalink
feat: remove dependency on nvim-treesitter. Needed for `nvim-treesi…
Browse files Browse the repository at this point in the history
…tter` rewrite. Requires Neovim >= 0.9.4 (#171)
  • Loading branch information
folke authored May 18, 2024
1 parent 531f483 commit aeb7090
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
matrix:
include:
- os: ubuntu-20.04
url: https://github.com/neovim/neovim/releases/download/v0.7.0/nvim-linux64.tar.gz
url: https://github.com/neovim/neovim/releases/download/v0.9.5/nvim-linux64.tar.gz
manager: sudo apt-get
packages: -y fd-find
steps:
Expand Down Expand Up @@ -43,5 +43,6 @@ jobs:
run: |
export PATH="${PWD}/_neovim/bin:${PATH}"
export VIM="${PWD}/_neovim/share/nvim/runtime"
nvim --headless -u tests/minimal.vim -c "TSInstallSync html javascript typescript svelte vue tsx php glimmer rescript" -c "q"
# nvim --headless -u tests/minimal.vim -c "q"
nvim --headless -u tests/minimal.vim -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.vim', sequential = true}"
make test
19 changes: 11 additions & 8 deletions lua/nvim-ts-autotag.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ local internal = require("nvim-ts-autotag.internal")
local M = {}

function M.init()
require "nvim-treesitter".define_modules {
autotag = {
module_path = 'nvim-ts-autotag.internal',
is_supported = function(lang)
return internal.is_supported(lang)
end
}
}
local ts = require('nvim-treesitter')
if ts and ts.define_modules then
require('nvim-treesitter').define_modules({
autotag = {
module_path = 'nvim-ts-autotag.internal',
is_supported = function(lang)
return internal.is_supported(lang)
end,
},
})
end
end

function M.setup(opts)
Expand Down
33 changes: 19 additions & 14 deletions lua/nvim-ts-autotag/internal.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
local _, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
local configs = require("nvim-treesitter.configs")
local parsers = require("nvim-treesitter.parsers")
local log = require("nvim-ts-autotag._log")
local utils = require("nvim-ts-autotag.utils")

Expand Down Expand Up @@ -160,7 +157,7 @@ local setup_ts_tag = function()
end

local function is_in_template_tag()
local cursor_node = ts_utils.get_node_at_cursor()
local cursor_node = vim.treesitter.get_node()
if not cursor_node then
return false
end
Expand Down Expand Up @@ -248,7 +245,7 @@ local function get_tag_name(node)
end

local function find_tag_node(opt)
local target = opt.target or ts_utils.get_node_at_cursor()
local target = opt.target or vim.treesitter.get_node()
local tag_pattern = opt.tag_pattern
local name_tag_pattern = opt.name_tag_pattern
local skip_tag_pattern = opt.skip_tag_pattern
Expand Down Expand Up @@ -324,7 +321,7 @@ local function check_close_tag(close_slash_tag)

if close_slash_tag then
-- Find start node from non closed tag
local current = ts_utils.get_node_at_cursor()
local current = vim.treesitter.get_node()
-- log.debug(current)
target = find_start_tag(current)
-- log.debug(target)
Expand Down Expand Up @@ -367,7 +364,7 @@ local function check_close_tag(close_slash_tag)
end

M.close_tag = function()
local buf_parser = parsers.get_parser()
local buf_parser = vim.treesitter.get_parser()
if not buf_parser then
return
end
Expand All @@ -380,7 +377,7 @@ M.close_tag = function()
end

M.close_slash_tag = function()
local buf_parser = parsers.get_parser()
local buf_parser = vim.treesitter.get_parser()
if not buf_parser then
return
end
Expand Down Expand Up @@ -553,17 +550,25 @@ local is_before_word = is_before('%w', 1)
local is_before_arrow = is_before('<', 0)

M.rename_tag = function()
if is_before_word() and parsers.has_parser() then
parsers.get_parser():parse()
rename_start_tag()
rename_end_tag()
if is_before_word() then
local ok, parser = pcall(vim.treesitter.get_parser)
if ok then
parser:parse()
rename_start_tag()
rename_end_tag()
end
end
end

M.attach = function(bufnr, lang)
M.lang = lang
local config = configs.get_module("autotag")
M.setup(config)
local ok, configs = pcall(require, "nvim-treesitter.configs")
if ok then
local config = configs.get_module("autotag")
M.setup(config)
else
M.setup()
end

if is_in_table(M.tbl_filetypes, vim.bo.filetype) then
setup_ts_tag()
Expand Down
3 changes: 1 addition & 2 deletions lua/nvim-ts-autotag/utils.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
local log = require('nvim-ts-autotag._log')
local get_node_text = vim.treesitter.get_node_text or vim.treesitter.query.get_node_text or ts_utils.get_node_text
local get_node_text = vim.treesitter.get_node_text or vim.treesitter.query.get_node_text
local M = {}

M.get_node_text = function(node)
Expand Down
7 changes: 7 additions & 0 deletions tests/minimal.vim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ _G.__is_log=true
_G.ts_filetypes = {
'html', 'javascript', 'typescript', 'svelte', 'vue', 'tsx', 'php', 'glimmer', 'rescript', 'embedded_template'
}
require("nvim-treesitter.configs").setup({
ensure_installed = _G.ts_filetypes,
highlight = { enable = true },
sync_install = true
})
vim.treesitter.language.register('tsx', 'typescriptreact')
vim.treesitter.language.register('embedded_template', 'eruby')
require("plenary/busted")
vim.cmd[[luafile ./tests/test-utils.lua]]
require("nvim-ts-autotag").setup({
Expand Down

0 comments on commit aeb7090

Please sign in to comment.