|
| 1 | +// #include "CodeFormatServer/FileManager.h" |
| 2 | +// #include <algorithm> |
| 3 | +// #include "Util/Utf8.h" |
| 4 | +// |
| 5 | +// FileManager::FileManager() |
| 6 | +// { |
| 7 | +// } |
| 8 | +// |
| 9 | +// FileManager::~FileManager() |
| 10 | +// { |
| 11 | +// } |
| 12 | +// |
| 13 | +// void FileManager::UpdateFile(std::string_view uri, vscode::Range range, std::string& text) |
| 14 | +// { |
| 15 | +// auto it = _fileMap.find(uri); |
| 16 | +// if (it == _fileMap.end()) |
| 17 | +// { |
| 18 | +// return UpdateFile(uri, text); |
| 19 | +// } |
| 20 | +// |
| 21 | +// auto textLines = GetTextLines(text); |
| 22 | +// |
| 23 | +// auto& file = it->second; |
| 24 | +// |
| 25 | +// std::vector<std::string> newFile; |
| 26 | +// |
| 27 | +// auto startLine = range.start.line; |
| 28 | +// auto endLine = range.end.line; |
| 29 | +// auto startCharacter = range.start.character; |
| 30 | +// auto endCharacter = range.end.character; |
| 31 | +// |
| 32 | +// for (std::size_t lineNumber = 0; lineNumber < startLine; lineNumber++) |
| 33 | +// { |
| 34 | +// if (lineNumber < file.size()) |
| 35 | +// { |
| 36 | +// newFile.emplace_back(std::move(file[lineNumber])); |
| 37 | +// } |
| 38 | +// else |
| 39 | +// { |
| 40 | +// newFile.emplace_back("\n"); |
| 41 | +// } |
| 42 | +// } |
| 43 | +// |
| 44 | +// if (startCharacter != 0 && startLine < file.size() && !textLines.empty()) |
| 45 | +// { |
| 46 | +// auto byteNumber = utf8::Utf8nByteNum(file[startLine].data(), file[startLine].size(), startCharacter + 1); |
| 47 | +// textLines.front() = file[startLine].substr(0, byteNumber) + textLines.front(); |
| 48 | +// } |
| 49 | +// |
| 50 | +// if (endLine < file.size() && !textLines.empty()) |
| 51 | +// { |
| 52 | +// auto byteNumber = utf8::Utf8nByteNum(file[endLine].data(), file[endLine].size(), endCharacter + 1); |
| 53 | +// |
| 54 | +// textLines.back().append(file[endLine].substr(byteNumber)); |
| 55 | +// } |
| 56 | +// |
| 57 | +// for (auto& line : textLines) |
| 58 | +// { |
| 59 | +// newFile.emplace_back(std::move(line)); |
| 60 | +// } |
| 61 | +// |
| 62 | +// |
| 63 | +// for (std::size_t lineNumber = endLine + 1; lineNumber < file.size(); lineNumber++) |
| 64 | +// { |
| 65 | +// newFile.emplace_back(std::move(file[lineNumber])); |
| 66 | +// } |
| 67 | +// |
| 68 | +// file = newFile; |
| 69 | +// } |
| 70 | +// |
| 71 | +// void FileManager::UpdateFile(std::string_view uri, std::string& text) |
| 72 | +// { |
| 73 | +// _fileMap[std::string(uri)] = GetTextLines(text); |
| 74 | +// } |
| 75 | +// |
| 76 | +// void FileManager::ReleaseFile(std::string_view uri) |
| 77 | +// { |
| 78 | +// auto it = _fileMap.find(uri); |
| 79 | +// |
| 80 | +// if (it != _fileMap.end()) |
| 81 | +// { |
| 82 | +// _fileMap.erase(it); |
| 83 | +// } |
| 84 | +// } |
| 85 | +// |
| 86 | +// std::string FileManager::GetFileText(std::string_view uri) |
| 87 | +// { |
| 88 | +// std::string fileText; |
| 89 | +// |
| 90 | +// auto it = _fileMap.find(uri); |
| 91 | +// if (it == _fileMap.end()) |
| 92 | +// { |
| 93 | +// return ""; |
| 94 | +// } |
| 95 | +// |
| 96 | +// std::size_t fileSize = std::accumulate(it->second.begin(), it->second.end(), 0, |
| 97 | +// [](auto x, const std::string& line) |
| 98 | +// { |
| 99 | +// return x + line.size(); |
| 100 | +// }); |
| 101 | +// |
| 102 | +// fileText.reserve(fileSize); |
| 103 | +// |
| 104 | +// for (auto& line : it->second) |
| 105 | +// { |
| 106 | +// fileText.append(line); |
| 107 | +// } |
| 108 | +// return fileText; |
| 109 | +// } |
| 110 | +// |
| 111 | +// |
| 112 | +// std::vector<std::string> FileManager::GetTextLines(std::string& text) |
| 113 | +// { |
| 114 | +// std::vector<std::string> textLines; |
| 115 | +// |
| 116 | +// std::string_view source = text; |
| 117 | +// std::size_t sepLen = 1; |
| 118 | +// |
| 119 | +// while (true) |
| 120 | +// { |
| 121 | +// const std::size_t sepIndex = source.find_first_of('\n', 0); |
| 122 | +// if (sepIndex == std::string_view::npos) |
| 123 | +// { |
| 124 | +// textLines.emplace_back(source); |
| 125 | +// |
| 126 | +// break; |
| 127 | +// } |
| 128 | +// else |
| 129 | +// { |
| 130 | +// textLines.emplace_back(source.substr(0, sepIndex + sepLen)); |
| 131 | +// source = source.substr(sepIndex + sepLen); |
| 132 | +// } |
| 133 | +// } |
| 134 | +// return textLines; |
| 135 | +// } |
0 commit comments