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

feat: add function for custom folds #88

Merged
merged 3 commits into from
Aug 16, 2023
Merged

Conversation

fecet
Copy link
Sponsor Contributor

@fecet fecet commented Aug 10, 2023

I think it would be great to make cells to be foldable 👀

image
fold:
image

Here is the setup for ufo(I note you also use this) https://github.com/fecet/nvim/blob/main/lua/modules/configs/editor/ufo.lua:

return function()
	local ufo = require("ufo")
	vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]
	vim.o.foldcolumn = "1" -- '0' is not bad
	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

	local function get_cell_folds(bufnr)
		local function handleFallbackException(err, providerName)
			if type(err) == "string" and err:match("UfoFallbackException") then
				return ufo.getFolds(bufnr, providerName)
			else
				return require("promise").reject(err)
			end
		end
		return ufo.getFolds(bufnr, "lsp")
			:catch(function(err)
				return handleFallbackException(err, "treesitter")
			end)
			:catch(function(err)
				return handleFallbackException(err, "indent")
			end)
			:thenCall(function(ufo_folds)
				local ok, jupynium = pcall(require, "jupynium")
				if ok then
					for _, fold in ipairs(jupynium.get_folds()) do
						table.insert(ufo_folds, fold)
					end
				end
				return ufo_folds
			end)
	end

	local ftMap = {
		python = get_cell_folds,
	}

	ufo.setup({
		provider_selector = function(bufnr, filetype, buftype)
			return ftMap[filetype]
		end,
	})
end

@kiyoon
Copy link
Owner

kiyoon commented Aug 10, 2023

I haven't tested this yet, but this looks good!

Is there any reason you set it as a draft?

@fecet
Copy link
Sponsor Contributor Author

fecet commented Aug 11, 2023

Given that this PR using cells.line_types_entire_buf(), there may be some performance concerns. For example, the default setting allow jupynium to be loaded for any python file, so this will scan entire buffer every edit for regular python file. To preempt any potentially disruptive changes (like line_types become a cached property), I set it to draft for now.

Additionally, the current implementation requires extra configurations in the UFO, which can be cumbersome. I'm seeking suggestions for a more streamlined way to offer this feature

@fecet
Copy link
Sponsor Contributor Author

fecet commented Aug 12, 2023

Is there any method to know wheter jupynium is start like require("jupynvim").is_start(), I tried require("jupynvim.highlighter").is_enabled() but it seems wasn't I want. Otherwise one should setup ufo like

			:thenCall(function(ufo_folds)
				local filename = vim.api.nvim_buf_get_name(bufnr)
				local ok, jupynium = pcall(require, "jupynium")
				if ok and string.match(filename, "%.ju.py$") then
					for _, fold in ipairs(jupynium.get_folds()) do
						table.insert(ufo_folds, fold)
					end
				end
				return ufo_folds

@kiyoon
Copy link
Owner

kiyoon commented Aug 12, 2023

In highlighter.lua, I also match the file name. To be consistent with your settings, you can use this condition.

if utils.list_wildcard_match(vim.fn.expand "%", options.opts.jupynium_file_pattern) then

@fecet fecet marked this pull request as ready for review August 12, 2023 12:35
@kiyoon
Copy link
Owner

kiyoon commented Aug 14, 2023

I just confirmed that it works very well. Can you mention this feature in README? It doesn't have to be long.

  1. Mention the feature and the function get_folds
  2. Mention that you can use plugins like nvim-ufo
  3. Refer to this PR, for an example setup. Don't write too long code in the README. Or maybe write the code in the README but make it expandable (click to see the code)
  4. Edit your main description of this PR to include the setup code, along with the link.

I think this much would be enough. Thanks a lot for your contribution!

@fecet
Copy link
Sponsor Contributor Author

fecet commented Aug 15, 2023

Sure, will do it later

@kiyoon kiyoon merged commit eba4b53 into kiyoon:master Aug 16, 2023
7 checks passed
@kiyoon
Copy link
Owner

kiyoon commented Aug 16, 2023

Thanks again for your contribution!

@fecet fecet deleted the feat/custom-folds branch August 16, 2023 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants