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

Method beautifyLine

kdevplatform/shell/sourceformattercontroller.cpp:682–718  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

680}
681
682void SourceFormatterController::beautifyLine()
683{
684 Q_D(const SourceFormatterController);
685
686 KDevelop::IDocumentController *docController = KDevelop::ICore::self()->documentController();
687 KDevelop::IDocument *doc = docController->activeDocument();
688 if (!doc)
689 return;
690 KTextEditor::Document *tDoc = doc->textDocument();
691 if (!tDoc)
692 return;
693 KTextEditor::View* view = doc->activeTextView();
694 if (!view)
695 return;
696 // load the appropriate formatter
697 FileFormatter ff(*doc);
698 if (!ff.readFormatterAndStyle(d->sourceFormatters)) {
699 qCDebug(SHELL) << "no formatter available for" << doc->url();
700 return;
701 }
702
703 const KTextEditor::Cursor cursor = view->cursorPosition();
704 const QString line = tDoc->line(cursor.line());
705 const QString prev = tDoc->text(KTextEditor::Range(0, 0, cursor.line(), 0));
706 const QString post = QLatin1Char('\n') + tDoc->text(KTextEditor::Range(KTextEditor::Cursor(cursor.line() + 1, 0), tDoc->documentEnd()));
707
708 const QString formatted = ff.format(line, prev, post);
709
710 // We don't use KTextEditor::Document directly, because CodeRepresentation transparently works
711 // around a possible tab-replacement incompatibility between kate and kdevelop
712 DynamicCodeRepresentation::Ptr code(dynamic_cast<DynamicCodeRepresentation*>( KDevelop::createCodeRepresentation( IndexedString( doc->url() ) ).data() ) );
713 Q_ASSERT( code );
714 code->replace( KTextEditor::Range(cursor.line(), 0, cursor.line(), line.length()), line, formatted );
715
716 // advance cursor one line
717 view->setCursorPosition(KTextEditor::Cursor(cursor.line() + 1, 0));
718}
719
720void SourceFormatterController::FileFormatter::formatDocument(IDocument& doc) const
721{

Callers

nothing calls this directly

Calls 15

createCodeRepresentationFunction · 0.85
documentControllerMethod · 0.80
activeDocumentMethod · 0.80
readFormatterAndStyleMethod · 0.80
formatMethod · 0.80
IndexedStringClass · 0.70
RangeClass · 0.50
CursorClass · 0.50
textDocumentMethod · 0.45
activeTextViewMethod · 0.45
urlMethod · 0.45
cursorPositionMethod · 0.45

Tested by

no test coverage detected