[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 4/4] Make readline completers a little more useful
From: |
Jose E. Marchesi |
Subject: |
Re: [PATCH 4/4] Make readline completers a little more useful |
Date: |
Thu, 14 Nov 2019 14:20:01 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) |
Hi John.
Overall the patch looks good.
struct pk_cmd file_cmd =
- {"file", "tf", "", 0, NULL, pk_cmd_file, "file (FILENAME|#ID)"};
+ {"file", "tf", "", 0, NULL, pk_cmd_file, "file (FILENAME|#ID)",
rl_filename_completion_function};
I like the idea of associating dot-commands with completers, but don't
you have to expand struct pk_cmd with a new pointer? I can't find that
in any of the patches you sent...
+extern struct pk_cmd *dot_cmds[];
+extern struct pk_cmd null_cmd;
+
+static char *
+poke_completion_function (const char *x, int state)
+{
+ static int idx = 0;
+ static struct pkl_ast_node_iter iter;
+ pkl_env env = pkl_get_env (poke_compiler);
+ if (state == 0)
+ {
+ pkl_env_iter_begin (env, &iter);
+ idx = 0;
+ }
+ else
+ {
+ if (pkl_env_iter_end (env, &iter))
+ idx++;
+ else
+ pkl_env_iter_next (env, &iter);
+ }
+
+ size_t len = strlen (x);
+
+ /* "Normal" commands. */
+ for (;;)
+ {
+ if (pkl_env_iter_end (env, &iter))
+ break;
+
+ pkl_ast_node decl_name = PKL_AST_DECL_NAME (iter.node);
+ const char *name = PKL_AST_IDENTIFIER_POINTER (decl_name);
+ if (0 != strncmp (name, x, len))
+ {
+ pkl_env_iter_next (env, &iter);
+ continue;
+ }
+ return strdup (name);
+ }
+
+ /* Dot commands */
+ for (;;)
+ {
+ struct pk_cmd **c = dot_cmds + idx;
+ if (*c == &null_cmd)
+ break;
+
+ char *name = xmalloc (strlen ( (*c)->name) + 1);
+ strcpy (name, ".");
+ strcat (name, (*c)->name);
+ if (0 != strncmp (name, x, len))
+ {
+ free (name);
+ idx++;
+ continue;
+ }
+ return name;
+ }
+
+ return NULL;
+}
What do you think about putting the completion logic for
compiler-dependent entities in the compiler? poke_completion_function
would call pkl_complete instead of looping through the declarations
itself.
My main concern is that I expect for the logic to become more complex
(include certain declarations but no others, etc) and I hesitate to put
that logic in pk-repl.c. My guts tell me the right place where to do
that is the compiler...