Skip to content

Commit

Permalink
vimode: try to improve fold support
Browse files Browse the repository at this point in the history
  • Loading branch information
scresto09 committed May 22, 2024
1 parent c9e0413 commit c902da4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
59 changes: 39 additions & 20 deletions vimode/src/cmds/fold.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 Jiri Techet <techet@gmail.com>
* Copyright 2024 Sylvain Cresto <scresto@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,29 +19,47 @@
#include "cmds/fold.h"
#include "utils.h"

static gint prepare_fold(CmdParams *p, gboolean first_parent)
/* fold command does not depend on state of parents */
#define FOLD_NONE 0
/* fold command depend on state of parents */
#define FOLD_PARENT 1
/* fold command depend on state of contracted parent if it exists */
#define FOLD_CONTRACTED 2

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

if (p->line == line && first_parent == TRUE)
if (p->line == line)
; /* we are already on the fold point line */
else
{
gint parent = p->line;
/* foldparent of the current line */
while (1) {
line = SSM(p->sci, SCI_GETFOLDPARENT, parent, 0);
if (line == -1 || first_parent == TRUE) break;
parent = line;
line = SSM(p->sci, SCI_GETFOLDPARENT, p->line, 0);
}

/* retreive first parent when filter != FOLD_NONE
when filter == FOLD_CONTRACTED we stop on first contracted parent if exist
*/
if (filter == FOLD_CONTRACTED && line != -1 && ! SSM(p->sci, SCI_GETFOLDEXPANDED, line, 0))
; /* this fold point is contracted and filter == FOLD_CONTRACTED */
else if (filter != FOLD_NONE)
{
gint prev_line = line;
while (prev_line != -1)
{
prev_line = SSM(p->sci, SCI_GETFOLDPARENT, prev_line, 0);
if (prev_line != -1)
{
line = prev_line;
if (filter == FOLD_CONTRACTED && ! SSM(p->sci, SCI_GETFOLDEXPANDED, line, 0))
break;
}
}
if (first_parent == FALSE)
line = parent;
}


if (line != -1)
{
/* move the cursor on the visible line before the fold */
Expand All @@ -52,48 +70,49 @@ static gint prepare_fold(CmdParams *p, gboolean first_parent)
return line;
}


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


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


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


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


void cmd_open_fold_child(CmdContext *c, CmdParams *p)
{
gint line = prepare_fold(p, TRUE);
gint line = prepare_fold(p, FOLD_NONE);
SSM(p->sci, SCI_FOLDCHILDREN, (uptr_t) line, SC_FOLDACTION_EXPAND);
}


void cmd_close_fold_child(CmdContext *c, CmdParams *p)
{
gint line = prepare_fold(p, TRUE);
gint line = prepare_fold(p, FOLD_PARENT);
if (line != -1)
SSM(p->sci, SCI_FOLDCHILDREN, (uptr_t) line, SC_FOLDACTION_CONTRACT);
}
Expand Down
2 changes: 1 addition & 1 deletion vimode/src/cmds/fold.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 Jiri Techet <techet@gmail.com>
* Copyright 2024 Sylvain Cresto <scresto@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit c902da4

Please sign in to comment.