Skip to content

Commit 9d88419

Browse files
authored
Merge pull request #2 from thorulf4/fix_find_process
Fixed issue where template name would cause bad lookup
2 parents 68ed6ad + 80828f2 commit 9d88419

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

include/uls/utap_extension.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ UTAP::declarations_t& navigate_xpath(UTAP::Document& doc, std::string_view path)
2222
*/
2323
UTAP::declarations_t& navigate_xpath(UTAP::Document& doc, std::string_view path, uint32_t pos);
2424

25-
UTAP::template_t& find_process(UTAP::Document& doc, std::string_view name);
25+
std::optional<std::reference_wrapper<UTAP::template_t>> find_process(UTAP::Document& doc, std::string_view name);
2626

2727
/**
2828
* This class is used to iterate through symbols from a given declarations

src/autocomplete.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,11 @@ void AutocompleteModule::configure(Server& server)
233233
if (std::optional<UtapEntity> entity = find_declaration(doc, decls, id.identifier.substr(0, offset))) {
234234
results.set_prefix(id.identifier.substr(0, offset + 1));
235235
std::visit(overloaded{[&](UTAP::symbol_t& sym) {
236-
if (is_template(sym) && is_query)
237-
results.add_template(find_process(doc, sym.get_name()));
238-
else if (is_struct(sym))
239-
results.add_struct(sym.get_type().get(0));
236+
if (is_template(sym) && is_query){
237+
if(auto process = find_process(doc, sym.get_name()))
238+
results.add_template(*process);
239+
} else if (is_struct(sym))
240+
results.add_struct(sym.get_type().get(0));
240241
},
241242
[&](UTAP::type_t& type) { results.add_struct(type); }},
242243
*entity);

src/declarations.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ std::optional<Sym> find_sym(UTAP::Document& doc, UTAP::declarations_t& decls, st
9696
if (struct_symbol.has_value()) {
9797
if (struct_symbol->type.is_constant())
9898
finder.set_source(struct_symbol->type.get(0).get(0));
99-
else if (struct_symbol->type.is(UTAP::Constants::INSTANCE))
100-
finder.set_source(&find_process(doc, label));
101-
else
99+
else if (struct_symbol->type.is(UTAP::Constants::INSTANCE)){
100+
if(auto opt_process = find_process(doc, label))
101+
finder.set_source(&static_cast<UTAP::template_t&>(*opt_process));
102+
} else
102103
finder.set_source(struct_symbol->type.get(0));
103104
} else {
104105
return std::nullopt;

src/utap_extension.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ UTAP::declarations_t& navigate_xpath(UTAP::Document& doc, std::string_view path,
5858
return decl;
5959
}
6060

61-
UTAP::template_t& find_process(UTAP::Document& doc, std::string_view name)
61+
std::optional<std::reference_wrapper<UTAP::template_t>> find_process(UTAP::Document& doc, std::string_view name)
6262
{
6363
for (auto& process : doc.get_processes()) {
6464
if (process.uid.get_name() == name) {
6565
if (process.templ)
6666
return *process.templ;
67-
else
68-
throw std::logic_error("Process did not have a template");
67+
return std::nullopt;
6968
}
7069
}
71-
throw std::logic_error("No such process");
70+
return std::nullopt;
7271
}

test/test_autocomplete.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,23 @@ TEST_CASE("Autocomplete template dont see locations")
365365
CHECK(unique_name_view(mock.receive()) == json{"xax", "point", "p_a", "func"});
366366
REQUIRE(mock.receive() == OK_RESPONSE);
367367
CHECK_EOF(mock);
368+
}
369+
370+
TEST_CASE("Autocomplete fails when identifier matches template name"){
371+
auto repo = SystemRepository{};
372+
auto autocomplete = AutocompleteModule{repo}; // Use an empty set of builtin items
373+
374+
auto mock = MockIO{};
375+
mock.send("upload", MODEL3);
376+
mock.send("autocomplete", {{"xpath", "/nta/queries!"}, {"identifier", "Template."}, {"offset", 1}});
377+
mock.send_cmd("exit");
378+
379+
auto server = Server{mock};
380+
server.add_close_command("exit").add_module(repo).add_module(autocomplete).start();
381+
382+
REQUIRE(mock.handshake());
383+
REQUIRE(mock.receive() == OK_RESPONSE);
384+
CHECK(unique_name_view(mock.receive()) == json{});
385+
REQUIRE(mock.receive() == OK_RESPONSE);
386+
CHECK_EOF(mock);
368387
}

0 commit comments

Comments
 (0)