Skip to content

Commit

Permalink
chore: autoformat files using luaformatter
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgriffing committed Jul 11, 2021
1 parent 918bd17 commit dace687
Show file tree
Hide file tree
Showing 10 changed files with 463 additions and 551 deletions.
53 changes: 24 additions & 29 deletions lua/nvim-biscuits/config.lua
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
local config = {}

config.default_config = function ()
return {
min_distance = 5,
max_length = 80,
prefix_string = " // ",
-- on_events example: { "InsertLeave", "CursorHoldI" }
on_events = {},
trim_by_words = false,
}
config.default_config = function()
return {
min_distance = 5,
max_length = 80,
prefix_string = " // ",
-- on_events example: { "InsertLeave", "CursorHoldI" }
on_events = {},
trim_by_words = false
}
end


local function get_default_config(final_config, config_key)
if final_config == nil then
return config.default_config()[config_key]
end
if final_config == nil then return config.default_config()[config_key] end

return final_config[config_key]
return final_config[config_key]
end

config.get_language_config = function(final_config, language, config_key)

if final_config == nil then
return get_default_config(final_config, config_key)
end
if final_config == nil then
return get_default_config(final_config, config_key)
end

if final_config.language_config == nil then
return get_default_config(final_config, config_key)
end
if final_config.language_config == nil then
return get_default_config(final_config, config_key)
end

if final_config.language_config[language] == nil then
return get_default_config(final_config, config_key)
end
if final_config.language_config[language] == nil then
return get_default_config(final_config, config_key)
end

if final_config.language_config[language][config_key] == nil then
return get_default_config(final_config, config_key)
end
if final_config.language_config[language][config_key] == nil then
return get_default_config(final_config, config_key)
end

return final_config.language_config[language][config_key]
return final_config.language_config[language][config_key]

end



return config
8 changes: 3 additions & 5 deletions lua/nvim-biscuits/dev.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ local dev = {}

local debug_path = '~/vim-biscuits.log'

dev.console_log = function (the_string)
Path:new(debug_path):write(json.encode(the_string)..'\n', 'a')
dev.console_log = function(the_string)
Path:new(debug_path):write(json.encode(the_string) .. '\n', 'a')
end

dev.clear_log = function ()
Path:new(debug_path):write('', 'w')
end
dev.clear_log = function() Path:new(debug_path):write('', 'w') end

return dev
238 changes: 111 additions & 127 deletions lua/nvim-biscuits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,178 +6,162 @@ local languages = require("nvim-biscuits.languages")
local final_config = config.default_config()

local has_ts, _ = pcall(require, 'nvim-treesitter')
if not has_ts then
error("nvim-treesitter must be installed")
end
if not has_ts then error("nvim-treesitter must be installed") end

local ts_parsers = require('nvim-treesitter.parsers')
local ts_utils = require('nvim-treesitter.ts_utils')
local nvim_biscuits = {}

local make_biscuit_hl_group = function(lang)
return 'BiscuitColor' .. lang
end

nvim_biscuits.decorate_nodes = function (bufnr, lang)
if config.get_language_config(final_config, lang, "disabled")
then
return
end

utils.console_log("decorating nodes")

local parser = ts_parsers.get_parser(bufnr, lang)
local make_biscuit_hl_group = function(lang) return 'BiscuitColor' .. lang end

if parser == nil then
utils.console_log('no parser for for '..lang)
return
end
nvim_biscuits.decorate_nodes = function(bufnr, lang)
if config.get_language_config(final_config, lang, "disabled") then return end

local biscuit_highlight_group = make_biscuit_hl_group(lang)
local root = parser:parse()[1]:root()
utils.console_log("decorating nodes")

local nodes = ts_utils.get_named_children(root)
local children = {}
local has_nodes = true
local parser = ts_parsers.get_parser(bufnr, lang)

while has_nodes do
for index, node in ipairs(nodes) do
children = utils.merge_arrays(children, ts_utils.get_named_children(node))

local start_line, start_col, end_line, end_col = ts_utils.get_node_range(node)
-- local text = ts_utils.get_node_text(node)[1]
if parser == nil then
utils.console_log('no parser for for ' .. lang)
return
end

local lines = vim.api.nvim_buf_get_lines(bufnr, start_line, start_line+1, false)
local biscuit_highlight_group = make_biscuit_hl_group(lang)
local root = parser:parse()[1]:root()

local text = lines[1]
local nodes = ts_utils.get_named_children(root)
local children = {}
local has_nodes = true

text = utils.trim(text)
while has_nodes do
for index, node in ipairs(nodes) do
children = utils.merge_arrays(children,
ts_utils.get_named_children(node))

local should_decorate = true
local start_line, start_col, end_line, end_col =
ts_utils.get_node_range(node)
-- local text = ts_utils.get_node_text(node)[1]

if text == '' then
should_decorate = false
end
local lines = vim.api.nvim_buf_get_lines(bufnr, start_line,
start_line + 1, false)

if string.len(text) <= 1 then
should_decorate = false
end
local text = lines[1]

if start_line == end_line then
should_decorate = false
end
text = utils.trim(text)

