From ca550720e4d25ba8f2c12b0f116a4cf14ea1dbc2 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 28 Oct 2018 21:39:17 -0700 Subject: [PATCH] Simplify and better compatibility with encodings retaining the feature of low bytes being 1-byte characters --- src/clang_complete.cc | 10 ++++------ src/messages/ccls_call.cc | 3 +-- src/messages/ccls_member.cc | 12 +++++------- src/messages/initialize.cc | 3 +-- src/messages/textDocument_hover.cc | 4 ++-- src/messages/textDocument_rename.cc | 7 +++---- src/pipeline.cc | 3 +-- src/pipeline.hh | 3 --- src/working_files.cc | 9 +++++---- src/working_files.hh | 2 +- 10 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/clang_complete.cc b/src/clang_complete.cc index 9566ebb5..16db7bcf 100644 --- a/src/clang_complete.cc +++ b/src/clang_complete.cc @@ -406,7 +406,7 @@ void *CompletionMain(void *manager_) { DiagnosticConsumer DC; WorkingFiles::Snapshot snapshot = - manager->wfiles_->AsSnapshot({StripFileType(path)}); + manager->wfiles_->AsSnapshot({StripFileType(path)}); std::vector> Bufs; auto Clang = BuildCompilerInstance(*session, std::move(CI), FS, DC, preamble.get(), snapshot, Bufs); @@ -563,13 +563,11 @@ std::shared_ptr CompletionSession::GetPreamble() { return preamble; } -CompletionManager::CompletionManager(Project *project, - WorkingFiles *wfiles, +CompletionManager::CompletionManager(Project *project, WorkingFiles *wfiles, OnDiagnostic on_diagnostic, OnDropped on_dropped) - : project_(project), wfiles_(wfiles), - on_diagnostic_(on_diagnostic), on_dropped_(on_dropped), - preloads(kMaxPreloadedSessions), + : project_(project), wfiles_(wfiles), on_diagnostic_(on_diagnostic), + on_dropped_(on_dropped), preloads(kMaxPreloadedSessions), sessions(kMaxCompletionSessions), PCH(std::make_shared()) { SpawnThread(ccls::CompletionMain, this); diff --git a/src/messages/ccls_call.cc b/src/messages/ccls_call.cc index 2cb703b4..363552d9 100644 --- a/src/messages/ccls_call.cc +++ b/src/messages/ccls_call.cc @@ -192,8 +192,7 @@ void MessageHandler::ccls_call(Reader &reader, ReplyOnce &reply) { QueryFile *file = FindFile(reply, param.textDocument.uri.GetPath()); if (!file) return; - WorkingFile *working_file = - wfiles->GetFileByFilename(file->def->path); + WorkingFile *working_file = wfiles->GetFileByFilename(file->def->path); for (SymbolRef sym : FindSymbolsAtLocation(working_file, file, param.position)) { if (sym.kind == SymbolKind::Func) { diff --git a/src/messages/ccls_member.cc b/src/messages/ccls_member.cc index e3d97fb4..3e6d1804 100644 --- a/src/messages/ccls_member.cc +++ b/src/messages/ccls_member.cc @@ -133,13 +133,13 @@ bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified, if (def1 && def1->spell) { // The declaration of target type. if (std::optional loc = - GetLsLocation(m->db, m->wfiles, *def1->spell)) + GetLsLocation(m->db, m->wfiles, *def1->spell)) entry1.location = *loc; } else if (def->spell) { // Builtin types have no declaration but the typedef declaration // itself is useful. if (std::optional loc = - GetLsLocation(m->db, m->wfiles, *def->spell)) + GetLsLocation(m->db, m->wfiles, *def->spell)) entry1.location = *loc; } if (def1 && qualified) @@ -160,8 +160,7 @@ bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified, Out_cclsMember entry1; entry1.fieldName = def1->Name(false); if (def1->spell) { - if (auto loc = - GetLsLocation(m->db, m->wfiles, *def1->spell)) + if (auto loc = GetLsLocation(m->db, m->wfiles, *def1->spell)) entry1.location = *loc; } else if (func1.declarations.size()) { if (auto loc = GetLsLocation(m->db, m->wfiles, @@ -181,12 +180,11 @@ bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified, Out_cclsMember entry1; entry1.fieldName = def1->Name(false); if (def1->spell) { - if (auto loc = - GetLsLocation(m->db, m->wfiles, *def1->spell)) + if (auto loc = GetLsLocation(m->db, m->wfiles, *def1->spell)) entry1.location = *loc; } else if (type1.declarations.size()) { if (auto loc = GetLsLocation(m->db, m->wfiles, - type1.declarations[0])) + type1.declarations[0])) entry1.location = *loc; } entry->children.push_back(std::move(entry1)); diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index 2e4b1fd9..4c6ef4bc 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -348,8 +348,7 @@ void *Indexer(void *arg_) { delete arg; std::string name = "indexer" + std::to_string(idx); set_thread_name(name.c_str()); - pipeline::Indexer_Main(h->clang_complete, h->vfs, h->project, - h->wfiles); + pipeline::Indexer_Main(h->clang_complete, h->vfs, h->project, h->wfiles); return nullptr; } } // namespace diff --git a/src/messages/textDocument_hover.cc b/src/messages/textDocument_hover.cc index a7f291e2..b9c4d707 100644 --- a/src/messages/textDocument_hover.cc +++ b/src/messages/textDocument_hover.cc @@ -89,8 +89,8 @@ void MessageHandler::textDocument_hover(TextDocumentPositionParam ¶m, Hover result; for (SymbolRef sym : FindSymbolsAtLocation(wfile, file, param.position)) { - std::optional ls_range = GetLsRange( - wfiles->GetFileByFilename(file->def->path), sym.range); + std::optional ls_range = + GetLsRange(wfiles->GetFileByFilename(file->def->path), sym.range); if (!ls_range) continue; diff --git a/src/messages/textDocument_rename.cc b/src/messages/textDocument_rename.cc index 4dd8f5e2..f241e8b9 100644 --- a/src/messages/textDocument_rename.cc +++ b/src/messages/textDocument_rename.cc @@ -6,13 +6,12 @@ namespace ccls { namespace { -lsWorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *wfiles, - SymbolRef sym, const std::string &new_text) { +lsWorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *wfiles, SymbolRef sym, + const std::string &new_text) { std::unordered_map path_to_edit; EachOccurrence(db, sym, true, [&](Use use) { - std::optional ls_location = - GetLsLocation(db, wfiles, use); + std::optional ls_location = GetLsLocation(db, wfiles, use); if (!ls_location) return; diff --git a/src/pipeline.cc b/src/pipeline.cc index 5bb20457..3f1b8dc7 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -388,8 +388,7 @@ void Main_OnIndexed(DB *db, WorkingFiles *wfiles, IndexUpdate *update) { // Update indexed content, skipped ranges, and semantic highlighting. if (update->files_def_update) { auto &def_u = *update->files_def_update; - if (WorkingFile *wfile = - wfiles->GetFileByFilename(def_u.first.path)) { + if (WorkingFile *wfile = wfiles->GetFileByFilename(def_u.first.path)) { // FIXME With index.onChange: true, use buffer_content only for // request.path wfile->SetIndexContent(g_config->index.onChange ? wfile->buffer_content diff --git a/src/pipeline.hh b/src/pipeline.hh index 6ac8bdf0..476eb0aa 100644 --- a/src/pipeline.hh +++ b/src/pipeline.hh @@ -60,9 +60,6 @@ template void Notify(const char *method, T &result) { } void Reply(lsRequestId id, const std::function &fn); -template void Reply(lsRequestId id, T &result) { - Reply(id, [&](Writer &w) { Reflect(w, result); }); -} void ReplyError(lsRequestId id, const std::function &fn); template void ReplyError(lsRequestId id, T &result) { diff --git a/src/working_files.cc b/src/working_files.cc index fe8ecc83..3a0743a4 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -526,12 +526,13 @@ WorkingFiles::AsSnapshot(const std::vector &filter_paths) { // text documents. // We use a UTF-8 iterator to approximate UTF-16 in the specification (weird). // This is good enough and fails only for UTF-16 surrogate pairs. -int GetOffsetForPosition(lsPosition position, std::string_view content) { +int GetOffsetForPosition(lsPosition pos, std::string_view content) { size_t i = 0; - for (; position.line > 0 && i < content.size(); i++) + for (; pos.line > 0 && i < content.size(); i++) if (content[i] == '\n') - position.line--; - for (; position.character > 0 && i < content.size(); position.character--) + pos.line--; + for (; pos.character > 0 && i < content.size() && content[i] != '\n'; + pos.character--) if (uint8_t(content[i++]) >= 128) { // Skip 0b10xxxxxx while (i < content.size() && uint8_t(content[i]) >= 128 && diff --git a/src/working_files.hh b/src/working_files.hh index a268c127..ad02acd8 100644 --- a/src/working_files.hh +++ b/src/working_files.hh @@ -120,7 +120,7 @@ struct WorkingFiles { std::mutex files_mutex; // Protects |files|. }; -int GetOffsetForPosition(lsPosition position, std::string_view content); +int GetOffsetForPosition(lsPosition pos, std::string_view content); std::string_view LexIdentifierAroundPos(lsPosition position, std::string_view content);