autocompletion update

This commit is contained in:
vaclavt
2022-03-01 20:29:18 +01:00
parent 830361050c
commit 9966c961d1
5 changed files with 23 additions and 31 deletions

View File

@@ -70,7 +70,6 @@ utils/local_install.sh
### TODO
- add test for >>> (range 1.5 (inc 5.5)) => (1.500000 2.500000 3.500000 4.500000 5.500000)
- ml_utils.cpp add all commands into const std::vector<std::string> commands
- unify -f and -run options
- add debug support, at least call stack
- multiline editing (see kilocpp editor)

14
ml.cpp
View File

@@ -2135,7 +2135,7 @@ bool MlEnvironment::has(const std::string &name) const {
std::map <const std::string, Builtin> builtin_funcs
std::map<const std::string, Builtin> builtin_funcs
{
// Special forms
std::make_pair("def", builtin::define),
@@ -2287,14 +2287,12 @@ MlValue MlEnvironment::get(const std::string &name) const {
}
// Get vector of executables in this scope
std::vector<std::string> MlEnvironment::get_lambdas_list() const {
std::vector<std::string> lambdas{128};
std::vector<std::string> MlEnvironment::get_lambdas_list(const std::string &token) const {
std::vector<std::string> lambdas{};
for (auto it = defs.begin(); it != defs.end(); it++) {
if (it->second.get_type_name() == FUNCTION_TYPE) {
for (auto it = defs.begin(); it != defs.end(); it++)
if (it->second.get_type_name() == FUNCTION_TYPE && (token.empty() || it->first.find(token) == 0))
lambdas.push_back(it->first);
}
}
return lambdas;
}
@@ -2306,7 +2304,7 @@ void repl(MlEnvironment &env) {
MlValue tmp;
std::vector<MlValue> parsed;
setup_linenoise(env);
setup_linenoise(env, builtin_funcs);
while (true) {
char *line = linenoise(">>> ");

2
ml.h
View File

@@ -50,7 +50,7 @@ public:
void setX(const std::string &name, const MlValue& value);
// Get vector of executables in this scope
std::vector<std::string> get_lambdas_list() const;
std::vector<std::string> get_lambdas_list(const std::string &token) const;
void combine(MlEnvironment const &other);

View File

@@ -3,19 +3,11 @@
#include <string>
#include <vector>
#include <algorithm>
#include <filesystem>
#include <iostream>
const std::vector<std::string> commands {
"eval", "type", "parse", "do", "if", "for", "while", "scope", "quote", "defn",
"def", "lambda", "benchmark", "=", "!=", ">", "<", ">=", "<=", "+", "-", "*", "/", "%",
"list", "insert", "index", "remove", "len", "push", "pop", "head", "tail", "first", "last",
"range", "map", "filter", "reduce", "exit", "quit", "print", "input", "random", "include",
"read-file", "write-file", "read-url", "system-cmd", "ls-dir", "is-file?", "is-dir?",
"parse-csv", "parse-json", "get-universal-time", "date-to-str", "str-to-date", "date-add", "debug",
"sprintf", "display", "string-replace", "string-regex?", "string-pad", "int", "float", "string",
"benchmark", "thread-create", "thread-under-lock", "thread-sleep", "threads-join", "try", "throw",
"usql"
};
std::vector<std::string> commands {};
std::string get_history_file_dir() {
std::string filename{".ml_history.txt"};
@@ -27,7 +19,7 @@ std::string get_history_file_dir() {
MlEnvironment * repl_env = nullptr;
void setup_linenoise(const MlEnvironment &env) {
void setup_linenoise(const MlEnvironment &env, const std::map<const std::string, Builtin> &builtins) {
repl_env = (MlEnvironment*) &env;
std::string history_file = get_history_file_dir();
@@ -37,6 +29,10 @@ void setup_linenoise(const MlEnvironment &env) {
linenoiseSetHintsCallback(hints);
linenoiseSetMultiLine(1);
linenoiseHistoryLoad(history_file.c_str());
commands.reserve(builtins.size());
for(auto it = builtins.begin(); it != builtins.end(); ++it)
commands.push_back(it->first);
}
void linenoise_line_read(char *line) {
@@ -68,15 +64,14 @@ void completion(const char *buf, linenoiseCompletions *lc) {
std::string token = str.substr(pos+1);
std::string begining = str.substr(0, pos+1);
// PERF optimize not to get all lambdas, but those beginning with token
std::vector<std::string> lambdas = repl_env->get_lambdas_list();
lambdas.insert(end(lambdas), begin(commands), end(commands));
auto suggestions = repl_env->get_lambdas_list(token);
std::copy_if(commands.begin(), commands.end(), std::back_inserter(suggestions),
[&token] (const std::string &cmd) { return token.empty() || cmd.find(token)==0; });
for (const auto & lambda : lambdas) {
if(lambda.find(token) == 0) {
std::string completion_string = begining + lambda;
linenoiseAddCompletion(lc, completion_string.c_str());
}
std::sort(suggestions.begin(), suggestions.end());
for (const auto &suggestion : suggestions) {
std::string completion = begining + suggestion;
linenoiseAddCompletion(lc, completion.c_str());
}
}
}

View File

@@ -4,7 +4,7 @@
#include "linenoise.h"
void setup_linenoise(const MlEnvironment &env);
void setup_linenoise(const MlEnvironment &env, const std::map<const std::string, Builtin> &builtins);
void linenoise_line_read(char *line);
void close_linenoise();