Skip to content

Commit 2e161f6

Browse files
committed
Add Selection info to completion
1 parent f3888cf commit 2e161f6

File tree

18 files changed

+157
-52
lines changed

18 files changed

+157
-52
lines changed

src/celengine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set(CELENGINE_SOURCES
1111
boundaries.h
1212
category.cpp
1313
category.h
14+
completion.cpp
15+
completion.h
1416
console.cpp
1517
console.h
1618
constellation.cpp

src/celengine/body.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ PlanetarySystem::find(std::string_view _name, bool deepSearch, bool i18n) const
11271127
}
11281128

11291129
void
1130-
PlanetarySystem::getCompletion(std::vector<std::string>& completion,
1130+
PlanetarySystem::getCompletion(std::vector<celestia::engine::Completion>& completion,
11311131
std::string_view _name,
11321132
bool deepSearch) const
11331133
{
@@ -1138,13 +1138,13 @@ PlanetarySystem::getCompletion(std::vector<std::string>& completion,
11381138

11391139
if (UTF8StartsWith(alias, _name))
11401140
{
1141-
completion.push_back(alias);
1141+
completion.emplace_back(alias, Selection(index.second));
11421142
}
11431143
else
11441144
{
11451145
std::string lname = D_(alias.c_str());
11461146
if (lname != alias && UTF8StartsWith(lname, _name))
1147-
completion.push_back(lname);
1147+
completion.emplace_back(lname, Selection(index.second));
11481148
}
11491149
}
11501150

src/celengine/body.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#pragma once
1111

1212
#include <celengine/astroobj.h>
13+
#include <celengine/completion.h>
1314
#include <celengine/surface.h>
1415
#include <celengine/star.h>
1516
#include <celengine/location.h>
@@ -64,7 +65,7 @@ class PlanetarySystem
6465
};
6566

6667
Body* find(std::string_view, bool deepSearch = false, bool i18n = false) const;
67-
void getCompletion(std::vector<std::string>& completion, std::string_view _name, bool rec = true) const;
68+
void getCompletion(std::vector<celestia::engine::Completion>& completion, std::string_view _name, bool rec = true) const;
6869

6970
private:
7071
void addBodyToNameIndex(Body* body);

src/celengine/completion.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// completion.cpp
2+
//
3+
// Copyright (C) 2024-present, the Celestia Development Team
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 2
8+
// of the License, or (at your option) any later version.
9+
10+
#include "completion.h"
11+
12+
namespace celestia::engine
13+
{
14+
15+
std::string Completion::getName() const
16+
{
17+
return name;
18+
}
19+
20+
Selection Completion::getSelection() const
21+
{
22+
return std::visit([this](auto& arg)
23+
{
24+
using T = std::decay_t<decltype(arg)>;
25+
if constexpr (std::is_same_v<T, Selection>)
26+
{
27+
return arg;
28+
}
29+
else
30+
{
31+
static_assert(std::is_same_v<T, std::function<Selection()>>);
32+
return arg();
33+
}
34+
}, selection);
35+
}
36+
37+
}

src/celengine/completion.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// completion.h
2+
//
3+
// Copyright (C) 2024-present, the Celestia Development Team
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 2
8+
// of the License, or (at your option) any later version.
9+
10+
#pragma once
11+
12+
#include <functional>
13+
#include <string>
14+
#include <variant>
15+
#include <celengine/selection.h>
16+
17+
namespace celestia::engine
18+
{
19+
20+
class Completion
21+
{
22+
public:
23+
Completion(const std::string& name, const std::variant<Selection, std::function<Selection()>>& selection) :
24+
name(name),
25+
selection(selection)
26+
{}
27+
28+
std::string getName() const;
29+
Selection getSelection() const;
30+
31+
private:
32+
std::string name;
33+
std::variant<Selection, std::function<Selection()>> selection;
34+
};
35+
36+
}

src/celengine/dsodb.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,23 @@ DSODatabase::find(std::string_view name, bool i18n) const
6868
}
6969

