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(fold): colorful foldtext #23471

Closed
wants to merge 2 commits into from
Closed

Conversation

lewis6991
Copy link
Member

@lewis6991 lewis6991 commented May 4, 2023

If 'foldtext' begins with =, it will be evaluated as a Lua expression. This Lua expression can be a list of text-hl tuples (just like virtual text).

function FoldText()
  return { { 'hello', 'Title'}, {''..vim.v.foldstart, 'Error' }}
end

vim.o.foldtext='=FoldText()'

image

Closes #12649

@github-actions github-actions bot added the folds label May 4, 2023
@lewis6991 lewis6991 changed the title feat/hlfold2 feat(fold): colorful foldtext May 4, 2023
@lewis6991 lewis6991 force-pushed the feat/hlfold2 branch 5 times, most recently from 32a7cd0 to a88bc23 Compare May 4, 2023 11:00
@haolian9

This comment was marked as off-topic.

@lewis6991
Copy link
Member Author

hi, may i ask why it's vim.o.foldtext='=FoldText()' instead of vim.o.foldtext='%!v:lua.FoldText()'? the latter form is used by &statusline and &tabline.

This would need to go through typval conversion and the code for parsing virtual text is implemented using API objects.

So supporting vimscript would be more work and slower.

@zeertzjq
Copy link
Member

Allowing only one option to use a Lua expression is quite strange. It's better to support that for all '*expr' options, like how the following two Vim patches improve the performance of calling a simple function:

@@ -1695,6 +1697,27 @@ static void foldDelMarker(buf_T *buf, linenr_T lnum, char *marker, size_t marker
}
}

static char *get_foldtext_lua(char *fdt, char *buf, VirtText *vt, int *vtwidth)
{
int len = snprintf(buf, FOLD_TEXT_LEN, "return %s", fdt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int len = snprintf(buf, FOLD_TEXT_LEN, "return %s", fdt);
int len = snprintf(buf, FOLD_TEXT_LEN, "return (%s)", fdt);


if (strncmp(wp->w_p_fdt, "=", 1) == 0) {
// evaluate as lua expression
text = get_foldtext_lua(wp->w_p_fdt + 1, buf, vt, vtwidth);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs textlock and maybe sandbox

@lewis6991
Copy link
Member Author

Your right. Let's go with your way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Highlighting Folded
3 participants