aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/lexer.c
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2025-05-02 10:44:18 +0300
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2025-05-02 11:59:31 +0300
commite15ffff6b808d2b60c08f2a4401ff35a3dc37bb7 (patch)
tree009756e985d3b1291bfeec4883bf85bfdd17b86b /src/lexer.c
parent62eb87b149166588d766d5a272e5232ecf86be5d (diff)
downloadsciteco-e15ffff6b808d2b60c08f2a4401ff35a3dc37bb7.tar.gz
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.
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c27
1 files changed, 26 insertions, 1 deletions
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++.