mirror of
https://github.com/MaskRay/ccls.git
synced 2025-01-31 18:00:26 +00:00
Elide long include path proposals
This commit is contained in:
parent
89b34a359c
commit
e182ac12ae
@ -1821,6 +1821,7 @@ bool QueryDbMainLoop(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cerr << "[complete] Returning " << complete_response.result.items.size() << " include completions" << std::endl;
|
||||||
ipc->SendOutMessageToClient(IpcId::TextDocumentCompletion, complete_response);
|
ipc->SendOutMessageToClient(IpcId::TextDocumentCompletion, complete_response);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -29,6 +29,10 @@ struct Config {
|
|||||||
// If true, document links are reported for #include directives.
|
// If true, document links are reported for #include directives.
|
||||||
bool showDocumentLinksOnIncludes = true;
|
bool showDocumentLinksOnIncludes = true;
|
||||||
|
|
||||||
|
// Maximum path length to show in completion results. Paths longer than this
|
||||||
|
// will be elided with ".." put at the front. Set to 0 or a negative number
|
||||||
|
// to disable eliding.
|
||||||
|
int includeCompletionMaximumPathLength = 30;
|
||||||
// Whitelist that file paths will be tested against. If a file path does not
|
// Whitelist that file paths will be tested against. If a file path does not
|
||||||
// end in one of these values, it will not be considered for auto-completion.
|
// end in one of these values, it will not be considered for auto-completion.
|
||||||
// An example value is { ".h", ".hpp" }
|
// An example value is { ".h", ".hpp" }
|
||||||
@ -57,6 +61,7 @@ MAKE_REFLECT_STRUCT(Config,
|
|||||||
indexerCount,
|
indexerCount,
|
||||||
enableIndexing, enableCacheWrite, enableCacheRead,
|
enableIndexing, enableCacheWrite, enableCacheRead,
|
||||||
|
|
||||||
|
includeCompletionMaximumPathLength,
|
||||||
includeCompletionWhitelistLiteralEnding,
|
includeCompletionWhitelistLiteralEnding,
|
||||||
includeCompletionWhitelist, includeCompletionBlacklist,
|
includeCompletionWhitelist, includeCompletionBlacklist,
|
||||||
|
|
||||||
|
@ -10,6 +10,17 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
std::string ElideLongPath(Config* config, const std::string& path) {
|
||||||
|
if (config->includeCompletionMaximumPathLength <= 0)
|
||||||
|
return path;
|
||||||
|
|
||||||
|
if (path.size() <= config->includeCompletionMaximumPathLength)
|
||||||
|
return path;
|
||||||
|
|
||||||
|
int start = path.size() - config->includeCompletionMaximumPathLength;
|
||||||
|
return ".." + path.substr(start + 2);
|
||||||
|
}
|
||||||
|
|
||||||
size_t TrimCommonPathPrefix(const std::string& result, const std::string& trimmer) {
|
size_t TrimCommonPathPrefix(const std::string& result, const std::string& trimmer) {
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while (i < result.size() && i < trimmer.size()) {
|
while (i < result.size() && i < trimmer.size()) {
|
||||||
@ -56,12 +67,22 @@ bool TrimPath(Project* project, const std::string& project_root, std::string& in
|
|||||||
return angle;
|
return angle;
|
||||||
}
|
}
|
||||||
|
|
||||||
lsCompletionItem BuildCompletionItem(std::string path, bool use_angle_brackets, bool is_stl) {
|
lsCompletionItem BuildCompletionItem(Config* config, std::string path, bool use_angle_brackets, bool is_stl) {
|
||||||
lsCompletionItem item;
|
lsCompletionItem item;
|
||||||
if (use_angle_brackets)
|
if (use_angle_brackets)
|
||||||
item.label = "#include <" + std::string(path) + ">";
|
item.label = "#include <" + ElideLongPath(config, path) + ">";
|
||||||
else
|
else
|
||||||
item.label = "#include \"" + std::string(path) + "\"";
|
item.label = "#include \"" + ElideLongPath(config, path) + "\"";
|
||||||
|
|
||||||
|
item.detail = path;
|
||||||
|
|
||||||
|
// Replace the entire existing content.
|
||||||
|
// NOTE: When submitting completion items, textEdit->range must be updated.
|
||||||
|
item.textEdit = lsTextEdit();
|
||||||
|
if (use_angle_brackets)
|
||||||
|
item.textEdit->newText = "#include <" + path + ">";
|
||||||
|
else
|
||||||
|
item.textEdit->newText = "#include \"" + path + "\"";
|
||||||
|
|
||||||
item.insertTextFormat = lsInsertTextFormat::PlainText;
|
item.insertTextFormat = lsInsertTextFormat::PlainText;
|
||||||
if (is_stl)
|
if (is_stl)
|
||||||
@ -69,11 +90,6 @@ lsCompletionItem BuildCompletionItem(std::string path, bool use_angle_brackets,
|
|||||||
else
|
else
|
||||||
item.kind = lsCompletionItemKind::File;
|
item.kind = lsCompletionItemKind::File;
|
||||||
|
|
||||||
// Replace the entire existing content.
|
|
||||||
// NOTE: When submitting completion items, textEdit->range must be updated.
|
|
||||||
item.textEdit = lsTextEdit();
|
|
||||||
item.textEdit->newText = item.label;
|
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +131,7 @@ void IncludeCompletion::AddFile(std::string path) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
bool use_angle_brackets = TrimPath(project_, config_->projectRoot, path);
|
bool use_angle_brackets = TrimPath(project_, config_->projectRoot, path);
|
||||||
lsCompletionItem item = BuildCompletionItem(path, use_angle_brackets, false /*is_stl*/);
|
lsCompletionItem item = BuildCompletionItem(config_, path, use_angle_brackets, false /*is_stl*/);
|
||||||
|
|
||||||
if (is_scanning) {
|
if (is_scanning) {
|
||||||
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
||||||
@ -136,7 +152,7 @@ void IncludeCompletion::InsertIncludesFromDirectory(
|
|||||||
if (match_ && !match_->IsMatch(directory + path))
|
if (match_ && !match_->IsMatch(directory + path))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
result.push_back(BuildCompletionItem(path, use_angle_brackets, false /*is_stl*/));
|
result.push_back(BuildCompletionItem(config_, path, use_angle_brackets, false /*is_stl*/));
|
||||||
});
|
});
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
||||||
@ -146,6 +162,6 @@ void IncludeCompletion::InsertIncludesFromDirectory(
|
|||||||
void IncludeCompletion::InsertStlIncludes() {
|
void IncludeCompletion::InsertStlIncludes() {
|
||||||
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
std::lock_guard<std::mutex> lock(completion_items_mutex);
|
||||||
for (const char* stl_header : kStandardLibraryIncludes) {
|
for (const char* stl_header : kStandardLibraryIncludes) {
|
||||||
completion_items.insert(BuildCompletionItem(stl_header, true /*use_angle_brackets*/, true /*is_stl*/));
|
completion_items.insert(BuildCompletionItem(config_, stl_header, true /*use_angle_brackets*/, true /*is_stl*/));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -461,7 +461,13 @@ struct lsCompletionItem {
|
|||||||
// a completion and a completion resolve request.
|
// a completion and a completion resolve request.
|
||||||
// data ? : any
|
// data ? : any
|
||||||
|
|
||||||
inline bool operator==(const lsCompletionItem& other) const { return label == other.label; }
|
inline bool operator==(const lsCompletionItem& other) const {
|
||||||
|
if (textEdit && other.textEdit)
|
||||||
|
return textEdit->newText == other.textEdit->newText;
|
||||||
|
if (!insertText.empty())
|
||||||
|
return insertText == other.insertText;
|
||||||
|
return label == other.label;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
MAKE_REFLECT_STRUCT(lsCompletionItem,
|
MAKE_REFLECT_STRUCT(lsCompletionItem,
|
||||||
label,
|
label,
|
||||||
@ -472,7 +478,17 @@ MAKE_REFLECT_STRUCT(lsCompletionItem,
|
|||||||
insertText,
|
insertText,
|
||||||
insertTextFormat,
|
insertTextFormat,
|
||||||
textEdit);
|
textEdit);
|
||||||
MAKE_HASHABLE(lsCompletionItem, t.label);
|
namespace std {
|
||||||
|
template<> struct hash<lsCompletionItem> {
|
||||||
|
std::size_t operator()(const lsCompletionItem& t) const {
|
||||||
|
if (t.textEdit)
|
||||||
|
return std::hash<std::string>()(t.textEdit->newText);
|
||||||
|
if (!t.insertText.empty())
|
||||||
|
return std::hash<std::string>()(t.insertText);
|
||||||
|
return std::hash<std::string>()(t.label);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
struct lsTextDocumentItem {
|
struct lsTextDocumentItem {
|
||||||
// The text document's URI.
|
// The text document's URI.
|
||||||
|
Loading…
Reference in New Issue
Block a user