Conditionally allow reindex on didChange

This commit is contained in:
Boris Staletic 2018-03-19 21:28:11 +01:00 committed by Fangrui Song
parent 4e76cdaaae
commit bf011fef71
3 changed files with 36 additions and 10 deletions

View File

@ -212,6 +212,10 @@ struct Config {
bool sort = true;
} workspaceSymbol;
// Allow indexing on textDocument/didChange.
// May be too slow for big projects, so it is off by default.
bool enableIndexOnDidChange = false;
struct Xref {
// If true, |Location[]| response will include lexical container.
bool container = false;
@ -272,6 +276,8 @@ MAKE_REFLECT_STRUCT(Config,
index,
workspaceSymbol,
xref,
enableIndexOnDidChange,
dumpAST);

View File

@ -1,6 +1,11 @@
#include "cache_manager.h"
#include "clang_complete.h"
#include "message_handler.h"
#include "project.h"
#include "working_files.h"
#include "queue_manager.h"
#include <loguru/loguru.hpp>
namespace {
struct Ipc_TextDocumentDidChange
@ -17,6 +22,18 @@ struct TextDocumentDidChangeHandler
void Run(Ipc_TextDocumentDidChange* request) override {
std::string path = request->params.textDocument.uri.GetPath();
working_files->OnChange(request->params);
if (config->enableIndexOnDidChange) {
optional<std::string> content = ReadContent(path);
if (!content) {
LOG_S(ERROR) << "Unable to read file content after saving " << path;
} else {
Project::Entry entry = project->FindCompilationEntryForFile(path);
QueueManager::instance()->index_request.PushBack(
Index_Request(entry.filename, entry.args, true /*is_interactive*/,
*content, ICacheManager::Make(config)),
true);
}
}
clang_complete->NotifyEdit(path);
clang_complete->DiagnosticsUpdate(
std::monostate(),

View File

@ -34,7 +34,8 @@ struct TextDocumentDidSaveHandler
// Send out an index request, and copy the current buffer state so we
// can update the cached index contents when the index is done.
//
// We also do not index if there is already an index request.
// We also do not index if there is already an index request or if
// the client requested indexing on didChange instead.
//
// TODO: Cancel outgoing index request. Might be tricky to make
// efficient since we have to cancel.
@ -44,15 +45,17 @@ struct TextDocumentDidSaveHandler
// mutex and check to see if we should skip the current request.
// if so, ignore that index response.
// TODO: send as priority request
optional<std::string> content = ReadContent(path);
if (!content) {
LOG_S(ERROR) << "Unable to read file content after saving " << path;
} else {
Project::Entry entry = project->FindCompilationEntryForFile(path);
QueueManager::instance()->index_request.PushBack(
Index_Request(entry.filename, entry.args, true /*is_interactive*/,
*content, ICacheManager::Make(config)),
true);
if (!config->enableIndexOnDidChange) {
optional<std::string> content = ReadContent(path);
if (!content) {
LOG_S(ERROR) << "Unable to read file content after saving " << path;
} else {
Project::Entry entry = project->FindCompilationEntryForFile(path);
QueueManager::instance()->index_request.PushBack(
Index_Request(entry.filename, entry.args, true /*is_interactive*/,
*content, ICacheManager::Make(config)),
true);
}
}
clang_complete->NotifySave(path);