Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only handle elements in html files #23

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lua/nvim-biscuits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local make_biscuit_hl_group_name =
nvim_biscuits.decorate_nodes = function(bufnr, lang)
if config.get_language_config(final_config, lang, "disabled") then return end

utils.console_log("decorating nodes")
utils.console_log("decorating nodes for " .. lang)

local parser = ts_parsers.get_parser(bufnr, lang)

Expand Down Expand Up @@ -121,11 +121,10 @@ nvim_biscuits.decorate_nodes = function(bufnr, lang)
vim.api.nvim_buf_clear_namespace(bufnr,
biscuit_highlight_group,
end_line, end_line + 1)
vim.api.nvim_buf_set_extmark(bufnr, biscuit_highlight_group, end_line, 0, {
vim.api.nvim_buf_set_extmark(bufnr, biscuit_highlight_group,
end_line, 0, {
virt_text_pos = "eol",
virt_text = {
{text, biscuit_highlight_group_name}
},
virt_text = {{text, biscuit_highlight_group_name}},
hl_mode = "combine"
})
end
Expand Down
2 changes: 2 additions & 0 deletions lua/nvim-biscuits/languages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local html = require("nvim-biscuits.languages.html")
local vue = require("nvim-biscuits.languages.vue")
local javascript = require("nvim-biscuits.languages.javascript")
local python = require("nvim-biscuits.languages.python")
local utils = require("nvim-biscuits.utils")

local languages = {}

Expand All @@ -14,6 +15,7 @@ local handled_languages = {

languages.should_decorate = function(language_name, ts_node, text)
local language = handled_languages[language_name]

if language == nil then return true end

return language.should_decorate(ts_node, text)
Expand Down
26 changes: 24 additions & 2 deletions lua/nvim-biscuits/languages/html.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ language.should_decorate = function(ts_node, text, bufnr)

if text == end_string then return false end
end
return true

local elements = {
"element", "jsx_element", "self_closing_element",
"jsx_self_closing_element"
}

if utils.list_contains(elements, ts_node:type()) then return true end

return false
end

language.transform_text = function(ts_node, text, bufnr)

local end_string = get_node_last_line(ts_node)
end_string = string.gsub(end_string, '/', '')
end_string = string.gsub(end_string, '>', '')
Expand All @@ -42,7 +51,20 @@ language.transform_text = function(ts_node, text, bufnr)
text = string.gsub(text, '>', '')
text = string.gsub(text, ' ', ' ')

return utils.trim(text)
text = utils.trim(text)

if text == '<p className="p0"' then
local type = ts_node:type()
local parent = ts_node:parent()
utils.console_log("-----------------------------------------------")
utils.console_log("HTML type: " .. type)
utils.console_log("HTML Parent type: " .. parent:type())
utils.console_log("HTML text: " .. text)
-- utils.console_log("JS name: " .. ts_node:named())
-- utils.console_log("JS sexpr: " .. ts_node:sexpr())
utils.console_log("-----------------------------------------------")
end
return text
end

return language
18 changes: 17 additions & 1 deletion lua/nvim-biscuits/languages/javascript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ local html = require("nvim-biscuits.languages.html")
local language = {}

language.should_decorate = function(ts_node, text, bufnr)
local should_decorate = html.should_decorate(ts_node, text)
local type = ts_node:type()
local elements = {"jsx_element", "jsx_self_closing_element"}

local should_decorate = true
if utils.list_contains(elements, type) then
should_decorate = html.should_decorate(ts_node, text)
end

local ignored_element_types = {
-- "identifier"
"jsx_text"
}

if utils.list_contains(ignored_element_types, type) then
should_decorate = false
end

return should_decorate
end

Expand Down
8 changes: 8 additions & 0 deletions lua/nvim-biscuits/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ utils.trim = function(s)
return s:match '^()%s*$' and '' or s:match '^%s*(.*%S)'
end

utils.list_contains = function(list, needle)
for index, value in ipairs(list) do
if value == needle then return true; end
end

return false;
end

return utils