autocompletion update
This commit is contained in:
35
ml_util.cpp
35
ml_util.cpp
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user