2017-12-29 16:29:47 +00:00
|
|
|
#include "include_complete.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "lex_utils.h"
|
|
|
|
#include "message_handler.h"
|
|
|
|
#include "query_utils.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "queue_manager.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
#include <doctest/doctest.h>
|
2017-12-06 04:39:44 +00:00
|
|
|
|
|
|
|
#include <climits>
|
2017-12-06 03:32:33 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
optional<int> FindIncludeLine(const std::vector<std::string>& lines,
|
|
|
|
const std::string& full_include_line) {
|
|
|
|
//
|
|
|
|
// This returns an include line. For example,
|
|
|
|
//
|
|
|
|
// #include <a> // 0
|
|
|
|
// #include <c> // 1
|
|
|
|
//
|
|
|
|
// Given #include <b>, this will return '1', which means that the
|
|
|
|
// #include <b> text should be inserted at the start of line 1. Inserting
|
|
|
|
// at the start of a line allows insertion at both the top and bottom of the
|
|
|
|
// document.
|
|
|
|
//
|
|
|
|
// If the include line is already in the document this returns nullopt.
|
|
|
|
//
|
|
|
|
|
|
|
|
optional<int> last_include_line;
|
|
|
|
optional<int> best_include_line;
|
|
|
|
|
|
|
|
// 1 => include line is gt content (ie, it should go after)
|
|
|
|
// -1 => include line is lt content (ie, it should go before)
|
|
|
|
int last_line_compare = 1;
|
|
|
|
|
|
|
|
for (int line = 0; line < (int)lines.size(); ++line) {
|
2018-01-14 18:50:54 +00:00
|
|
|
std::string text = Trim(lines[line]);
|
|
|
|
if (!StartsWith(text, "#include")) {
|
2017-12-06 03:32:33 +00:00
|
|
|
last_line_compare = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_include_line = line;
|
|
|
|
|
2018-01-14 18:50:54 +00:00
|
|
|
int current_line_compare = full_include_line.compare(text);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (current_line_compare == 0)
|
|
|
|
return nullopt;
|
|
|
|
|
|
|
|
if (last_line_compare == 1 && current_line_compare == -1)
|
|
|
|
best_include_line = line;
|
|
|
|
last_line_compare = current_line_compare;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best_include_line)
|
|
|
|
return *best_include_line;
|
|
|
|
// If |best_include_line| didn't match that means we likely didn't find an
|
|
|
|
// include which was lt the new one, so put it at the end of the last include
|
|
|
|
// list.
|
|
|
|
if (last_include_line)
|
|
|
|
return *last_include_line + 1;
|
|
|
|
// No includes, use top of document.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<QueryFileId> GetImplementationFile(QueryDatabase* db,
|
|
|
|
QueryFileId file_id,
|
|
|
|
QueryFile* file) {
|
|
|
|
for (SymbolRef sym : file->def->outline) {
|
2018-02-09 17:42:10 +00:00
|
|
|
switch (sym.kind) {
|
2017-12-06 03:32:33 +00:00
|
|
|
case SymbolKind::Func: {
|
2018-02-18 07:09:05 +00:00
|
|
|
if (const auto* def = db->GetFunc(sym).AnyDef()) {
|
|
|
|
// Note: we ignore the definition if it is in the same file (ie,
|
|
|
|
// possibly a header).
|
|
|
|
if (def->extent) {
|
|
|
|
QueryFileId t = def->extent->file;
|
|
|
|
if (t != file_id)
|
|
|
|
return t;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Var: {
|
2018-02-18 05:52:14 +00:00
|
|
|
const QueryVar::Def* def = db->GetVar(sym).AnyDef();
|
2017-12-06 03:32:33 +00:00
|
|
|
// Note: we ignore the definition if it is in the same file (ie,
|
|
|
|
// possibly a header).
|
2018-02-18 05:52:14 +00:00
|
|
|
if (def && def->extent) {
|
|
|
|
QueryFileId t = def->extent->file;
|
2018-02-10 01:30:22 +00:00
|
|
|
if (t != file_id)
|
|
|
|
return t;
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No associated definition, scan the project for a file in the same
|
|
|
|
// directory with the same base-name.
|
2018-01-30 00:55:51 +00:00
|
|
|
NormalizedPath original_path(file->def->path);
|
|
|
|
std::string target_path = original_path.path;
|
2017-12-06 03:32:33 +00:00
|
|
|
size_t last = target_path.find_last_of('.');
|
|
|
|
if (last != std::string::npos) {
|
|
|
|
target_path = target_path.substr(0, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_S(INFO) << "!! Looking for impl file that starts with " << target_path;
|
|
|
|
|
|
|
|
for (auto& entry : db->usr_to_file) {
|
2018-01-30 00:55:51 +00:00
|
|
|
const NormalizedPath& path = entry.first;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
// Do not consider header files for implementation files.
|
|
|
|
// TODO: make file extensions configurable.
|
2018-01-30 00:55:51 +00:00
|
|
|
if (EndsWith(path.path, ".h") || EndsWith(path.path, ".hpp"))
|
2017-12-06 03:32:33 +00:00
|
|
|
continue;
|
|
|
|
|
2018-01-30 00:55:51 +00:00
|
|
|
if (StartsWith(path.path, target_path) && path != original_path) {
|
2017-12-06 03:32:33 +00:00
|
|
|
return entry.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnsureImplFile(QueryDatabase* db,
|
|
|
|
QueryFileId file_id,
|
|
|
|
optional<lsDocumentUri>& impl_uri,
|
|
|
|
optional<QueryFileId>& impl_file_id) {
|
|
|
|
if (!impl_uri.has_value()) {
|
|
|
|
QueryFile& file = db->files[file_id.id];
|
|
|
|
assert(file.def);
|
|
|
|
|
|
|
|
impl_file_id = GetImplementationFile(db, file_id, &file);
|
|
|
|
if (!impl_file_id.has_value())
|
|
|
|
impl_file_id = file_id;
|
|
|
|
|
|
|
|
QueryFile& impl_file = db->files[impl_file_id->id];
|
|
|
|
if (impl_file.def)
|
|
|
|
impl_uri = lsDocumentUri::FromPath(impl_file.def->path);
|
|
|
|
else
|
|
|
|
impl_uri = lsDocumentUri::FromPath(file.def->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<lsTextEdit> BuildAutoImplementForFunction(QueryDatabase* db,
|
|
|
|
WorkingFiles* working_files,
|
|
|
|
WorkingFile* working_file,
|
|
|
|
int default_line,
|
|
|
|
QueryFileId decl_file_id,
|
|
|
|
QueryFileId impl_file_id,
|
|
|
|
QueryFunc& func) {
|
2018-02-18 07:09:05 +00:00
|
|
|
const QueryFunc::Def* def = func.AnyDef();
|
|
|
|
assert(def);
|
2018-02-12 04:22:47 +00:00
|
|
|
for (Use decl : func.declarations) {
|
|
|
|
if (decl.file != decl_file_id)
|
2017-12-06 03:32:33 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
optional<lsRange> ls_decl = GetLsRange(working_file, decl.range);
|
|
|
|
if (!ls_decl)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
optional<std::string> type_name;
|
|
|
|
optional<lsPosition> same_file_insert_end;
|
2018-02-18 07:09:05 +00:00
|
|
|
if (def->declaring_type) {
|
|
|
|
QueryType& declaring_type = db->types[def->declaring_type->id];
|
|
|
|
if (const auto* def1 = declaring_type.AnyDef()) {
|
|
|
|
type_name = std::string(def1->ShortName());
|
|
|
|
optional<lsRange> ls_type_extent =
|
|
|
|
GetLsRange(working_file, def1->extent->range);
|
2018-02-11 04:01:10 +00:00
|
|
|
if (ls_type_extent) {
|
|
|
|
same_file_insert_end = ls_type_extent->end;
|
2017-12-06 03:32:33 +00:00
|
|
|
same_file_insert_end->character += 1; // move past semicolon.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string insert_text;
|
|
|
|
int newlines_after_name = 0;
|
|
|
|
LexFunctionDeclaration(working_file->buffer_content, ls_decl->start,
|
|
|
|
type_name, &insert_text, &newlines_after_name);
|
|
|
|
|
|
|
|
if (!same_file_insert_end) {
|
|
|
|
same_file_insert_end = ls_decl->end;
|
|
|
|
same_file_insert_end->line += newlines_after_name;
|
|
|
|
same_file_insert_end->character = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
lsTextEdit edit;
|
|
|
|
|
|
|
|
if (decl_file_id == impl_file_id) {
|
|
|
|
edit.range.start = *same_file_insert_end;
|
|
|
|
edit.range.end = *same_file_insert_end;
|
|
|
|
edit.newText = "\n\n" + insert_text;
|
|
|
|
} else {
|
|
|
|
lsPosition best_pos;
|
|
|
|
best_pos.line = default_line;
|
|
|
|
int best_dist = INT_MAX;
|
|
|
|
|
|
|
|
QueryFile& file = db->files[impl_file_id.id];
|
|
|
|
assert(file.def);
|
|
|
|
for (SymbolRef sym : file.def->outline) {
|
2018-02-09 17:42:10 +00:00
|
|
|
switch (sym.kind) {
|
2017-12-06 03:32:33 +00:00
|
|
|
case SymbolKind::Func: {
|
2018-02-10 03:07:45 +00:00
|
|
|
QueryFunc& sym_func = db->GetFunc(sym);
|
2018-02-18 07:09:05 +00:00
|
|
|
const QueryFunc::Def* def1 = sym_func.AnyDef();
|
|
|
|
if (!def1 || !def1->extent)
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
|
2018-02-12 04:22:47 +00:00
|
|
|
for (Use func_decl : sym_func.declarations) {
|
|
|
|
if (func_decl.file == decl_file_id) {
|
2017-12-06 03:32:33 +00:00
|
|
|
int dist = func_decl.range.start.line - decl.range.start.line;
|
|
|
|
if (abs(dist) < abs(best_dist)) {
|
2018-02-22 07:34:32 +00:00
|
|
|
optional<lsLocation> def_loc =
|
|
|
|
GetLsLocation(db, working_files, *def1->extent);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (!def_loc)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
best_dist = dist;
|
|
|
|
|
|
|
|
if (dist > 0)
|
|
|
|
best_pos = def_loc->range.start;
|
|
|
|
else
|
|
|
|
best_pos = def_loc->range.end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Var: {
|
|
|
|
// TODO: handle vars.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SymbolKind::Invalid:
|
|
|
|
case SymbolKind::File:
|
|
|
|
case SymbolKind::Type:
|
|
|
|
LOG_S(WARNING) << "Unexpected SymbolKind "
|
2018-02-09 17:42:10 +00:00
|
|
|
<< static_cast<int>(sym.kind);
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
edit.range.start = best_pos;
|
|
|
|
edit.range.end = best_pos;
|
|
|
|
if (best_dist < 0)
|
|
|
|
edit.newText = "\n\n" + insert_text;
|
|
|
|
else
|
|
|
|
edit.newText = insert_text + "\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return edit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
|
2017-12-06 04:39:44 +00:00
|
|
|
struct Ipc_TextDocumentCodeAction
|
2018-01-19 08:56:09 +00:00
|
|
|
: public RequestMessage<Ipc_TextDocumentCodeAction> {
|
2017-12-06 04:39:44 +00:00
|
|
|
const static IpcId kIpcId = IpcId::TextDocumentCodeAction;
|
|
|
|
// Contains additional diagnostic information about the context in which
|
|
|
|
// a code action is run.
|
|
|
|
struct lsCodeActionContext {
|
|
|
|
// An array of diagnostics.
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsDiagnostic> diagnostics;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
// Params for the CodeActionRequest
|
|
|
|
struct lsCodeActionParams {
|
|
|
|
// The document in which the command was invoked.
|
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
// The range for which the command was invoked.
|
|
|
|
lsRange range;
|
|
|
|
// Context carrying additional information.
|
|
|
|
lsCodeActionContext context;
|
|
|
|
};
|
|
|
|
lsCodeActionParams params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeAction::lsCodeActionContext,
|
|
|
|
diagnostics);
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeAction::lsCodeActionParams,
|
|
|
|
textDocument,
|
|
|
|
range,
|
|
|
|
context);
|
|
|
|
MAKE_REFLECT_STRUCT(Ipc_TextDocumentCodeAction, id, params);
|
|
|
|
REGISTER_IPC_MESSAGE(Ipc_TextDocumentCodeAction);
|
|
|
|
|
|
|
|
struct Out_TextDocumentCodeAction
|
|
|
|
: public lsOutMessage<Out_TextDocumentCodeAction> {
|
|
|
|
struct CommandArgs {
|
|
|
|
lsDocumentUri textDocumentUri;
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsTextEdit> edits;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
using Command = lsCommand<CommandArgs>;
|
|
|
|
|
|
|
|
lsRequestId id;
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<Command> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT_WRITER_AS_ARRAY(Out_TextDocumentCodeAction::CommandArgs,
|
|
|
|
textDocumentUri,
|
|
|
|
edits);
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentCodeAction, jsonrpc, id, result);
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
struct TextDocumentCodeActionHandler
|
|
|
|
: BaseMessageHandler<Ipc_TextDocumentCodeAction> {
|
|
|
|
void Run(Ipc_TextDocumentCodeAction* request) override {
|
|
|
|
// NOTE: This code snippet will generate some FixIts for testing:
|
|
|
|
//
|
|
|
|
// struct origin { int x, int y };
|
|
|
|
// void foo() {
|
|
|
|
// point origin = {
|
|
|
|
// x: 0.0,
|
|
|
|
// y: 0.0
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
|
|
|
|
QueryFileId file_id;
|
|
|
|
QueryFile* file;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2017-12-06 03:32:33 +00:00
|
|
|
request->params.textDocument.uri.GetPath(), &file,
|
|
|
|
&file_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkingFile* working_file = working_files->GetFileByFilename(
|
|
|
|
request->params.textDocument.uri.GetPath());
|
|
|
|
if (!working_file) {
|
|
|
|
// TODO: send error response.
|
|
|
|
LOG_S(WARNING)
|
|
|
|
<< "[error] textDocument/codeAction could not find working file";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Out_TextDocumentCodeAction out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
|
|
|
// TODO: auto-insert namespace?
|
|
|
|
|
2018-01-14 18:50:54 +00:00
|
|
|
int default_line = (int)working_file->buffer_lines.size();
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
// Make sure to call EnsureImplFile before using these. We lazy load
|
|
|
|
// them because computing the values could involve an entire project
|
|
|
|
// scan.
|
|
|
|
optional<lsDocumentUri> impl_uri;
|
|
|
|
optional<QueryFileId> impl_file_id;
|
|
|
|
|
|
|
|
std::vector<SymbolRef> syms =
|
|
|
|
FindSymbolsAtLocation(working_file, file, request->params.range.start);
|
|
|
|
for (SymbolRef sym : syms) {
|
2018-02-09 17:42:10 +00:00
|
|
|
switch (sym.kind) {
|
2017-12-06 03:32:33 +00:00
|
|
|
case SymbolKind::Type: {
|
2018-02-10 03:07:45 +00:00
|
|
|
QueryType& type = db->GetType(sym);
|
2018-02-18 07:09:05 +00:00
|
|
|
const QueryType::Def* def = type.AnyDef();
|
|
|
|
if (!def)
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
int num_edits = 0;
|
|
|
|
|
|
|
|
// Get implementation file.
|
|
|
|
Out_TextDocumentCodeAction::Command command;
|
|
|
|
|
2018-02-21 01:50:48 +00:00
|
|
|
EachDefinedEntity(db->funcs, def->funcs, [&](QueryFunc& func_def) {
|
2018-02-18 07:09:05 +00:00
|
|
|
const QueryFunc::Def* def1 = func_def.AnyDef();
|
|
|
|
if (def1->extent)
|
2018-02-04 00:20:14 +00:00
|
|
|
return;
|
2017-12-06 03:32:33 +00:00
|
|
|
EnsureImplFile(db, file_id, impl_uri /*out*/, impl_file_id /*out*/);
|
|
|
|
optional<lsTextEdit> edit = BuildAutoImplementForFunction(
|
|
|
|
db, working_files, working_file, default_line, file_id,
|
|
|
|
*impl_file_id, func_def);
|
|
|
|
if (!edit)
|
2018-02-04 00:20:14 +00:00
|
|
|
return;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
++num_edits;
|
|
|
|
|
|
|
|
// Merge edits together if they are on the same line.
|
|
|
|
// TODO: be smarter about newline merging? ie, don't end up
|
|
|
|
// with foo()\n\n\n\nfoo(), we want foo()\n\nfoo()\n\n
|
|
|
|
//
|
|
|
|
if (!command.arguments.edits.empty() &&
|
|
|
|
command.arguments.edits[command.arguments.edits.size() - 1]
|
|
|
|
.range.end.line == edit->range.start.line) {
|
|
|
|
command.arguments.edits[command.arguments.edits.size() - 1]
|
|
|
|
.newText += edit->newText;
|
|
|
|
} else {
|
|
|
|
command.arguments.edits.push_back(*edit);
|
|
|
|
}
|
2018-02-04 00:20:14 +00:00
|
|
|
});
|
2017-12-06 03:32:33 +00:00
|
|
|
if (command.arguments.edits.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If we're inserting at the end of the document, put a newline
|
|
|
|
// before the insertion.
|
|
|
|
if (command.arguments.edits[0].range.start.line >= default_line)
|
|
|
|
command.arguments.edits[0].newText.insert(0, "\n");
|
|
|
|
|
|
|
|
command.arguments.textDocumentUri = *impl_uri;
|
|
|
|
command.title = "Auto-Implement " + std::to_string(num_edits) +
|
2018-02-18 07:09:05 +00:00
|
|
|
" methods on " + std::string(def->ShortName());
|
2017-12-06 03:32:33 +00:00
|
|
|
command.command = "cquery._autoImplement";
|
|
|
|
out.result.push_back(command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SymbolKind::Func: {
|
2018-02-10 03:07:45 +00:00
|
|
|
QueryFunc& func = db->GetFunc(sym);
|
2018-02-18 07:09:05 +00:00
|
|
|
const QueryFunc::Def* def = func.AnyDef();
|
|
|
|
if (!def || def->extent)
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
EnsureImplFile(db, file_id, impl_uri /*out*/, impl_file_id /*out*/);
|
|
|
|
|
|
|
|
// Get implementation file.
|
|
|
|
Out_TextDocumentCodeAction::Command command;
|
2018-02-18 07:09:05 +00:00
|
|
|
command.title = "Auto-Implement " + std::string(def->ShortName());
|
2017-12-06 03:32:33 +00:00
|
|
|
command.command = "cquery._autoImplement";
|
|
|
|
command.arguments.textDocumentUri = *impl_uri;
|
|
|
|
optional<lsTextEdit> edit = BuildAutoImplementForFunction(
|
|
|
|
db, working_files, working_file, default_line, file_id,
|
|
|
|
*impl_file_id, func);
|
|
|
|
if (!edit)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If we're inserting at the end of the document, put a newline
|
|
|
|
// before the insertion.
|
|
|
|
if (edit->range.start.line >= default_line)
|
|
|
|
edit->newText.insert(0, "\n");
|
|
|
|
command.arguments.edits.push_back(*edit);
|
|
|
|
out.result.push_back(command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only show one auto-impl section.
|
|
|
|
if (!out.result.empty())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<lsDiagnostic> diagnostics;
|
|
|
|
working_files->DoAction(
|
|
|
|
[&]() { diagnostics = working_file->diagnostics_; });
|
|
|
|
for (lsDiagnostic& diag : diagnostics) {
|
|
|
|
if (diag.range.start.line != request->params.range.start.line)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// For error diagnostics, provide an action to resolve an include.
|
|
|
|
// TODO: find a way to index diagnostic contents so line numbers
|
|
|
|
// don't get mismatched when actively editing a file.
|
2018-02-22 23:49:16 +00:00
|
|
|
std::string_view include_query =
|
|
|
|
LexIdentifierAroundPos(diag.range.start, working_file->buffer_content);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (diag.severity == lsDiagnosticSeverity::Error &&
|
|
|
|
!include_query.empty()) {
|
|
|
|
const size_t kMaxResults = 20;
|
|
|
|
|
|
|
|
std::unordered_set<std::string> include_absolute_paths;
|
|
|
|
|
|
|
|
// Find include candidate strings.
|
2018-01-31 05:55:53 +00:00
|
|
|
for (size_t i = 0; i < db->symbols.size(); ++i) {
|
2017-12-06 03:32:33 +00:00
|
|
|
if (include_absolute_paths.size() > kMaxResults)
|
|
|
|
break;
|
2018-02-02 01:59:01 +00:00
|
|
|
if (db->GetSymbolDetailedName(i).find(include_query) ==
|
|
|
|
std::string::npos)
|
2017-12-06 03:32:33 +00:00
|
|
|
continue;
|
|
|
|
|
2018-02-10 08:19:17 +00:00
|
|
|
Maybe<QueryFileId> decl_file_id =
|
2017-12-06 03:32:33 +00:00
|
|
|
GetDeclarationFileForSymbol(db, db->symbols[i]);
|
|
|
|
if (!decl_file_id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QueryFile& decl_file = db->files[decl_file_id->id];
|
|
|
|
if (!decl_file.def)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
include_absolute_paths.insert(decl_file.def->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build include strings.
|
|
|
|
std::unordered_set<std::string> include_insert_strings;
|
|
|
|
include_insert_strings.reserve(include_absolute_paths.size());
|
|
|
|
|
|
|
|
for (const std::string& path : include_absolute_paths) {
|
|
|
|
optional<lsCompletionItem> item =
|
|
|
|
include_complete->FindCompletionItemForAbsolutePath(path);
|
|
|
|
if (!item)
|
|
|
|
continue;
|
|
|
|
if (item->textEdit)
|
|
|
|
include_insert_strings.insert(item->textEdit->newText);
|
|
|
|
else if (!item->insertText.empty())
|
|
|
|
include_insert_strings.insert(item->insertText);
|
2018-02-20 20:43:02 +00:00
|
|
|
else {
|
|
|
|
// FIXME https://github.com/cquery-project/cquery/issues/463
|
|
|
|
LOG_S(WARNING) << "unable to determine insert string for include "
|
|
|
|
"completion item";
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build code action.
|
|
|
|
if (!include_insert_strings.empty()) {
|
|
|
|
Out_TextDocumentCodeAction::Command command;
|
|
|
|
|
|
|
|
// Build edits.
|
|
|
|
for (const std::string& include_insert_string :
|
|
|
|
include_insert_strings) {
|
|
|
|
lsTextEdit edit;
|
|
|
|
optional<int> include_line = FindIncludeLine(
|
2018-01-14 18:50:54 +00:00
|
|
|
working_file->buffer_lines, include_insert_string);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (!include_line)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
edit.range.start.line = *include_line;
|
|
|
|
edit.range.end.line = *include_line;
|
|
|
|
edit.newText = include_insert_string + "\n";
|
|
|
|
command.arguments.edits.push_back(edit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup metadata and send to client.
|
|
|
|
if (include_insert_strings.size() == 1)
|
|
|
|
command.title = "Insert " + *include_insert_strings.begin();
|
|
|
|
else
|
|
|
|
command.title = "Pick one of " +
|
|
|
|
std::to_string(command.arguments.edits.size()) +
|
|
|
|
" includes to insert";
|
|
|
|
command.command = "cquery._insertInclude";
|
|
|
|
command.arguments.textDocumentUri = request->params.textDocument.uri;
|
|
|
|
out.result.push_back(command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clang does not provide accurate enough column reporting for
|
|
|
|
// diagnostics to do good column filtering, so report all
|
|
|
|
// diagnostics on the line.
|
|
|
|
if (!diag.fixits_.empty()) {
|
|
|
|
Out_TextDocumentCodeAction::Command command;
|
|
|
|
command.title = "FixIt: " + diag.message;
|
|
|
|
command.command = "cquery._applyFixIt";
|
|
|
|
command.arguments.textDocumentUri = request->params.textDocument.uri;
|
|
|
|
command.arguments.edits = diag.fixits_;
|
|
|
|
out.result.push_back(command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::TextDocumentCodeAction, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
REGISTER_MESSAGE_HANDLER(TextDocumentCodeActionHandler);
|
|
|
|
|
|
|
|
TEST_SUITE("FindIncludeLine") {
|
|
|
|
TEST_CASE("in document") {
|
|
|
|
std::vector<std::string> lines = {
|
|
|
|
"#include <bbb>", // 0
|
|
|
|
"#include <ddd>" // 1
|
|
|
|
};
|
|
|
|
|
|
|
|
REQUIRE(FindIncludeLine(lines, "#include <bbb>") == nullopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("insert before") {
|
|
|
|
std::vector<std::string> lines = {
|
|
|
|
"#include <bbb>", // 0
|
|
|
|
"#include <ddd>" // 1
|
|
|
|
};
|
|
|
|
|
|
|
|
REQUIRE(FindIncludeLine(lines, "#include <aaa>") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("insert middle") {
|
|
|
|
std::vector<std::string> lines = {
|
|
|
|
"#include <bbb>", // 0
|
|
|
|
"#include <ddd>" // 1
|
|
|
|
};
|
|
|
|
|
|
|
|
REQUIRE(FindIncludeLine(lines, "#include <ccc>") == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("insert after") {
|
|
|
|
std::vector<std::string> lines = {
|
|
|
|
"#include <bbb>", // 0
|
|
|
|
"#include <ddd>", // 1
|
|
|
|
"", // 2
|
|
|
|
};
|
|
|
|
|
|
|
|
REQUIRE(FindIncludeLine(lines, "#include <eee>") == 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("ignore header") {
|
|
|
|
std::vector<std::string> lines = {
|
|
|
|
"// FOOBAR", // 0
|
|
|
|
"// FOOBAR", // 1
|
|
|
|
"// FOOBAR", // 2
|
|
|
|
"// FOOBAR", // 3
|
|
|
|
"", // 4
|
|
|
|
"#include <bbb>", // 5
|
|
|
|
"#include <ddd>", // 6
|
|
|
|
"", // 7
|
|
|
|
};
|
|
|
|
|
|
|
|
REQUIRE(FindIncludeLine(lines, "#include <a>") == 5);
|
|
|
|
REQUIRE(FindIncludeLine(lines, "#include <c>") == 6);
|
|
|
|
REQUIRE(FindIncludeLine(lines, "#include <e>") == 7);
|
|
|
|
}
|
|
|
|
}
|
2017-12-31 03:18:33 +00:00
|
|
|
} // namespace
|