poke-devel
[Top][All Lists]
Advanced

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

[PATCH] repl: Enable auto-completion for structs


From: Carlo Caione
Subject: [PATCH] repl: Enable auto-completion for structs
Date: Fri, 14 Feb 2020 10:34:58 +0100

Enable auto-completion for struct fields when a TAB is pressed after a
'.' appended to the struct name (struct.<TAB>).

Signed-off-by: Carlo Caione <address@hidden>

        * src/pk-repl.c (pk_complete_struct): new auto-complete function
---
 src/pk-repl.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/src/pk-repl.c b/src/pk-repl.c
index 574b9935..87660420 100644
--- a/src/pk-repl.c
+++ b/src/pk-repl.c
@@ -35,6 +35,80 @@
 #include <signal.h>
 #include <unistd.h>
 
+static char *
+pk_complete_struct (int *idx, const char *x, size_t len, int state)
+{
+  static pvm_val val;
+  char *base;
+
+  base = strndup (x, len - strlen (strrchr (x, '.')));
+
+  if (state == 0)
+    {
+      pkl_env compiler_env;
+      pkl_ast_node decl;
+      char *ecmd, *end, *root;
+      int ret, back, over;
+
+      root = strndup (x, len - strlen (strchr (x, '.')));
+
+      /* Check if the root object does exist otherwise when compiling the
+       * statement we get an ugly error message */
+      compiler_env = pkl_get_env (poke_compiler);
+      decl = pkl_env_lookup (compiler_env, root, &back, &over);
+      free (root);
+
+      if ((decl == NULL) || PKL_AST_DECL_KIND (decl) != PKL_AST_DECL_KIND_VAR)
+        goto out;
+
+      ecmd = xmalloc (strlen (base) + 2);
+      strcpy (ecmd, base);
+      strcat (ecmd, ";");
+
+      /* we need to compile the statement to expand nested structs (i.e. 
XX.YY.) */
+      ret = pkl_compile_statement (poke_compiler, ecmd, &end, &val);
+      free (ecmd);
+
+      if (!ret)
+        goto out;
+    }
+
+  if (PVM_IS_SCT (val))
+    {
+      size_t nfields;
+
+      nfields = PVM_VAL_ULONG (PVM_VAL_SCT_NFIELDS (val));
+
+      for (; (*idx) < nfields; (*idx)++)
+        {
+          char *field, *name;
+          pvm_val elem_name;
+
+          elem_name = PVM_VAL_SCT_FIELD_NAME (val, (*idx));
+          field = PVM_VAL_STR (elem_name);
+          name = xmalloc (strlen (base) + strlen (field) + 2);
+
+          strcpy (name, base);
+          strcat (name, ".");
+          strcat (name, field);
+
+          if (0 != strncmp (x, name, len))
+            {
+              free (name);
+              continue;
+            }
+
+          (*idx)++;
+          free (base);
+          return name;
+        }
+    }
+
+out:
+    free (base);
+    return NULL;
+}
+
 static char *
 poke_completion_function (const char *x, int state)
 {
@@ -55,6 +129,10 @@ poke_completion_function (const char *x, int state)
     }
 
   size_t len = strlen (x);
+
+  if ((x[0] != '.') && (strchr(x, '.') != NULL))
+    return pk_complete_struct (&idx, x, len, state);
+
   char *function_name;
   function_name = pkl_env_get_next_matching_decl (env, &iter, x, len);
   if (function_name)
-- 
2.20.1




reply via email to

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