From 72813cffcd20e6b297301c76a932e787c7fce238 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 9 Aug 2023 16:08:24 +0200 Subject: [PATCH 1/1] object editing, press space to edit existing primitive node --- main2.c | 103 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 35 deletions(-) diff --git a/main2.c b/main2.c index 80f6a17..3dcbf84 100644 --- a/main2.c +++ b/main2.c @@ -34,6 +34,13 @@ isBackspace(char c) { } +// Key defines + +#define KEY_CTRL_C 3 +#define KEY_BACKSPACE1 8 +#define KEY_BACKSPACE2 127 + + // getch #ifdef _WIN32 @@ -188,6 +195,14 @@ JSONNodeRemove(JSONNode * node) { } } +bool +JSONNodeEditable(JSONNode * node) { + if (node == NULL) return false; + else if (node->kind == JSONNodeKind_Int) return true; + else if (node->kind == JSONNodeKind_Str) return true; + return false; +} + void Indent(int indent) { for (int i = 0; i < indent; i++) @@ -229,15 +244,11 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) { while (ptr != NULL) { Indent(indent); JSONNodePrint(ptr, currNode); + printf(": "); + JSONNodePrint(ptr->firstChild, currNode); + if (ptr->next != NULL) + printf(","); ptr = ptr->next; - - if (ptr != NULL) { - printf(": "); - JSONNodePrint(ptr, currNode); - if (ptr->next != NULL) - printf(","); - ptr = ptr->next; - } printf("\n"); } indent--; @@ -308,64 +319,86 @@ GetNode() { int c; int strLen = 0; + typedef enum { + InputState_Initial, + InputState_Primitive, + InputState_Object, + InputState_KeyValue, + InputState_Array, + } InputState; + InputState inputState; + bool editingNode = false; while (true) { c = GetChar(); - if (c == 3) + if (c == KEY_CTRL_C) break; if (isNewline(c)) { if (node == NULL || node->parent == NULL) break; - editingNode = false; - g_CurrNode = node = node->parent; + if (node->parent->kind == JSONNodeKind_Obj) // editing obj key + g_CurrNode = node = node->firstChild; + else if (node->parent->kind == JSONNodeKind_Str) // editing obj value + g_CurrNode = node = node->parent->parent; + else + g_CurrNode = node = node->parent; + + editingNode = JSONNodeEditable(node); continue; } - if (! editingNode) { + if (JSONNodeEditable(node) && editingNode) { + if (node->data == (size_t)NULL) { + node->data = (size_t)NEWARR(char, 16); + strLen = 0; + } + char * str = (char *)node->data; + + if (isBackspace(c)) { + str[strLen-1] = '\0'; + strLen--; + } + else if (strLen < 16 - 1) { + str[strLen] = c; + str[strLen+1] = '\0'; + strLen++; + } + } + else { JSONNode * old = node; /**/ if (c == 'i') { node = JSONNodeNew(JSONNodeKind_Int); editingNode = true; } else if (c == 's') { node = JSONNodeNew(JSONNodeKind_Str); editingNode = true; } else if (c == 'o') { node = JSONNodeNew(JSONNodeKind_Obj); } else if (c == 'a') { node = JSONNodeNew(JSONNodeKind_Arr); } - else if (isBackspace(c) && node != NULL) { - g_CurrNode = node = JSONNodeRemove(node); - continue; - } else if (c == 'h') { if (node->prev != NULL) g_CurrNode = node = node->prev; continue; } else if (c == 'l') { if (node->next != NULL) g_CurrNode = node = node->next; continue; } else if (c == 'k') { if (node->parent != NULL) g_CurrNode = node = node->parent; continue; } else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; continue; } + else if (c == ' ') { if (JSONNodeEditable(node)) editingNode = true; continue; } + else if (isBackspace(c) && node != NULL) { g_CurrNode = node = JSONNodeRemove(node); continue; } else { continue; } - g_CurrNode = node; - - if (old != NULL) + if (old != NULL) { + // new node was added to an object + if (old->kind == JSONNodeKind_Obj) { + JSONNode * keyNode = JSONNodeNew(JSONNodeKind_Str); + JSONNodePush(keyNode, node); + node = keyNode; + editingNode = true; + } JSONNodePush(old, node); - else + } + else { g_DrawNode = result = node; - } - else { - if (node->data == (size_t)NULL) { - node->data = (size_t)NEWARR(char, 16); - strLen = 0; } - char * str = (char *)node->data; - if (isBackspace(c)) { - str[strLen-1] = '\0'; - strLen--; - } - else if (strLen < 16 - 1) { - str[strLen] = c; - str[strLen+1] = '\0'; - strLen++; - } + g_CurrNode = node; } } -- 2.50.1