Skip to content

Commit

Permalink
add plugin nvim-ufo
Browse files Browse the repository at this point in the history
Currently there are display issues that needs to be addressed in nvim
core, see kevinhwang91/nvim-ufo#4 .
  • Loading branch information
jdhao committed Aug 16, 2022
1 parent 64a7a23 commit 2096485
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/options.vim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
scriptencoding utf-8

" change fillchars for folding, vertical split, end of buffer, and message separator
set fillchars=fold:\ ,vert:\│,eob:\ ,msgsep:‾
set fillchars=fold:\ ,foldopen:,foldsep:\ ,foldclose:,vert:\│,eob:\ ,msgsep:‾

" Paste mode toggle, it seems that Nvim's bracketed paste mode
" does not work very well for nvim-qt, so we use good-old paste mode
Expand Down
5 changes: 5 additions & 0 deletions lua/config/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ local capabilities = lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
capabilities.textDocument.completion.completionItem.snippetSupport = true

capabilities.textDocument.foldingRange = {
dynamicRegistration = false,
lineFoldingOnly = true
}

local lspconfig = require("lspconfig")

if utils.executable('pylsp') then
Expand Down
41 changes: 41 additions & 0 deletions lua/config/nvim-ufo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
vim.o.foldcolumn = '1'
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

-- Using ufo provider need remap `zR` and `zM`. If Neovim is 0.6.1, remap yourself
vim.keymap.set('n', 'zR', require('ufo').openAllFolds)
vim.keymap.set('n', 'zM', require('ufo').closeAllFolds)

local handler = function(virtText, lnum, endLnum, width, truncate)
local newVirtText = {}
local suffix = ('  %d '):format(endLnum - lnum)
local sufWidth = vim.fn.strdisplaywidth(suffix)
local targetWidth = width - sufWidth
local curWidth = 0
for _, chunk in ipairs(virtText) do
local chunkText = chunk[1]
local chunkWidth = vim.fn.strdisplaywidth(chunkText)
if targetWidth > curWidth + chunkWidth then
table.insert(newVirtText, chunk)
else
chunkText = truncate(chunkText, targetWidth - curWidth)
local hlGroup = chunk[2]
table.insert(newVirtText, {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(newVirtText, {suffix, 'MoreMsg'})
return newVirtText
end

-- global handler
require('ufo').setup({
fold_virt_text_handler = handler
})
5 changes: 5 additions & 0 deletions lua/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ packer.startup({
use { 'ii14/emmylua-nvim', ft = 'lua' }

use { 'j-hui/fidget.nvim', after = 'nvim-lspconfig', config = [[require('config.fidget-nvim')]]}

-- quick fold
use {'kevinhwang91/nvim-ufo', after = 'nvim-lspconfig', config = [[require('config.nvim-ufo')]],
requires = 'kevinhwang91/promise-async'
}
end,
config = {
max_jobs = 16,
Expand Down

0 comments on commit 2096485

Please sign in to comment.