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

feat: add max file size config #49

Merged
merged 1 commit into from
Apr 14, 2024
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
Plug 'code-biscuits/nvim-biscuits'
call plug#end()
```

Using Packer:

```lua
Expand Down Expand Up @@ -174,6 +175,18 @@ require('nvim-biscuits').setup({
EOF
```

## Configuration (Max File Size)

You can set a maximum file size to bail out of biscuits if you think they are slowing down your editor. The value can be a string evaluating to a human readable file size or a number in bytes. Values are 1024 based. Supported case-insensitive suffixes: b, kb, kib, mb, mib, gb, gib, tb, tib, pb, pib

```lua
lua <<EOF
require('nvim-biscuits').setup({
max_file_size = '100kb'
})
EOF
```

## Supported Languages

We currently support all the languages supported in tree-sitter. Not all languages have specialized support, though most will probably need some.
Expand Down
42 changes: 42 additions & 0 deletions lua/nvim-biscuits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,42 @@ require('nvim-treesitter')
local utils = require("nvim-biscuits.utils")
local config = require("nvim-biscuits.config")
local languages = require("nvim-biscuits.languages")
local parse = require("vendor.parse")

local final_config = config.default_config()

local function is_file_too_big()
local file_size = vim.fn.wordcount().bytes

local max_file_size = final_config.max_file_size
if final_config.max_file_size == nil then
max_file_size = 0
end

local original_max_file_size = max_file_size

if type(max_file_size) == "string" then
max_file_size = parse.file_size(max_file_size)
else
return nil
end

if max_file_size == nil then
vim.notify("nvim-biscuits: max_file_size is invalid. Valid case-insensitive values include: b, kb, kib, mb, mib, gb, gib, tb, tib, pb, pib")
max_file_size = 0
end

return max_file_size > 0 and file_size > max_file_size
end

local has_ts, _ = pcall(require, 'nvim-treesitter')
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 = {should_render_biscuits = true}


local make_biscuit_hl_group_name =
function(lang)
return 'BiscuitColor' .. lang
Expand Down Expand Up @@ -59,20 +85,26 @@ nvim_biscuits.decorate_nodes = function(bufnr, lang)

local should_decorate = true

-- bail out of empty text
if text == '' then should_decorate = false end

-- bail out of short text
if string.len(text) <= 1 then should_decorate = false end

-- bail out if start line and end line are the same
if start_line == end_line then should_decorate = false end

-- bail out distance is less than minimum distance
if end_line - start_line < final_config.min_distance then
should_decorate = false
end

-- bail out if this node should not be decorated based on language specific filters
if languages.should_decorate(lang, node, text, bufnr) == false then
should_decorate = false
end

-- bail out if the user has cursor line only on and we are not on their cursor line
local cursor = vim.api.nvim_win_get_cursor(0)
local should_clear = false
if final_config.cursor_line_only and end_line + 1 ~= cursor[1] then
Expand Down Expand Up @@ -154,6 +186,11 @@ nvim_biscuits.BufferAttach = function(bufnr, lang)
{ noremap = false, desc = "toggle biscuits" })
end

if is_file_too_big() then
vim.notify_once("nvim-biscuits: File is larger than configured max_file_size")
return
end

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

if lang then
Expand Down Expand Up @@ -230,6 +267,11 @@ nvim_biscuits.setup = function(user_config)
end

nvim_biscuits.toggle_biscuits = function()
if is_file_too_big() then
vim.notify("nvim-biscuits: File is larger than configured max_file_size")
return
end

nvim_biscuits.should_render_biscuits =
not nvim_biscuits.should_render_biscuits
local bufnr = vim.api.nvim_get_current_buf()
Expand Down
8 changes: 7 additions & 1 deletion lua/nvim-biscuits/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ end

utils.clear_log = function() if is_debug_mode then Dev.clear_log() end end

utils.clone_table = function(_table)
local new_table = { }
for k, v in pairs(_table) do new_table[k] = v end
return setmetatable(new_table, getmetatable(_table))
end

utils.merge_arrays = function(a, b)
local result = {unpack(a)}
local result = utils.clone_table(a)
for i = 1, #b do result[#a + i] = b[i] end
return result
end
Expand Down
42 changes: 42 additions & 0 deletions lua/vendor/parse.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- Generated by Twitch viewer AusCryptor using OpanAI ChatGPT

local parse = {}

function parse_file_size(sizeStr)
-- Trim the string to avoid whitespace issues
local trimmed = sizeStr:lower():match("^%s*(.-)%s*$")

-- Match the numeric part and the unit
local num, unit = trimmed:match("^(%d+%.?%d*)%s*([kmgtpi].b?)$")
num = tonumber(num)

-- Return nil if there is no numeric part or unit
if not num or not unit then return nil end

-- Define the multipliers for each unit
local multipliers = {
b = 1,
kb = 1024,
kib = 1024,
mb = 1024 ^ 2,
mib = 1024 ^ 2,
gb = 1024 ^ 3,
gib = 1024 ^ 3,
tb = 1024 ^ 4,
tib = 1024 ^ 4,
pb = 1024 ^ 5,
pib = 1024 ^ 5 -- adding petabyte for completeness
}

-- Calculate the number of bytes
local multiplier = multipliers[unit]
if not multiplier then
return nil
end

return num * multiplier
end

parse.file_size = parse_file_size

return parse