The docsearch function is now working.

This commit is contained in:
2026-01-14 14:34:54 -05:00
parent 850b4aa43b
commit 03e9928e03
6 changed files with 163 additions and 68 deletions

View File

@@ -29,7 +29,7 @@ LuaDefine(makeclass, "classname", "create a class if it doesn't already exist")
return LS.result();
}
LuaDefine(getclass, "classname", "get the classtab with the specified name") {
LuaDefine(getclass, "x", "get a class table") {
LuaArg classname;
LuaRet classtab;
LuaDefStack LS(L, classname, classtab);
@@ -40,7 +40,7 @@ LuaDefine(getclass, "classname", "get the classtab with the specified name") {
return LS.result();
}
LuaDefine(classname, "classtable", "get the class name from a class table") {
LuaDefine(classname, "x", "get a class name") {
LuaArg table;
LuaRet result;
LuaDefStack LS(L, table, result);
@@ -53,17 +53,52 @@ LuaDefine(classname, "classtable", "get the class name from a class table") {
return LS.result();
}
static void get_reg_name(std::string_view name, std::string_view &classname, std::string_view &funcname) {
static std::string_view get_reg_classname(std::string_view name)
{
size_t upos = name.find('_');
if (upos == std::string_view::npos) {
funcname = name;
classname = "";
return "";
} else {
funcname = name.substr(upos + 1);
classname = name.substr(0, upos);
return name.substr(0, upos);
}
}
static std::string_view get_reg_funcname(std::string_view name) {
size_t upos = name.find('_');
if (upos == std::string_view::npos) {
return name;
} else {
return name.substr(upos + 1);
}
}
static eng::string get_reg_fullname(std::string_view name)
{
size_t upos = name.find('_');
if (upos == std::string_view::npos) {
return eng::string(name);
} else {
return eng::string(name.substr(0, upos)) + "." + eng::string(name.substr(upos + 1));
}
}
static eng::string get_reg_prototype(const LuaFunctionReg *reg) {
eng::ostringstream oss;
oss << "function " << get_reg_fullname(reg->get_name());
oss << "(" << reg->get_args() << ")";
return oss.str();
}
static bool lines_contain_substring(const util::StringVec &lines, int lo, int hi, const eng::string &substring) {
for (int i = lo; i < hi; i++) {
if (sv::contains_substring_utf8(lines[i], substring)) {
return true;
}
}
return false;
}
static void get_info_table(LuaCoreStack &LS, LuaSlot db, LuaSlot info, const eng::string &fn) {
LS.rawget(info, db, fn);
if (!LS.istable(info)) {
@@ -84,7 +119,6 @@ static void calculate_loadresult(LuaCoreStack &LS0, LuaSlot info, const eng::str
}
}
void SourceDB::diff(const SourceDB &auth, StreamBuffer *sb) {
LuaVar sdb, sfn, sinfo, shash, sseq;
LuaVar mdb, mfn, minfo, mhash, mseq, mcode;
@@ -260,9 +294,8 @@ static void source_load_cfunctions(lua_State *L) {
for (auto r = LuaFunctionReg::All; r != nullptr; r=r->next()) {
lua_CFunction func = r->get_func();
if ((func != nullptr) && (!r->get_sandbox())) {
std::string_view classname;
std::string_view funcname;
get_reg_name(r->get_name(), classname, funcname);
std::string_view classname = get_reg_classname(r->get_name());
std::string_view funcname = get_reg_funcname(r->get_name());
if (classname.empty()) {
LS.getglobaltable(classobj);
LS.rawset(classobj, funcname, func);
@@ -285,9 +318,8 @@ static void source_load_cconstants(lua_State *L) {
} else {
LS.set(value, r->get_tokenvalue());
}
std::string_view classname;
std::string_view funcname;
get_reg_name(r->get_name(), classname, funcname);
std::string_view classname = get_reg_classname(r->get_name());
std::string_view funcname = get_reg_funcname(r->get_name());
if (classname.empty()) {
LS.getglobaltable(classobj);
LS.rawset(classobj, funcname, value);
@@ -500,9 +532,8 @@ void SourceDB::register_lua_builtins() {
// the prototype lua state into the registry, then remove the closure
// from the prototype.
for (auto reg = LuaFunctionReg::All; reg != nullptr; reg=reg->next()) {
std::string_view funcname;
std::string_view classname;
get_reg_name(reg->get_name(), classname, funcname);
std::string_view classname = get_reg_classname(reg->get_name());
std::string_view funcname = get_reg_funcname(reg->get_name());
if (classname.empty()) {
LS.getglobaltable(classtab);
} else {
@@ -545,64 +576,89 @@ void SourceDB::register_lua_builtins() {
lua_close(L);
}
util::StringVec SourceDB::search_docs(const eng::string &substring) {
// This map will hold the results. It maps function name
// to a documentation line.
eng::map<eng::string, eng::string> results;
bool SourceDB::search_docs(const eng::string &substring, std::ostream &ostream) {
bool found_anything = false;
// Search the built-in functions.
// for (const LuaFunctionReg *reg = LuaFunctionReg::All; reg != nullptr; reg=reg->next()) {
// }
util::StringVec resultvec;
for (const auto &pair : results) {
resultvec.push_back(pair.second);
for (const LuaFunctionReg *reg = LuaFunctionReg::All; reg != nullptr; reg=reg->next()) {
eng::string proto = get_reg_prototype(reg);
if (sv::contains_substring_utf8(proto, substring) ||
sv::contains_substring_utf8(reg->get_docs(), substring)) {
ostream << proto;
util::StringVec docs = util::split_docstring(reg->get_docs());
for (const eng::string &line : docs) {
if (sv::contains_substring_utf8(line, substring)) {
ostream << " -- " << sv::trim(line);
break;
}
}
ostream << std::endl;
found_anything = true;
}
}
return resultvec;
// Search the lua source code.
for (const eng::string &module : modules()) {
if (module == "CORE") continue;
eng::string code = get_source(module);
if (code.empty()) continue;
util::StringVec lines = util::split_lines(code);
int comment_lines = 0;
for (int i = 0; i < int(lines.size()); i++) {
if (sv::is_lua_function_prototype(lines[i])) {
if (lines_contain_substring(lines, i - comment_lines, i+1, substring)) {
ostream << lines[i];
for (int j = i - comment_lines; j < i; j++) {
if (sv::contains_substring_utf8(lines[j], substring)) {
ostream << " " << sv::trim(lines[j]);
break;
}
}
ostream << std::endl;
found_anything = true;
}
comment_lines = 0;
} else if (sv::is_lua_comment(lines[i])) {
comment_lines++;
} else {
comment_lines = 0;
}
}
}
return found_anything;
}
eng::string SourceDB::function_docs(const LuaCoreStack &LS, LuaSlot fn) {
bool SourceDB::function_docs(const LuaCoreStack &LS, LuaSlot fn, std::ostream &ostream) {
lua_State *L = LS.state();
if (LS.iscfunction(fn)) {
lua_CFunction cfn = lua_tocfunction(L, fn.index());
const LuaFunctionReg *reg = LuaFunctionReg::lookup(cfn);
if (reg == nullptr) {
return "";
}
std::string_view classname;
std::string_view funcname;
get_reg_name(reg->get_name(), classname, funcname);
eng::ostringstream oss;
if (reg == nullptr) return false;
ostream << get_reg_prototype(reg) << std::endl;
util::StringVec docs = util::split_docstring(reg->get_docs());
oss << "function ";
if (!classname.empty()) {
oss << classname << ".";
}
oss << funcname << "(" << reg->get_args() << ")" << std::endl;
oss << "--" << std::endl;
ostream << "--" << std::endl;
for (const eng::string &line : docs) {
oss << "-- " << line << std::endl;
ostream << "-- " << line << std::endl;
}
oss << "--" << std::endl;
return oss.str();
ostream << "--" << std::endl;
return true;
} else if (LS.isfunction(fn)) {
lua_Debug ar;
lua_pushvalue(L, fn.index());
int status = lua_getinfo(L, ">S", &ar);
if (status == 0) return "";
if (status == 0) return false;
// Get the source code.
util::StringVec lines = util::split_lines(get_source(eng::string(ar.short_src)));
if (lines.empty()) return "";
if (lines.empty()) return false;
// Find the line of code containing the function prototype.
// Lua numbers source lines from 1, but we number lines from 0,
// so we have to subtract one.
int linehi = ar.linedefined - 1;
if ((linehi < 0) || (linehi >= int(lines.size()))) {
return "";
return false;
}
// Incorporate the function comment.
@@ -610,16 +666,15 @@ eng::string SourceDB::function_docs(const LuaCoreStack &LS, LuaSlot fn) {
while ((linelo > 0) && (sv::is_lua_comment(lines[linelo-1]))) linelo -= 1;
// Output the docs.
eng::ostringstream result;
result << lines[linehi] << std::endl;
result << "--" << std::endl;
ostream << lines[linehi] << std::endl;
ostream << "--" << std::endl;
for (int i = linelo; i < linehi; i++) {
result << lines[i] << std::endl;
ostream << lines[i] << std::endl;
}
result << "--" << std::endl;
return result.str();
ostream << "--" << std::endl;
return true;
} else {
return "";
return false;
}
}