diff --git a/src/plugins/clangformat/clangformat_dependencies.pri b/src/plugins/clangformat/clangformat_dependencies.pri index 5f3f81e1328..6822c5d66be 100644 --- a/src/plugins/clangformat/clangformat_dependencies.pri +++ b/src/plugins/clangformat/clangformat_dependencies.pri @@ -5,4 +5,4 @@ QTC_LIB_DEPENDS += \ QTC_PLUGIN_DEPENDS += \ cpptools \ projectexplorer \ - texteditor + cppeditor diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index dd66a53bd2a..6aa14b9a71a 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -26,6 +26,7 @@ #include "clangformatconfigwidget.h" #include "clangformatconstants.h" +#include "clangformatindenter.h" #include "clangformatsettings.h" #include "clangformatutils.h" #include "ui_clangformatconfigwidget.h" @@ -33,8 +34,14 @@ #include #include +#include +#include #include #include +#include +#include +#include +#include #include #include @@ -81,6 +88,17 @@ void ClangFormatConfigWidget::initialize() m_ui->applyButton->show(); hideGlobalCheckboxes(); + m_preview = new TextEditor::SnippetEditorWidget(this); + m_ui->horizontalLayout_2->addWidget(m_preview); + m_preview->setPlainText(QLatin1String(CppTools::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0])); + m_preview->textDocument()->setIndenter(new ClangFormatIndenter(m_preview->document())); + m_preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings()); + m_preview->textDocument()->setSyntaxHighlighter(new CppEditor::CppHighlighter); + + TextEditor::DisplaySettings displaySettings = m_preview->displaySettings(); + displaySettings.m_visualizeWhitespace = true; + m_preview->setDisplaySettings(displaySettings); + QLayoutItem *lastItem = m_ui->verticalLayout->itemAt(m_ui->verticalLayout->count() - 1); if (lastItem->spacerItem()) m_ui->verticalLayout->removeItem(lastItem); @@ -101,9 +119,11 @@ void ClangFormatConfigWidget::initialize() m_ui->createFileButton->hide(); + Utils::FileName fileName; if (m_project) { m_ui->projectHasClangFormat->hide(); connect(m_ui->applyButton, &QPushButton::clicked, this, &ClangFormatConfigWidget::apply); + fileName = m_project->projectFilePath().appendPath("snippet.cpp"); } else { const Project *currentProject = SessionManager::startupProject(); if (!currentProject @@ -119,9 +139,21 @@ void ClangFormatConfigWidget::initialize() createStyleFileIfNeeded(true); showGlobalCheckboxes(); m_ui->applyButton->hide(); + fileName = Utils::FileName::fromString(Core::ICore::userResourcePath()) + .appendPath("snippet.cpp"); } + m_preview->textDocument()->indenter()->setFileName(fileName); fillTable(); + updatePreview(); +} + +void ClangFormatConfigWidget::updatePreview() +{ + QTextCursor cursor(m_preview->document()); + cursor.setPosition(0); + cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); + m_preview->textDocument()->autoFormatOrIndent(cursor); } void ClangFormatConfigWidget::fillTable() @@ -153,6 +185,7 @@ void ClangFormatConfigWidget::apply() tr("Error in ClangFormat configuration"), QString::fromStdString(error.message())); fillTable(); + updatePreview(); return; } @@ -167,6 +200,8 @@ void ClangFormatConfigWidget::apply() file.write(text.toUtf8()); file.close(); + + updatePreview(); } } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatconfigwidget.h b/src/plugins/clangformat/clangformatconfigwidget.h index 7d3818b7a52..58570d9bf68 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.h +++ b/src/plugins/clangformat/clangformatconfigwidget.h @@ -29,7 +29,12 @@ #include -namespace ProjectExplorer { class Project; } +namespace ProjectExplorer { +class Project; +} +namespace TextEditor { +class SnippetEditorWidget; +} namespace ClangFormat { @@ -54,7 +59,10 @@ private: void hideGlobalCheckboxes(); void showGlobalCheckboxes(); + void updatePreview(); + ProjectExplorer::Project *m_project; + TextEditor::SnippetEditorWidget *m_preview; std::unique_ptr m_ui; }; diff --git a/src/plugins/clangformat/clangformatconfigwidget.ui b/src/plugins/clangformat/clangformatconfigwidget.ui index 9586f14d868..41e40ef1fdc 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.ui +++ b/src/plugins/clangformat/clangformatconfigwidget.ui @@ -55,7 +55,11 @@ - + + + + + diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp index 075791b021d..a7531acf1ed 100644 --- a/src/plugins/cppeditor/cpphighlighter.cpp +++ b/src/plugins/cppeditor/cpphighlighter.cpp @@ -35,7 +35,7 @@ #include -using namespace CppEditor::Internal; +using namespace CppEditor; using namespace TextEditor; using namespace CPlusPlus; diff --git a/src/plugins/cppeditor/cpphighlighter.h b/src/plugins/cppeditor/cpphighlighter.h index e43ce63172b..a982191b7ec 100644 --- a/src/plugins/cppeditor/cpphighlighter.h +++ b/src/plugins/cppeditor/cpphighlighter.h @@ -25,6 +25,8 @@ #pragma once +#include "cppeditor_global.h" + #include #include @@ -33,10 +35,7 @@ namespace CppEditor { -namespace Internal { - - -class CppHighlighter : public TextEditor::SyntaxHighlighter +class CPPEDITOR_EXPORT CppHighlighter : public TextEditor::SyntaxHighlighter { Q_OBJECT @@ -58,5 +57,4 @@ private: CPlusPlus::LanguageFeatures m_languageFeatures = CPlusPlus::LanguageFeatures::defaultFeatures(); }; -} // namespace Internal } // namespace CppEditor diff --git a/src/plugins/cpptools/cppcodestylesettingspage.cpp b/src/plugins/cpptools/cppcodestylesettingspage.cpp index 39517e6e353..981a0bda617 100644 --- a/src/plugins/cpptools/cppcodestylesettingspage.cpp +++ b/src/plugins/cpptools/cppcodestylesettingspage.cpp @@ -49,170 +49,6 @@ #include #include -static const char *defaultCodeStyleSnippets[] = { - "#include \n" - "\n" - "class Complex\n" - " {\n" - "public:\n" - " Complex(double re, double im)\n" - " : _re(re), _im(im)\n" - " {}\n" - " double modulus() const\n" - " {\n" - " return sqrt(_re * _re + _im * _im);\n" - " }\n" - "private:\n" - " double _re;\n" - " double _im;\n" - " };\n" - "\n" - "void bar(int i)\n" - " {\n" - " static int counter = 0;\n" - " counter += i;\n" - " }\n" - "\n" - "namespace Foo\n" - " {\n" - " namespace Bar\n" - " {\n" - " void foo(int a, int b)\n" - " {\n" - " for (int i = 0; i < a; i++)\n" - " {\n" - " if (i < b)\n" - " bar(i);\n" - " else\n" - " {\n" - " bar(i);\n" - " bar(b);\n" - " }\n" - " }\n" - " }\n" - " } // namespace Bar\n" - " } // namespace Foo\n" - , - "#include \n" - "\n" - "class Complex\n" - " {\n" - "public:\n" - " Complex(double re, double im)\n" - " : _re(re), _im(im)\n" - " {}\n" - " double modulus() const\n" - " {\n" - " return sqrt(_re * _re + _im * _im);\n" - " }\n" - "private:\n" - " double _re;\n" - " double _im;\n" - " };\n" - "\n" - "void bar(int i)\n" - " {\n" - " static int counter = 0;\n" - " counter += i;\n" - " }\n" - "\n" - "namespace Foo\n" - " {\n" - " namespace Bar\n" - " {\n" - " void foo(int a, int b)\n" - " {\n" - " for (int i = 0; i < a; i++)\n" - " {\n" - " if (i < b)\n" - " bar(i);\n" - " else\n" - " {\n" - " bar(i);\n" - " bar(b);\n" - " }\n" - " }\n" - " }\n" - " } // namespace Bar\n" - " } // namespace Foo\n" - , - "namespace Foo\n" - "{\n" - "namespace Bar\n" - "{\n" - "class FooBar\n" - " {\n" - "public:\n" - " FooBar(int a)\n" - " : _a(a)\n" - " {}\n" - " int calculate() const\n" - " {\n" - " if (a > 10)\n" - " {\n" - " int b = 2 * a;\n" - " return a * b;\n" - " }\n" - " return -a;\n" - " }\n" - "private:\n" - " int _a;\n" - " };\n" - "}\n" - "}\n" - , - "#include \"bar.h\"\n" - "\n" - "int foo(int a)\n" - " {\n" - " switch (a)\n" - " {\n" - " case 1:\n" - " bar(1);\n" - " break;\n" - " case 2:\n" - " {\n" - " bar(2);\n" - " break;\n" - " }\n" - " case 3:\n" - " default:\n" - " bar(3);\n" - " break;\n" - " }\n" - " return 0;\n" - " }\n" - , - "void foo() {\n" - " if (a &&\n" - " b)\n" - " c;\n" - "\n" - " while (a ||\n" - " b)\n" - " break;\n" - " a = b +\n" - " c;\n" - " myInstance.longMemberName +=\n" - " foo;\n" - " myInstance.longMemberName += bar +\n" - " foo;\n" - "}\n" - , - "int *foo(const Bar &b1, Bar &&b2, int*, int *&rpi)\n" - "{\n" - " int*pi = 0;\n" - " int*const*const cpcpi = π\n" - " int*const*pcpi = π\n" - " int**const cppi = π\n" - "\n" - " void (*foo)(char *s) = 0;\n" - " int (*bar)[] = 0;\n" - "\n" - " return pi;\n" - "}\n" -}; - using namespace TextEditor; namespace CppTools { @@ -270,7 +106,7 @@ CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent) << m_ui->previewTextEditBraces << m_ui->previewTextEditSwitch << m_ui->previewTextEditPadding << m_ui->previewTextEditPointerReferences; for (int i = 0; i < m_previews.size(); ++i) - m_previews[i]->setPlainText(QLatin1String(defaultCodeStyleSnippets[i])); + m_previews[i]->setPlainText(QLatin1String(Constants::DEFAULT_CODE_STYLE_SNIPPETS[i])); decorateEditors(TextEditorSettings::fontSettings()); connect(TextEditorSettings::instance(), &TextEditorSettings::fontSettingsChanged, diff --git a/src/plugins/cpptools/cpptoolsconstants.h b/src/plugins/cpptools/cpptoolsconstants.h index 2e1aebfd432..781f5ebdf89 100644 --- a/src/plugins/cpptools/cpptoolsconstants.h +++ b/src/plugins/cpptools/cpptoolsconstants.h @@ -105,5 +105,163 @@ constexpr const char TIDY_DOCUMENTATION_URL_TEMPLATE[] constexpr const char CLAZY_DOCUMENTATION_URL_TEMPLATE[] = "https://github.com/KDE/clazy/blob/master/docs/checks/README-%1.md"; +static const char *DEFAULT_CODE_STYLE_SNIPPETS[] + = {"#include \n" + "\n" + "class Complex\n" + " {\n" + "public:\n" + " Complex(double re, double im)\n" + " : _re(re), _im(im)\n" + " {}\n" + " double modulus() const\n" + " {\n" + " return sqrt(_re * _re + _im * _im);\n" + " }\n" + "private:\n" + " double _re;\n" + " double _im;\n" + " };\n" + "\n" + "void bar(int i)\n" + " {\n" + " static int counter = 0;\n" + " counter += i;\n" + " }\n" + "\n" + "namespace Foo\n" + " {\n" + " namespace Bar\n" + " {\n" + " void foo(int a, int b)\n" + " {\n" + " for (int i = 0; i < a; i++)\n" + " {\n" + " if (i < b)\n" + " bar(i);\n" + " else\n" + " {\n" + " bar(i);\n" + " bar(b);\n" + " }\n" + " }\n" + " }\n" + " } // namespace Bar\n" + " } // namespace Foo\n", + "#include \n" + "\n" + "class Complex\n" + " {\n" + "public:\n" + " Complex(double re, double im)\n" + " : _re(re), _im(im)\n" + " {}\n" + " double modulus() const\n" + " {\n" + " return sqrt(_re * _re + _im * _im);\n" + " }\n" + "private:\n" + " double _re;\n" + " double _im;\n" + " };\n" + "\n" + "void bar(int i)\n" + " {\n" + " static int counter = 0;\n" + " counter += i;\n" + " }\n" + "\n" + "namespace Foo\n" + " {\n" + " namespace Bar\n" + " {\n" + " void foo(int a, int b)\n" + " {\n" + " for (int i = 0; i < a; i++)\n" + " {\n" + " if (i < b)\n" + " bar(i);\n" + " else\n" + " {\n" + " bar(i);\n" + " bar(b);\n" + " }\n" + " }\n" + " }\n" + " } // namespace Bar\n" + " } // namespace Foo\n", + "namespace Foo\n" + "{\n" + "namespace Bar\n" + "{\n" + "class FooBar\n" + " {\n" + "public:\n" + " FooBar(int a)\n" + " : _a(a)\n" + " {}\n" + " int calculate() const\n" + " {\n" + " if (a > 10)\n" + " {\n" + " int b = 2 * a;\n" + " return a * b;\n" + " }\n" + " return -a;\n" + " }\n" + "private:\n" + " int _a;\n" + " };\n" + "}\n" + "}\n", + "#include \"bar.h\"\n" + "\n" + "int foo(int a)\n" + " {\n" + " switch (a)\n" + " {\n" + " case 1:\n" + " bar(1);\n" + " break;\n" + " case 2:\n" + " {\n" + " bar(2);\n" + " break;\n" + " }\n" + " case 3:\n" + " default:\n" + " bar(3);\n" + " break;\n" + " }\n" + " return 0;\n" + " }\n", + "void foo() {\n" + " if (a &&\n" + " b)\n" + " c;\n" + "\n" + " while (a ||\n" + " b)\n" + " break;\n" + " a = b +\n" + " c;\n" + " myInstance.longMemberName +=\n" + " foo;\n" + " myInstance.longMemberName += bar +\n" + " foo;\n" + "}\n", + "int *foo(const Bar &b1, Bar &&b2, int*, int *&rpi)\n" + "{\n" + " int*pi = 0;\n" + " int*const*const cpcpi = π\n" + " int*const*pcpi = π\n" + " int**const cppi = π\n" + "\n" + " void (*foo)(char *s) = 0;\n" + " int (*bar)[] = 0;\n" + "\n" + " return pi;\n" + "}\n"}; + } // namespace Constants } // namespace CppTools