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

Method adaptEditorIndentationMode

kdevplatform/shell/sourceformattercontroller.cpp:766–820  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

764* */
765
766void SourceFormatterController::FileFormatter::adaptEditorIndentationMode(KTextEditor::Document* doc,
767 bool ignoreModeline) const
768{
769 Q_ASSERT(m_formatter);
770 if (!doc
771 || !m_sourceFormatterConfig.readEntry(SourceFormatterController::kateOverrideIndentationConfigKey(), false))
772 return;
773
774 qCDebug(SHELL) << "adapting mode for" << m_url;
775
776 QRegExp kateModelineWithNewline(QStringLiteral("\\s*\\n//\\s*kate:(.*)$"));
777
778 // modelines should always take precedence
779 if( !ignoreModeline && kateModelineWithNewline.indexIn( doc->text() ) != -1 )
780 {
781 qCDebug(SHELL) << "ignoring because a kate modeline was found";
782 return;
783 }
784
785 const auto indentation = m_formatter->indentation(m_style, m_url, m_mimeType);
786 if(indentation.isValid())
787 {
788 struct CommandCaller {
789 explicit CommandCaller(KTextEditor::Document* _doc) : doc(_doc), editor(KTextEditor::Editor::instance()) {
790 Q_ASSERT(editor);
791 }
792 void operator()(const QString& cmd) {
793 KTextEditor::Command* command = editor->queryCommand( cmd );
794 Q_ASSERT(command);
795 QString msg;
796 qCDebug(SHELL) << "calling" << cmd;
797 const auto views = doc->views();
798 for (KTextEditor::View* view : views) {
799 if (!command->exec(view, cmd, msg))
800 qCWarning(SHELL) << "setting indentation width failed: " << msg;
801 }
802 }
803
804 KTextEditor::Document* doc;
805 KTextEditor::Editor* editor;
806 } call(doc);
807
808 if( indentation.indentWidth ) // We know something about indentation-width
809 call( QStringLiteral("set-indent-width %1").arg(indentation.indentWidth ) );
810
811 if( indentation.indentationTabWidth != 0 ) // We know something about tab-usage
812 {
813 call( QStringLiteral("set-replace-tabs %1").arg( (indentation.indentationTabWidth == -1) ? 1 : 0 ) );
814 if( indentation.indentationTabWidth > 0 )
815 call( QStringLiteral("set-tab-width %1").arg(indentation.indentationTabWidth ) );
816 }
817 }else{
818 qCDebug(SHELL) << "found no valid indentation";
819 }
820}
821
822void SourceFormatterController::formatFiles()
823{

Callers 4

documentLoadedMethod · 0.80
projectOpenedMethod · 0.80
beautifySourceMethod · 0.80
settingsChangedMethod · 0.80

Calls 4

readEntryMethod · 0.80
textMethod · 0.45
indentationMethod · 0.45
isValidMethod · 0.45

Tested by

no test coverage detected