From c0b582567a724e9bc581a4bc78dc7dcfb38d1d78 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 20 Feb 2023 15:48:59 -0800 Subject: [PATCH] Handle completion of symbols with a prefix... You can type stuff and then tab and it will work. Before it would try to complete the whole line, which didn't work. --- implementations/C/try_rax.c | 41 +++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/implementations/C/try_rax.c b/implementations/C/try_rax.c index 9a1cc9a..9a1966f 100644 --- a/implementations/C/try_rax.c +++ b/implementations/C/try_rax.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "linenoise.h" #include "rax.h" @@ -11,19 +12,51 @@ rax *rt; #define INSERT(key) raxInsert(rt, (unsigned char*)(key), sizeof((key)), NULL, NULL); +int +index_of_last_symbol_char(const char *buf) +{ + int n = strlen(buf); + while (n) { + if (strchr(" []", buf[n - 1])) { + break; + } + n--; + } + return n; +} + + void completion(const char *buf, linenoiseCompletions *lc) { - int n = strnlen(buf, 1024); + char *prefix; + int n = index_of_last_symbol_char(buf); + int buffer_length = strlen(buf); + if (n) { + prefix = malloc(1024); /* Just assume 1k is enough for now... TODO: fix! */ + if (prefix == NULL) return; + memcpy(prefix, buf, n); + buf += n; + buffer_length -= n; + } raxIterator iter; raxStart(&iter, rt); - raxSeek(&iter, ">=", (unsigned char*)buf, n); + raxSeek(&iter, ">=", (unsigned char*)buf, buffer_length); while(raxNext(&iter)) { - if (strncmp((const char *)iter.key, buf, n)) + if (strncmp((const char *)iter.key, buf, buffer_length)) break; - linenoiseAddCompletion(lc, (const char *)iter.key); + if (n) { + prefix[n] = 0; + strlcat(prefix + n, (const char *)iter.key, 1024 - n); + linenoiseAddCompletion(lc, (const char *)prefix); + } else { + linenoiseAddCompletion(lc, (const char *)iter.key); + } } raxStop(&iter); + if (n) { + free(prefix); + } }