Skip to content

Commit

Permalink
[script] add: basic completer function
Browse files Browse the repository at this point in the history
This shows how trivial it is to find completions from any AstNode
  • Loading branch information
jd28 committed Nov 12, 2023
1 parent 97ba7e2 commit 3ce2ffd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/nw/script/Nss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,15 @@ const Ast& Nss::ast() const { return ast_; }

std::string_view Nss::text() const noexcept { return data_.bytes.string_view(); }

std::vector<std::string> completer(const std::string& needle, immer::map<std::string, Export> haystack)
{
std::vector<std::string> result;
for (const auto& [name, _] : haystack) {
if (has_match(needle.c_str(), name.c_str())) {
result.push_back(name);
}
}
return result;
}

} // namespace nw::script
2 changes: 2 additions & 0 deletions lib/nw/script/Nss.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ struct Nss {
bool parsed_ = false;
};

std::vector<std::string> completer(const std::string& needle, immer::map<std::string, Export> haystack);

} // namespace nw::script
29 changes: 29 additions & 0 deletions tests/script_nss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ TEST(Nss, ParseNwscript)
EXPECT_NO_THROW(nss2.parse());
EXPECT_NO_THROW(nss2.resolve());
EXPECT_EQ(nss2.errors(), 0);

// Env
auto decl2 = nss.locate_export("TRUE", false);
EXPECT_NE(decl2, nullptr);
auto d2 = dynamic_cast<nw::script::VarDecl*>(decl2);
if (d2) {
EXPECT_EQ(d2->env.size(), 1);
}

auto decl3 = nss.locate_export("GetFirstObjectInShape", false);
EXPECT_NE(decl3, nullptr);
auto d3 = dynamic_cast<nw::script::FunctionDecl*>(decl3);
EXPECT_NE(d3, nullptr);
auto completions2 = nw::script::completer("OBJECT_type_", d3->env);
std::sort(std::begin(completions2), std::end(completions2));
std::vector<std::string> expect2{
"OBJECT_TYPE_ALL",
"OBJECT_TYPE_AREA_OF_EFFECT",
"OBJECT_TYPE_CREATURE",
"OBJECT_TYPE_DOOR",
"OBJECT_TYPE_ENCOUNTER",
"OBJECT_TYPE_INVALID",
"OBJECT_TYPE_ITEM",
"OBJECT_TYPE_PLACEABLE",
"OBJECT_TYPE_STORE",
"OBJECT_TYPE_TRIGGER",
"OBJECT_TYPE_WAYPOINT",
};
EXPECT_EQ(completions2, expect2);
}

TEST(Nss, Variables)
Expand Down

0 comments on commit 3ce2ffd

Please sign in to comment.