kevinhwang91 / nvim-ufo

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

Support for Vim's foldmarkers #22

Closed Andy3153 closed 1 year ago

Andy3153 commented 2 years ago

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)

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.

kevinhwang91 commented 2 years ago

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 commented 1 year ago

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

msva commented 1 year ago

@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?

kevinhwang91 commented 1 year ago

@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.