Skip to content

Commit

Permalink
feat(autocmd): conceal html class attributes (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
n3wborn authored Nov 2, 2023
1 parent a27b49c commit 29bd058
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion lua/custom/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ vim.api.nvim_create_autocmd({ 'LspAttach' }, {

-- rename current symbol
if capabilities.renameProvider then
vim.keymap.set('n', '<leader>R', vim.lsp.buf.rename, { buffer = args.buf })
vim.keymap.set('n', '<leader>R', ':IncRename ', { buffer = args.buf })
end

-- show code actions available
Expand All @@ -146,3 +146,35 @@ vim.api.nvim_create_autocmd({ 'LspAttach' }, {
end
end,
})

vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost', 'TextChanged', 'InsertLeave' }, {
pattern = { '*.html', '*.php', '*.blade' },
callback = function()
local bufnr = vim.api.nvim_get_current_buf()
local conceal_ns = vim.api.nvim_create_namespace('class_conceal')

local language_tree = vim.treesitter.get_parser(bufnr, 'html')
local syntax_tree = language_tree:parse()
local root = syntax_tree[1]:root()

local query = vim.treesitter.query.parse(
'html',
[[
((attribute
(attribute_name) @att_name (#eq? @att_name "class")
(quoted_attribute_value (attribute_value) @class_value) (#set! @class_value conceal "…")))
]]
)

for _, captures, metadata in query:iter_matches(root, bufnr, root:start(), root:end_()) do
local start_row, start_col, end_row, end_col = captures[2]:range()
vim.api.nvim_buf_set_extmark(bufnr, conceal_ns, start_row, start_col, {
end_line = end_row,
end_col = end_col,
conceal = metadata[2].conceal,
})
end
end,
---Ref: https://gist.github.com/mactep/430449fd4f6365474bfa15df5c02d27b
desc = 'Conceal HTML class attributes. Ideal for big TailwindCSS class lists',
})

0 comments on commit 29bd058

Please sign in to comment.