ClangFormat: Add the preview text editor to the settings

Allows the user to see how the current style applies to
the code snippet. The action is triggered by the 'Apply' button.

Change-Id: I820d989519cfdfb6e617ed6e8e9e5751be6619ea
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-02-22 09:48:46 +01:00
parent c6b58a063f
commit 956543e462
8 changed files with 213 additions and 174 deletions

View File

@@ -5,4 +5,4 @@ QTC_LIB_DEPENDS += \
QTC_PLUGIN_DEPENDS += \
cpptools \
projectexplorer \
texteditor
cppeditor

View File

@@ -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 <clang/Format/Format.h>
#include <coreplugin/icore.h>
#include <cppeditor/cpphighlighter.h>
#include <cpptools/cpptoolsconstants.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <texteditor/displaysettings.h>
#include <texteditor/snippets/snippeteditor.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditorsettings.h>
#include <QFile>
#include <QMessageBox>
@@ -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

View File

@@ -29,7 +29,12 @@
#include <memory>
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<Ui::ClangFormatConfigWidget> m_ui;
};

View File

@@ -55,7 +55,11 @@
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="clangFormatOptionsTable"/>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPlainTextEdit" name="clangFormatOptionsTable"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">

View File

@@ -35,7 +35,7 @@
#include <QTextDocument>
using namespace CppEditor::Internal;
using namespace CppEditor;
using namespace TextEditor;
using namespace CPlusPlus;

View File

@@ -25,6 +25,8 @@
#pragma once
#include "cppeditor_global.h"
#include <texteditor/syntaxhighlighter.h>
#include <cplusplus/Token.h>
@@ -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

View File

@@ -49,170 +49,6 @@
#include <QTextBlock>
#include <QTextStream>
static const char *defaultCodeStyleSnippets[] = {
"#include <math.h>\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 <math.h>\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 = &pi;\n"
" int*const*pcpi = &pi;\n"
" int**const cppi = &pi;\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,

View File

@@ -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 <math.h>\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 <math.h>\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 = &pi;\n"
" int*const*pcpi = &pi;\n"
" int**const cppi = &pi;\n"
"\n"
" void (*foo)(char *s) = 0;\n"
" int (*bar)[] = 0;\n"
"\n"
" return pi;\n"
"}\n"};
} // namespace Constants
} // namespace CppTools