2017-05-27 04:21:00 +00:00
|
|
|
#include "include_complete.h"
|
2017-05-21 19:51:15 +00:00
|
|
|
|
|
|
|
#include "match.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "project.h"
|
|
|
|
#include "standard_includes.h"
|
|
|
|
#include "timer.h"
|
2017-09-13 03:35:27 +00:00
|
|
|
#include "work_thread.h"
|
2017-05-21 19:51:15 +00:00
|
|
|
|
2017-05-21 23:48:21 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2017-05-21 19:51:15 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-05-23 07:24:14 +00:00
|
|
|
struct CompletionCandidate {
|
|
|
|
std::string absolute_path;
|
|
|
|
lsCompletionItem completion_item;
|
|
|
|
};
|
|
|
|
|
2017-05-21 21:01:52 +00:00
|
|
|
std::string ElideLongPath(Config* config, const std::string& path) {
|
2018-02-22 05:50:36 +00:00
|
|
|
if (config->completion.includeMaxPathSize <= 0)
|
2017-05-21 21:01:52 +00:00
|
|
|
return path;
|
|
|
|
|
2018-02-22 05:50:36 +00:00
|
|
|
if ((int)path.size() <= config->completion.includeMaxPathSize)
|
2017-05-21 21:01:52 +00:00
|
|
|
return path;
|
|
|
|
|
2018-02-22 05:50:36 +00:00
|
|
|
size_t start = path.size() - config->completion.includeMaxPathSize;
|
2017-05-21 21:01:52 +00:00
|
|
|
return ".." + path.substr(start + 2);
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
size_t TrimCommonPathPrefix(const std::string& result,
|
|
|
|
const std::string& trimmer) {
|
2017-05-21 19:51:15 +00:00
|
|
|
size_t i = 0;
|
|
|
|
while (i < result.size() && i < trimmer.size()) {
|
|
|
|
char a = result[i];
|
|
|
|
char b = trimmer[i];
|
|
|
|
#if defined(_WIN32)
|
2017-05-21 23:22:00 +00:00
|
|
|
a = (char)tolower(a);
|
|
|
|
b = (char)tolower(b);
|
2017-05-21 19:51:15 +00:00
|
|
|
#endif
|
|
|
|
if (a != b)
|
|
|
|
break;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == trimmer.size())
|
|
|
|
return i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff angle brackets should be used.
|
2017-09-22 01:14:57 +00:00
|
|
|
bool TrimPath(Project* project,
|
|
|
|
const std::string& project_root,
|
|
|
|
std::string* insert_path) {
|
2017-05-21 19:51:15 +00:00
|
|
|
size_t start = 0;
|
|
|
|
bool angle = false;
|
|
|
|
|
2017-05-23 07:24:14 +00:00
|
|
|
size_t len = TrimCommonPathPrefix(*insert_path, project_root);
|
2017-05-21 19:51:15 +00:00
|
|
|
if (len > start)
|
|
|
|
start = len;
|
|
|
|
|
|
|
|
for (auto& include_dir : project->quote_include_directories) {
|
2017-05-23 07:24:14 +00:00
|
|
|
len = TrimCommonPathPrefix(*insert_path, include_dir);
|
2017-05-21 19:51:15 +00:00
|
|
|
if (len > start)
|
|
|
|
start = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& include_dir : project->angle_include_directories) {
|
2017-05-23 07:24:14 +00:00
|
|
|
len = TrimCommonPathPrefix(*insert_path, include_dir);
|
2017-05-21 19:51:15 +00:00
|
|
|
if (len > start) {
|
|
|
|
start = len;
|
|
|
|
angle = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 07:24:14 +00:00
|
|
|
*insert_path = insert_path->substr(start);
|
2017-05-21 19:51:15 +00:00
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
lsCompletionItem BuildCompletionItem(Config* config,
|
|
|
|
const std::string& path,
|
2018-02-09 08:55:53 +00:00
|
|
|
bool use_angle_brackets,
|
2017-09-22 01:14:57 +00:00
|
|
|
bool is_stl) {
|
2017-05-21 19:51:15 +00:00
|
|
|
lsCompletionItem item;
|
2018-01-29 14:26:13 +00:00
|
|
|
item.label = ElideLongPath(config, path);
|
2018-02-22 07:34:32 +00:00
|
|
|
item.detail = path; // the include path, used in de-duplicating
|
2017-05-21 21:01:52 +00:00
|
|
|
item.textEdit = lsTextEdit();
|
2018-01-29 14:26:13 +00:00
|
|
|
item.textEdit->newText = path;
|
2017-05-21 19:51:15 +00:00
|
|
|
item.insertTextFormat = lsInsertTextFormat::PlainText;
|
2018-02-09 08:55:53 +00:00
|
|
|
item.use_angle_brackets_ = use_angle_brackets;
|
2018-01-29 14:26:13 +00:00
|
|
|
if (is_stl) {
|
2017-05-21 19:51:15 +00:00
|
|
|
item.kind = lsCompletionItemKind::Module;
|
2018-01-29 14:26:13 +00:00
|
|
|
item.priority_ = 2;
|
|
|
|
} else {
|
2017-05-21 19:51:15 +00:00
|
|
|
item.kind = lsCompletionItemKind::File;
|
2018-01-29 14:26:13 +00:00
|
|
|
item.priority_ = 1;
|
|
|
|
}
|
2017-05-21 19:51:15 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-05-27 04:21:00 +00:00
|
|
|
IncludeComplete::IncludeComplete(Config* config, Project* project)
|
2017-05-21 23:48:21 +00:00
|
|
|
: is_scanning(false), config_(config), project_(project) {}
|
2017-05-21 19:51:15 +00:00
|
|
|
|
2017-05-27 04:21:00 +00:00
|
|
|
void IncludeComplete::Rescan() {
|
2017-05-21 19:51:15 +00:00
|
|
|
if (is_scanning)
|
|
|
|
return;
|
|
|
|
|
|
|
|
completion_items.clear();
|
2017-05-29 23:57:19 +00:00
|
|
|
absolute_path_to_completion_item.clear();
|
2018-02-09 08:55:53 +00:00
|
|
|
inserted_paths.clear();
|
2017-05-21 19:51:15 +00:00
|
|
|
|
2018-02-22 05:50:36 +00:00
|
|
|
if (!match_ && (!config_->completion.includeWhitelist.empty() ||
|
|
|
|
!config_->completion.includeBlacklist.empty()))
|
2018-03-10 23:40:27 +00:00
|
|
|
match_ = std::make_unique<GroupMatch>(config_->completion.includeWhitelist,
|
2018-02-22 05:50:36 +00:00
|
|
|
config_->completion.includeBlacklist);
|
2017-05-21 19:51:15 +00:00
|
|
|
|
|
|
|
is_scanning = true;
|
2018-01-07 23:17:14 +00:00
|
|
|
WorkThread::StartThread("scan_includes", [this]() {
|
2017-05-21 19:51:15 +00:00
|
|
|
Timer timer;
|
|
|
|
|
|
|
|
InsertStlIncludes();
|
2018-02-08 23:27:46 +00:00
|
|
|
InsertIncludesFromDirectory(config_->projectRoot,
|
|
|
|
false /*use_angle_brackets*/);
|
2017-05-21 19:51:15 +00:00
|
|
|
for (const std::string& dir : project_->quote_include_directories)
|
|
|
|
InsertIncludesFromDirectory(dir, false /*use_angle_brackets*/);
|
|
|
|
for (const std::string& dir : project_->angle_include_directories)
|
|
|
|
InsertIncludesFromDirectory(dir, true /*use_angle_brackets*/);
|
|
|
|
|
|
|
|
timer.ResetAndPrint("[perf] Scanning for includes");
|
|
|
|
is_scanning = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-09 08:55:53 +00:00
|
|
|
void IncludeComplete::InsertCompletionItem(const std::string& absolute_path,
|
|
|
|
lsCompletionItem&& item) {
|
|
|
|
if (inserted_paths.insert({item.detail, inserted_paths.size()}).second) {
|
|
|
|
completion_items.push_back(item);
|
|
|
|
// insert if not found or with shorter include path
|
|
|
|
auto it = absolute_path_to_completion_item.find(absolute_path);
|
|
|
|
if (it == absolute_path_to_completion_item.end() ||
|
|
|
|
completion_items[it->second].detail.length() > item.detail.length())
|
|
|
|
absolute_path_to_completion_item[absolute_path] = completion_items.size();
|
|
|
|
} else {
|
2018-02-22 07:34:32 +00:00
|
|
|
lsCompletionItem& inserted_item =
|
|
|
|
completion_items[inserted_paths[item.detail]];
|
2018-02-09 08:55:53 +00:00
|
|
|
// Update |use_angle_brackets_|, prefer quotes.
|
|
|
|
if (!item.use_angle_brackets_)
|
|
|
|
inserted_item.use_angle_brackets_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 04:21:00 +00:00
|
|
|
void IncludeComplete::AddFile(const std::string& absolute_path) {
|
2018-02-22 07:34:32 +00:00
|
|
|
if (!EndsWithAny(absolute_path, config_->completion.includeSuffixWhitelist))
|
2017-05-21 19:51:15 +00:00
|
|
|
return;
|
2017-05-23 07:24:14 +00:00
|
|
|
if (match_ && !match_->IsMatch(absolute_path))
|
2017-05-21 19:51:15 +00:00
|
|
|
return;
|
|
|
|
|
2017-05-23 07:24:14 +00:00
|
|
|
std::string trimmed_path = absolute_path;
|
2017-09-22 01:14:57 +00:00
|
|
|
bool use_angle_brackets =
|
|
|
|
TrimPath(project_, config_->projectRoot, &trimmed_path);
|
|
|
|
lsCompletionItem item = BuildCompletionItem(
|
|
|
|
config_, trimmed_path, use_angle_brackets, false /*is_stl*/);
|
2017-05-29 23:57:19 +00:00
|
|
|
|
2018-02-09 08:55:53 +00:00
|
|
|
std::unique_lock<std::mutex> lock(completion_items_mutex, std::defer_lock);
|
|
|
|
if (is_scanning)
|
|
|
|
lock.lock();
|
|
|
|
InsertCompletionItem(absolute_path, std::move(item));
|
|
|
|
if (lock)
|
|
|
|
lock.unlock();
|
2017-05-21 19:51:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void IncludeComplete::InsertIncludesFromDirectory(std::string directory,
|
|
|
|
bool use_angle_brackets) {
|
2017-05-23 07:24:14 +00:00
|
|
|
directory = NormalizePath(directory);
|
|
|
|
EnsureEndsInSlash(directory);
|
2018-02-06 18:53:31 +00:00
|
|
|
if (match_ && !match_->IsMatch(directory)) {
|
|
|
|
// Don't even enter the directory if it fails the patterns.
|
|
|
|
return;
|
|
|
|
}
|
2017-05-23 07:24:14 +00:00
|
|
|
|
|
|
|
std::vector<CompletionCandidate> results;
|
2017-09-22 01:14:57 +00:00
|
|
|
GetFilesInFolder(
|
|
|
|
directory, true /*recursive*/, false /*add_folder_to_path*/,
|
|
|
|
[&](const std::string& path) {
|
2018-02-22 07:34:32 +00:00
|
|
|
if (!EndsWithAny(path, config_->completion.includeSuffixWhitelist))
|
2017-09-22 01:14:57 +00:00
|
|
|
return;
|
|
|
|
if (match_ && !match_->IsMatch(directory + path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
CompletionCandidate candidate;
|
|
|
|
candidate.absolute_path = directory + path;
|
|
|
|
candidate.completion_item = BuildCompletionItem(
|
|
|
|
config_, path, use_angle_brackets, false /*is_stl*/);
|
|
|
|
results.push_back(candidate);
|
|
|
|
});
|
2017-05-21 19:51:15 +00:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
2018-02-09 08:55:53 +00:00
|
|
|
for (CompletionCandidate& result : results)
|
|
|
|
InsertCompletionItem(result.absolute_path,
|
|
|
|
std::move(result.completion_item));
|
2017-05-21 19:51:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 04:21:00 +00:00
|
|
|
void IncludeComplete::InsertStlIncludes() {
|
2017-05-21 19:51:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
|
|
|
for (const char* stl_header : kStandardLibraryIncludes) {
|
2017-09-22 01:14:57 +00:00
|
|
|
completion_items.push_back(BuildCompletionItem(
|
|
|
|
config_, stl_header, true /*use_angle_brackets*/, true /*is_stl*/));
|
2017-05-21 19:51:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-29 23:57:19 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
optional<lsCompletionItem> IncludeComplete::FindCompletionItemForAbsolutePath(
|
|
|
|
const std::string& absolute_path) {
|
2017-05-29 23:57:19 +00:00
|
|
|
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
|
|
|
|
|
|
|
auto it = absolute_path_to_completion_item.find(absolute_path);
|
|
|
|
if (it == absolute_path_to_completion_item.end())
|
|
|
|
return nullopt;
|
|
|
|
return completion_items[it->second];
|
|
|
|
}
|