Skip to content

Commit ccdba80

Browse files
committed
Replaced Lua code to calculate gutter width with built-in C function.
1 parent 5e1b21c commit ccdba80

File tree

1 file changed

+6
-42
lines changed

1 file changed

+6
-42
lines changed

lua/pretty-fold/init.lua

+6-42
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local ffi = require("ffi")
12
local wo = vim.wo
23
local fn = vim.fn
34
local api = vim.api
@@ -22,12 +23,6 @@ local default_config = {
2223
---@type string|boolean
2324
comment_signs = 'spaces',
2425

25-
-- We can't calculate precisely the current foldcolumn width so we
26-
-- take its maximum value from 'foldcolumn' option. But if it is
27-
-- set to 'auto' we have no digit to use. This value will be use in
28-
-- this case.
29-
foldcolumn = 3,
30-
3126
sections = {
3227
left = {
3328
'content',
@@ -69,38 +64,12 @@ local function fold_text(config)
6964
end
7065
end
7166

72-
-- Calculate widths of the number column.
73-
local num_col_width = math.max( #tostring(api.nvim_buf_line_count(0)),
74-
wo.numberwidth )
75-
76-
-- We can't calculate precisely the current foldcolumn width.
77-
-- So we assume it has the maximum value taken from 'foldcolumn' option ...
78-
local fold_col_width = wo.foldcolumn:match('%d+$') or config.foldcolumn
79-
80-
-- Calculate width of the signs column.
81-
local sign_col_width = 0
82-
local signcolumn = wo.signcolumn
83-
if signcolumn:match('^auto') or
84-
(signcolumn:match('^number') and not wo.number)
85-
then
86-
-- Calculate the maximum number of signes placed on any line
87-
-- in current buffer.
88-
local signs = vim.fn.sign_getplaced('%', { group = '*' })[1].signs
89-
local spl = {} -- signs per line
90-
for _, sign in ipairs(signs) do
91-
spl[sign.lnum] = (spl[sign.lnum] or 0) + 1
92-
end
93-
local max_spl = math.max( unpack(vim.tbl_values(spl)) or 0 )
67+
---The offset of a window, occupied by line number column,
68+
---fold column and sign column.
69+
---@type number
70+
local gutter_width = ffi.C.curwin_col_off()
9471

95-
signcolumn = signcolumn:match('%d+$') or math.huge
96-
sign_col_width = math.min(signcolumn, max_spl)
97-
elseif signcolumn:match('^yes') then
98-
sign_col_width = signcolumn:match('%d+$') or 1
99-
end
100-
sign_col_width = sign_col_width * 2
101-
102-
local visible_win_width =
103-
api.nvim_win_get_width(0) - num_col_width - fold_col_width - sign_col_width
72+
local visible_win_width = api.nvim_win_get_width(0) - gutter_width
10473

10574
-- Calculate the summation length of all the sections of the fold text string.
10675
local fold_text_len = 0
@@ -110,15 +79,10 @@ local function fold_text(config)
11079

11180
r.expansion_str = string.rep(config.fill_char, visible_win_width - fold_text_len)
11281

113-
-- ... but real foldcolumn doesn't always have its maximum value, so we need
114-
-- to close the gap between right section and the boder of the window.
115-
r.end_str = string.rep(config.fill_char, fold_col_width-1)
116-
11782
local result = ''
11883
for _, str in ipairs(r.left) do result = result .. str end
11984
result = result .. r.expansion_str
12085
for _, str in ipairs(r.right) do result = result .. str end
121-
result = result .. r.end_str
12286

12387
return result
12488
end

0 commit comments

Comments
 (0)