feat: ranged candidate replace

Adds candidate attribute `replace_start`, which allows a candidate
to replace a middle part of the original text.
This commit is contained in:
CismonX 2023-09-08 07:06:23 +08:00
parent a82e6ba002
commit 4e346fc097
Signed by: cismonx
GPG Key ID: 3094873E29A482FB
5 changed files with 19 additions and 7 deletions

View File

@ -39,6 +39,7 @@ type is defined as:
struct arif_cand {
char const *text;
int len;
int replace_start;
int replace_len;
char const *transform;
int transform_len;
@ -57,7 +58,8 @@ is its length in bytes.
Member
.I replace_len
is the number of leading bytes of the original text that are meant to
be replaced by the candidate.
be replaced by the candidate, starting from byte offset
.IR replace_start .
.PP
Should the original text passed to
.BR arif_query ()

View File

@ -227,13 +227,14 @@ copy_candidate (
char const *text = menu->candidates[candidate_idx].text;
int text_len = strlen(text);
char const *line = composition->preedit;
int line_len = strlen(line);
int line_len = composition->length;
char *buf = malloc(text_len + line_len);
assert(buf != NULL);
dest->text = memcpy(buf, text, text_len);
dest->len = text_len;
dest->replace_start = composition->sel_start;
dest->replace_len = composition->sel_end - composition->sel_start;
dest->transform = memcpy(buf + text_len, line, line_len);
dest->transform_len = line_len;

View File

@ -64,6 +64,7 @@ typedef int (arif_engine_query_func) (
struct arif_cand {
char const *text;
int len;
int replace_start;
int replace_len;
char const *transform;
int transform_len;

View File

@ -131,6 +131,7 @@ copy_candidate (
*dest = (struct arif_cand) {
.text = src->text,
.len = src->len,
.replace_start = src->replace_start,
.replace_len = src->replace_len,
.transform = src->transform,
.transform_len = src->transform_len,

View File

@ -87,12 +87,19 @@ arif_rl_complete(
char *match = malloc(sizeof(char) * (len + 1));
assert(match != NULL);
memcpy(match, text, skip);
memcpy(match + skip, cand->text, cand->len);
memcpy(match + skip + cand->len, line + cand->replace_len,
len - cand->len - skip);
match[len] = '\0';
comp_list[idx] = match;
comp_list[idx] = memcpy(match, text, skip);
int offset = skip;
memcpy(match + offset, line, cand->replace_start);
offset += cand->replace_start;
memcpy(match + offset, cand->text, cand->len);
offset += cand->len;
memcpy(match + offset,
line + cand->replace_start + cand->replace_len,
len - offset);
}
comp_list[num + 1] = NULL;