From 15f038aa855414156ab7df6b439c5518ff3bfe86 Mon Sep 17 00:00:00 2001 From: Heath123 Date: Sun, 29 Jan 2023 14:36:12 +0000 Subject: [PATCH] Better comments, again (again) --- src/node.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/node.c b/src/node.c index 42207d4..0bb2395 100644 --- a/src/node.c +++ b/src/node.c @@ -20,9 +20,12 @@ enum cursorMoveResult TeX_node_cursor_action(struct TeX_Node *node, struct editContext * context, enum cursorAction action) { + // Text + // TODO: Store the length of the text in the node + // THat way we won't have to call strlen every time if (node->type == 0) { - // Text + // If this isn't where the cursor is, continue the recursive search if (!context->elementIsText || context->cursorText != node) return CURSOR_NOT_HERE; @@ -34,7 +37,14 @@ enum cursorMoveResult TeX_node_cursor_action(struct TeX_Node *node, struct editC context->offset--; return SUCCESS; } - if (action == CURSOR_MOVE_RIGHT) { + else if (action == CURSOR_MOVE_RIGHT) { + // We take away 2 because offset 0 is actually one character into the text, + // and the last possible offset is one character before the end of the text, + // because if the cursor is at the start or end it goes in the parent flow. + + // TODO: It would make more sense to still have offset 0 be the first character, + // but just make sure that offset is never actually set to 0 before the cursor + // returns to the parent flow. if (context->offset >= strlen(node->text) - 2) return CURSOR_PAST_END; @@ -42,35 +52,43 @@ enum cursorMoveResult TeX_node_cursor_action(struct TeX_Node *node, struct editC return SUCCESS; } } - if (node->type > 1) + // Regular nodes + else if (node->type > 1) { + // If this isn't where the cursor is, continue the recursive search if (node->args[0] == NULL) return CURSOR_NOT_HERE; + // Propagate the cursor action to the first child flow int returnCode = TeX_flow_cursor_action(node->args[0], context, action); if (returnCode == SUCCESS) return returnCode; - if (returnCode == CURSOR_PAST_END) + else if (returnCode == CURSOR_PAST_END) { - // Go to the denominator + // If it moved past the end of the first child, move to the second child + + // If there is no second child, leave the node if (node->args[1] == NULL) return CURSOR_PAST_END; - + + // Update the cursor to be in the right place context->elementIsText = false; context->cursorFlow = node->args[1]; context->offset = 0; return SUCCESS; } - if (returnCode != CURSOR_NOT_HERE) + else if (returnCode != CURSOR_NOT_HERE) return returnCode; if (node->args[1] == NULL) return CURSOR_NOT_HERE; + // Propagate the cursor action to the second child flow returnCode = TeX_flow_cursor_action(node->args[1], context, action); if (returnCode == CURSOR_PAST_START) { - // Go to the numerator + // If it moved past the start of the second child, move to the first child + // The first child should always exist if the second child exists so we don't need to check context->elementIsText = false; context->cursorFlow = node->args[0]; context->offset = node->args[0]->elements;