pojokcodeid.nvim-lazy/lua/custom/plugins/nvimufo.lua

336 lines
12 KiB
Lua
Raw Normal View History

2024-04-12 16:38:44 +07:00
function TABLE_CONTAINS(tbl, x)
local found = false
for _, v in pairs(tbl) do
if v == x then
found = true
end
end
return found
end
2024-05-21 14:20:50 +07:00
local icons = vim.g.pcode_icons.folding
2024-04-18 07:17:54 +07:00
2024-03-28 12:17:30 +07:00
return {
2024-04-09 05:51:53 +07:00
"kevinhwang91/nvim-ufo",
dependencies = {
"kevinhwang91/promise-async",
{
"luukvbaal/statuscol.nvim",
config = function()
local builtin = require("statuscol.builtin")
require("statuscol").setup({
-- relculright = true,
-- segments = {
-- { text = { builtin.foldfunc }, click = "v:lua.ScFa" },
-- { text = { "%s" }, click = "v:lua.ScSa" },
-- { text = { builtin.lnumfunc, " " }, click = "v:lua.ScLa" },
-- },
setopt = true,
relculright = true,
segments = {
{ text = { "%s" }, click = "v:lua.ScSa" },
{
2024-04-12 07:56:50 +07:00
-- text = { builtin.foldfunc, " " },
text = { builtin.foldfunc },
2024-04-09 05:51:53 +07:00
condition = { builtin.not_empty, true, builtin.not_empty },
click = "v:lua.ScFa",
},
{ text = { builtin.lnumfunc, " " }, click = "v:lua.ScLa" },
2024-04-12 07:44:49 +07:00
-- { text = { builtin.lnumfunc }, click = "v:lua.ScLa" },
2024-04-09 05:51:53 +07:00
},
})
end,
},
},
2024-04-12 10:48:32 +07:00
enabled = true,
2024-04-09 05:51:53 +07:00
lazy = true,
-- event = "BufReadPost",
2024-04-18 07:17:54 +07:00
-- event = { "BufReadPost", "BufRead", "InsertEnter", "BufNewFile" },
event = "VeryLazy",
2024-04-09 05:51:53 +07:00
config = function()
vim.o.foldcolumn = "1" -- '0' is not bad
vim.o.foldlevel = 99 -- Using ufo provider need a large value, feel free to decrease the value
vim.o.foldlevelstart = 99
vim.o.foldenable = true
-- vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]
2024-04-11 22:12:20 +07:00
-- vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]
2024-04-12 07:09:29 +07:00
-- vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep:│,foldclose:]]
2024-04-11 22:12:20 +07:00
-- vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep:│,foldclose:]]
-- vim.o.fillchars = [[eob: ,fold: ,foldopen:󰛲,foldsep:│,foldclose:󰜄]]
2024-04-12 07:09:29 +07:00
-- vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep:│,foldclose:]]
2024-04-18 07:17:54 +07:00
-- vim.o.fillchars = [[eob: ,fold: ,foldopen:▾,foldsep:│,foldclose:▸]]
vim.opt.fillchars = {
vert = icons.vert,
fold = icons.fold,
eob = icons.eob,
diff = icons.diff,
msgsep = icons.msgsep,
foldopen = icons.foldopen,
foldsep = icons.foldsep,
foldclose = icons.foldclose,
}
2024-04-09 05:51:53 +07:00
-- these are "extra", change them as you like
vim.keymap.set("n", "zR", require("ufo").openAllFolds)
vim.keymap.set("n", "zM", require("ufo").closeAllFolds)
vim.cmd("highlight FoldColumn guifg=" .. vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID("Comment")), "fg"))
2024-04-11 22:12:20 +07:00
-- vim.cmd("highlight FoldColumn guifg=" .. vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID("IblIndent")), "fg"))
2024-04-09 05:51:53 +07:00
-- Option 3: treesitter as a main provider instead
-- Only depend on `nvim-treesitter/queries/filetype/folds.scm`,
-- performance and stability are better than `foldmethod=nvim_treesitter#foldexpr()`
2024-04-12 10:48:32 +07:00
2024-04-12 12:13:19 +07:00
--[[require("ufo").setup({
-- provider_selector = function(bufnr, filetype, buftype)
-- return { "treesitter", "indent" }
-- end,
2024-04-09 05:51:53 +07:00
open_fold_hl_timeout = 150,
2024-04-12 10:48:32 +07:00
close_fold_kinds_for_ft = {
default = { "imports", "comment" },
json = { "array" },
c = { "comment", "region" },
},
2024-04-09 05:51:53 +07:00
preview = {
win_config = {
border = { "", "", "", "", "", "", "", "" },
2024-04-12 12:13:19 +07:00
-- winhighlight = 'Normal:Normal',
-- winhighlight = 'IncSearch:Folded',
winhighlight = "Normal:UfoPreviewNormal,FloatBorder:UfoPreviewBorder,CursorLine:UfoPreviewCursorLine",
2024-04-09 05:51:53 +07:00
winblend = 0,
},
mappings = {
scrollU = "<C-u>",
scrollD = "<C-d>",
jumpTop = "[",
jumpBot = "]",
},
},
2024-04-12 12:13:19 +07:00
provider_selector = function(_, filetype)
return { "treesitter", "indent" }
2024-04-09 05:51:53 +07:00
end,
fold_virt_text_handler = function(virt_text, lnum, end_lnum, width, truncate)
local result = {}
local _end = end_lnum - 1
local final_text = vim.trim(vim.api.nvim_buf_get_text(0, _end, 0, _end, -1, {})[1])
local suffix = final_text:format(end_lnum - lnum)
local suffix_width = vim.fn.strdisplaywidth(suffix)
local target_width = width - suffix_width
local cur_width = 0
for _, chunk in ipairs(virt_text) do
local chunk_text = chunk[1]
local chunk_width = vim.fn.strdisplaywidth(chunk_text)
if target_width > cur_width + chunk_width then
table.insert(result, chunk)
else
chunk_text = truncate(chunk_text, target_width - cur_width)
local hl_group = chunk[2]
table.insert(result, { chunk_text, hl_group })
chunk_width = vim.fn.strdisplaywidth(chunk_text)
-- str width returned from truncate() may less than 2nd argument, need padding
if cur_width + chunk_width < target_width then
suffix = suffix .. (" "):rep(target_width - cur_width - chunk_width)
end
break
end
cur_width = cur_width + chunk_width
end
table.insert(result, { "", "NonText" })
2024-04-12 08:57:11 +07:00
if vim.bo.filetype ~= "json" then
table.insert(result, { suffix, "TSPunctBracket" })
end
2024-04-09 05:51:53 +07:00
return result
end,
2024-04-12 12:13:19 +07:00
})]]
--
-- start ini bagian code support comment dan import
local ftMap = {
vim = "indent",
python = { "indent" },
git = "",
2024-04-12 23:26:42 +07:00
-- javascriptreact = { "treesitter", "indent" },
-- typescriptreact = { "treesitter", "indent" },
2024-04-12 12:13:19 +07:00
}
2024-04-12 16:38:44 +07:00
local function customizeSelector(bufnr)
local function handleFallbackException(err, providerName)
if type(err) == "string" and err:match("UfoFallbackException") then
return require("ufo").getFolds(bufnr, providerName)
else
return require("promise").reject(err)
end
end
return require("ufo")
.getFolds(bufnr, "lsp")
:catch(function(err)
return handleFallbackException(err, "treesitter")
end)
:catch(function(err)
return handleFallbackException(err, "indent")
end)
end
2024-04-12 12:13:19 +07:00
require("ufo").setup({
open_fold_hl_timeout = 150,
close_fold_kinds_for_ft = {
2024-04-18 07:17:54 +07:00
-- default = { "imports", "comment" },
2024-04-12 16:38:44 +07:00
-- json = { "array" },
2024-04-18 07:17:54 +07:00
-- c = { "comment", "region" },
2024-04-12 12:13:19 +07:00
},
preview = {
win_config = {
border = { "", "", "", "", "", "", "", "" },
winhighlight = "Normal:Folded",
winblend = 0,
},
mappings = {
scrollU = "<C-u>",
scrollD = "<C-d>",
jumpTop = "[",
jumpBot = "]",
},
},
provider_selector = function(bufnr, filetype, buftype)
-- if you prefer treesitter provider rather than lsp,
2024-04-12 16:38:44 +07:00
-- return ftMap[filetype]
2024-04-12 12:13:19 +07:00
-- return ftMap[filetype] or {'treesitter', 'indent'}
2024-04-12 16:38:44 +07:00
return ftMap[filetype] or customizeSelector
2024-04-12 12:13:19 +07:00
-- refer to ./doc/example.lua for detail
end,
2024-04-12 16:38:44 +07:00
fold_virt_text_handler = function(virt_text, lnum, end_lnum, width, truncate)
local result = {}
2024-04-12 23:05:29 +07:00
local closed_fold_text = "comments ..." -- Teks yang ingin ditampilkan
local import_fold_text = "import ..." -- Teks yang ingin ditampilkan
2024-04-12 16:38:44 +07:00
local is_comment = false -- Variabel untuk mengecek apakah ini komentar
local is_import = false
2024-04-12 23:26:42 +07:00
local is_bracket = false
2024-04-12 16:38:44 +07:00
-- Memeriksa apakah baris awal dari fold adalah komentar
local start_line = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, false)[1]
2024-04-14 20:31:00 +07:00
-- cari comentar dengan awalan /* untuk generaal comment
if start_line:find("^%s*%/%*") then
2024-04-12 16:38:44 +07:00
is_comment = true
2024-04-14 20:31:00 +07:00
-- cara commentar dengan awalan <!-- untuk html
2024-04-12 16:38:44 +07:00
elseif start_line:find("^%s*<!--") then
is_comment = true
2024-04-14 20:31:00 +07:00
-- cari comentar dengan awalan -- untuk lua
2024-04-12 16:38:44 +07:00
elseif start_line:find("^%s*%-%-") then
is_comment = true
end
2024-04-14 20:31:00 +07:00
-- cek fold yang berawalan import
2024-04-12 16:38:44 +07:00
if start_line:find("^%s*import") then
is_import = true
end
2024-04-14 20:31:00 +07:00
-- cek fold dengan akhiran {
if start_line:find("%s*{%s*$") then
2024-04-12 23:26:42 +07:00
is_bracket = true
end
2024-04-12 16:38:44 +07:00
if is_comment then
local suffix = string.format(" %s ", closed_fold_text)
local target_width = width - vim.fn.strdisplaywidth(suffix)
local cur_width = 0
for _, chunk in ipairs(virt_text) do
local chunk_text = chunk[1]
local chunk_width = vim.fn.strdisplaywidth(chunk_text)
if target_width > cur_width + chunk_width then
table.insert(result, chunk)
else
chunk_text = truncate(chunk_text, target_width - cur_width)
local hl_group = chunk[2]
table.insert(result, { chunk_text, hl_group })
break
end
cur_width = cur_width + chunk_width
end
-- Menambahkan teks 'Comments ...' ke akhir baris yang dilipat
table.insert(result, { suffix, "NonText" })
elseif is_import then
2024-04-12 23:05:29 +07:00
--[[
2024-04-12 16:38:44 +07:00
local suffix = string.format(" %s ", import_fold_text)
local target_width = width - vim.fn.strdisplaywidth(suffix)
local cur_width = 0
for _, chunk in ipairs(virt_text) do
local chunk_text = chunk[1]
local chunk_width = vim.fn.strdisplaywidth(chunk_text)
if target_width > cur_width + chunk_width then
table.insert(result, chunk)
else
chunk_text = truncate(chunk_text, target_width - cur_width)
local hl_group = chunk[2]
table.insert(result, { chunk_text, hl_group })
break
end
cur_width = cur_width + chunk_width
end
-- Menambahkan teks 'Comments ...' ke akhir baris yang dilipat
table.insert(result, { suffix, "NonText" })
2024-04-12 23:05:29 +07:00
]]
--
local suffix = (" 󰁂 %d "):format(end_lnum - lnum)
local sufWidth = vim.fn.strdisplaywidth(suffix)
local targetWidth = width - sufWidth
local curWidth = 0
for _, chunk in ipairs(virt_text) do
local chunkText = chunk[1]
local chunkWidth = vim.fn.strdisplaywidth(chunkText)
if targetWidth > curWidth + chunkWidth then
table.insert(result, chunk)
else
chunkText = truncate(chunkText, targetWidth - curWidth)
local hlGroup = chunk[2]
table.insert(result, { chunkText, hlGroup })
chunkWidth = vim.fn.strdisplaywidth(chunkText)
-- str width returned from truncate() may less than 2nd argument, need padding
if curWidth + chunkWidth < targetWidth then
suffix = suffix .. (" "):rep(targetWidth - curWidth - chunkWidth)
end
break
end
curWidth = curWidth + chunkWidth
end
table.insert(result, { " import⋯ ", "NonText" })
table.insert(result, { suffix, "MoreMsg" })
2024-04-12 16:38:44 +07:00
else
-- Jika bukan komentar, tampilkan teks asli
2024-04-12 23:05:29 +07:00
-- for _, chunk in ipairs(virt_text) do
-- table.insert(result, chunk)
-- end
local _end = end_lnum - 1
local final_text = vim.trim(vim.api.nvim_buf_get_text(0, _end, 0, _end, -1, {})[1])
local suffix = final_text:format(end_lnum - lnum)
local suffix_width = vim.fn.strdisplaywidth(suffix)
local target_width = width - suffix_width
local cur_width = 0
2024-04-12 16:38:44 +07:00
for _, chunk in ipairs(virt_text) do
2024-04-12 23:05:29 +07:00
local chunk_text = chunk[1]
local chunk_width = vim.fn.strdisplaywidth(chunk_text)
if target_width > cur_width + chunk_width then
table.insert(result, chunk)
else
chunk_text = truncate(chunk_text, target_width - cur_width)
local hl_group = chunk[2]
table.insert(result, { chunk_text, hl_group })
chunk_width = vim.fn.strdisplaywidth(chunk_text)
-- str width returned from truncate() may less than 2nd argument, need padding
if cur_width + chunk_width < target_width then
suffix = suffix .. (" "):rep(target_width - cur_width - chunk_width)
end
break
end
cur_width = cur_width + chunk_width
2024-04-12 16:38:44 +07:00
end
table.insert(result, { "", "NonText" })
2024-04-12 23:05:29 +07:00
local l = { "javascriptreact", "typescriptreact" }
2024-04-12 23:26:42 +07:00
if TABLE_CONTAINS(l, vim.bo.filetype) and not is_bracket then
2024-04-12 23:05:29 +07:00
table.insert(result, { suffix, "TSPunctBracket" })
end
2024-04-12 16:38:44 +07:00
end
return result
end,
2024-04-09 05:51:53 +07:00
})
2024-04-12 10:48:32 +07:00
-- end bagian code support comment dan import
2024-04-09 05:51:53 +07:00
end,
2024-03-28 12:17:30 +07:00
}