poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[COMMITTED] poked: Remove auto-complete input channel and use Poke code


From: Mohammad-Reza Nabipoor
Subject: [COMMITTED] poked: Remove auto-complete input channel and use Poke code
Date: Tue, 22 Mar 2022 21:50:31 +0430

We don't need a standalone input channel for auto-completion.
Also changed the output protocol to include the auto-completion prefix.

2022-03-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * poked/poked.pk (plet_autocomplete): New function.
        (PLET_AUTOCMPL_IDENT): New constant.
        (PLET_AUTOCMPL_IOS): Likewise.
        (__poked_autocmpl_kind): New variable.
        (__poked_autocmpl_string): Likewise.
        (__poked_autocmpl_p): Likewise.
        (__poked_autocmpl_reset): New function.
        * poked/poked.c (bufb_append): Add missing const type qualifier.
        (poked_autocmpl_send_one): New function to send auto-completion
        for a string.
        (poked_autocmpl_send): New function to send all requested
        auto-completions.
        (poked_compile): Process auto-completions requested from Poke.
        Remove input channel for auto-completion.
---
 ChangeLog      |  17 +++++++
 poked/poked.c  | 134 +++++++++++++++++++++++++++----------------------
 poked/poked.pk |  24 +++++++++
 3 files changed, 114 insertions(+), 61 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index dba44a84..727355fb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2022-03-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * poked/poked.pk (plet_autocomplete): New function.
+       (PLET_AUTOCMPL_IDENT): New constant.
+       (PLET_AUTOCMPL_IOS): Likewise.
+       (__poked_autocmpl_kind): New variable.
+       (__poked_autocmpl_string): Likewise.
+       (__poked_autocmpl_p): Likewise.
+       (__poked_autocmpl_reset): New function.
+       * poked/poked.c (bufb_append): Add missing const type qualifier.
+       (poked_autocmpl_send_one): New function to send auto-completion
+       for a string.
+       (poked_autocmpl_send): New function to send all requested
+       auto-completions.
+       (poked_compile): Process auto-completions requested from Poke.
+       Remove input channel for auto-completion.
+
 2022-03-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
 
        * poked/usock.h (USOCK_CHAN_IN_AUTOCMPL): New macro.
diff --git a/poked/poked.c b/poked/poked.c
index e8a36fbe..34d2107c 100644
--- a/poked/poked.c
+++ b/poked/poked.c
@@ -81,6 +81,8 @@ termout_eval (void)
   termout_cmdkind = OUTCMD_EVAL;
 }
 
+//---
+
 static void *
 srvthread (void *data)
 {
@@ -90,6 +92,8 @@ srvthread (void *data)
   return NULL;
 }
 
