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

Method beautifySource

kdevplatform/shell/sourceformattercontroller.cpp:636–680  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

634}
635
636void 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
682void SourceFormatterController::beautifyLine()
683{

Callers

nothing calls this directly

Calls 15

createCodeRepresentationFunction · 0.85
activeDocumentMethod · 0.80
documentControllerMethod · 0.80
readFormatterAndStyleMethod · 0.80
formatMethod · 0.80
endsWithMethod · 0.80
formatDocumentMethod · 0.80
IndexedStringClass · 0.70
RangeClass · 0.50
CursorClass · 0.50
activeTextViewMethod · 0.45

Tested by

no test coverage detected