2022-08-19 15:59:36 +02:00
// Copyright (C) 2016 The Qt Company Ltd.
2022-12-21 10:12:09 +01:00
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
2011-07-06 10:38:53 +02:00
2013-01-15 14:26:24 +01:00
# include "cppcodestylesettingspage.h"
2013-03-27 18:54:03 +01:00
2022-11-17 09:16:19 +01:00
# include "cppcodeformatter.h"
2013-03-12 12:06:41 +01:00
# include "cppcodestylepreferences.h"
2019-02-25 22:39:22 +02:00
# include "cppcodestylesnippets.h"
2021-08-30 10:58:08 +02:00
# include "cppeditorconstants.h"
2013-01-15 14:26:24 +01:00
# include "cpppointerdeclarationformatter.h"
2011-02-03 15:48:14 +01:00
# include "cpptoolssettings.h"
2013-01-15 14:26:24 +01:00
# include <coreplugin/icore.h>
# include <cppeditor/cppeditorconstants.h>
# include <texteditor/codestyleeditor.h>
2022-11-17 09:16:19 +01:00
# include <texteditor/displaysettings.h>
2020-10-23 19:56:50 +02:00
# include <texteditor/fontsettings.h>
2018-11-20 11:23:30 +01:00
# include <texteditor/icodestylepreferencesfactory.h>
2017-04-24 15:52:04 +02:00
# include <texteditor/snippets/snippetprovider.h>
2022-11-17 09:16:19 +01:00
# include <texteditor/snippets/snippeteditor.h>
# include <texteditor/tabsettings.h>
# include <texteditor/tabsettingswidget.h>
# include <texteditor/textdocument.h>
2013-01-15 14:26:24 +01:00
# include <texteditor/texteditorsettings.h>
2022-11-17 09:16:19 +01:00
# include <utils/layoutbuilder.h>
2021-11-01 16:20:08 +01:00
# include <utils/qtcassert.h>
2013-01-15 14:26:24 +01:00
2015-03-05 08:22:48 +01:00
# include <cplusplus/Overview.h>
2013-03-27 18:54:03 +01:00
# include <cplusplus/pp.h>
# include <extensionsystem/pluginmanager.h>
2022-11-17 09:16:19 +01:00
# include <QCheckBox>
# include <QTabWidget>
2012-02-15 10:42:41 +01:00
# include <QTextBlock>
2011-02-03 15:48:14 +01:00
using namespace TextEditor ;
2022-11-23 10:28:47 +01:00
using namespace Utils ;
2011-02-03 15:48:14 +01:00
2021-08-30 10:58:08 +02:00
namespace CppEditor : : Internal {
2011-02-03 15:48:14 +01:00
2015-02-04 17:01:07 +02:00
static void applyRefactorings ( QTextDocument * textDocument , TextEditorWidget * editor ,
2013-01-15 14:26:24 +01:00
const CppCodeStyleSettings & settings )
{
// Preprocess source
2022-11-23 10:28:47 +01:00
CPlusPlus : : Environment env ;
2019-01-14 01:40:53 +01:00
Preprocessor preprocess ( nullptr , & env ) ;
2022-11-23 10:28:47 +01:00
FilePath noFileFile = FilePath : : fromPathPart ( u " <no-file> " ) ;
2013-01-15 14:26:24 +01:00
const QByteArray preprocessedSource
2022-11-23 10:28:47 +01:00
= preprocess . run ( noFileFile , textDocument - > toPlainText ( ) . toUtf8 ( ) ) ;
2013-01-15 14:26:24 +01:00
2022-11-23 10:28:47 +01:00
Document : : Ptr cppDocument = Document : : create ( noFileFile ) ;
2013-01-15 14:26:24 +01:00
cppDocument - > setUtf8Source ( preprocessedSource ) ;
cppDocument - > parse ( Document : : ParseTranlationUnit ) ;
cppDocument - > check ( ) ;
CppRefactoringFilePtr cppRefactoringFile = CppRefactoringChanges : : file ( editor , cppDocument ) ;
// Run the formatter
Overview overview ;
overview . showReturnTypes = true ;
2020-01-17 15:50:46 +01:00
overview . starBindFlags = { } ;
2013-01-15 14:26:24 +01:00
if ( settings . bindStarToIdentifier )
overview . starBindFlags | = Overview : : BindToIdentifier ;
if ( settings . bindStarToTypeName )
overview . starBindFlags | = Overview : : BindToTypeName ;
if ( settings . bindStarToLeftSpecifier )
overview . starBindFlags | = Overview : : BindToLeftSpecifier ;
if ( settings . bindStarToRightSpecifier )
overview . starBindFlags | = Overview : : BindToRightSpecifier ;
PointerDeclarationFormatter formatter ( cppRefactoringFile , overview ) ;
Utils : : ChangeSet change = formatter . format ( cppDocument - > translationUnit ( ) - > ast ( ) ) ;
// Apply change
QTextCursor cursor ( textDocument ) ;
change . apply ( & cursor ) ;
}
2011-02-03 15:48:14 +01:00
// ------------------ CppCodeStyleSettingsWidget
2022-11-17 09:16:19 +01:00
class CppCodeStylePreferencesWidgetPrivate
2011-02-03 15:48:14 +01:00
{
2022-11-17 09:16:19 +01:00
Q_DECLARE_TR_FUNCTIONS ( CppEditor : : Internal : : CppCodeStyleSettingsPage )
public :
CppCodeStylePreferencesWidgetPrivate ( CppCodeStylePreferencesWidget * widget )
: q ( widget )
, m_indentAccessSpecifiers ( createCheckBox ( tr ( " \" public \" , \" protected \" and \n "
" \" private \" within class body " ) ) )
, m_indentDeclarationsRelativeToAccessSpecifiers (
createCheckBox ( tr ( " Declarations relative to \" public \" , \n "
" \" protected \" and \" private \" " ) ) )
, m_indentFunctionBody ( createCheckBox ( tr ( " Statements within function body " ) ) )
, m_indentBlockBody ( createCheckBox ( tr ( " Statements within blocks " ) ) )
, m_indentNamespaceBody ( createCheckBox ( tr ( " Declarations within \n "
" \" namespace \" definition " ) ) )
, m_indentClassBraces ( createCheckBox ( tr ( " Class declarations " ) ) )
, m_indentNamespaceBraces ( createCheckBox ( tr ( " Namespace declarations " ) ) )
, m_indentEnumBraces ( createCheckBox ( tr ( " Enum declarations " ) ) )
, m_indentFunctionBraces ( createCheckBox ( tr ( " Function declarations " ) ) )
, m_indentBlockBraces ( createCheckBox ( tr ( " Blocks " ) ) )
, m_indentSwitchLabels ( createCheckBox ( tr ( " \" case \" or \" default \" " ) ) )
, m_indentCaseStatements ( createCheckBox ( tr ( " Statements relative to \n "
" \" case \" or \" default \" " ) ) )
, m_indentCaseBlocks ( createCheckBox ( tr ( " Blocks relative to \n "
" \" case \" or \" default \" " ) ) )
, m_indentCaseBreak ( createCheckBox ( tr ( " \" break \" statement relative to \n "
" \" case \" or \" default \" " ) ) )
, m_alignAssignments ( createCheckBox ( tr ( " Align after assignments " ) ,
tr ( " <html><head/><body> \n "
" Enables alignment to tokens after =, += etc. When the option is disabled, regular continuation line indentation will be used.<br> \n "
" <br> \n "
" With alignment: \n "
" <pre> \n "
" a = a + \n "
" b \n "
" </pre> \n "
" Without alignment: \n "
" <pre> \n "
" a = a + \n "
" b \n "
" </pre> \n "
" </body></html> " ) ) )
, m_extraPaddingConditions ( createCheckBox ( tr ( " Add extra padding to conditions \n "
" if they would align to the next line " ) ,
tr ( " <html><head/><body> \n "
" Adds an extra level of indentation to multiline conditions in the switch, if, while and foreach statements if they would otherwise have the same or less indentation than a nested statement. \n "
" \n "
" For four-spaces indentation only if statement conditions are affected. Without extra padding: \n "
" <pre> \n "
" if (a && \n "
" b) \n "
" c; \n "
" </pre> \n "
" With extra padding: \n "
" <pre> \n "
" if (a && \n "
" b) \n "
" c; \n "
" </pre> \n "
" </body></html> " ) ) )
, m_bindStarToIdentifier ( createCheckBox ( tr ( " Identifier " ) ,
tr ( " <html><head/><body>This does not apply to the star and reference symbol in pointer/reference to functions and arrays, e.g.: \n "
" <pre> int (&rf)() = ...; \n "
" int (*pf)() = ...; \n "
" \n "
" int (&ra)[2] = ...; \n "
" int (*pa)[2] = ...; \n "
" \n "
" </pre></body></html> " ) ) )
, m_bindStarToTypeName ( createCheckBox ( tr ( " Type name " ) ) )
, m_bindStarToLeftSpecifier ( createCheckBox ( tr ( " Left const/volatile " ) ) )
, m_bindStarToRightSpecifier ( createCheckBox ( tr ( " Right const/volatile " ) ,
tr ( " This does not apply to references. " ) ) )
, m_categoryTab ( new QTabWidget )
, m_tabSettingsWidget ( new TabSettingsWidget )
{
m_categoryTab - > setProperty ( " _q_custom_style_disabled " , true ) ;
QSizePolicy sizePolicy ( QSizePolicy : : Fixed , QSizePolicy : : Preferred ) ;
sizePolicy . setHorizontalStretch ( 0 ) ;
sizePolicy . setVerticalStretch ( 0 ) ;
sizePolicy . setHeightForWidth ( m_tabSettingsWidget - > sizePolicy ( ) . hasHeightForWidth ( ) ) ;
m_tabSettingsWidget - > setSizePolicy ( sizePolicy ) ;
m_tabSettingsWidget - > setFocusPolicy ( Qt : : TabFocus ) ;
QObject : : connect ( m_tabSettingsWidget , & TabSettingsWidget : : settingsChanged ,
q , & CppCodeStylePreferencesWidget : : slotTabSettingsChanged ) ;
using namespace Utils : : Layouting ;
QWidget * generalTab = new QWidget ;
Row {
Column {
m_tabSettingsWidget ,
st
} ,
createPreview ( 0 )
} . attachTo ( generalTab ) ;
m_categoryTab - > addTab ( generalTab , tr ( " General " ) ) ;
m_controllers . append ( m_tabSettingsWidget ) ;
QWidget * contentTab = new QWidget ;
Group contentGroup {
title ( tr ( " Indent " ) ) ,
Column {
m_indentAccessSpecifiers ,
m_indentDeclarationsRelativeToAccessSpecifiers ,
m_indentFunctionBody ,
m_indentBlockBody ,
m_indentNamespaceBody ,
st
}
} ;
Row {
contentGroup ,
createPreview ( 1 )
} . attachTo ( contentTab ) ;
m_categoryTab - > addTab ( contentTab , tr ( " Content " ) ) ;
m_controllers . append ( contentGroup . widget ) ;
QWidget * bracesTab = new QWidget ;
Group bracesGroup {
title ( tr ( " Indent Braces " ) ) ,
Column {
m_indentClassBraces ,
m_indentNamespaceBraces ,
m_indentEnumBraces ,
m_indentFunctionBraces ,
m_indentBlockBraces ,
st
}
} ;
Row {
bracesGroup ,
createPreview ( 2 )
} . attachTo ( bracesTab ) ;
m_categoryTab - > addTab ( bracesTab , tr ( " Braces " ) ) ;
m_controllers . append ( bracesGroup . widget ) ;
QWidget * switchTab = new QWidget ;
Group switchGroup {
title ( tr ( " Indent within \" switch \" " ) ) ,
Column {
m_indentSwitchLabels ,
m_indentCaseStatements ,
m_indentCaseBlocks ,
m_indentCaseBreak ,
st
}
} ;
Row {
switchGroup ,
createPreview ( 3 )
} . attachTo ( switchTab ) ;
m_categoryTab - > addTab ( switchTab , tr ( " \" switch \" " ) ) ;
m_controllers . append ( switchGroup . widget ) ;
QWidget * alignmentTab = new QWidget ;
Group alignmentGroup {
title ( tr ( " Align " ) ) ,
Column {
m_alignAssignments ,
m_extraPaddingConditions ,
st
}
} ;
Row {
alignmentGroup ,
createPreview ( 4 )
} . attachTo ( alignmentTab ) ;
m_categoryTab - > addTab ( alignmentTab , tr ( " Alignment " ) ) ;
m_controllers . append ( alignmentGroup . widget ) ;
QWidget * typesTab = new QWidget ;
Group typesGroup {
title ( tr ( " Bind '*' and '&&' in types/declarations to " ) ) ,
Column {
m_bindStarToIdentifier ,
m_bindStarToTypeName ,
m_bindStarToLeftSpecifier ,
m_bindStarToRightSpecifier ,
st
}
} ;
Row {
typesGroup ,
createPreview ( 5 )
} . attachTo ( typesTab ) ;
m_categoryTab - > addTab ( typesTab , tr ( " Pointers and References " ) ) ;
m_controllers . append ( typesGroup . widget ) ;
Row { m_categoryTab } . attachTo ( q ) ;
}
2011-02-03 15:48:14 +01:00
2022-11-17 09:16:19 +01:00
QCheckBox * createCheckBox ( const QString & text , const QString & toolTip = { } )
{
QCheckBox * checkBox = new QCheckBox ( text ) ;
checkBox - > setToolTip ( toolTip ) ;
QObject : : connect ( checkBox , & QCheckBox : : toggled ,
q , & CppCodeStylePreferencesWidget : : slotCodeStyleSettingsChanged ) ;
return checkBox ;
}
2011-02-03 15:48:14 +01:00
2022-11-17 09:16:19 +01:00
SnippetEditorWidget * createPreview ( int i )
{
SnippetEditorWidget * editor = new SnippetEditorWidget ;
editor - > setPlainText ( QLatin1String ( Constants : : DEFAULT_CODE_STYLE_SNIPPETS [ i ] ) ) ;
m_previews . append ( editor ) ;
return editor ;
}
CppCodeStylePreferencesWidget * q = nullptr ;
QCheckBox * m_indentAccessSpecifiers = nullptr ;
QCheckBox * m_indentDeclarationsRelativeToAccessSpecifiers = nullptr ;
QCheckBox * m_indentFunctionBody = nullptr ;
QCheckBox * m_indentBlockBody = nullptr ;
QCheckBox * m_indentNamespaceBody = nullptr ;
QCheckBox * m_indentClassBraces = nullptr ;
QCheckBox * m_indentNamespaceBraces = nullptr ;
QCheckBox * m_indentEnumBraces = nullptr ;
QCheckBox * m_indentFunctionBraces = nullptr ;
QCheckBox * m_indentBlockBraces = nullptr ;
QCheckBox * m_indentSwitchLabels = nullptr ;
QCheckBox * m_indentCaseStatements = nullptr ;
QCheckBox * m_indentCaseBlocks = nullptr ;
QCheckBox * m_indentCaseBreak = nullptr ;
QCheckBox * m_alignAssignments = nullptr ;
QCheckBox * m_extraPaddingConditions = nullptr ;
QCheckBox * m_bindStarToIdentifier = nullptr ;
QCheckBox * m_bindStarToTypeName = nullptr ;
QCheckBox * m_bindStarToLeftSpecifier = nullptr ;
QCheckBox * m_bindStarToRightSpecifier = nullptr ;
QList < SnippetEditorWidget * > m_previews ;
QList < QWidget * > m_controllers ;
QTabWidget * m_categoryTab = nullptr ;
TabSettingsWidget * m_tabSettingsWidget = nullptr ;
} ;
CppCodeStylePreferencesWidget : : CppCodeStylePreferencesWidget ( QWidget * parent )
: TextEditor : : CodeStyleEditorWidget ( parent ) ,
d ( new CppCodeStylePreferencesWidgetPrivate ( this ) )
{
2013-09-19 17:59:27 +02:00
decorateEditors ( TextEditorSettings : : fontSettings ( ) ) ;
2016-03-12 22:53:45 +02:00
connect ( TextEditorSettings : : instance ( ) , & TextEditorSettings : : fontSettingsChanged ,
this , & CppCodeStylePreferencesWidget : : decorateEditors ) ;
2011-02-03 15:48:14 +01:00
setVisualizeWhitespace ( true ) ;
2022-11-17 09:16:19 +01:00
// m_ui->categoryTab->setCurrentIndex(0);
2011-02-03 15:48:14 +01:00
}
CppCodeStylePreferencesWidget : : ~ CppCodeStylePreferencesWidget ( )
{
2022-11-17 09:16:19 +01:00
delete d ;
2011-02-03 15:48:14 +01:00
}
2021-08-30 10:58:08 +02:00
void CppCodeStylePreferencesWidget : : setCodeStyle ( CppCodeStylePreferences * codeStylePreferences )
2011-02-03 15:48:14 +01:00
{
2011-06-21 18:03:44 +02:00
// code preferences
2011-08-16 10:45:23 +02:00
m_preferences = codeStylePreferences ;
2011-02-03 15:48:14 +01:00
2015-01-30 11:02:24 +01:00
connect ( m_preferences , & CppCodeStylePreferences : : currentTabSettingsChanged ,
this , & CppCodeStylePreferencesWidget : : setTabSettings ) ;
2016-03-12 22:53:45 +02:00
connect ( m_preferences , & CppCodeStylePreferences : : currentCodeStyleSettingsChanged ,
2021-08-30 10:58:08 +02:00
this , [ this ] ( const CppCodeStyleSettings & codeStyleSettings ) {
2016-03-12 22:53:45 +02:00
setCodeStyleSettings ( codeStyleSettings ) ;
} ) ;
connect ( m_preferences , & ICodeStylePreferences : : currentPreferencesChanged ,
this , [ this ] ( TextEditor : : ICodeStylePreferences * currentPreferences ) {
slotCurrentPreferencesChanged ( currentPreferences ) ;
} ) ;
2011-02-03 15:48:14 +01:00
2022-11-24 16:09:24 +01:00
setTabSettings ( m_preferences - > currentTabSettings ( ) ) ;
setCodeStyleSettings ( m_preferences - > currentCodeStyleSettings ( ) , false ) ;
2011-08-16 10:45:23 +02:00
slotCurrentPreferencesChanged ( m_preferences - > currentPreferences ( ) , false ) ;
2011-06-21 18:03:44 +02:00
2023-01-02 11:43:18 +01:00
m_originalCppCodeStyleSettings = cppCodeStyleSettings ( ) ;
m_originalTabSettings = tabSettings ( ) ;
2011-02-03 15:48:14 +01:00
updatePreview ( ) ;
}
CppCodeStyleSettings CppCodeStylePreferencesWidget : : cppCodeStyleSettings ( ) const
{
CppCodeStyleSettings set ;
2022-11-17 09:16:19 +01:00
set . indentBlockBraces = d - > m_indentBlockBraces - > isChecked ( ) ;
set . indentBlockBody = d - > m_indentBlockBody - > isChecked ( ) ;
set . indentClassBraces = d - > m_indentClassBraces - > isChecked ( ) ;
set . indentEnumBraces = d - > m_indentEnumBraces - > isChecked ( ) ;
set . indentNamespaceBraces = d - > m_indentNamespaceBraces - > isChecked ( ) ;
set . indentNamespaceBody = d - > m_indentNamespaceBody - > isChecked ( ) ;
set . indentAccessSpecifiers = d - > m_indentAccessSpecifiers - > isChecked ( ) ;
set . indentDeclarationsRelativeToAccessSpecifiers = d - > m_indentDeclarationsRelativeToAccessSpecifiers - > isChecked ( ) ;
set . indentFunctionBody = d - > m_indentFunctionBody - > isChecked ( ) ;
set . indentFunctionBraces = d - > m_indentFunctionBraces - > isChecked ( ) ;
set . indentSwitchLabels = d - > m_indentSwitchLabels - > isChecked ( ) ;
set . indentStatementsRelativeToSwitchLabels = d - > m_indentCaseStatements - > isChecked ( ) ;
set . indentBlocksRelativeToSwitchLabels = d - > m_indentCaseBlocks - > isChecked ( ) ;
set . indentControlFlowRelativeToSwitchLabels = d - > m_indentCaseBreak - > isChecked ( ) ;
set . bindStarToIdentifier = d - > m_bindStarToIdentifier - > isChecked ( ) ;
set . bindStarToTypeName = d - > m_bindStarToTypeName - > isChecked ( ) ;
set . bindStarToLeftSpecifier = d - > m_bindStarToLeftSpecifier - > isChecked ( ) ;
set . bindStarToRightSpecifier = d - > m_bindStarToRightSpecifier - > isChecked ( ) ;
set . extraPaddingForConditionsIfConfusingAlign = d - > m_extraPaddingConditions - > isChecked ( ) ;
set . alignAssignments = d - > m_alignAssignments - > isChecked ( ) ;
2011-02-03 15:48:14 +01:00
return set ;
}
2015-02-04 17:01:07 +02:00
void CppCodeStylePreferencesWidget : : setTabSettings ( const TabSettings & settings )
2011-08-16 10:45:23 +02:00
{
2022-11-17 09:16:19 +01:00
d - > m_tabSettingsWidget - > setTabSettings ( settings ) ;
2011-08-16 10:45:23 +02:00
}
2021-11-01 16:20:08 +01:00
TextEditor : : TabSettings CppCodeStylePreferencesWidget : : tabSettings ( ) const
{
2022-11-17 09:16:19 +01:00
return d - > m_tabSettingsWidget - > tabSettings ( ) ;
2021-11-01 16:20:08 +01:00
}
2011-08-16 10:45:23 +02:00
void CppCodeStylePreferencesWidget : : setCodeStyleSettings ( const CppCodeStyleSettings & s , bool preview )
2011-02-03 15:48:14 +01:00
{
2011-06-21 18:03:44 +02:00
const bool wasBlocked = m_blockUpdates ;
m_blockUpdates = true ;
2022-11-17 09:16:19 +01:00
d - > m_indentBlockBraces - > setChecked ( s . indentBlockBraces ) ;
d - > m_indentBlockBody - > setChecked ( s . indentBlockBody ) ;
d - > m_indentClassBraces - > setChecked ( s . indentClassBraces ) ;
d - > m_indentEnumBraces - > setChecked ( s . indentEnumBraces ) ;
d - > m_indentNamespaceBraces - > setChecked ( s . indentNamespaceBraces ) ;
d - > m_indentNamespaceBody - > setChecked ( s . indentNamespaceBody ) ;
d - > m_indentAccessSpecifiers - > setChecked ( s . indentAccessSpecifiers ) ;
d - > m_indentDeclarationsRelativeToAccessSpecifiers - > setChecked ( s . indentDeclarationsRelativeToAccessSpecifiers ) ;
d - > m_indentFunctionBody - > setChecked ( s . indentFunctionBody ) ;
d - > m_indentFunctionBraces - > setChecked ( s . indentFunctionBraces ) ;
d - > m_indentSwitchLabels - > setChecked ( s . indentSwitchLabels ) ;
d - > m_indentCaseStatements - > setChecked ( s . indentStatementsRelativeToSwitchLabels ) ;
d - > m_indentCaseBlocks - > setChecked ( s . indentBlocksRelativeToSwitchLabels ) ;
d - > m_indentCaseBreak - > setChecked ( s . indentControlFlowRelativeToSwitchLabels ) ;
d - > m_bindStarToIdentifier - > setChecked ( s . bindStarToIdentifier ) ;
d - > m_bindStarToTypeName - > setChecked ( s . bindStarToTypeName ) ;
d - > m_bindStarToLeftSpecifier - > setChecked ( s . bindStarToLeftSpecifier ) ;
d - > m_bindStarToRightSpecifier - > setChecked ( s . bindStarToRightSpecifier ) ;
d - > m_extraPaddingConditions - > setChecked ( s . extraPaddingForConditionsIfConfusingAlign ) ;
d - > m_alignAssignments - > setChecked ( s . alignAssignments ) ;
2011-06-21 18:03:44 +02:00
m_blockUpdates = wasBlocked ;
if ( preview )
updatePreview ( ) ;
2011-02-03 15:48:14 +01:00
}
2015-02-04 17:01:07 +02:00
void CppCodeStylePreferencesWidget : : slotCurrentPreferencesChanged ( ICodeStylePreferences * preferences , bool preview )
2011-02-03 15:48:14 +01:00
{
2022-08-01 17:28:42 +02:00
const bool enable = ! preferences - > isReadOnly ( ) ;
2022-11-17 09:16:19 +01:00
for ( QWidget * widget : d - > m_controllers )
widget - > setEnabled ( enable ) ;
2011-06-21 18:03:44 +02:00
if ( preview )
updatePreview ( ) ;
2011-02-03 15:48:14 +01:00
}
2011-08-16 10:45:23 +02:00
void CppCodeStylePreferencesWidget : : slotCodeStyleSettingsChanged ( )
2011-02-03 15:48:14 +01:00
{
2011-06-21 18:03:44 +02:00
if ( m_blockUpdates )
return ;
2011-02-03 15:48:14 +01:00
2023-01-02 11:43:18 +01:00
if ( m_preferences ) {
auto current = qobject_cast < CppCodeStylePreferences * > ( m_preferences - > currentPreferences ( ) ) ;
if ( current )
current - > setCodeStyleSettings ( cppCodeStyleSettings ( ) ) ;
}
2021-11-01 16:20:08 +01:00
emit codeStyleSettingsChanged ( cppCodeStyleSettings ( ) ) ;
2011-08-16 10:45:23 +02:00
updatePreview ( ) ;
}
2015-02-04 17:01:07 +02:00
void CppCodeStylePreferencesWidget : : slotTabSettingsChanged ( const TabSettings & settings )
2011-08-16 10:45:23 +02:00
{
if ( m_blockUpdates )
return ;
2023-01-02 11:43:18 +01:00
if ( m_preferences ) {
auto current = qobject_cast < CppCodeStylePreferences * > ( m_preferences - > currentPreferences ( ) ) ;
if ( current )
current - > setTabSettings ( settings ) ;
}
2021-11-01 16:20:08 +01:00
emit tabSettingsChanged ( settings ) ;
2011-02-03 15:48:14 +01:00
updatePreview ( ) ;
}
void CppCodeStylePreferencesWidget : : updatePreview ( )
{
2011-08-16 10:45:23 +02:00
CppCodeStylePreferences * cppCodeStylePreferences = m_preferences
? m_preferences
: CppToolsSettings : : instance ( ) - > cppCodeStyle ( ) ;
const CppCodeStyleSettings ccss = cppCodeStylePreferences - > currentCodeStyleSettings ( ) ;
2015-02-04 17:01:07 +02:00
const TabSettings ts = cppCodeStylePreferences - > currentTabSettings ( ) ;
2011-08-16 10:45:23 +02:00
QtStyleCodeFormatter formatter ( ts , ccss ) ;
2022-11-17 09:16:19 +01:00
for ( SnippetEditorWidget * preview : std : : as_const ( d - > m_previews ) ) {
2014-08-01 23:31:56 +02:00
preview - > textDocument ( ) - > setTabSettings ( ts ) ;
2011-08-16 10:45:23 +02:00
preview - > setCodeStyle ( cppCodeStylePreferences ) ;
QTextDocument * doc = preview - > document ( ) ;
2011-02-03 15:48:14 +01:00
formatter . invalidateCache ( doc ) ;
QTextBlock block = doc - > firstBlock ( ) ;
QTextCursor tc = preview - > textCursor ( ) ;
tc . beginEditBlock ( ) ;
while ( block . isValid ( ) ) {
2019-01-16 09:37:54 +01:00
preview - > textDocument ( ) - > indenter ( ) - > indentBlock ( block , QChar : : Null , ts ) ;
2011-02-03 15:48:14 +01:00
block = block . next ( ) ;
}
2013-01-15 14:26:24 +01:00
applyRefactorings ( doc , preview , ccss ) ;
2011-02-03 15:48:14 +01:00
tc . endEditBlock ( ) ;
}
}
2015-02-04 17:01:07 +02:00
void CppCodeStylePreferencesWidget : : decorateEditors ( const FontSettings & fontSettings )
2011-02-03 15:48:14 +01:00
{
2022-11-17 09:16:19 +01:00
for ( SnippetEditorWidget * editor : std : : as_const ( d - > m_previews ) ) {
2014-08-01 23:31:56 +02:00
editor - > textDocument ( ) - > setFontSettings ( fontSettings ) ;
2018-02-06 15:59:05 +01:00
SnippetProvider : : decorateEditor ( editor , CppEditor : : Constants : : CPP_SNIPPETS_GROUP_ID ) ;
2011-05-31 16:36:58 +02:00
}
2011-02-03 15:48:14 +01:00
}
void CppCodeStylePreferencesWidget : : setVisualizeWhitespace ( bool on )
{
2022-11-17 09:16:19 +01:00
for ( SnippetEditorWidget * editor : std : : as_const ( d - > m_previews ) ) {
2011-02-03 15:48:14 +01:00
DisplaySettings displaySettings = editor - > displaySettings ( ) ;
displaySettings . m_visualizeWhitespace = on ;
editor - > setDisplaySettings ( displaySettings ) ;
}
}
2021-11-01 16:20:08 +01:00
void CppCodeStylePreferencesWidget : : addTab ( CppCodeStyleWidget * page , QString tabName )
2021-10-07 15:08:46 +02:00
{
2022-04-22 17:24:39 +02:00
if ( ! page )
return ;
2022-11-17 09:16:19 +01:00
d - > m_categoryTab - > insertTab ( 0 , page , tabName ) ;
d - > m_categoryTab - > setCurrentIndex ( 0 ) ;
2021-11-01 16:20:08 +01:00
connect ( page , & CppEditor : : CppCodeStyleWidget : : codeStyleSettingsChanged ,
this , [ this ] ( const CppEditor : : CppCodeStyleSettings & settings ) {
setCodeStyleSettings ( settings , true ) ;
} ) ;
connect ( page , & CppEditor : : CppCodeStyleWidget : : tabSettingsChanged ,
this , & CppCodeStylePreferencesWidget : : setTabSettings ) ;
connect ( this , & CppCodeStylePreferencesWidget : : codeStyleSettingsChanged ,
page , & CppCodeStyleWidget : : setCodeStyleSettings ) ;
connect ( this , & CppCodeStylePreferencesWidget : : tabSettingsChanged ,
page , & CppCodeStyleWidget : : setTabSettings ) ;
2022-03-29 15:48:15 +02:00
connect ( this , & CppCodeStylePreferencesWidget : : applyEmitted ,
page , & CppCodeStyleWidget : : apply ) ;
2022-08-01 17:28:42 +02:00
connect ( this , & CppCodeStylePreferencesWidget : : finishEmitted ,
page , & CppCodeStyleWidget : : finish ) ;
2021-11-01 16:20:08 +01:00
page - > synchronize ( ) ;
2021-10-07 15:08:46 +02:00
}
2011-02-03 15:48:14 +01:00
2022-03-29 15:48:15 +02:00
void CppCodeStylePreferencesWidget : : apply ( )
{
2023-01-02 11:43:18 +01:00
m_originalTabSettings = tabSettings ( ) ;
m_originalCppCodeStyleSettings = cppCodeStyleSettings ( ) ;
2022-12-08 14:15:23 +01:00
2022-03-29 15:48:15 +02:00
emit applyEmitted ( ) ;
}
2022-08-01 17:28:42 +02:00
void CppCodeStylePreferencesWidget : : finish ( )
{
2023-01-02 11:43:18 +01:00
if ( m_preferences ) {
auto current = qobject_cast < CppCodeStylePreferences * > ( m_preferences - > currentDelegate ( ) ) ;
if ( current ) {
current - > setCodeStyleSettings ( m_originalCppCodeStyleSettings ) ;
current - > setTabSettings ( m_originalTabSettings ) ;
}
}
2022-08-01 17:28:42 +02:00
emit finishEmitted ( ) ;
}
2011-02-03 15:48:14 +01:00
// ------------------ CppCodeStyleSettingsPage
2020-02-04 17:38:55 +01:00
CppCodeStyleSettingsPage : : CppCodeStyleSettingsPage ( )
2011-02-03 15:48:14 +01:00
{
2013-01-16 15:22:58 +01:00
setId ( Constants : : CPP_CODE_STYLE_SETTINGS_ID ) ;
2021-08-30 10:58:08 +02:00
setDisplayName ( QCoreApplication : : translate ( " CppEditor " , Constants : : CPP_CODE_STYLE_SETTINGS_NAME ) ) ;
2012-12-29 03:37:27 +01:00
setCategory ( Constants : : CPP_SETTINGS_CATEGORY ) ;
2011-02-03 15:48:14 +01:00
}
2013-12-03 14:17:03 +01:00
QWidget * CppCodeStyleSettingsPage : : widget ( )
2011-02-03 15:48:14 +01:00
{
2013-12-03 14:17:03 +01:00
if ( ! m_widget ) {
2018-11-20 11:23:30 +01:00
CppCodeStylePreferences * originalCodeStylePreferences = CppToolsSettings : : instance ( )
- > cppCodeStyle ( ) ;
m_pageCppCodeStylePreferences = new CppCodeStylePreferences ( ) ;
m_pageCppCodeStylePreferences - > setDelegatingPool (
originalCodeStylePreferences - > delegatingPool ( ) ) ;
m_pageCppCodeStylePreferences - > setCodeStyleSettings (
originalCodeStylePreferences - > codeStyleSettings ( ) ) ;
m_pageCppCodeStylePreferences - > setCurrentDelegate (
originalCodeStylePreferences - > currentDelegate ( ) ) ;
2013-12-03 14:17:03 +01:00
// we set id so that it won't be possible to set delegate to the original prefs
m_pageCppCodeStylePreferences - > setId ( originalCodeStylePreferences - > id ( ) ) ;
2021-08-30 10:58:08 +02:00
m_widget = TextEditorSettings : : codeStyleFactory ( CppEditor : : Constants : : CPP_SETTINGS_ID )
2018-11-20 11:23:30 +01:00
- > createCodeStyleEditor ( m_pageCppCodeStylePreferences ) ;
2013-12-03 14:17:03 +01:00
}
2011-02-03 15:48:14 +01:00
return m_widget ;
}
void CppCodeStyleSettingsPage : : apply ( )
{
if ( m_widget ) {
2012-01-24 15:36:40 +01:00
QSettings * s = Core : : ICore : : settings ( ) ;
2011-02-03 15:48:14 +01:00
2011-08-16 10:45:23 +02:00
CppCodeStylePreferences * originalCppCodeStylePreferences = CppToolsSettings : : instance ( ) - > cppCodeStyle ( ) ;
if ( originalCppCodeStylePreferences - > codeStyleSettings ( ) ! = m_pageCppCodeStylePreferences - > codeStyleSettings ( ) ) {
originalCppCodeStylePreferences - > setCodeStyleSettings ( m_pageCppCodeStylePreferences - > codeStyleSettings ( ) ) ;
2021-08-30 10:58:08 +02:00
originalCppCodeStylePreferences - > toSettings ( QLatin1String ( CppEditor : : Constants : : CPP_SETTINGS_ID ) , s ) ;
2011-02-03 15:48:14 +01:00
}
2011-08-16 10:45:23 +02:00
if ( originalCppCodeStylePreferences - > tabSettings ( ) ! = m_pageCppCodeStylePreferences - > tabSettings ( ) ) {
originalCppCodeStylePreferences - > setTabSettings ( m_pageCppCodeStylePreferences - > tabSettings ( ) ) ;
2021-08-30 10:58:08 +02:00
originalCppCodeStylePreferences - > toSettings ( QLatin1String ( CppEditor : : Constants : : CPP_SETTINGS_ID ) , s ) ;
2011-02-03 15:48:14 +01:00
}
2011-08-16 10:45:23 +02:00
if ( originalCppCodeStylePreferences - > currentDelegate ( ) ! = m_pageCppCodeStylePreferences - > currentDelegate ( ) ) {
originalCppCodeStylePreferences - > setCurrentDelegate ( m_pageCppCodeStylePreferences - > currentDelegate ( ) ) ;
2021-08-30 10:58:08 +02:00
originalCppCodeStylePreferences - > toSettings ( QLatin1String ( CppEditor : : Constants : : CPP_SETTINGS_ID ) , s ) ;
2011-02-03 15:48:14 +01:00
}
2019-01-22 14:16:30 +01:00
m_widget - > apply ( ) ;
2011-02-03 15:48:14 +01:00
}
}
2013-12-03 14:17:03 +01:00
void CppCodeStyleSettingsPage : : finish ( )
2011-02-03 15:48:14 +01:00
{
2022-08-17 15:03:52 +10:00
if ( m_widget )
m_widget - > finish ( ) ;
2013-12-03 14:17:03 +01:00
delete m_widget ;
2011-02-03 15:48:14 +01:00
}
2021-08-30 10:58:08 +02:00
} // namespace CppEditor::Internal