1 #pragma once
2 
3 #include "lsp_completion.h"
4 
5 #include <atomic>
6 #include <mutex>
7 #include <unordered_set>
8 
9 struct GroupMatch;
10 struct Project;
11 
12 struct IncludeComplete {
13   IncludeComplete(Project* project);
14 
15   // Starts scanning directories. Clears existing cache.
16   void Rescan();
17 
18   // Ensures the one-off file is inside |completion_items|.
19   void AddFile(const std::string& absolute_path);
20 
21   // Scans the given directory and inserts all includes from this. This is a
22   // blocking function and should be run off the querydb thread.
23   void InsertIncludesFromDirectory(std::string directory,
24                                    bool use_angle_brackets);
25   void InsertStlIncludes();
26 
27   optional<lsCompletionItem> FindCompletionItemForAbsolutePath(
28       const std::string& absolute_path);
29 
30   // Insert item to |completion_items|.
31   // Update |absolute_path_to_completion_item| and |inserted_paths|.
32   void InsertCompletionItem(const std::string& absolute_path,
33                             lsCompletionItem&& item);
34 
35   // Guards |completion_items| when |is_scanning| is true.
36   std::mutex completion_items_mutex;
37   std::atomic<bool> is_scanning;
38   std::vector<lsCompletionItem> completion_items;
39 
40   // Absolute file path to the completion item in |completion_items|.
41   // Keep the one with shortest include path.
42   std::unordered_map<std::string, int> absolute_path_to_completion_item;
43 
44   // Only one completion item per include path.
45   std::unordered_map<std::string, int> inserted_paths;
46 
47   // Cached references
48   Project* project_;
49   std::unique_ptr<GroupMatch> match_;
50 };
51