geany-plugins icon indicating copy to clipboard operation
geany-plugins copied to clipboard

Vimode: Added folding support

Open scresto09 opened this issue 1 year ago • 1 comments

Supported commands are: za / zo / zc toggle / open / close fold on one level of folding zA / zO / zC toggle / open / close fold on all folding levels zR / zM open / close all folds

scresto09 avatar Apr 16 '24 10:04 scresto09

Looks good in general, but I think folding should work when the cursor is anywhere in the block, not just on the fold point (vim seems to work this way). I played with it a little and came up with something like this:

static gint prepare_fold(CmdParams *p)
{
	/* foldparent of the next line */
	gint line = SSM(p->sci, SCI_GETFOLDPARENT, p->line + 1, 0);

	if (p->line == line)
		;  /* we are already on the fold point line */
	else
	{
		/* foldparent of the current line */
		line = SSM(p->sci, SCI_GETFOLDPARENT, p->line, 0);
	}

	if (line != -1)
	{
		/* move the cursor on the visible line before the fold */
		gint pos = SSM(p->sci, SCI_POSITIONFROMLINE, line, 0);
		SET_POS_NOX(p->sci, pos, TRUE);
	}

	return line;
}


void cmd_toggle_fold(CmdContext *c, CmdParams *p)
{
	gint line = prepare_fold(p);
	if (line != -1)
		SSM(p->sci, SCI_FOLDLINE, (uptr_t) line, SC_FOLDACTION_TOGGLE);
}

If the code looks OK to you and works as expected, could you update all the folding functions similarly to cmd_toggle_fold() above?

In addition, these aren't exactly "edit" commands and should be placed in a separate file. Could you create fold.c/h, add them to Makefile.am and move all these commands there?

techee avatar Apr 25 '24 17:04 techee

If you are planning to continue working on this PR, there's one catch. Because of

https://github.com/geany/geany-plugins/commit/33984abc9bd24bbf03db3ac517eb45ca2c7e6b7f

you have to first move the cursor on the visible line before performing a fold - otherwise folding won't work because the above code will expand the fold (I experienced this problem myself when testing the code above and it took me a while to realize what was going on).

techee avatar May 21 '24 22:05 techee

OK, I tried to make some changes to improve fold support and react more like vim. With za/zo/zc we fold the current block as before. With zA we change the fold depending on the state of the parent, if there is one that is contracted we expand it otherwise we contract it. I was unable to reproduce the issue you are talking about with commit 33984ab, let me know if the issue persists. And another thing, sorry but I'm not sure I did the right things with git/merge, let me know if I need to fix anything on this. Thanks.

scresto09 avatar May 22 '24 09:05 scresto09

One more thing, the README file contains all the supported vi commands - would you copy the added commands from index.txt in the vimode directory into README?

techee avatar May 22 '24 22:05 techee

@scresto09 You seem to have pushed an unmodified branch and the pull request closed as a result. There are no commits in this pull request now.

techee avatar May 23 '24 13:05 techee

Sorry, I made a mistake and this closed this pull request...

I just created a new pull request #1350 with all these changes.. sorry!

scresto09 avatar May 24 '24 10:05 scresto09