+//---
+
 static void
 poked_buf_send_one (pk_val arr, pk_val pchan)
 {
@@ -128,6 +132,8 @@ poked_buf_send (void)
 
 #define poked_buf_send_one private_
 
+//---
+
 static void
 iteration_send (struct usock *srv, uint64_t n_iteration, int begin_p)
 {
@@ -190,7 +196,7 @@ bufb_realloc (struct bufb *b, size_t nelem)
 }
 
 static int
-bufb_append (struct bufb *b, void *data, size_t len)
+bufb_append (struct bufb *b, const void *data, size_t len)
 {
   size_t brem = b->end - b->cur;
 
@@ -219,6 +225,69 @@ bufb_free (struct bufb *b)
 
 //---
 
+static void
+poked_autocmpl_send_one (pk_val pkind, pk_val pstring)
+{
+  assert (pkind != PK_NULL);
+  assert (pstring != PK_NULL);
+
+  uint32_t kind = (uint32_t)pk_uint_value (pkind);
+  const char *string = pk_string_str (pstring);
+  struct bufb b;
+  char *candidate;
+
+  assert (kind == AUTOCMPL_IDENT || kind == AUTOCMPL_IOS);
+
+  if (bufb_init (&b, malloc (1024), 1024) != OK)
+    err (1, "bufb_init() failed");
+
+#define APPEND(str)                                                           \
+  do                                                                          \
+    {                                                                         \
+      if (bufb_append (&b, (str), strlen (str) + 1) != OK)                    \
+        err (1, "bufb_append() failed");                                      \
+    }                                                                         \
+  while (0)
+#define FUNC                                                                  \
+  (kind == AUTOCMPL_IDENT ? pk_completion_function                            \
+                          : pk_ios_completion_function)
+
+  APPEND (string);
+  if ((candidate = FUNC (pkc, string, 0)) != NULL)
+    {
+      APPEND (candidate);
+      while ((candidate = FUNC (pkc, string, 1)) != NULL)
+        APPEND (candidate);
+    }
+  usock_out (srv, USOCK_CHAN_OUT_AUTOCMPL, kind, b.mem, b.cur - b.mem);
+
+#undef FUNC
+#undef APPEND
+
+  bufb_free (&b);
+}
+
+static void
+poked_autocmpl_send (void)
+{
+  pk_val kind_arr = pk_decl_val (pkc, "__poked_autocmpl_kind");
+  pk_val string_arr = pk_decl_val (pkc, "__poked_autocmpl_string");
+  uint64_t nelem = pk_uint_value (pk_array_nelem (kind_arr));
+  pk_val exc;
+
+  assert (pk_uint_value (pk_array_nelem (string_arr)) == nelem);
+
+  for (uint64_t i = 0; i < nelem; ++i)
+    poked_autocmpl_send_one (pk_array_elem_value (kind_arr, i),
+                             pk_array_elem_value (string_arr, i));
+  (void)pk_call (pkc, pk_decl_val (pkc, "__poked_autocmpl_reset"), NULL, &exc,
+                 0);
+}
+
+#define poked_autocmpl_send_one private_
+
+//---
+
 static int
 poked_compile (const char *src, uint8_t chan, int *poked_restart_p,
                int *done_p)
@@ -278,6 +347,8 @@ poked_compile (const char *src, uint8_t chan, int 
*poked_restart_p,
       *done_p = 1;
       return ok;
     }
+  if (pk_int_value (pk_decl_val (pkc, "__poked_autocmpl_p")))
+    poked_autocmpl_send ();
   if (pk_int_value (pk_decl_val (pkc, "__vu_do_p")))
     {
       const char *filt = pk_string_str (pk_decl_val (pkc, "__vu_filter"));
@@ -435,7 +506,7 @@ poked_restart:
                   printf ("< '%.*s'\n", (int)srclen, src);
                 n_iteration++;
                 iteration_begin (srv, n_iteration);
-                poked_compile (src, chan, &poked_restart_p, &done_p);
+                (void)poked_compile (src, chan, &poked_restart_p, &done_p);
                 iteration_end (srv, n_iteration);
                 if (poked_restart_p)
                   {
@@ -450,65 +521,6 @@ poked_restart:
                   }
               }
               break;
-            case USOCK_CHAN_IN_AUTOCMPL:
-              {
-                struct bufb b;
-                char *candidate;
-                unsigned char *data;
-                size_t data_len;
-                unsigned char cmd;
-
-                data = usock_buf_data (inbuf, &data_len);
-                if (data_len == 0)
-                  {
-                    if (poked_options.debug_p)
-                      printf ("data_len == 0 in auto-completion channel\n");
-                    goto eol;
-                  }
-                if (bufb_init (&b, malloc (1024), 1024) != OK)
-                  err (1, "bufb_init() failed");
-
-#define BAPPEND(str)                                                          \
-  do                                                                          \
-    {                                                                         \
-      if (bufb_append (&b, (str), strlen (str) + 1) != OK)                    \
-        err (1, "bufb_append() failed");                                      \
-    }                                                                         \
-  while (0)
-
-                cmd = data[0];
-                if (cmd == AUTOCMPL_IDENT || cmd == AUTOCMPL_IOS)
-                  {
-                    const char *ident = (const char *)data + 1;
-                    size_t blen;
-
-#define FUNC                                                                  \
-  (cmd == AUTOCMPL_IDENT ? pk_completion_function : pk_ios_completion_function)
-
-                    if ((candidate = FUNC (pkc, ident, 0)) != NULL)
-                      {
-                        BAPPEND (candidate);
-                        while ((candidate = FUNC (pkc, ident, 1)) != NULL)
-                          BAPPEND (candidate);
-                      }
-                    blen = b.cur - b.mem;
-                    if (blen)
-                      usock_out (srv, USOCK_CHAN_OUT_AUTOCMPL, cmd, b.mem,
-                                 blen);
-                    else
-                      /* empty payload.  */
-                      usock_out (srv, USOCK_CHAN_OUT_AUTOCMPL, cmd, "", 1);
-#undef FUNC
-                  }
-                else
-                  {
-                    if (poked_options.debug_p)
-                      printf ("unknown completion command\n");
-                  }
-#undef BAPPEND
-                bufb_free (&b);
-              }
-              break;
             default:
               if (poked_options.debug_p)
                 printf ("unsupported input channel\n");
diff --git a/poked/poked.pk b/poked/poked.pk
index 2e4a3d15..18121bd3 100644
--- a/poked/poked.pk
+++ b/poked/poked.pk
@@ -391,6 +391,30 @@ fun plet_disasm = (string arch, offset<uint64,B> adr, 
byte[] code) void:
     close (fd);
   }
 
+//--- auto completion
+
+var __poked_autocmpl_kind = uint[] (),
+    __poked_autocmpl_string = string[] (),
+    __poked_autocmpl_p = 0;
+fun __poked_autocmpl_reset = void:
+  {
+    __poked_autocmpl_kind = uint[] ();
+    __poked_autocmpl_string = string[] ();
+    __poked_autocmpl_p = 0;
+  }
+
+var PLET_AUTOCMPL_IDENT = 1U, /* Identifier: variable, function, type.  */
+    PLET_AUTOCMPL_IOS   = 2U; /* IO Space.  */
+
+fun plet_autocomplete = (uint kind, string str) void:
+  {
+    assert (kind in [PLET_AUTOCMPL_IDENT, PLET_AUTOCMPL_IOS]);
+
+    apush (__poked_autocmpl_kind, kind);
+    apush (__poked_autocmpl_string, str);
+    __poked_autocmpl_p = 1;
+  }
+
 //---
 
 print "//---\nGNU poke in daemon mode!\n";
-- 
2.35.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]