A small refactor to SourceDB to prepare for doc-search functionality
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
LuaDefine(makeclass, "classname", "create a class if it doesn't already exist") {
|
||||
LuaArg classname;
|
||||
LuaRet classtab;
|
||||
@@ -297,6 +298,28 @@ static void source_load_cconstants(lua_State *L) {
|
||||
}
|
||||
}
|
||||
|
||||
eng::string SourceDB::get_source(const eng::string &fn)
|
||||
{
|
||||
LuaVar sourcedb, fname, finfo, code;
|
||||
LuaExtStack LS(lua_state_, sourcedb, fname, finfo, code);
|
||||
|
||||
// Get the source database.
|
||||
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
|
||||
if (!LS.istable(sourcedb)) return "";
|
||||
|
||||
// Get the finfo table from the source db.
|
||||
LS.set(fname, fn);
|
||||
LS.rawget(finfo, sourcedb, fname);
|
||||
if (!LS.istable(finfo)) return "";
|
||||
|
||||
// Get the code from the finfo table.
|
||||
LS.rawget(code, finfo, "code");
|
||||
if (!LS.isstring(code)) return "";
|
||||
|
||||
return LS.ckstring(code);
|
||||
}
|
||||
|
||||
|
||||
eng::vector<eng::string> SourceDB::modules() {
|
||||
eng::vector<eng::string> result;
|
||||
LuaVar sourcedb, key, info, seq;
|
||||
@@ -522,12 +545,8 @@ void SourceDB::register_lua_builtins() {
|
||||
lua_close(L);
|
||||
}
|
||||
|
||||
|
||||
eng::string SourceDB::function_docs(const LuaCoreStack &LS0, LuaSlot fn) {
|
||||
lua_State *L = LS0.state();
|
||||
LuaVar sourcedb, fname, finfo, code;
|
||||
LuaExtStack LS(L, sourcedb, fname, finfo, code);
|
||||
|
||||
eng::string SourceDB::function_docs(const LuaCoreStack &LS, LuaSlot fn) {
|
||||
lua_State *L = LS.state();
|
||||
if (LS.iscfunction(fn)) {
|
||||
lua_CFunction cfn = lua_tocfunction(L, fn.index());
|
||||
const LuaFunctionReg *reg = LuaFunctionReg::lookup(cfn);
|
||||
@@ -556,27 +575,13 @@ eng::string SourceDB::function_docs(const LuaCoreStack &LS0, LuaSlot fn) {
|
||||
int status = lua_getinfo(L, ">S", &ar);
|
||||
if (status == 0) return "";
|
||||
|
||||
// Get the source database.
|
||||
LS.rawget(sourcedb, LuaRegistry, "sourcedb");
|
||||
if (!LS.istable(sourcedb)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Get the finfo table from the source db.
|
||||
LS.set(fname, eng::string(ar.short_src));
|
||||
LS.rawget(finfo, sourcedb, fname);
|
||||
if (!LS.istable(finfo)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Get the code from the finfo table.
|
||||
LS.rawget(code, finfo, "code");
|
||||
if (!LS.isstring(code)) {
|
||||
return "";
|
||||
}
|
||||
// Get the source code.
|
||||
util::StringVec lines = util::split_lines(get_source(eng::string(ar.short_src)));
|
||||
if (lines.empty()) return "";
|
||||
|
||||
// Split the code into lines.
|
||||
util::StringVec lines = util::split_lines(LS.ckstring(code));
|
||||
// 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 "";
|
||||
|
||||
Reference in New Issue
Block a user