Skip to content

Commit

Permalink
Merge fix completion of members of anonymous structs (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikucionisaau authored Dec 15, 2023
2 parents a465160 + bd3e007 commit aa45c34
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/autocomplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ struct Serializer<std::vector<Suggestion>>
}
};

bool is_struct(const UTAP::symbol_t& sym)
std::optional<UTAP::type_t> get_struct_type(const UTAP::symbol_t& sym)
{
return sym.get_type().size() == 1 && sym.get_type().get(0).is(UTAP::Constants::RECORD);
if(sym.get_type().size() == 1 && sym.get_type().get(0).is(UTAP::Constants::RECORD))
return sym.get_type().get(0);
else if(sym.get_type().is(UTAP::Constants::RECORD))
return sym.get_type();
return std::nullopt;
}

bool is_template(const UTAP::symbol_t& sym) { return sym.get_type().is(UTAP::Constants::INSTANCE); }
Expand Down Expand Up @@ -237,8 +241,8 @@ void AutocompleteModule::configure(Server& server)
if (is_template(sym) && is_query){
if(auto process = find_process(doc, sym.get_name()))
results.add_template(*process);
} else if (is_struct(sym))
results.add_struct(sym.get_type().get(0));
} else if (auto struct_type = get_struct_type(sym))
results.add_struct(*struct_type);
},
[&](UTAP::type_t& type) { results.add_struct(type); }},
*entity);
Expand Down
48 changes: 48 additions & 0 deletions test/test_autocomplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,52 @@ TEST_CASE("Autocomplete fails when identifier matches template name"){
CHECK(unique_name_view(mock.receive()) == json{});
REQUIRE(mock.receive() == OK_RESPONSE);
CHECK_EOF(mock);
}

const std::string MODEL4 = R"(<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.5//EN' 'http://www.it.uu.se/research/group/darts/uppaal/flat-1_5.dtd'>
<nta>
<declaration>
struct {
int x;
int y;
int z;
} anon;
</declaration>
<template>
<name x="5" y="5">Template</name>
<parameter>int xax</parameter>
<declaration></declaration>
<location id="id0" x="-76" y="-68">
<name x="-86" y="-102">Init</name>
</location>
<location id="id1" x="-86" y="-68">
</location>
<init ref="id0"/>
</template>
<system>
p = Template();
system p;
</system>
</nta>)";

TEST_CASE("autocompleting anonymous struct members")
{
auto repo = SystemRepository{};
auto autocomplete = AutocompleteModule{repo};

auto mock = MockIO{};
mock.send("upload", MODEL4);
mock.send("autocomplete", {{"xpath", "/nta/declaration!"}, {"identifier", "anon."}, {"offset", 51}});
mock.send_cmd("exit");

auto server = Server{mock};
server.add_close_command("exit").add_module(repo).add_module(autocomplete).start();

REQUIRE(mock.handshake());
REQUIRE(mock.receive() == OK_RESPONSE);
CHECK(name_view(mock.receive()) == json{"anon.x", "anon.y", "anon.z"});
REQUIRE(mock.receive() == OK_RESPONSE);
CHECK_EOF(mock);
}

0 comments on commit aa45c34

Please sign in to comment.