diff --git a/src/clang_complete.cc b/src/clang_complete.cc index 2f29a688..5bcbf1c1 100644 --- a/src/clang_complete.cc +++ b/src/clang_complete.cc @@ -298,7 +298,8 @@ void TryEnsureDocumentParsed(ClangCompleteManager* manager, args.push_back("-fspell-checking"); } - std::vector unsaved = session->working_files->AsUnsavedFiles(); + WorkingFilesSnapshot snapshot = session->working_files->AsSnapshot(); + std::vector unsaved = snapshot.AsUnsavedFiles(); LOG_S(INFO) << "Creating completion session with arguments " << StringJoin(args); @@ -388,8 +389,8 @@ void CompletionQueryMain(ClangCompleteManager* completion_manager) { if (!session->tu) continue; - std::vector unsaved = - completion_manager->working_files_->AsUnsavedFiles(); + WorkingFilesSnapshot snapshot = completion_manager->working_files_->AsSnapshot(); + std::vector unsaved = snapshot.AsUnsavedFiles(); // Emit code completion data. if (request->position) { diff --git a/src/working_files.cc b/src/working_files.cc index e88d03d4..244218cc 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -265,14 +265,6 @@ lsPosition WorkingFile::FindStableCompletionSource( return GetPositionForOffset(buffer_content, offset); } -CXUnsavedFile WorkingFile::AsUnsavedFile() const { - CXUnsavedFile result; - result.Filename = filename.c_str(); - result.Contents = buffer_content.c_str(); - result.Length = (unsigned long)buffer_content.size(); - return result; -} - WorkingFile* WorkingFiles::GetFileByFilename(const std::string& filename) { std::lock_guard lock(files_mutex); return GetFileByFilenameNoLock(filename); @@ -369,13 +361,27 @@ void WorkingFiles::OnClose(const lsTextDocumentItem& close) { << " because it was not open"; } -std::vector WorkingFiles::AsUnsavedFiles() { - std::lock_guard lock(files_mutex); - +std::vector WorkingFilesSnapshot::AsUnsavedFiles() const { std::vector result; result.reserve(files.size()); + for (auto& file : files) { + CXUnsavedFile unsaved; + unsaved.Filename = file.filename.c_str(); + unsaved.Contents = file.content.c_str(); + unsaved.Length = (unsigned long)file.content.size(); + + result.push_back(unsaved); + } + return result; +} + +WorkingFilesSnapshot WorkingFiles::AsSnapshot() { + std::lock_guard lock(files_mutex); + + WorkingFilesSnapshot result; + result.files.reserve(files.size()); for (auto& file : files) - result.push_back(file->AsUnsavedFile()); + result.files.push_back({file->filename, file->buffer_content}); return result; } diff --git a/src/working_files.h b/src/working_files.h index 42ec5260..9938477f 100644 --- a/src/working_files.h +++ b/src/working_files.h @@ -70,8 +70,15 @@ struct WorkingFile { lsPosition FindStableCompletionSource(lsPosition position, bool* is_global_completion, std::string* existing_completion) const; +}; - CXUnsavedFile AsUnsavedFile() const; +struct WorkingFilesSnapshot { + std::vector AsUnsavedFiles() const; + struct File { + std::string filename; + std::string content; + }; + std::vector files; }; struct WorkingFiles { @@ -94,7 +101,7 @@ struct WorkingFiles { void OnChange(const lsTextDocumentDidChangeParams& change); void OnClose(const lsTextDocumentItem& close); - std::vector AsUnsavedFiles(); + WorkingFilesSnapshot AsSnapshot(); // Use unique_ptrs so we can handout WorkingFile ptrs and not have them // invalidated if we resize files.