Simplify and better compatibility with encodings retaining the feature of low bytes being 1-byte characters

This commit is contained in:
Fangrui Song 2018-10-28 21:39:17 -07:00
parent df20969788
commit 5599ddd343
10 changed files with 23 additions and 33 deletions

View File

@ -417,7 +417,7 @@ void *CompletionMain(void *manager_) {
DiagnosticConsumer DC; DiagnosticConsumer DC;
WorkingFiles::Snapshot snapshot = WorkingFiles::Snapshot snapshot =
manager->wfiles_->AsSnapshot({StripFileType(path)}); manager->wfiles_->AsSnapshot({StripFileType(path)});
std::vector<std::unique_ptr<llvm::MemoryBuffer>> Bufs; std::vector<std::unique_ptr<llvm::MemoryBuffer>> Bufs;
auto Clang = BuildCompilerInstance(*session, std::move(CI), FS, DC, auto Clang = BuildCompilerInstance(*session, std::move(CI), FS, DC,
preamble.get(), snapshot, Bufs); preamble.get(), snapshot, Bufs);
@ -574,13 +574,11 @@ std::shared_ptr<PreambleData> CompletionSession::GetPreamble() {
return preamble; return preamble;
} }
CompletionManager::CompletionManager(Project *project, CompletionManager::CompletionManager(Project *project, WorkingFiles *wfiles,
WorkingFiles *wfiles,
OnDiagnostic on_diagnostic, OnDiagnostic on_diagnostic,
OnDropped on_dropped) OnDropped on_dropped)
: project_(project), wfiles_(wfiles), : project_(project), wfiles_(wfiles), on_diagnostic_(on_diagnostic),
on_diagnostic_(on_diagnostic), on_dropped_(on_dropped), on_dropped_(on_dropped), preloads(kMaxPreloadedSessions),
preloads(kMaxPreloadedSessions),
sessions(kMaxCompletionSessions), sessions(kMaxCompletionSessions),
PCH(std::make_shared<PCHContainerOperations>()) { PCH(std::make_shared<PCHContainerOperations>()) {
SpawnThread(ccls::CompletionMain, this); SpawnThread(ccls::CompletionMain, this);

View File

@ -204,8 +204,7 @@ void MessageHandler::ccls_call(Reader &reader, ReplyOnce &reply) {
QueryFile *file = FindFile(reply, param.textDocument.uri.GetPath()); QueryFile *file = FindFile(reply, param.textDocument.uri.GetPath());
if (!file) if (!file)
return; return;
WorkingFile *working_file = WorkingFile *working_file = wfiles->GetFileByFilename(file->def->path);
wfiles->GetFileByFilename(file->def->path);
for (SymbolRef sym : for (SymbolRef sym :
FindSymbolsAtLocation(working_file, file, param.position)) { FindSymbolsAtLocation(working_file, file, param.position)) {
if (sym.kind == SymbolKind::Func) { if (sym.kind == SymbolKind::Func) {

View File

@ -145,13 +145,13 @@ bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified,
if (def1 && def1->spell) { if (def1 && def1->spell) {
// The declaration of target type. // The declaration of target type.
if (std::optional<lsLocation> loc = if (std::optional<lsLocation> loc =
GetLsLocation(m->db, m->wfiles, *def1->spell)) GetLsLocation(m->db, m->wfiles, *def1->spell))
entry1.location = *loc; entry1.location = *loc;
} else if (def->spell) { } else if (def->spell) {
// Builtin types have no declaration but the typedef declaration // Builtin types have no declaration but the typedef declaration
// itself is useful. // itself is useful.
if (std::optional<lsLocation> loc = if (std::optional<lsLocation> loc =
GetLsLocation(m->db, m->wfiles, *def->spell)) GetLsLocation(m->db, m->wfiles, *def->spell))
entry1.location = *loc; entry1.location = *loc;
} }
if (def1 && qualified) if (def1 && qualified)
@ -172,8 +172,7 @@ bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified,
Out_cclsMember entry1; Out_cclsMember entry1;
entry1.fieldName = def1->Name(false); entry1.fieldName = def1->Name(false);
if (def1->spell) { if (def1->spell) {
if (auto loc = if (auto loc = GetLsLocation(m->db, m->wfiles, *def1->spell))
GetLsLocation(m->db, m->wfiles, *def1->spell))
entry1.location = *loc; entry1.location = *loc;
} else if (func1.declarations.size()) { } else if (func1.declarations.size()) {
if (auto loc = GetLsLocation(m->db, m->wfiles, if (auto loc = GetLsLocation(m->db, m->wfiles,
@ -193,12 +192,11 @@ bool Expand(MessageHandler *m, Out_cclsMember *entry, bool qualified,
Out_cclsMember entry1; Out_cclsMember entry1;
entry1.fieldName = def1->Name(false); entry1.fieldName = def1->Name(false);
if (def1->spell) { if (def1->spell) {
if (auto loc = if (auto loc = GetLsLocation(m->db, m->wfiles, *def1->spell))
GetLsLocation(m->db, m->wfiles, *def1->spell))
entry1.location = *loc; entry1.location = *loc;
} else if (type1.declarations.size()) { } else if (type1.declarations.size()) {
if (auto loc = GetLsLocation(m->db, m->wfiles, if (auto loc = GetLsLocation(m->db, m->wfiles,
type1.declarations[0])) type1.declarations[0]))
entry1.location = *loc; entry1.location = *loc;
} }
entry->children.push_back(std::move(entry1)); entry->children.push_back(std::move(entry1));

View File

@ -360,8 +360,7 @@ void *Indexer(void *arg_) {
delete arg; delete arg;
std::string name = "indexer" + std::to_string(idx); std::string name = "indexer" + std::to_string(idx);
set_thread_name(name.c_str()); set_thread_name(name.c_str());
pipeline::Indexer_Main(h->clang_complete, h->vfs, h->project, pipeline::Indexer_Main(h->clang_complete, h->vfs, h->project, h->wfiles);
h->wfiles);
return nullptr; return nullptr;
} }
} // namespace } // namespace

View File

@ -101,8 +101,8 @@ void MessageHandler::textDocument_hover(TextDocumentPositionParam &param,
Hover result; Hover result;
for (SymbolRef sym : FindSymbolsAtLocation(wfile, file, param.position)) { for (SymbolRef sym : FindSymbolsAtLocation(wfile, file, param.position)) {
std::optional<lsRange> ls_range = GetLsRange( std::optional<lsRange> ls_range =
wfiles->GetFileByFilename(file->def->path), sym.range); GetLsRange(wfiles->GetFileByFilename(file->def->path), sym.range);
if (!ls_range) if (!ls_range)
continue; continue;

View File

@ -18,13 +18,12 @@ limitations under the License.
namespace ccls { namespace ccls {
namespace { namespace {
lsWorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *wfiles, lsWorkspaceEdit BuildWorkspaceEdit(DB *db, WorkingFiles *wfiles, SymbolRef sym,
SymbolRef sym, const std::string &new_text) { const std::string &new_text) {
std::unordered_map<int, lsTextDocumentEdit> path_to_edit; std::unordered_map<int, lsTextDocumentEdit> path_to_edit;
EachOccurrence(db, sym, true, [&](Use use) { EachOccurrence(db, sym, true, [&](Use use) {
std::optional<lsLocation> ls_location = std::optional<lsLocation> ls_location = GetLsLocation(db, wfiles, use);
GetLsLocation(db, wfiles, use);
if (!ls_location) if (!ls_location)
return; return;

View File

@ -400,8 +400,7 @@ void Main_OnIndexed(DB *db, WorkingFiles *wfiles, IndexUpdate *update) {
// Update indexed content, skipped ranges, and semantic highlighting. // Update indexed content, skipped ranges, and semantic highlighting.
if (update->files_def_update) { if (update->files_def_update) {
auto &def_u = *update->files_def_update; auto &def_u = *update->files_def_update;
if (WorkingFile *wfile = if (WorkingFile *wfile = wfiles->GetFileByFilename(def_u.first.path)) {
wfiles->GetFileByFilename(def_u.first.path)) {
// FIXME With index.onChange: true, use buffer_content only for // FIXME With index.onChange: true, use buffer_content only for
// request.path // request.path
wfile->SetIndexContent(g_config->index.onChange ? wfile->buffer_content wfile->SetIndexContent(g_config->index.onChange ? wfile->buffer_content

View File

@ -57,9 +57,6 @@ template <typename T> void Notify(const char *method, T &result) {
} }
void Reply(lsRequestId id, const std::function<void(Writer &)> &fn); void Reply(lsRequestId id, const std::function<void(Writer &)> &fn);
template <typename T> void Reply(lsRequestId id, T &result) {
Reply(id, [&](Writer &w) { Reflect(w, result); });
}
void ReplyError(lsRequestId id, const std::function<void(Writer &)> &fn); void ReplyError(lsRequestId id, const std::function<void(Writer &)> &fn);
template <typename T> void ReplyError(lsRequestId id, T &result) { template <typename T> void ReplyError(lsRequestId id, T &result) {

View File

@ -538,12 +538,13 @@ WorkingFiles::AsSnapshot(const std::vector<std::string> &filter_paths) {
// text documents. // text documents.
// We use a UTF-8 iterator to approximate UTF-16 in the specification (weird). // 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. // 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; size_t i = 0;
for (; position.line > 0 && i < content.size(); i++) for (; pos.line > 0 && i < content.size(); i++)
if (content[i] == '\n') if (content[i] == '\n')
position.line--; pos.line--;
for (; position.character > 0 && i < content.size(); position.character--) for (; pos.character > 0 && i < content.size() && content[i] != '\n';
pos.character--)
if (uint8_t(content[i++]) >= 128) { if (uint8_t(content[i++]) >= 128) {
// Skip 0b10xxxxxx // Skip 0b10xxxxxx
while (i < content.size() && uint8_t(content[i]) >= 128 && while (i < content.size() && uint8_t(content[i]) >= 128 &&

View File

@ -132,7 +132,7 @@ struct WorkingFiles {
std::mutex files_mutex; // Protects |files|. 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 LexIdentifierAroundPos(lsPosition position,
std::string_view content); std::string_view content);