| 634 | } |
| 635 | |
| 636 | void SourceFormatterController::beautifySource() |
| 637 | { |
| 638 | Q_D(const SourceFormatterController); |
| 639 | |
| 640 | IDocument* idoc = KDevelop::ICore::self()->documentController()->activeDocument(); |
| 641 | if (!idoc) |
| 642 | return; |
| 643 | KTextEditor::View* view = idoc->activeTextView(); |
| 644 | if (!view) |
| 645 | return; |
| 646 | KTextEditor::Document* doc = view->document(); |
| 647 | // load the appropriate formatter |
| 648 | FileFormatter ff(*idoc); |
| 649 | if (!ff.readFormatterAndStyle(d->sourceFormatters)) { |
| 650 | qCDebug(SHELL) << "no formatter available for" << idoc->url(); |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | // Ignore the modeline, as the modeline will be changed anyway |
| 655 | ff.adaptEditorIndentationMode(doc, true); |
| 656 | |
| 657 | bool has_selection = view->selection(); |
| 658 | |
| 659 | if (has_selection) { |
| 660 | QString original = view->selectionText(); |
| 661 | |
| 662 | QString output = |
| 663 | ff.format(view->selectionText(), |
| 664 | doc->text(KTextEditor::Range(KTextEditor::Cursor(0, 0), view->selectionRange().start())), |
| 665 | doc->text(KTextEditor::Range(view->selectionRange().end(), doc->documentRange().end()))); |
| 666 | |
| 667 | //remove the final newline character, unless it should be there |
| 668 | if (!original.endsWith(QLatin1Char('\n')) && output.endsWith(QLatin1Char('\n'))) |
| 669 | output.resize(output.length() - 1); |
| 670 | //there was a selection, so only change the part of the text related to it |
| 671 | |
| 672 | // We don't use KTextEditor::Document directly, because CodeRepresentation transparently works |
| 673 | // around a possible tab-replacement incompatibility between kate and kdevelop |
| 674 | DynamicCodeRepresentation::Ptr code( dynamic_cast<DynamicCodeRepresentation*>( KDevelop::createCodeRepresentation( IndexedString( doc->url() ) ).data() ) ); |
| 675 | Q_ASSERT( code ); |
| 676 | code->replace( view->selectionRange(), original, output ); |
| 677 | } else { |
| 678 | ff.formatDocument(*idoc); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | void SourceFormatterController::beautifyLine() |
| 683 | { |
nothing calls this directly
no test coverage detected