Break up completion into simple and prefixed cases.

This commit is contained in:
Simon Forman 2023-02-20 16:04:12 -08:00
parent c0b582567a
commit eeb935fa07
1 changed files with 36 additions and 17 deletions

View File

@ -27,35 +27,54 @@ index_of_last_symbol_char(const char *buf)
void void
completion(const char *buf, linenoiseCompletions *lc) prefixed_completion(const char *buf, linenoiseCompletions *lc, int n)
{ {
char *prefix; raxIterator iter;
int n = index_of_last_symbol_char(buf); char *prefix = malloc(1024); /* Just assume 1k is enough for now... TODO: fix! */
if (NULL == prefix)
return;
int buffer_length = strlen(buf); int buffer_length = strlen(buf);
if (n) { memcpy(prefix, buf, n);
prefix = malloc(1024); /* Just assume 1k is enough for now... TODO: fix! */ buf += n;
if (prefix == NULL) return; buffer_length -= n;
memcpy(prefix, buf, n); raxStart(&iter, rt);
buf += n; raxSeek(&iter, ">=", (unsigned char*)buf, buffer_length);
buffer_length -= n; while(raxNext(&iter)) {
if (strncmp((const char *)iter.key, buf, buffer_length))
break;
prefix[n] = 0;
strlcat(prefix + n, (const char *)iter.key, 1024 - n);
linenoiseAddCompletion(lc, (const char *)prefix);
} }
raxStop(&iter);
free(prefix);
}
void
simple_completion(const char *buf, linenoiseCompletions *lc)
{
int buffer_length = strlen(buf);
raxIterator iter; raxIterator iter;
raxStart(&iter, rt); raxStart(&iter, rt);
raxSeek(&iter, ">=", (unsigned char*)buf, buffer_length); raxSeek(&iter, ">=", (unsigned char*)buf, buffer_length);
while(raxNext(&iter)) { while(raxNext(&iter)) {
if (strncmp((const char *)iter.key, buf, buffer_length)) if (strncmp((const char *)iter.key, buf, buffer_length))
break; break;
if (n) { linenoiseAddCompletion(lc, (const char *)iter.key);
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); raxStop(&iter);
}
void
completion(const char *buf, linenoiseCompletions *lc)
{
int n = index_of_last_symbol_char(buf);
if (n) { if (n) {
free(prefix); prefixed_completion(buf, lc, n);
} else {
simple_completion(buf, lc);
} }
} }