| 1 |
#include "LibLsp/lsp/working_files.h" |
| 2 |
#include <algorithm> |
| 3 |
#include <climits> |
| 4 |
#include <numeric> |
| 5 |
#include "LibLsp/lsp/utils.h" |
| 6 |
#include <memory> |
| 7 |
#include "LibLsp/lsp/AbsolutePath.h" |
| 8 |
using namespace lsp; |
| 9 |
struct WorkingFilesData |
| 10 |
{ |
| 11 |
std::map<AbsolutePath, std::shared_ptr<WorkingFile>> files; |
| 12 |
std::mutex files_mutex; // Protects |d_ptr->files|. |
| 13 |
}; |
| 14 |
|
| 15 |
WorkingFile::WorkingFile(WorkingFiles& _parent, AbsolutePath const& filename, std::string const& buffer_content) |
| 16 |
: filename(filename), directory(filename), parent(_parent), counter(0), buffer_content(buffer_content) |
| 17 |
{ |
| 18 |
directory = Directory(GetDirName(filename.path)); |
| 19 |
} |
| 20 |
|
| 21 |
WorkingFile::WorkingFile(WorkingFiles& _parent, AbsolutePath const& filename, std::string&& buffer_content) |
| 22 |
: filename(filename), directory(filename), parent(_parent), counter(0), buffer_content(buffer_content) |
| 23 |
{ |
| 24 |
directory = Directory(GetDirName(filename.path)); |
| 25 |
} |
| 26 |
|
| 27 |
WorkingFiles::WorkingFiles() : d_ptr(new WorkingFilesData()) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
WorkingFiles::~WorkingFiles() |
| 32 |
{ |
| 33 |
delete d_ptr; |
| 34 |
} |
| 35 |
|
| 36 |
void WorkingFiles::CloseFilesInDirectory(std::vector<Directory> const& directories) |
| 37 |
{ |
| 38 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 39 |
std::vector<AbsolutePath> files_to_be_delete; |
| 40 |
|
| 41 |
for (auto& it : d_ptr->files) |
| 42 |
{ |
| 43 |
for (auto& dir : directories) |
| 44 |
{ |
| 45 |
if (it.second->directory == dir) |
| 46 |
{ |
| 47 |
files_to_be_delete.emplace_back(it.first); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
for (auto& it : files_to_be_delete) |
| 53 |
{ |
| 54 |
d_ptr->files.erase(it); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
std::shared_ptr<WorkingFile> WorkingFiles::GetFileByFilename(AbsolutePath const& filename) |
| 59 |
{ |
| 60 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 61 |
return GetFileByFilenameNoLock(filename); |
| 62 |
} |
| 63 |
|
| 64 |
std::shared_ptr<WorkingFile> WorkingFiles::GetFileByFilenameNoLock(AbsolutePath const& filename) |
| 65 |
{ |
| 66 |
auto const findIt = d_ptr->files.find(filename); |
| 67 |
if (findIt != d_ptr->files.end()) |
| 68 |
{ |
| 69 |
return findIt->second; |
| 70 |
} |
| 71 |
return nullptr; |
| 72 |
} |
| 73 |
|
| 74 |
std::shared_ptr<WorkingFile> WorkingFiles::OnOpen(lsTextDocumentItem& open) |
| 75 |
{ |
| 76 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 77 |
|
| 78 |
AbsolutePath filename = open.uri.GetAbsolutePath(); |
| 79 |
|
| 80 |
// The file may already be open. |
| 81 |
if (auto file = GetFileByFilenameNoLock(filename)) |
| 82 |
{ |
| 83 |
file->version = open.version; |
| 84 |
file->buffer_content.swap(open.text); |
| 85 |
|
| 86 |
return file; |
| 87 |
} |
| 88 |
|
| 89 |
auto const& it = |
| 90 |
d_ptr->files.insert({filename, std::make_shared<WorkingFile>(*this, filename, std::move(open.text))}); |
| 91 |
return it.first->second; |
| 92 |
} |
| 93 |
|
| 94 |
std::shared_ptr<WorkingFile> WorkingFiles::OnChange(lsTextDocumentDidChangeParams const& change) |
| 95 |
{ |
| 96 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 97 |
|
| 98 |
AbsolutePath filename = change.textDocument.uri.GetAbsolutePath(); |
| 99 |
auto file = GetFileByFilenameNoLock(filename); |
| 100 |
if (!file) |
| 101 |
{ |
| 102 |
return {}; |
| 103 |
} |
| 104 |
|
| 105 |
if (change.textDocument.version) |
| 106 |
{ |
| 107 |
file->version = *change.textDocument.version; |
| 108 |
} |
| 109 |
file->counter.fetch_add(1, std::memory_order_relaxed); |
| 110 |
for (lsTextDocumentContentChangeEvent const& diff : change.contentChanges) |
| 111 |
{ |
| 112 |
// Per the spec replace everything if the rangeLength and range are not set. |
| 113 |
// See https://github.com/Microsoft/language-server-protocol/issues/9. |
| 114 |
if (!diff.range) |
| 115 |
{ |
| 116 |
file->buffer_content = diff.text; |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
int start_offset = GetOffsetForPosition(diff.range->start, file->buffer_content); |
| 121 |
// Ignore TextDocumentContentChangeEvent.rangeLength which causes trouble |
| 122 |
// when UTF-16 surrogate pairs are used. |
| 123 |
int end_offset = GetOffsetForPosition(diff.range->end, file->buffer_content); |
| 124 |
file->buffer_content.replace( |
| 125 |
file->buffer_content.begin() + start_offset, file->buffer_content.begin() + end_offset, diff.text |
| 126 |
); |
| 127 |
} |
| 128 |
} |
| 129 |
return file; |
| 130 |
} |
| 131 |
|
| 132 |
bool WorkingFiles::OnClose(lsTextDocumentIdentifier const& close) |
| 133 |
{ |
| 134 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 135 |
|
| 136 |
AbsolutePath filename = close.uri.GetAbsolutePath(); |
| 137 |
auto const findIt = d_ptr->files.find(filename); |
| 138 |
if (findIt != d_ptr->files.end()) |
| 139 |
{ |
| 140 |
d_ptr->files.erase(findIt); |
| 141 |
return true; |
| 142 |
} |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
std::shared_ptr<WorkingFile> WorkingFiles::OnSave(lsTextDocumentIdentifier const& _save) |
| 147 |
{ |
| 148 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 149 |
|
| 150 |
AbsolutePath filename = _save.uri.GetAbsolutePath(); |
| 151 |
auto const findIt = d_ptr->files.find(filename); |
| 152 |
if (findIt != d_ptr->files.end()) |
| 153 |
{ |
| 154 |
std::shared_ptr<WorkingFile>& file = findIt->second; |
| 155 |
lsp::WriteToFile(file->filename, file->GetContentNoLock()); |
| 156 |
return findIt->second; |
| 157 |
} |
| 158 |
return {}; |
| 159 |
} |
| 160 |
|
| 161 |
bool WorkingFiles::GetFileBufferContent(std::shared_ptr<WorkingFile>& file, std::string& out) |
| 162 |
{ |
| 163 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 164 |
if (file) |
| 165 |
{ |
| 166 |
out = file->buffer_content; |
| 167 |
return true; |
| 168 |
} |
| 169 |
return false; |
| 170 |
} |
| 171 |
bool WorkingFiles::GetFileBufferContent(std::shared_ptr<WorkingFile>& file, std::wstring& out) |
| 172 |
{ |
| 173 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 174 |
if (file) |
| 175 |
{ |
| 176 |
out = lsp::s2ws(file->buffer_content); |
| 177 |
return true; |
| 178 |
} |
| 179 |
return false; |
| 180 |
} |
| 181 |
void WorkingFiles::Clear() |
| 182 |
{ |
| 183 |
std::lock_guard<std::mutex> lock(d_ptr->files_mutex); |
| 184 |
d_ptr->files.clear(); |
| 185 |
} |