2018-08-21 05:27:52 +00:00
|
|
|
/* Copyright 2017-2018 ccls Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
2018-05-28 00:50:02 +00:00
|
|
|
#include "pipeline.hh"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "query_utils.h"
|
|
|
|
|
2018-02-12 09:01:02 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
2018-09-23 19:10:40 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using namespace ccls;
|
2018-02-12 09:01:02 +00:00
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/definition";
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
struct In_TextDocumentDefinition : public RequestMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2017-12-06 04:39:44 +00:00
|
|
|
lsTextDocumentPositionParams params;
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentDefinition, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentDefinition);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::vector<Use> GetNonDefDeclarationTargets(DB *db, SymbolRef sym) {
|
2018-02-09 17:42:10 +00:00
|
|
|
switch (sym.kind) {
|
2018-08-09 17:08:14 +00:00
|
|
|
case SymbolKind::Var: {
|
|
|
|
std::vector<Use> ret = GetNonDefDeclarations(db, sym);
|
2018-09-16 18:29:48 +00:00
|
|
|
// If there is no declaration, jump to its type.
|
2018-08-09 17:08:14 +00:00
|
|
|
if (ret.empty()) {
|
|
|
|
for (auto &def : db->GetVar(sym).def)
|
|
|
|
if (def.type) {
|
2018-10-04 23:59:33 +00:00
|
|
|
if (Maybe<DeclRef> use = GetDefinitionSpell(
|
2018-08-09 17:08:14 +00:00
|
|
|
db, SymbolIdx{def.type, SymbolKind::Type})) {
|
|
|
|
ret.push_back(*use);
|
|
|
|
break;
|
2018-02-20 21:56:56 +00:00
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
}
|
2017-12-20 06:20:44 +00:00
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return GetNonDefDeclarations(db, sym);
|
2017-12-20 06:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
struct Handler_TextDocumentDefinition
|
|
|
|
: BaseMessageHandler<In_TextDocumentDefinition> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2018-08-09 17:08:14 +00:00
|
|
|
void Run(In_TextDocumentDefinition *request) override {
|
|
|
|
auto ¶ms = request->params;
|
2018-04-30 04:49:03 +00:00
|
|
|
int file_id;
|
2018-08-09 17:08:14 +00:00
|
|
|
QueryFile *file;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (!FindFileOrFail(db, project, request->id,
|
2018-04-08 17:03:50 +00:00
|
|
|
params.textDocument.uri.GetPath(), &file, &file_id))
|
2017-12-06 03:32:33 +00:00
|
|
|
return;
|
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
std::vector<lsLocation> result;
|
2018-02-21 04:26:17 +00:00
|
|
|
Maybe<Use> on_def;
|
2018-08-09 17:08:14 +00:00
|
|
|
WorkingFile *wfile = working_files->GetFileByFilename(file->def->path);
|
|
|
|
lsPosition &ls_pos = params.position;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-10-01 05:54:48 +00:00
|
|
|
for (SymbolRef sym : FindSymbolsAtLocation(wfile, file, ls_pos, true)) {
|
2017-12-06 03:32:33 +00:00
|
|
|
// Special cases which are handled:
|
|
|
|
// - symbol has declaration but no definition (ie, pure virtual)
|
|
|
|
// - goto declaration while in definition of recursive type
|
2018-02-20 23:40:31 +00:00
|
|
|
std::vector<Use> uses;
|
2018-08-09 17:08:14 +00:00
|
|
|
EachEntityDef(db, sym, [&](const auto &def) {
|
2018-09-16 18:29:48 +00:00
|
|
|
if (def.spell) {
|
2018-02-20 23:40:31 +00:00
|
|
|
Use spell = *def.spell;
|
2018-04-30 04:49:03 +00:00
|
|
|
if (spell.file_id == file_id &&
|
2018-04-08 17:03:50 +00:00
|
|
|
spell.range.Contains(ls_pos.line, ls_pos.character)) {
|
2018-02-21 04:26:17 +00:00
|
|
|
on_def = spell;
|
2018-02-20 23:40:31 +00:00
|
|
|
uses.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uses.push_back(spell);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
2018-02-20 23:40:31 +00:00
|
|
|
return true;
|
|
|
|
});
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-09-16 18:29:48 +00:00
|
|
|
// |uses| is empty if on a declaration/definition, otherwise it includes
|
|
|
|
// all declarations/definitions.
|
2018-02-21 04:26:17 +00:00
|
|
|
if (uses.empty()) {
|
2018-09-16 18:29:48 +00:00
|
|
|
for (Use use : GetNonDefDeclarationTargets(db, sym))
|
|
|
|
if (!(use.file_id == file_id &&
|
|
|
|
use.range.Contains(ls_pos.line, ls_pos.character)))
|
|
|
|
uses.push_back(use);
|
2018-02-21 04:26:17 +00:00
|
|
|
// There is no declaration but the cursor is on a definition.
|
|
|
|
if (uses.empty() && on_def)
|
|
|
|
uses.push_back(*on_def);
|
|
|
|
}
|
2018-10-04 23:13:30 +00:00
|
|
|
auto locs = GetLsLocations(db, working_files, uses);
|
2019-01-09 07:19:17 +00:00
|
|
|
result.insert(result.end(), locs.begin(), locs.end());
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
if (result.size()) {
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
result.erase(std::unique(result.begin(), result.end()), result.end());
|
2018-09-03 02:11:10 +00:00
|
|
|
} else {
|
2018-10-01 05:54:48 +00:00
|
|
|
Maybe<Range> range;
|
2018-09-03 02:11:10 +00:00
|
|
|
// Check #include
|
2018-08-09 17:08:14 +00:00
|
|
|
for (const IndexInclude &include : file->def->includes) {
|
2018-04-08 17:03:50 +00:00
|
|
|
if (include.line == ls_pos.line) {
|
2019-01-09 07:19:17 +00:00
|
|
|
result.push_back(
|
2018-10-04 23:13:30 +00:00
|
|
|
lsLocation{lsDocumentUri::FromPath(include.resolved_path)});
|
2018-09-03 22:08:34 +00:00
|
|
|
range = {{0, 0}, {0, 0}};
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 09:01:02 +00:00
|
|
|
// Find the best match of the identifier at point.
|
2018-09-03 22:08:34 +00:00
|
|
|
if (!range) {
|
2018-02-13 19:12:08 +00:00
|
|
|
lsPosition position = request->params.position;
|
2018-08-09 17:08:14 +00:00
|
|
|
const std::string &buffer = wfile->buffer_content;
|
2018-02-22 23:49:16 +00:00
|
|
|
std::string_view query = LexIdentifierAroundPos(position, buffer);
|
2018-03-29 23:57:10 +00:00
|
|
|
std::string_view short_query = query;
|
|
|
|
{
|
|
|
|
auto pos = query.rfind(':');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
short_query = query.substr(pos + 1);
|
|
|
|
}
|
2018-02-12 09:01:02 +00:00
|
|
|
|
2018-02-20 20:43:02 +00:00
|
|
|
// For symbols whose short/detailed names contain |query| as a
|
2018-03-29 23:57:10 +00:00
|
|
|
// substring, we use the tuple <length difference, negative position,
|
2018-02-20 20:43:02 +00:00
|
|
|
// not in the same file, line distance> to find the best match.
|
|
|
|
std::tuple<int, int, bool, int> best_score{INT_MAX, 0, true, 0};
|
2018-05-30 06:56:14 +00:00
|
|
|
SymbolIdx best_sym;
|
|
|
|
best_sym.kind = SymbolKind::Invalid;
|
|
|
|
auto fn = [&](SymbolIdx sym) {
|
|
|
|
std::string_view short_name = db->GetSymbolName(sym, false),
|
2018-04-06 00:00:07 +00:00
|
|
|
name = short_query.size() < query.size()
|
2018-05-30 06:56:14 +00:00
|
|
|
? db->GetSymbolName(sym, true)
|
2018-04-06 00:00:07 +00:00
|
|
|
: short_name;
|
|
|
|
if (short_name != short_query)
|
2018-05-30 06:56:14 +00:00
|
|
|
return;
|
2018-10-04 23:59:33 +00:00
|
|
|
if (Maybe<DeclRef> dr = GetDefinitionSpell(db, sym)) {
|
2018-03-29 23:57:10 +00:00
|
|
|
std::tuple<int, int, bool, int> score{
|
2018-04-06 00:00:07 +00:00
|
|
|
int(name.size() - short_query.size()), 0,
|
2018-10-04 23:59:33 +00:00
|
|
|
dr->file_id != file_id,
|
|
|
|
std::abs(dr->range.start.line - position.line)};
|
2018-03-29 23:57:10 +00:00
|
|
|
// Update the score with qualified name if the qualified name
|
|
|
|
// occurs in |name|.
|
2018-04-06 00:00:07 +00:00
|
|
|
auto pos = name.rfind(query);
|
2018-03-29 23:57:10 +00:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
std::get<0>(score) = int(name.size() - query.size());
|
2018-05-15 05:13:18 +00:00
|
|
|
std::get<1>(score) = -int(pos);
|
2018-03-29 23:57:10 +00:00
|
|
|
}
|
|
|
|
if (score < best_score) {
|
|
|
|
best_score = score;
|
2018-05-30 06:56:14 +00:00
|
|
|
best_sym = sym;
|
2018-03-29 23:57:10 +00:00
|
|
|
}
|
2018-02-12 09:01:02 +00:00
|
|
|
}
|
2018-05-30 06:56:14 +00:00
|
|
|
};
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &func : db->funcs)
|
2018-05-30 06:56:14 +00:00
|
|
|
fn({func.usr, SymbolKind::Func});
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &type : db->types)
|
2018-05-30 06:56:14 +00:00
|
|
|
fn({type.usr, SymbolKind::Type});
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &var : db->vars)
|
2018-05-30 06:56:14 +00:00
|
|
|
if (var.def.size() && !var.def[0].is_local())
|
|
|
|
fn({var.usr, SymbolKind::Var});
|
|
|
|
|
|
|
|
if (best_sym.kind != SymbolKind::Invalid) {
|
2018-10-04 23:59:33 +00:00
|
|
|
Maybe<DeclRef> dr = GetDefinitionSpell(db, best_sym);
|
|
|
|
assert(dr);
|
|
|
|
if (auto loc = GetLsLocation(db, working_files, *dr))
|
2019-01-09 07:19:17 +00:00
|
|
|
result.push_back(*loc);
|
2018-02-12 09:01:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 07:19:17 +00:00
|
|
|
pipeline::Reply(request->id, result);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDefinition);
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|