From e15ffff6b808d2b60c08f2a4401ff35a3dc37bb7 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Fri, 2 May 2025 10:44:18 +0300 Subject: implemented folding for the SciTECO lexer * This currently folds only {...} string arguments and embedded braces, most prominently `@^Um{...}` macro definitions.. * Any additional folding for loops and IF-statements should rely on book keeping by the main parser. This would also help catch syntactic errors, like dangling IFs. * We do keep track of the loop nesting, but currently only in execution mode. * It cannot be disabled via the "fold" property. Lexers in the container do not have properties. --- src/lexer.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lexer.c b/src/lexer.c index 5e6202d..6bc696f 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -126,13 +126,38 @@ teco_lexer_step(teco_view_t *view, teco_machine_main_t *machine, machine->macro_pc = g_utf8_next_char(macro+machine->macro_pc) - macro; gunichar escape_char = machine->expectstring.machine.escape_char; + guint fold_level = SC_FOLDLEVELBASE+machine->expectstring.nesting-1+ + (escape_char == '{' ? 1 : 0); + style = teco_lexer_getstyle(view, machine, chr); + /* + * Apply folding. This currently folds only {...} string arguments + * and all its embedded braces. + * We could fold loops and IF-statements as well, but that would + * require manually keeping track of the nesting in parse-only mode, + * which should better be in the parser itself. + * + * FIXME: You cannot practically disable folding via properties. + */ + if (teco_view_ssm(view, SCI_GETPROPERTYINT, (uptr_t)"fold", TRUE)) { + guint next_fold_level = SC_FOLDLEVELBASE+machine->expectstring.nesting-1+ + (machine->expectstring.machine.escape_char == '{' ? 1 : 0); + + if (next_fold_level > fold_level) + /* `chr` opened a {...} string argument */ + teco_view_ssm(view, SCI_SETFOLDLEVEL, *cur_line, + fold_level | SC_FOLDLEVELHEADERFLAG); + else if (!*cur_col) + teco_view_ssm(view, SCI_SETFOLDLEVEL, *cur_line, fold_level); + } + /* * Optionally style @^Uq{ ... } contents like macro definitions. * The curly braces will be styled like regular commands. * - * FIXME: This will not work with nested macro definitions. + * FIXME: This works only for top-level macro definitions, + * not for embedded definitions. * FIXME: This cannot currently be disabled, not even with SCI_SETPROPERTY. * We could only map it to an ED flag or * rewrite the lexer against the ILexer5 interface, which requires C++. -- cgit v1.2.3