if end_line - start_line < final_config.min_distance then
should_decorate = false
end
local should_decorate = true

if languages.should_decorate(lang, node, text, bufnr) == false then
should_decorate = false
end
if text == '' then should_decorate = false end

if should_decorate then
if string.len(text) <= 1 then should_decorate = false end

local trim_by_words = config.get_language_config(final_config, lang, "trim_by_words")
local max_length = config.get_language_config(final_config, lang, "max_length")
if start_line == end_line then should_decorate = false end

if trim_by_words == true then
local words = {}
for word in string.gmatch(text, "%w+") do
words[#words+1] = word
if #words >= max_length then
break
if end_line - start_line < final_config.min_distance then
should_decorate = false
end
end
text = table.concat(words, " ")
else
if string.len(text) >= max_length then
text = string.sub(text, 1, max_length)
text = text..'...'
end
end

text = text:gsub("\n", ' ')

local prefix_string = config.get_language_config(final_config, lang, "prefix_string")

-- language specific text filter
text = languages.transform_text(lang, node, text, bufnr)

if utils.trim(text) ~= '' then
text = prefix_string..text
if languages.should_decorate(lang, node, text, bufnr) == false then
should_decorate = false
end

vim.api.nvim_buf_clear_namespace(bufnr, 0, end_line, end_line + 1)
vim.api.nvim_buf_set_virtual_text(bufnr, 0, end_line, { {text, biscuit_highlight_group} }, {})
if should_decorate then

local trim_by_words = config.get_language_config(final_config,
lang,
"trim_by_words")
local max_length = config.get_language_config(final_config,
lang, "max_length")

if trim_by_words == true then
local words = {}
for word in string.gmatch(text, "%w+") do
words[#words + 1] = word
if #words >= max_length then
break
end
end
text = table.concat(words, " ")
else
if string.len(text) >= max_length then
text = string.sub(text, 1, max_length)
text = text .. '...'
end
end

text = text:gsub("\n", ' ')

local prefix_string = config.get_language_config(final_config,
lang,
"prefix_string")

-- language specific text filter
text = languages.transform_text(lang, node, text, bufnr)

if utils.trim(text) ~= '' then
text = prefix_string .. text

vim.api.nvim_buf_clear_namespace(bufnr, 0, end_line,
end_line + 1)
vim.api.nvim_buf_set_virtual_text(bufnr, 0, end_line, {
{text, biscuit_highlight_group}
}, {})
end
else
-- utils.console_log('empty')
end
end
else
-- utils.console_log('empty')
end
end

nodes = children
children = {}
nodes = children
children = {}

if table.getn(nodes) == 0 then
has_nodes = false
if table.getn(nodes) == 0 then has_nodes = false end
end
end
end

nvim_biscuits.setup = function (user_config)
final_config = utils.merge_tables(final_config, user_config)
nvim_biscuits.setup = function(user_config)
final_config = utils.merge_tables(final_config, user_config)

if user_config.default_config then
final_config = utils.merge_tables(final_config, user_config.default_config)
end
if user_config.default_config then
final_config = utils.merge_tables(final_config,
user_config.default_config)
end

utils.clear_log()
utils.clear_log()
end

local attached_buffers = {}
nvim_biscuits.BufferAttach = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
if attached_buffers[bufnr] then
return
end
bufnr = bufnr or vim.api.nvim_get_current_buf()
if attached_buffers[bufnr] then return end

attached_buffers[bufnr] = true

local lang = ts_parsers.get_buf_lang(bufnr)
local on_lines = function()
nvim_biscuits.decorate_nodes(bufnr, lang)
end
attached_buffers[bufnr] = true

local lang = ts_parsers.get_buf_lang(bufnr)
local on_lines = function() nvim_biscuits.decorate_nodes(bufnr, lang) end

vim.cmd("highlight default link " .. make_biscuit_hl_group(lang) .. " BiscuitColor")
vim.cmd("highlight default link " .. make_biscuit_hl_group(lang) ..
" BiscuitColor")

-- we need to fire once at the very start
nvim_biscuits.decorate_nodes(bufnr, lang)
-- we need to fire once at the very start
nvim_biscuits.decorate_nodes(bufnr, lang)

local on_events = table.concat(final_config.on_events, ',')
if on_events ~= "" then
vim.api.nvim_exec(
string.format([[
local on_events = table.concat(final_config.on_events, ',')
if on_events ~= "" then
vim.api.nvim_exec(string.format([[
augroup Biscuits
au!
au %s <buffer=%s> :lua require("nvim-biscuits").decorate_nodes(%s, "%s")
augroup END
]],
on_events,
bufnr,
bufnr,
lang
),
false
)
else
vim.api.nvim_buf_attach(bufnr, false, {
on_lines = on_lines,

on_detach = function()
attached_buffers[bufnr] = nil
end,
})
end
]], on_events, bufnr, bufnr, lang), false)
else
vim.api.nvim_buf_attach(bufnr, false,
{
on_lines = on_lines,

on_detach = function() attached_buffers[bufnr] = nil end
})
end
end

return nvim_biscuits
Loading

0 comments on commit dace687

Please sign in to comment.