7070
void
71-
DSODatabase::getCompletion(std::vector<std::string>& completion, std::string_view name) const
71+
DSODatabase::getCompletion(std::vector<celestia::engine::Completion>& completion, std::string_view name) const
7272
{
7373
// only named DSOs are supported by completion.
7474
if (!name.empty())
75-
m_namesDB->getCompletion(completion, name);
75+
{
76+
std::vector<std::pair<std::string, AstroCatalog::IndexNumber>> namesWithIndices;
77+
m_namesDB->getCompletion(namesWithIndices, name);
78+
79+
for (const auto& [dsoName, index] : namesWithIndices)
80+
{
81+
auto capturedIndex = index;
82+
completion.emplace_back(dsoName, [this, capturedIndex]
83+
{
84+
return Selection(find(capturedIndex));
85+
});
86+
}
87+
}
7688
}
7789

7890
std::string

src/celengine/dsodb.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#include <Eigen/Core>
2222
#include <Eigen/Geometry>
2323

24-
#include "dsooctree.h"
24+
#include <celengine/completion.h>
25+
#include <celengine/dsooctree.h>
2526

2627
class DeepSkyObject;
2728
class DSODatabaseBuilder;
@@ -49,7 +50,7 @@ class DSODatabase
4950
DeepSkyObject* find(const AstroCatalog::IndexNumber catalogNumber) const;
5051
DeepSkyObject* find(std::string_view, bool i18n) const;
5152

52-
void getCompletion(std::vector<std::string>&, std::string_view) const;
53+
void getCompletion(std::vector<celestia::engine::Completion>&, std::string_view) const;
5354

5455
void findVisibleDSOs(celestia::engine::DSOHandler& dsoHandler,
5556
const Eigen::Vector3d& obsPosition,

src/celengine/name.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@ NameDatabase::getFinalNameIter() const
8282
}
8383

8484
void
85-
NameDatabase::getCompletion(std::vector<std::string>& completion, std::string_view name) const
85+
NameDatabase::getCompletion(std::vector<std::pair<std::string, AstroCatalog::IndexNumber>>& completion, std::string_view name) const
8686
{
8787
std::string name2 = ReplaceGreekLetter(name);
88-
for (const auto &[n, _] : nameIndex)
88+
for (const auto &[n, index] : nameIndex)
8989
{
9090
if (UTF8StartsWith(n, name2, true))
91-
completion.push_back(n);
91+
completion.emplace_back(n, index);
9292
}
9393

9494
#ifdef ENABLE_NLS
95-
for (const auto &[n, _] : localizedNameIndex)
95+
for (const auto &[n, index] : localizedNameIndex)
9696
{
9797
if (UTF8StartsWith(n, name2, true))
98-
completion.push_back(n);
98+
completion.emplace_back(n, index);
9999
}
100100
#endif
101101
}

src/celengine/name.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class NameDatabase
3939
NumberIndex::const_iterator getFirstNameIter(AstroCatalog::IndexNumber catalogNumber) const;
4040
NumberIndex::const_iterator getFinalNameIter() const;
4141

42-
void getCompletion(std::vector<std::string>& completion, std::string_view name) const;
42+
void getCompletion(std::vector<std::pair<std::string, AstroCatalog::IndexNumber>>& completion, std::string_view name) const;
4343

4444
private:
4545
NameIndex nameIndex;

src/celengine/simulation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ Selection Simulation::findObjectFromPath(std::string_view s, bool i18n) const
457457
}
458458

459459

460-
void Simulation::getObjectCompletion(std::vector<std::string>& completion,
460+
void Simulation::getObjectCompletion(std::vector<celestia::engine::Completion>& completion,
461461
std::string_view s,
462462
bool withLocations) const
463463
{
@@ -485,7 +485,7 @@ void Simulation::getObjectCompletion(std::vector<std::string>& completion,
485485
universe->getCompletionPath(completion, s, {path, nPathEntries}, withLocations);
486486

487487
std::sort(completion.begin(), completion.end(),
488-
[](const std::string &s1, const std::string &s2) { return strnatcmp(s1, s2) < 0; });
488+
[](const celestia::engine::Completion &s1, const celestia::engine::Completion &s2) { return strnatcmp(s1.getName(), s2.getName()) < 0; });
489489
}
490490

491491

0 commit comments

Comments
 (0)