autocompletion update
This commit is contained in:
@@ -70,7 +70,6 @@ utils/local_install.sh
|
|||||||
|
|
||||||
### TODO
|
### TODO
|
||||||
- add test for >>> (range 1.5 (inc 5.5)) => (1.500000 2.500000 3.500000 4.500000 5.500000)
|
- 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
|
- unify -f and -run options
|
||||||
- add debug support, at least call stack
|
- add debug support, at least call stack
|
||||||
- multiline editing (see kilocpp editor)
|
- multiline editing (see kilocpp editor)
|
||||||
|
|||||||
14
ml.cpp
14
ml.cpp
@@ -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
|
// Special forms
|
||||||
std::make_pair("def", builtin::define),
|
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
|
// Get vector of executables in this scope
|
||||||
std::vector<std::string> MlEnvironment::get_lambdas_list() const {
|
std::vector<std::string> MlEnvironment::get_lambdas_list(const std::string &token) const {
|
||||||
std::vector<std::string> lambdas{128};
|
std::vector<std::string> lambdas{};
|
||||||
|
|
||||||
for (auto it = defs.begin(); it != defs.end(); it++) {
|
for (auto it = defs.begin(); it != defs.end(); it++)
|
||||||
if (it->second.get_type_name() == FUNCTION_TYPE) {
|
if (it->second.get_type_name() == FUNCTION_TYPE && (token.empty() || it->first.find(token) == 0))
|
||||||
lambdas.push_back(it->first);
|
lambdas.push_back(it->first);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return lambdas;
|
return lambdas;
|
||||||
}
|
}
|
||||||
@@ -2306,7 +2304,7 @@ void repl(MlEnvironment &env) {
|
|||||||
MlValue tmp;
|
MlValue tmp;
|
||||||
std::vector<MlValue> parsed;
|
std::vector<MlValue> parsed;
|
||||||
|
|
||||||
setup_linenoise(env);
|
setup_linenoise(env, builtin_funcs);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
char *line = linenoise(">>> ");
|
char *line = linenoise(">>> ");
|
||||||
|
|||||||
2
ml.h
2
ml.h
@@ -50,7 +50,7 @@ public:
|
|||||||
void setX(const std::string &name, const MlValue& value);
|
void setX(const std::string &name, const MlValue& value);
|
||||||
|
|
||||||
// Get vector of executables in this scope
|
// 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);
|
void combine(MlEnvironment const &other);
|
||||||
|
|
||||||
|
|||||||
35
ml_util.cpp
35
ml_util.cpp
@@ -3,19 +3,11 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
const std::vector<std::string> commands {
|
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::string get_history_file_dir() {
|
std::string get_history_file_dir() {
|
||||||
std::string filename{".ml_history.txt"};
|
std::string filename{".ml_history.txt"};
|
||||||
@@ -27,7 +19,7 @@ std::string get_history_file_dir() {
|
|||||||
|
|
||||||
MlEnvironment * repl_env = nullptr;
|
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;
|
repl_env = (MlEnvironment*) &env;
|
||||||
|
|
||||||
std::string history_file = get_history_file_dir();
|
std::string history_file = get_history_file_dir();
|
||||||
@@ -37,6 +29,10 @@ void setup_linenoise(const MlEnvironment &env) {
|
|||||||
linenoiseSetHintsCallback(hints);
|
linenoiseSetHintsCallback(hints);
|
||||||
linenoiseSetMultiLine(1);
|
linenoiseSetMultiLine(1);
|
||||||
linenoiseHistoryLoad(history_file.c_str());
|
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) {
|
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 token = str.substr(pos+1);
|
||||||
std::string begining = str.substr(0, pos+1);
|
std::string begining = str.substr(0, pos+1);
|
||||||
|
|
||||||
// PERF optimize not to get all lambdas, but those beginning with token
|
auto suggestions = repl_env->get_lambdas_list(token);
|
||||||
std::vector<std::string> lambdas = repl_env->get_lambdas_list();
|
std::copy_if(commands.begin(), commands.end(), std::back_inserter(suggestions),
|
||||||
lambdas.insert(end(lambdas), begin(commands), end(commands));
|
[&token] (const std::string &cmd) { return token.empty() || cmd.find(token)==0; });
|
||||||
|
|
||||||
for (const auto & lambda : lambdas) {
|
std::sort(suggestions.begin(), suggestions.end());
|
||||||
if(lambda.find(token) == 0) {
|
for (const auto &suggestion : suggestions) {
|
||||||
std::string completion_string = begining + lambda;
|
std::string completion = begining + suggestion;
|
||||||
linenoiseAddCompletion(lc, completion_string.c_str());
|
linenoiseAddCompletion(lc, completion.c_str());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
#include "linenoise.h"
|
#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 linenoise_line_read(char *line);
|
||||||
void close_linenoise();
|
void close_linenoise();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user