Skip to content

Commit

Permalink
Add buffer_characters
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jun 7, 2024
1 parent c1c13ef commit 6e45bfa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
9 changes: 4 additions & 5 deletions lua/fittencode/actions/identify_programming_language.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ local function _identify_current_buffer()
return
end

local count = 0
local lines = api.nvim_buf_get_lines(buffer, 0, -1, false)
vim.tbl_map(function(line)
count = count + #line
end, lines)
local count, lines = Base.buffer_characters(buffer)
if not count or not lines then
return
end
if count > Config.options.prompt.max_characters then
return
end
Expand Down
14 changes: 14 additions & 0 deletions lua/fittencode/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ function M.rfind(s, sub)
end)()
end

---@param buffer? number
function M.buffer_characters(buffer)
buffer = buffer or api.nvim_get_current_buf()
if not api.nvim_buf_is_valid(buffer) then
return
end
local count = 0
local lines = api.nvim_buf_get_lines(buffer, 0, -1, false)
vim.tbl_map(function(line)
count = count + #line
end, lines)
return count, lines
end

---@class NeovimVersion
---@field nvim string
---@field buildtype string
Expand Down
10 changes: 5 additions & 5 deletions lua/fittencode/prompt_providers/default.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local api = vim.api

local Base = require('fittencode.base')
local Config = require('fittencode.config')
local Log = require('fittencode.log')

Expand Down Expand Up @@ -37,11 +38,10 @@ function M:execute(ctx)
return
end

local count = 0
local lines = api.nvim_buf_get_lines(ctx.buffer, 0, -1, false)
vim.tbl_map(function(line)
count = count + #line
end, lines)
local count = Base.buffer_characters(ctx.buffer)
if not count then
return
end
if count > Config.options.prompt.max_characters then
return
end
Expand Down

0 comments on commit 6e45bfa

Please sign in to comment.