Skip to content

Commit bd3e007

Browse files
committed
Fixed autocomplete on anoymous types
1 parent d105dee commit bd3e007

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/autocomplete.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ struct Serializer<std::vector<Suggestion>>
125125
}
126126
};
127127

128-
bool is_struct(const UTAP::symbol_t& sym)
128+
std::optional<UTAP::type_t> get_struct_type(const UTAP::symbol_t& sym)
129129
{
130-
return sym.get_type().size() == 1 && sym.get_type().get(0).is(UTAP::Constants::RECORD);
130+
if(sym.get_type().size() == 1 && sym.get_type().get(0).is(UTAP::Constants::RECORD))
131+
return sym.get_type().get(0);
132+
else if(sym.get_type().is(UTAP::Constants::RECORD))
133+
return sym.get_type();
134+
return std::nullopt;
131135
}
132136

133137
bool is_template(const UTAP::symbol_t& sym) { return sym.get_type().is(UTAP::Constants::INSTANCE); }
@@ -236,8 +240,8 @@ void AutocompleteModule::configure(Server& server)
236240
if (is_template(sym) && is_query){
237241
if(auto process = find_process(doc, sym.get_name()))
238242
results.add_template(*process);
239-
} else if (is_struct(sym))
240-
results.add_struct(sym.get_type().get(0));
243+
} else if (auto struct_type = get_struct_type(sym))
244+
results.add_struct(*struct_type);
241245
},
242246
[&](UTAP::type_t& type) { results.add_struct(type); }},
243247
*entity);

test/test_autocomplete.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,52 @@ TEST_CASE("Autocomplete fails when identifier matches template name"){
384384
CHECK(unique_name_view(mock.receive()) == json{});
385385
REQUIRE(mock.receive() == OK_RESPONSE);
386386
CHECK_EOF(mock);
387+
}
388+
389+
const std::string MODEL4 = R"(<?xml version="1.0" encoding="utf-8"?>
390+
<!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.5//EN' 'http://www.it.uu.se/research/group/darts/uppaal/flat-1_5.dtd'>
391+
<nta>
392+
<declaration>
393+
struct {
394+
int x;
395+
int y;
396+
int z;
397+
} anon;
398+
399+
</declaration>
400+
<template>
401+
<name x="5" y="5">Template</name>
402+
<parameter>int xax</parameter>
403+
<declaration></declaration>
404+
<location id="id0" x="-76" y="-68">
405+
<name x="-86" y="-102">Init</name>
406+
</location>
407+
<location id="id1" x="-86" y="-68">
408+
</location>
409+
<init ref="id0"/>
410+
</template>
411+
<system>
412+
p = Template();
413+
system p;
414+
</system>
415+
</nta>)";
416+
417+
TEST_CASE("autocompleting anonymous struct members")
418+
{
419+
auto repo = SystemRepository{};
420+
auto autocomplete = AutocompleteModule{repo};
421+
422+
auto mock = MockIO{};
423+
mock.send("upload", MODEL4);
424+
mock.send("autocomplete", {{"xpath", "/nta/declaration!"}, {"identifier", "anon."}, {"offset", 51}});
425+
mock.send_cmd("exit");
426+
427+
auto server = Server{mock};
428+
server.add_close_command("exit").add_module(repo).add_module(autocomplete).start();
429+
430+
REQUIRE(mock.handshake());
431+
REQUIRE(mock.receive() == OK_RESPONSE);
432+
CHECK(name_view(mock.receive()) == json{"anon.x", "anon.y", "anon.z"});
433+
REQUIRE(mock.receive() == OK_RESPONSE);
434+
CHECK_EOF(mock);
387435
}

0 commit comments

Comments
 (0)