1 // Copyright 2017-2018 ccls Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include "lsp.hh"
7 
8 #include <algorithm>
9 #include <queue>
10 
11 namespace ccls {
12 template <typename Node>
flattenHierarchy(const std::optional<Node> & root)13 std::vector<Location> flattenHierarchy(const std::optional<Node> &root) {
14   if (!root)
15     return {};
16   std::vector<Location> ret;
17   std::queue<const Node *> q;
18   for (auto &entry : root->children)
19     q.push(&entry);
20   while (q.size()) {
21     auto *entry = q.front();
22     q.pop();
23     if (entry->location.uri.raw_uri.size())
24       ret.push_back({entry->location});
25     for (auto &entry1 : entry->children)
26       q.push(&entry1);
27   }
28   std::sort(ret.begin(), ret.end());
29   ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
30   return ret;
31 }
32 } // namespace ccls
33