2018-09-03 22:08:34 +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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
#include "message_handler.hh"
|
2018-10-29 04:21:21 +00:00
|
|
|
#include "query_utils.hh"
|
2018-09-03 22:08:34 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
namespace ccls {
|
2018-09-03 22:08:34 +00:00
|
|
|
namespace {
|
2018-10-28 17:49:31 +00:00
|
|
|
struct Param {
|
2018-11-03 20:52:43 +00:00
|
|
|
TextDocumentIdentifier textDocument;
|
2018-11-04 18:30:18 +00:00
|
|
|
Position position;
|
2018-10-28 17:49:31 +00:00
|
|
|
std::string direction;
|
2018-09-03 22:08:34 +00:00
|
|
|
};
|
2018-10-28 17:49:31 +00:00
|
|
|
MAKE_REFLECT_STRUCT(Param, textDocument, position, direction);
|
2018-09-03 22:08:34 +00:00
|
|
|
|
2018-11-04 18:30:18 +00:00
|
|
|
Maybe<Range> FindParent(QueryFile *file, Pos pos) {
|
2018-10-19 05:11:43 +00:00
|
|
|
Maybe<Range> parent;
|
|
|
|
for (auto [sym, refcnt] : file->symbol2refcnt)
|
|
|
|
if (refcnt > 0 && sym.extent.Valid() && sym.extent.start <= pos &&
|
|
|
|
pos < sym.extent.end &&
|
|
|
|
(!parent || (parent->start == sym.extent.start
|
|
|
|
? parent->end < sym.extent.end
|
|
|
|
: parent->start < sym.extent.start)))
|
|
|
|
parent = sym.extent;
|
|
|
|
return parent;
|
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
} // namespace
|
2018-10-19 05:11:43 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
void MessageHandler::ccls_navigate(Reader &reader,
|
|
|
|
ReplyOnce &reply) {
|
|
|
|
Param param;
|
|
|
|
Reflect(reader, param);
|
|
|
|
QueryFile *file = FindFile(reply, param.textDocument.uri.GetPath());
|
|
|
|
if (!file)
|
|
|
|
return;
|
2018-09-03 22:08:34 +00:00
|
|
|
|
2018-10-29 04:21:21 +00:00
|
|
|
WorkingFile *wfile = wfiles->GetFileByFilename(file->def->path);
|
2018-11-04 18:30:18 +00:00
|
|
|
Position ls_pos = param.position;
|
2018-10-28 17:49:31 +00:00
|
|
|
if (wfile && wfile->index_lines.size())
|
|
|
|
if (auto line = wfile->GetIndexPosFromBufferPos(ls_pos.line,
|
|
|
|
&ls_pos.character, false))
|
|
|
|
ls_pos.line = *line;
|
2018-11-04 18:30:18 +00:00
|
|
|
Pos pos{(int16_t)ls_pos.line, (int16_t)ls_pos.character};
|
2018-09-03 22:08:34 +00:00
|
|
|
|
2018-10-28 17:49:31 +00:00
|
|
|
Maybe<Range> res;
|
|
|
|
switch (param.direction[0]) {
|
|
|
|
case 'D': {
|
|
|
|
Maybe<Range> parent = FindParent(file, pos);
|
|
|
|
for (auto [sym, refcnt] : file->symbol2refcnt)
|
|
|
|
if (refcnt > 0 && pos < sym.extent.start &&
|
|
|
|
(!parent || sym.extent.end <= parent->end) &&
|
|
|
|
(!res || sym.extent.start < res->start))
|
|
|
|
res = sym.extent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'L':
|
|
|
|
for (auto [sym, refcnt] : file->symbol2refcnt)
|
|
|
|
if (refcnt > 0 && sym.extent.Valid() && sym.extent.end <= pos &&
|
|
|
|
(!res || (res->end == sym.extent.end ? sym.extent.start < res->start
|
|
|
|
: res->end < sym.extent.end)))
|
|
|
|
res = sym.extent;
|
|
|
|
break;
|
|
|
|
case 'R': {
|
|
|
|
Maybe<Range> parent = FindParent(file, pos);
|
|
|
|
if (parent && parent->start.line == pos.line && pos < parent->end) {
|
|
|
|
pos = parent->end;
|
|
|
|
if (pos.column)
|
|
|
|
pos.column--;
|
2018-09-03 22:08:34 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
for (auto [sym, refcnt] : file->symbol2refcnt)
|
|
|
|
if (refcnt > 0 && sym.extent.Valid() && pos < sym.extent.start &&
|
|
|
|
(!res ||
|
|
|
|
(sym.extent.start == res->start ? res->end < sym.extent.end
|
|
|
|
: sym.extent.start < res->start)))
|
|
|
|
res = sym.extent;
|
|
|
|
break;
|
2018-09-03 22:08:34 +00:00
|
|
|
}
|
2018-10-28 17:49:31 +00:00
|
|
|
case 'U':
|
|
|
|
default:
|
|
|
|
for (auto [sym, refcnt] : file->symbol2refcnt)
|
|
|
|
if (refcnt > 0 && sym.extent.Valid() && sym.extent.start < pos &&
|
|
|
|
pos < sym.extent.end && (!res || res->start < sym.extent.start))
|
|
|
|
res = sym.extent;
|
|
|
|
break;
|
|
|
|
}
|
2018-11-03 20:52:43 +00:00
|
|
|
std::vector<Location> result;
|
2018-10-28 17:49:31 +00:00
|
|
|
if (res)
|
|
|
|
if (auto ls_range = GetLsRange(wfile, *res)) {
|
2018-11-03 20:52:43 +00:00
|
|
|
Location &ls_loc = result.emplace_back();
|
2018-10-28 17:49:31 +00:00
|
|
|
ls_loc.uri = param.textDocument.uri;
|
|
|
|
ls_loc.range = *ls_range;
|
|
|
|
}
|
|
|
|
reply(result);
|
|
|
|
}
|
|
|
|
} // namespace ccls
|