summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c31
1 files changed, 17 insertions, 14 deletions
diff --git a/main.c b/main.c
index eb7d03d..6a412fa 100644
--- a/main.c
+++ b/main.c
@@ -227,29 +227,19 @@ static inline int bracket_dir(u32 c) {
}
}
+/* TODO: reintroduce match_func_end_to-start */
int match_bracket(TxtLoc l, TxtLoc *out, u32 c) {
u32 depth = 1;
u32 o = bracket_opp(c);
if (txt_chr(l) != c) return 0;
int dir = bracket_dir(c);
if (!dir) return 0;
- int in_str = 0, in_chr = 0;
while (depth > 0) {
if (dir < 0 && at_start(l)) break;
if (dir > 0 && at_end(l)) break;
l = dir < 0 ? cprev(l) : cnext(l);
u32 x = txt_chr(l);
- int bs = (txt_chr(cprev(l)) == '\\') && (txt_chr(cprev(cprev(l))) != '\\');
- if (in_chr) {
- if (!bs && x == '\'') in_chr = 0;
- } else if (in_str) {
- if (!bs && x == '"') in_str = 0;
- } else {
- if (x == '\'') in_chr = 1;
- else if (x == '"') in_str = 1;
- else if (x == c) depth++;
- else if (x == o) depth--;
- }
+ depth += (x == c) - (x == o);
}
if (depth == 0) {
*out = l;
@@ -259,6 +249,17 @@ int match_bracket(TxtLoc l, TxtLoc *out, u32 c) {
}
}
+static inline TxtLoc inner_line_end(TxtLoc l) {
+ l = end_of_line(l);
+ if (txt_chr(cprev(l)) != '\n') l = cprev(l);
+ return l;
+}
+
+static inline int is_func_start(TxtLoc l) {
+ return txt_chr(inner_line_end(l)) == '{'
+ && !is_space(txt_chr(start_of_line(l)));
+}
+
static inline int is_func_end(TxtLoc l) {
TxtLoc n = cnext(l);
return txt_chr(l) == '}' && (txt_chr(n) == '\n' || at_end(n));
@@ -299,9 +300,11 @@ int empty_line(TxtLoc l) {
/* TODO: support having a newline before the open brace */
static inline int func_end_to_start(TxtLoc l, TxtLoc *out) {
- if (!match_bracket(l, &l, '}')) return 0;
- if (txt_chr(cprev(l)) == '\n')
+ while (!at_start(l) && !is_func_start(l))
+ l = prev_line_start(l);
+ if (txt_chr(start_of_line(l)) == '{')
l = start_of_line(cprev(l));
+ l = end_of_line(l);
*out = l;
return 1;
}