69 lines
1.9 KiB
C
69 lines
1.9 KiB
C
|
|
#pragma once
|
||
|
|
#include <algorithm>
|
||
|
|
#include <map>
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
namespace whetstone {
|
||
|
|
|
||
|
|
struct CompletionItem {
|
||
|
|
std::string label;
|
||
|
|
int kind = 1; // 1=Text 2=Method 3=Function 6=Variable etc.
|
||
|
|
std::string detail;
|
||
|
|
std::string source; // language server that produced this item
|
||
|
|
};
|
||
|
|
|
||
|
|
class LSPCompletionMerger {
|
||
|
|
public:
|
||
|
|
// Add items from a named source. Replaces any previous items from that source.
|
||
|
|
void addItems(const std::string& source,
|
||
|
|
const std::vector<CompletionItem>& items) {
|
||
|
|
bySource_[source] = items;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Return all items merged from all sources, deduplicated by label.
|
||
|
|
// When two sources offer the same label, the first encountered wins (sorted source order).
|
||
|
|
std::vector<CompletionItem> getItems() const {
|
||
|
|
std::vector<CompletionItem> result;
|
||
|
|
std::map<std::string, bool> seen;
|
||
|
|
for (const auto& [src, items] : bySource_) {
|
||
|
|
for (const auto& item : items) {
|
||
|
|
if (!seen.count(item.label)) {
|
||
|
|
seen[item.label] = true;
|
||
|
|
result.push_back(item);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::sort(result.begin(), result.end(),
|
||
|
|
[](const CompletionItem& a, const CompletionItem& b){
|
||
|
|
return a.label < b.label;
|
||
|
|
});
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Remove all items from one source.
|
||
|
|
void clearSource(const std::string& source) {
|
||
|
|
bySource_.erase(source);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Remove all items.
|
||
|
|
void clearAll() {
|
||
|
|
bySource_.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Count unique labels across all sources.
|
||
|
|
int uniqueCount() const {
|
||
|
|
return static_cast<int>(getItems().size());
|
||
|
|
}
|
||
|
|
|
||
|
|
int sourceCount() const {
|
||
|
|
return static_cast<int>(bySource_.size());
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
// source → items
|
||
|
|
std::map<std::string, std::vector<CompletionItem>> bySource_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace whetstone
|