From eacbc1e1e73357bfd9aa6b4a8691585fbaff7458 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 24 Nov 2018 09:49:58 -0800 Subject: [PATCH] Make DocumentLink::range narrower Thanks to Riatre #135 --- src/messages/textDocument_document.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/messages/textDocument_document.cc b/src/messages/textDocument_document.cc index 2ff5d218..48734552 100644 --- a/src/messages/textDocument_document.cc +++ b/src/messages/textDocument_document.cc @@ -90,13 +90,22 @@ MAKE_REFLECT_STRUCT(DocumentLink, range, target); void MessageHandler::textDocument_documentLink(TextDocumentParam ¶m, ReplyOnce &reply) { QueryFile *file = FindFile(reply, param.textDocument.uri.GetPath()); - if (!file) + WorkingFile *wf = file ? wfiles->GetFileByFilename(file->def->path) : nullptr; + if (!wf) { + reply.Error(ErrorCode::InternalError, "not opened"); return; + } std::vector result; for (const IndexInclude &include : file->def->includes) - result.push_back({lsRange{{include.line, 0}, {include.line + 1, 0}}, - DocumentUri::FromPath(include.resolved_path)}); + if (std::optional bline = + wf->GetBufferPosFromIndexPos(include.line, nullptr, false)) { + const std::string &line = wf->buffer_lines[*bline]; + auto start = line.find_first_of("\"<"), end = line.find_last_of("\">"); + if (start < end) + result.push_back({lsRange{{*bline, (int)start + 1}, {*bline, (int)end}}, + DocumentUri::FromPath(include.resolved_path)}); + } reply(result); } // namespace ccls