MCPcopy Create free account
hub / github.com/KDE/kdevelop / generateNewText

Method generateNewText

kdevplatform/language/codegen/documentchangeset.cpp:375–495  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

373}
374
375DocumentChangeSet::ChangeResult DocumentChangeSetPrivate::generateNewText(const IndexedString& file,
376 ChangesList& sortedChanges,
377 const CodeRepresentation* repr,
378 QString& output)
379{
380 //Create the actual new modified file
381 QStringList textLines = repr->text().split(QLatin1Char('\n'));
382
383 ISourceFormatterController::FileFormatterPtr formatter;
384 if (formatPolicy != DocumentChangeSet::NoAutoFormat) {
385 formatter = ICore::self()->sourceFormatterController()->fileFormatter(file.toUrl());
386 }
387
388 QVector<int> removedLines;
389
390 for (int pos = sortedChanges.size() - 1; pos >= 0; --pos) {
391 DocumentChange& change(*sortedChanges[pos]);
392 QString encountered;
393 if (changeIsValid(change, textLines) && //We demand this, although it should be fixed
394 ((encountered = rangeText(change.m_range, textLines)) == change.m_oldText || change.m_ignoreOldText)) {
395 ///Problem: This does not work if the other changes significantly alter the context @todo Use the changed context
396 QString leftContext = QStringList(textLines.mid(0, change.m_range.start().line() + 1)).join(QLatin1Char(
397 '\n'));
398 leftContext.chop(textLines[change.m_range.start().line()].length() - change.m_range.start().column());
399
400 QString rightContext = QStringList(textLines.mid(change.m_range.end().line())).join(QLatin1Char('\n')).mid(
401 change.m_range.end().column());
402
403 if (formatter) {
404 QString oldNewText = change.m_newText;
405 change.m_newText = formatter->format(change.m_newText, leftContext, rightContext);
406
407 if (formatPolicy == DocumentChangeSet::AutoFormatChangesKeepIndentation) {
408 // Reproduce the previous indentation
409 const QStringList oldLines = oldNewText.split(QLatin1Char('\n'));
410 QStringList newLines = change.m_newText.split(QLatin1Char('\n'));
411
412 if (oldLines.size() == newLines.size()) {
413 for (int line = 0; line < newLines.size(); ++line) {
414 // Keep the previous indentation
415 QString oldIndentation;
416 for (const QChar a : oldLines[line]) {
417 if (a.isSpace()) {
418 oldIndentation.append(a);
419 } else {
420 break;
421 }
422 }
423
424 int newIndentationLength = 0;
425
426 for (int a = 0; a < newLines[line].size(); ++a) {
427 if (newLines[line][a].isSpace()) {
428 newIndentationLength = a;
429 } else {
430 break;
431 }
432 }

Callers 1

applyAllChangesMethod · 0.80

Calls 15

changeIsValidFunction · 0.85
rangeTextFunction · 0.85
QStringListClass · 0.85
printRangeFunction · 0.85
ChangeResultClass · 0.85
sortFunction · 0.85
fileFormatterMethod · 0.80
joinMethod · 0.80
midMethod · 0.80
formatMethod · 0.80
strMethod · 0.80

Tested by

no test coverage detected