Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marker provider not works #233

Open
shy-robin opened this issue Jul 13, 2024 · 4 comments
Open

Marker provider not works #233

shy-robin opened this issue Jul 13, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@shy-robin
Copy link

Neovim version (nvim -v | head -n1)

0.10.0

Operating system/version

macOS 14.5

How to reproduce the issue

I can't use za to fold the range between // #region xxx and // #endregion markers. It will prompt the error: Cannot find fold.

2024-07-13.21.09.19.mov

This is my lazyvim config:

-- Adding number suffix of folded lines instead of the default ellipsis
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

return {
  "kevinhwang91/nvim-ufo",
  dependencies = {
    "kevinhwang91/promise-async",
  },
  event = "BufRead",
  opts = {
    filetype_exclude = { "help", "alpha", "dashboard", "neo-tree", "Trouble", "lazy", "mason" },
  },
  keys = {
    {
      "zR",
      function()
        require("ufo").openAllFolds()
      end,
    },
    {
      "zM",
      function()
        require("ufo").closeAllFolds()
      end,
    },
    {
      "zr",
      function()
        require("ufo").openFoldsExceptKinds()
      end,
    },
    {
      "zm",
      function()
        require("ufo").closeFoldsWith()
      end,
    },
    -- 注意不要设置为 K,因为会被 LazyVim 设置的 K 覆盖
    -- TODO: conflict with coc
    {
      "gp",
      function()
        local winid = require("ufo").peekFoldedLinesUnderCursor()
        if not winid then
          vim.lsp.buf.hover()
        end
      end,
      desc = "Preview Fold",
    },
  },
  config = function()
    vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]
    -- 不需要设置,否则会有多余的间隙
    -- vim.o.foldcolumn = "1"
    vim.o.foldlevel = 99
    vim.o.foldlevelstart = 99
    vim.o.foldenable = true

    -- 清除折叠侧边栏所在列的高亮
    vim.api.nvim_set_hl(0, "FoldColumn", {})
    -- 高亮折叠的行
    vim.api.nvim_set_hl(0, "Folded", { bg = "#01579B" })

    require("ufo").setup({
      enable_get_fold_virt_text = true,
      open_fold_hl_timeout = 150,
      preview = {
        win_config = {
          border = { "", "", "", "", "", "", "", "" },
          winhighlight = "Normal:Folded",
          winblend = 0,
        },
        mappings = {
          -- TODO: conflict with coc
          -- scrollU = '<C-u>',
          -- scrollD = '<C-d>',
          jumpTop = "[",
          jumpBot = "]",
        },
      },
      fold_virt_text_handler = handler,
      provider_selector = function(bufnr, filetype, buftype)
        return { "treesitter", "marker" }
      end,
    })
  end,
}

Expected behavior

Fold the code between #region and #endregion like vscode.

Actual behavior

The code won't be folded and throw the error: cannot find fold.

@shy-robin shy-robin added the bug Something isn't working label Jul 13, 2024
@kevinhwang91
Copy link
Owner

kevinhwang91 commented Jul 13, 2024

marker is a fallback provider with your config. For now, the ranges return from marker provider are not merged with the main provider.

Will make it become default behavior after solving the performance issue for marker.

@dmmulroy
Copy link

timely - was just coming to ask about this - is #219 what I should be watching for updates?

@kevinhwang91
Copy link
Owner

Watching this issue is enough.

@kevinhwang91
Copy link
Owner

Neovim can't handle the cross ranges like:

local ranges = {
  {
    endLine = 5,
    kind = "comment",
    startLine = 3
  }, {
    endLine = 7,
    kind = "marker",
    startLine = 4
  }
}

The two ranges are distorted and become [3,5] and [43,7]. You can enter visual selection mode and type zf to create two folds manually to verify the issue. echo foldlevel('.') may get the level under the cursor.

I have no idea how to solve this cross ranges issue. Users should decide which kind is a high priority range and discard the low priority by themselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants