| 288 | } |
| 289 | |
| 290 | void TabWidgetPrivate::replaceRange(const QString &fileName, const newlsp::Range &range, const QString &text) |
| 291 | { |
| 292 | auto editor = findEditor(fileName); |
| 293 | if (editor) { |
| 294 | editor->replaceRange(range.start.line, range.start.character, |
| 295 | range.end.line, range.end.character, text); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | // Modify the file directly |
| 300 | if (range.start.line != range.end.line) { |
| 301 | qWarning() << "Failed, The start line is inconsistent with the end line"; |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | QFile changeFile(fileName); |
| 306 | QString cacheData; |
| 307 | if (changeFile.open(QFile::ReadOnly)) { |
| 308 | int i = 0; |
| 309 | while (i != range.start.line) { |
| 310 | cacheData += changeFile.readLine(); |
| 311 | i++; |
| 312 | } |
| 313 | QString changeLine = changeFile.readLine(); |
| 314 | int removeLength = range.end.character - range.start.character; |
| 315 | changeLine = changeLine.replace(range.start.character, removeLength, text); |
| 316 | cacheData += changeLine; |
| 317 | QByteArray array = changeFile.readLine(); |
| 318 | while (!array.isEmpty()) { |
| 319 | cacheData += array; |
| 320 | array = changeFile.readLine(); |
| 321 | } |
| 322 | changeFile.close(); |
| 323 | } |
| 324 | |
| 325 | if (changeFile.open(QFile::WriteOnly | QFile::Truncate)) { |
| 326 | auto writeCount = changeFile.write(cacheData.toLatin1()); |
| 327 | if (writeCount != cacheData.size()) { |
| 328 | qWarning() << "Failed, Write size does not match expectations." |
| 329 | << "Expectation: " << cacheData |
| 330 | << "Actual: " << writeCount; |
| 331 | } |
| 332 | changeFile.close(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void TabWidgetPrivate::doSave() |
| 337 | { |
no test coverage detected