[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 3/6] bistromathic: properly compute the lcp, as expected by readl
From: |
Akim Demaille |
Subject: |
[PATCH 3/6] bistromathic: properly compute the lcp, as expected by readline |
Date: |
Tue, 3 Mar 2020 18:35:23 +0100 |
Currently completion on "at" proposes only "atan", but does not
actually complete "at" into "atan".
* examples/c/bistromathic/parse.y (completion): Install the lcp in
matches[0].
* examples/c/bistromathic/bistromathic.test: Check that case.
---
examples/c/bistromathic/bistromathic.test | 8 ++++++++
examples/c/bistromathic/parse.y | 17 +++++++++++++++--
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/examples/c/bistromathic/bistromathic.test
b/examples/c/bistromathic/bistromathic.test
index 0bec1cb3..a877bea3 100755
--- a/examples/c/bistromathic/bistromathic.test
+++ b/examples/c/bistromathic/bistromathic.test
@@ -85,6 +85,7 @@ run 0 '> 1 / 0
## Completion. ##
## ------------ ##
+# Check completion after an operator.
sed -e 's/\\t/ /g' >input <<EOF
(1+\t\t
EOF
@@ -94,3 +95,10 @@ run 0 '> (1+
atan exp sqrt
> (1+
> err: 1.4: syntax error: expected - or ( or double precision number or
> function or variable before end of file'
+
+# Check the completion of a word.
+sed -e 's/\\t/ /g' >input <<EOF
+(at\t\t
+EOF
+run 0 '> (atan ( ''
+> err: 1.9: syntax error: expected - or ( or double precision number or
function or variable before end of file'
diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y
index 16e327b0..468562b2 100644
--- a/examples/c/bistromathic/parse.y
+++ b/examples/c/bistromathic/parse.y
@@ -384,8 +384,7 @@ completion (const char *text, int start, int end)
const int len = strlen (text);
// Need initial prefix and final NULL.
char **matches = calloc (ntokens + symbol_count () + 2, sizeof *matches);
- int match = 0;
- matches[match++] = strdup (text);
+ int match = 1;
for (int i = 0; i < ntokens; ++i)
if (tokens[i] == YYTRANSLATE (TOK_VAR))
{
@@ -406,6 +405,20 @@ completion (const char *text, int start, int end)
matches[match++] = strdup (token);
}
+ // Find the longest common prefix, and install it in matches[0], as
+ // required by readline.
+ if (match == 1)
+ matches[0] = strdup (text);
+ else
+ {
+ int lcplen = strlen (matches[1]);
+ for (int i = 2; i < match && lcplen; ++i)
+ for (int j = 0; j < lcplen; ++j)
+ if (matches[1][j] != matches[i][j])
+ lcplen = j;
+ matches[0] = strndup (matches[1], lcplen);
+ }
+
if (yydebug)
{
fprintf (stderr, "completion(\"%.*s[%.*s]%s\") = ",
--
2.25.1
- [PATCH 0/6] Address some limitations in the bistro example, Akim Demaille, 2020/03/03
- [PATCH 1/6] bistromathic: check completion, Akim Demaille, 2020/03/03
- [PATCH 2/6] bistromathic: don't require spaces after operators for completion, Akim Demaille, 2020/03/03
- [PATCH 3/6] bistromathic: properly compute the lcp, as expected by readline,
Akim Demaille <=
- [PATCH 4/6] style: formatting changes, Akim Demaille, 2020/03/03
- [PATCH 6/6] yacc.c: push: undefine the pstate macros for the epilogue, Akim Demaille, 2020/03/03
- [PATCH 5/6] yacc.c: push: initialize the pstate variables in pstate_new, Akim Demaille, 2020/03/03