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-13 01:15:19 +00:00
|
|
|
#include "symbol.h"
|
2018-10-01 05:54:48 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-05-28 00:50:02 +00:00
|
|
|
using namespace ccls;
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/documentHighlight";
|
|
|
|
|
2018-10-01 05:54:48 +00:00
|
|
|
struct lsDocumentHighlight {
|
|
|
|
enum Kind { Text = 1, Read = 2, Write = 3 };
|
|
|
|
|
|
|
|
lsRange range;
|
|
|
|
int kind = 1;
|
|
|
|
|
|
|
|
// ccls extension
|
|
|
|
Role role = Role::None;
|
|
|
|
|
|
|
|
bool operator<(const lsDocumentHighlight &o) const {
|
|
|
|
return !(range == o.range) ? range < o.range : kind < o.kind;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(lsDocumentHighlight, range, kind, role);
|
|
|
|
|
2018-03-22 05:01:21 +00:00
|
|
|
struct In_TextDocumentDocumentHighlight : public RequestInMessage {
|
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_TextDocumentDocumentHighlight, id, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentDocumentHighlight);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
|
|
|
struct Out_TextDocumentDocumentHighlight
|
|
|
|
: public lsOutMessage<Out_TextDocumentDocumentHighlight> {
|
|
|
|
lsRequestId id;
|
2017-12-12 05:20:29 +00:00
|
|
|
std::vector<lsDocumentHighlight> result;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_TextDocumentDocumentHighlight, jsonrpc, id, result);
|
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
struct Handler_TextDocumentDocumentHighlight
|
|
|
|
: BaseMessageHandler<In_TextDocumentDocumentHighlight> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2018-08-09 17:08:14 +00:00
|
|
|
void Run(In_TextDocumentDocumentHighlight *request) override {
|
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,
|
2017-12-06 03:32:33 +00:00
|
|
|
request->params.textDocument.uri.GetPath(), &file,
|
2018-10-01 05:54:48 +00:00
|
|
|
&file_id))
|
2017-12-06 03:32:33 +00:00
|
|
|
return;
|
2018-08-09 17:08:14 +00:00
|
|
|
WorkingFile *working_file =
|
2017-12-06 03:32:33 +00:00
|
|
|
working_files->GetFileByFilename(file->def->path);
|
|
|
|
|
|
|
|
Out_TextDocumentDocumentHighlight out;
|
|
|
|
out.id = request->id;
|
|
|
|
|
2018-10-01 05:54:48 +00:00
|
|
|
std::vector<SymbolRef> syms = FindSymbolsAtLocation(
|
|
|
|
working_file, file, request->params.position, true);
|
|
|
|
for (auto [sym, refcnt] : file->symbol2refcnt) {
|
|
|
|
if (refcnt <= 0)
|
|
|
|
continue;
|
|
|
|
Usr usr = sym.usr;
|
|
|
|
SymbolKind kind = sym.kind;
|
|
|
|
if (std::none_of(syms.begin(), syms.end(), [&](auto &sym1) {
|
|
|
|
return usr == sym1.usr && kind == sym1.kind;
|
|
|
|
}))
|
|
|
|
continue;
|
2018-10-04 23:13:30 +00:00
|
|
|
if (auto loc = GetLsLocation(db, working_files, sym, file_id)) {
|
2018-10-01 05:54:48 +00:00
|
|
|
lsDocumentHighlight highlight;
|
2018-10-04 23:13:30 +00:00
|
|
|
highlight.range = loc->range;
|
2018-10-01 05:54:48 +00:00
|
|
|
if (sym.role & Role::Write)
|
|
|
|
highlight.kind = lsDocumentHighlight::Write;
|
|
|
|
else if (sym.role & Role::Read)
|
|
|
|
highlight.kind = lsDocumentHighlight::Read;
|
|
|
|
else
|
|
|
|
highlight.kind = lsDocumentHighlight::Text;
|
|
|
|
highlight.role = sym.role;
|
|
|
|
out.result.push_back(highlight);
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
2018-10-01 05:54:48 +00:00
|
|
|
std::sort(out.result.begin(), out.result.end());
|
2018-05-28 00:50:02 +00:00
|
|
|
pipeline::WriteStdout(kMethodType, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDocumentHighlight);
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|