kevinhwang91 / nvim-ufo

Not UFO in the sky, but an ultra fold in Neovim.
BSD 3-Clause "New" or "Revised" License
2.3k stars 47 forks source link

Export some default `fold_virt_text_handler` functions #170

Closed 9999years closed 11 months ago

9999years commented 11 months ago

Feature description

It feels a little clumsy to have to copy fold_virt_text_handler functions into my configuration. I also suspect that most users will want more or less the same behavior, so we can cover many needs by exporting a few from a module like ufo.virt_text_handlers.

Describe the solution you'd like

A new ufo.virt_text_handlers module containing some default virtual text handlers, like the one in the README.

Additional context

Thanks so much for this plugin, it works great!

Here's my fold_virt_text_handler function, which shows how many lines have been folded and then inserts subsequent lines until it runs out of room.

Screenshot of Neovim showing folded text with the folded text spilling over to the right in dark gray.
local more_msg_highlight = vim.api.nvim_get_hl_id_by_name("MoreMsg")
local non_text_highlight = vim.api.nvim_get_hl_id_by_name("NonText")

require("ufo").setup({
  fold_virt_text_handler = function(
    -- The start_line's text.
    virtual_text_chunks,
    -- Start and end lines of fold.
    start_line,
    end_line,
    -- Total text width.
    text_width,
    -- fun(str: string, width: number): string Trunctation function.
    truncate,
    -- Context for the fold.
    ctx
  )
    local line_delta = (" 󰁂 %d "):format(end_line - start_line)
    local remaining_width = text_width - vim.fn.strdisplaywidth(ctx.text) - vim.fn.strdisplaywidth(line_delta)
    table.insert(virtual_text_chunks, { line_delta, more_msg_highlight })
    local line = start_line
    while remaining_width > 0 and line < end_line do
      line = line + 1
      local line_text = vim.api.nvim_buf_get_lines(ctx.bufnr, line, line + 1, true)[1]
      line_text = " " .. vim.trim(line_text)
      local line_text_width = vim.fn.strdisplaywidth(line_text)
      if line_text_width <= remaining_width - 2 then
        remaining_width = remaining_width - line_text_width
      else
        line_text = truncate(line_text, remaining_width - 2) .. "…"
        remaining_width = remaining_width - vim.fn.strdisplaywidth(line_text)
      end
      table.insert(virtual_text_chunks, { line_text, non_text_highlight })
    end
    return virtual_text_chunks
  end,
})
kevinhwang91 commented 11 months ago

https://github.com/kevinhwang91/nvim-ufo/issues/99