nvim-ufo icon indicating copy to clipboard operation
nvim-ufo copied to clipboard

Support for Vim's foldmarkers

Open Andy3153 opened this issue 2 years ago • 1 comments

Feature description

Currently, while using this plugin, you can't use the fold markers you created previously. I'd like to have the possibility to use Vim's basic fold method, the manual one using the fold markers.

Describe the solution you'd like

The solution I'm proposing is to just replicate the original Vim foldmarker support while also having the advanced functionality of this plugin available.

Additional context

Some helpful stuff:

  1. The currently set fold markers can be used inside Lua by reading the options (yes, they're stored inside an array):
local fmrOpen  = vim.opt.foldmarker:get()[1]
local fmrClose = vim.opt.foldmarker:get()[2]

This will read the foldmarkers that are set by anything: be it user configuration, a plugin, the modeline etc.

  1. Vim actually doesn't care if the foldmarkers are part of the code, or if they're inside comments (a good example is this), it just searches for the foldmarkers wherever it finds them in the buffer. This usually gets solved by using different foldmarkers, but, if you don't want this to happen at all and want to go the approach that foldmarkers should be only inside comments, you could also do some matching with the code's comment string, but this isn't perfect.
local commentString = vim.opt.commentstring:get()

Why isn't it perfect? Well, it renders pretty different results. Take a couple of examples (%s is a placeholder for the comment)

  • The commentstring for Lua is --%s (this example is expectable)
  • The commentstring for C/C++ is /*%s*/ (for C/C++, it's apparently a multi-line comment, even though there's a single-line comment too)
  • The commmentstring for Python is # %s (yes, with a space, even though Python doesn't require that space for comments)

So, using the commentstring isn't perfect, and it requires a bit more work. I just recommend going the Vim way and matching the foldmarkers wherever they appear in the text, I think it's the better way.

Andy3153 avatar Jun 21 '22 13:06 Andy3153

nvim-ufo can provide the marker provider, but it's not able to combine the fold ranges between two providers using general setup.

Must program yourself under provider_selector with require('ufo').getFolds API. https://github.com/kevinhwang91/nvim-ufo/blob/c412ff87165eb8712e2810799dfa90b728c40028/doc/example.lua#L20-L31

kevinhwang91 avatar Jun 21 '22 14:06 kevinhwang91

Look like #90 @msva is interested in this feature, close this issue because of duplicate.

kevinhwang91 avatar Oct 28 '22 02:10 kevinhwang91

@kevinhwang91

with require('ufo').getFolds API.

And is it any documentation about how to extend the result of call to that function with custom folds?

msva avatar Apr 26 '23 17:04 msva

@msva No, only source code https://github.com/kevinhwang91/nvim-ufo/blob/9e829d5cfa3de6a2ff561d86399772b0339ae49d/lua/ufo.lua#L109-L113

And search getFolds and read related code from doc/example.lua . You can also search getFolds from GitHub issue, I remember that someone has asked for info about this API.

kevinhwang91 avatar Apr 27 '23 00:04 kevinhwang91