Line length for QML/JS editing automatic formatting

Adding parameters to functions in the QML/JS formatter
Adding widget and setting to the QML/JS editing settings

Fixes: QTCREATORBUG-23411
Change-Id: Ib9d3ac3b22443e81cd636fbc276c6544dab1511b
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@qt.io>
This commit is contained in:
Xavier BESSON
2022-04-02 12:15:26 +02:00
parent 4762d54401
commit 87e52ad189
21 changed files with 780 additions and 61 deletions

View File

@@ -98,6 +98,7 @@ class Rewriter : protected Visitor
bool _hadEmptyLine = false; bool _hadEmptyLine = false;
int _binaryExpDepth = 0; int _binaryExpDepth = 0;
bool _hasOpenComment = false; bool _hasOpenComment = false;
int _lineLength = 80;
public: public:
Rewriter(Document::Ptr doc) Rewriter(Document::Ptr doc)
@@ -107,6 +108,7 @@ public:
void setIndentSize(int size) { _formatter.setIndentSize(size); } void setIndentSize(int size) { _formatter.setIndentSize(size); }
void setTabSize(int size) { _formatter.setTabSize(size); } void setTabSize(int size) { _formatter.setTabSize(size); }
void setLineLength(int size) { _lineLength = size; }
QString operator()(Node *node) QString operator()(Node *node)
{ {
@@ -286,10 +288,10 @@ protected:
QStringList lines; QStringList lines;
qreal badnessFromSplits; qreal badnessFromSplits;
qreal badness() qreal badness(int lineLength)
{ {
const int maxLineLength = 80; const int maxLineLength = lineLength;
const int strongMaxLineLength = 100; const int strongMaxLineLength = lineLength + 20;
const int minContentLength = 10; const int minContentLength = 10;
qreal result = badnessFromSplits; qreal result = badnessFromSplits;
@@ -369,7 +371,7 @@ protected:
nested.lines.prepend(newContextLine); nested.lines.prepend(newContextLine);
nested.badnessFromSplits += possibleSplits.at(i).badness; nested.badnessFromSplits += possibleSplits.at(i).badness;
if (nested.badness() < result.badness()) if (nested.badness(_lineLength) < result.badness(_lineLength))
result = nested; result = nested;
} }
@@ -391,6 +393,7 @@ protected:
_indent = tryIndent(_line); _indent = tryIndent(_line);
adjustIndent(&_line, &_possibleSplits, _indent); adjustIndent(&_line, &_possibleSplits, _indent);
if (_lineLength > 0) {
// maybe make multi-line? // maybe make multi-line?
BestSplit split = computeBestSplits(QStringList(), _line, _possibleSplits); BestSplit split = computeBestSplits(QStringList(), _line, _possibleSplits);
if (!split.lines.isEmpty() && split.lines.size() > 1) { if (!split.lines.isEmpty() && split.lines.size() > 1) {
@@ -401,6 +404,7 @@ protected:
} }
} }
} }
}
finishLine(); finishLine();
_possibleSplits.clear(); _possibleSplits.clear();
@@ -1414,10 +1418,11 @@ QString QmlJS::reformat(const Document::Ptr &doc)
return rewriter(doc->ast()); return rewriter(doc->ast());
} }
QString QmlJS::reformat(const Document::Ptr &doc, int indentSize, int tabSize) QString QmlJS::reformat(const Document::Ptr &doc, int indentSize, int tabSize, int lineLength)
{ {
Rewriter rewriter(doc); Rewriter rewriter(doc);
rewriter.setIndentSize(indentSize); rewriter.setIndentSize(indentSize);
rewriter.setTabSize(tabSize); rewriter.setTabSize(tabSize);
rewriter.setLineLength(lineLength);
return rewriter(doc->ast()); return rewriter(doc->ast());
} }

View File

@@ -31,5 +31,5 @@
namespace QmlJS { namespace QmlJS {
QMLJS_EXPORT QString reformat(const Document::Ptr &doc); QMLJS_EXPORT QString reformat(const Document::Ptr &doc);
QMLJS_EXPORT QString reformat(const Document::Ptr &doc, int indentSize, int tabSize); QMLJS_EXPORT QString reformat(const Document::Ptr &doc, int indentSize, int tabSize, int lineLength);
} } // namespace QmlJS

View File

@@ -28,12 +28,12 @@
#include <qmljs/qmljsmodelmanagerinterface.h> #include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljs/parser/qmljsast_p.h> #include <qmljs/parser/qmljsast_p.h>
#include <qmljstools/qmljsindenter.h> #include <qmljstools/qmljsindenter.h>
#include <qmljstools/qmljscodestylepreferences.h>
#include <qmljseditor/qmljseditordocument.h> #include <qmljseditor/qmljseditordocument.h>
#include <qmljseditor/qmljscomponentfromobjectdef.h> #include <qmljseditor/qmljscomponentfromobjectdef.h>
#include <qmljseditor/qmljscompletionassist.h> #include <qmljseditor/qmljscompletionassist.h>
#include <qmljstools/qmljstoolssettings.h> #include <qmljstools/qmljstoolssettings.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <texteditor/simplecodestylepreferences.h>
#include <utils/changeset.h> #include <utils/changeset.h>
#include <typeinfo> #include <typeinfo>

View File

@@ -38,6 +38,8 @@
#include <qmljs/qmljsmodelmanagerinterface.h> #include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljs/qmljsreformatter.h> #include <qmljs/qmljsreformatter.h>
#include <qmljstools/qmljstoolsconstants.h> #include <qmljstools/qmljstoolsconstants.h>
#include <qmljstools/qmljstoolssettings.h>
#include <qmljstools/qmljscodestylepreferences.h>
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
@@ -274,7 +276,8 @@ void QmlJSEditorPluginPrivate::reformatFile()
TextEditor::TabSettings tabSettings = m_currentDocument->tabSettings(); TextEditor::TabSettings tabSettings = m_currentDocument->tabSettings();
const QString &newText = QmlJS::reformat(document, const QString &newText = QmlJS::reformat(document,
tabSettings.m_indentSize, tabSettings.m_indentSize,
tabSettings.m_tabSize); tabSettings.m_tabSize,
QmlJSTools::QmlJSToolsSettings::globalCodeStyle()->currentCodeStyleSettings().lineLength);
// QTextDocument::setPlainText cannot be used, as it would reset undo/redo history // QTextDocument::setPlainText cannot be used, as it would reset undo/redo history
const auto setNewText = [this, &newText]() { const auto setNewText = [this, &newText]() {

View File

@@ -5,6 +5,10 @@ add_qtc_plugin(QmlJSTools
SOURCES SOURCES
qmljsbundleprovider.cpp qmljsbundleprovider.h qmljsbundleprovider.cpp qmljsbundleprovider.h
qmljscodestylepreferencesfactory.cpp qmljscodestylepreferencesfactory.h qmljscodestylepreferencesfactory.cpp qmljscodestylepreferencesfactory.h
qmljscodestylepreferences.cpp qmljscodestylepreferences.h
qmljscodestylepreferenceswidget.cpp qmljscodestylepreferenceswidget.h
qmljscodestylesettings.cpp qmljscodestylesettings.h
qmljscodestylesettingswidget.cpp qmljscodestylesettingswidget.h qmljscodestylesettingswidget.ui
qmljscodestylesettingspage.cpp qmljscodestylesettingspage.h qmljscodestylesettingspage.ui qmljscodestylesettingspage.cpp qmljscodestylesettingspage.h qmljscodestylesettingspage.ui
qmljsfunctionfilter.cpp qmljsfunctionfilter.h qmljsfunctionfilter.cpp qmljsfunctionfilter.h
qmljsindenter.cpp qmljsindenter.h qmljsindenter.cpp qmljsindenter.h

View File

@@ -0,0 +1,110 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "qmljscodestylepreferences.h"
namespace QmlJSTools {
QmlJSCodeStylePreferences::QmlJSCodeStylePreferences(QObject *parent) :
ICodeStylePreferences(parent)
{
setSettingsSuffix("CodeStyleSettings");
connect(this, &QmlJSCodeStylePreferences::currentValueChanged,
this, &QmlJSCodeStylePreferences::slotCurrentValueChanged);
}
QVariant QmlJSCodeStylePreferences::value() const
{
QVariant v;
v.setValue(codeStyleSettings());
return v;
}
void QmlJSCodeStylePreferences::setValue(const QVariant &data)
{
if (!data.canConvert<QmlJSCodeStyleSettings>())
return;
setCodeStyleSettings(data.value<QmlJSCodeStyleSettings>());
}
QmlJSCodeStyleSettings QmlJSCodeStylePreferences::codeStyleSettings() const
{
return m_data;
}
void QmlJSCodeStylePreferences::setCodeStyleSettings(const QmlJSCodeStyleSettings &data)
{
if (m_data == data)
return;
m_data = data;
QVariant v;
v.setValue(data);
emit valueChanged(v);
emit codeStyleSettingsChanged(m_data);
if (!currentDelegate())
emit currentValueChanged(v);
}
QmlJSCodeStyleSettings QmlJSCodeStylePreferences::currentCodeStyleSettings() const
{
QVariant v = currentValue();
if (!v.canConvert<QmlJSCodeStyleSettings>()) {
// warning
return {};
}
return v.value<QmlJSCodeStyleSettings>();
}
void QmlJSCodeStylePreferences::slotCurrentValueChanged(const QVariant &value)
{
if (!value.canConvert<QmlJSCodeStyleSettings>())
return;
emit currentCodeStyleSettingsChanged(value.value<QmlJSCodeStyleSettings>());
}
QVariantMap QmlJSCodeStylePreferences::toMap() const
{
QVariantMap map = ICodeStylePreferences::toMap();
if (!currentDelegate()) {
const QVariantMap dataMap = m_data.toMap();
for (auto it = dataMap.begin(), end = dataMap.end(); it != end; ++it)
map.insert(it.key(), it.value());
}
return map;
}
void QmlJSCodeStylePreferences::fromMap(const QVariantMap &map)
{
ICodeStylePreferences::fromMap(map);
if (!currentDelegate())
m_data.fromMap(map);
}
} // namespace QmlJSTools

View File

@@ -0,0 +1,65 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "qmljstools_global.h"
#include "qmljscodestylesettings.h"
#include <texteditor/icodestylepreferences.h>
namespace QmlJSTools {
class QMLJSTOOLS_EXPORT QmlJSCodeStylePreferences : public TextEditor::ICodeStylePreferences
{
Q_OBJECT
public:
explicit QmlJSCodeStylePreferences(QObject *parent = nullptr);
QVariant value() const override;
void setValue(const QVariant &) override;
QmlJSCodeStyleSettings codeStyleSettings() const;
// tracks parent hierarchy until currentParentSettings is null
QmlJSCodeStyleSettings currentCodeStyleSettings() const;
QVariantMap toMap() const override;
void fromMap(const QVariantMap &map) override;
public slots:
void setCodeStyleSettings(const QmlJSCodeStyleSettings &data);
signals:
void codeStyleSettingsChanged(const QmlJSCodeStyleSettings &);
void currentCodeStyleSettingsChanged(const QmlJSCodeStyleSettings &);
private:
void slotCurrentValueChanged(const QVariant &);
QmlJSCodeStyleSettings m_data;
};
} // namespace QmlJSTools

View File

@@ -28,7 +28,7 @@
#include "qmljstoolsconstants.h" #include "qmljstoolsconstants.h"
#include "qmljsindenter.h" #include "qmljsindenter.h"
#include <texteditor/simplecodestylepreferences.h> #include <qmljscodestylepreferences.h>
#include <qmljseditor/qmljseditorconstants.h> #include <qmljseditor/qmljseditorconstants.h>
@@ -68,16 +68,19 @@ QString QmlJSCodeStylePreferencesFactory::displayName()
TextEditor::ICodeStylePreferences *QmlJSCodeStylePreferencesFactory::createCodeStyle() const TextEditor::ICodeStylePreferences *QmlJSCodeStylePreferencesFactory::createCodeStyle() const
{ {
return new TextEditor::SimpleCodeStylePreferences(); return new QmlJSCodeStylePreferences();
} }
TextEditor::CodeStyleEditorWidget *QmlJSCodeStylePreferencesFactory::createEditor( TextEditor::CodeStyleEditorWidget *QmlJSCodeStylePreferencesFactory::createEditor(
TextEditor::ICodeStylePreferences *preferences, TextEditor::ICodeStylePreferences *preferences,
QWidget *parent) const QWidget *parent) const
{ {
auto qmlJSPreferences = qobject_cast<QmlJSCodeStylePreferences *>(preferences);
if (!qmlJSPreferences)
return nullptr;
auto widget = new Internal::QmlJSCodeStylePreferencesWidget(parent); auto widget = new Internal::QmlJSCodeStylePreferencesWidget(parent);
widget->layout()->setContentsMargins(0, 0, 0, 0); widget->layout()->setContentsMargins(0, 0, 0, 0);
widget->setPreferences(preferences); widget->setPreferences(qmlJSPreferences);
return widget; return widget;
} }

View File

@@ -0,0 +1,94 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "qmljscodestylepreferenceswidget.h"
#include "qmljscodestylepreferences.h"
#include "qmljscodestylesettingswidget.h"
#include "qmljscodestylesettings.h"
#include <QVBoxLayout>
#include <QLabel>
namespace QmlJSTools {
QmlJSCodeStylePreferencesWidget::QmlJSCodeStylePreferencesWidget(QWidget *parent) :
QWidget(parent)
{
m_codeStyleSettingsWidget = new QmlJSCodeStyleSettingsWidget(this);
auto layout = new QVBoxLayout(this);
layout->addWidget(m_codeStyleSettingsWidget);
layout->setContentsMargins(QMargins());
m_codeStyleSettingsWidget->setEnabled(false);
}
void QmlJSCodeStylePreferencesWidget::setPreferences(QmlJSCodeStylePreferences *preferences)
{
if (m_preferences == preferences)
return; // nothing changes
// cleanup old
if (m_preferences) {
disconnect(m_preferences, &QmlJSCodeStylePreferences::currentCodeStyleSettingsChanged,
m_codeStyleSettingsWidget, &QmlJSCodeStyleSettingsWidget::setCodeStyleSettings);
disconnect(m_preferences, &QmlJSCodeStylePreferences::currentPreferencesChanged,
this, &QmlJSCodeStylePreferencesWidget::slotCurrentPreferencesChanged);
disconnect(m_codeStyleSettingsWidget, &QmlJSCodeStyleSettingsWidget::settingsChanged,
this, &QmlJSCodeStylePreferencesWidget::slotSettingsChanged);
}
m_preferences = preferences;
// fillup new
if (m_preferences) {
slotCurrentPreferencesChanged(m_preferences->currentPreferences());
m_codeStyleSettingsWidget->setCodeStyleSettings(m_preferences->currentCodeStyleSettings());
connect(m_preferences, &QmlJSCodeStylePreferences::currentCodeStyleSettingsChanged,
m_codeStyleSettingsWidget, &QmlJSCodeStyleSettingsWidget::setCodeStyleSettings);
connect(m_preferences, &QmlJSCodeStylePreferences::currentPreferencesChanged,
this, &QmlJSCodeStylePreferencesWidget::slotCurrentPreferencesChanged);
connect(m_codeStyleSettingsWidget, &QmlJSCodeStyleSettingsWidget::settingsChanged,
this, &QmlJSCodeStylePreferencesWidget::slotSettingsChanged);
}
m_codeStyleSettingsWidget->setEnabled(m_preferences);
}
void QmlJSCodeStylePreferencesWidget::slotCurrentPreferencesChanged(TextEditor::ICodeStylePreferences *preferences)
{
m_codeStyleSettingsWidget->setEnabled(!preferences->isReadOnly() && !m_preferences->currentDelegate());
}
void QmlJSCodeStylePreferencesWidget::slotSettingsChanged(const QmlJSCodeStyleSettings &settings)
{
if (!m_preferences)
return;
QmlJSCodeStylePreferences *current = qobject_cast<QmlJSCodeStylePreferences*>(m_preferences->currentPreferences());
if (!current)
return;
current->setCodeStyleSettings(settings);
}
} // namespace QmlJSTools

View File

@@ -0,0 +1,60 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "qmljstools_global.h"
#include <QWidget>
namespace TextEditor {
class ICodeStylePreferences;
}
namespace QmlJSTools {
class QmlJSCodeStyleSettings;
class QmlJSCodeStyleSettingsWidget;
class QmlJSCodeStylePreferences;
namespace Ui { class TabPreferencesWidget; }
class QMLJSTOOLS_EXPORT QmlJSCodeStylePreferencesWidget : public QWidget
{
Q_OBJECT
public:
explicit QmlJSCodeStylePreferencesWidget(QWidget *parent = nullptr);
void setPreferences(QmlJSCodeStylePreferences *tabPreferences);
private:
void slotCurrentPreferencesChanged(TextEditor::ICodeStylePreferences* preferences);
void slotSettingsChanged(const QmlJSCodeStyleSettings &settings);
QmlJSCodeStyleSettingsWidget *m_codeStyleSettingsWidget;
QmlJSCodeStylePreferences *m_preferences = nullptr;
};
} // namespace QmlJSTools

View File

@@ -0,0 +1,83 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "qmljscodestylesettings.h"
#include "qmljscodestylepreferences.h"
#include "qmljstoolssettings.h"
#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h>
#include <texteditor/tabsettings.h>
#include <cplusplus/Overview.h>
#include <utils/qtcassert.h>
#include <utils/settingsutils.h>
static const char lineLengthKey[] = "LineLength";
namespace QmlJSTools {
// ------------------ QmlJSCodeStyleSettingsWidget
QmlJSCodeStyleSettings::QmlJSCodeStyleSettings() = default;
QVariantMap QmlJSCodeStyleSettings::toMap() const
{
return {
{lineLengthKey, lineLength}
};
}
void QmlJSCodeStyleSettings::fromMap(const QVariantMap &map)
{
lineLength = map.value(lineLengthKey, lineLength).toInt();
}
bool QmlJSCodeStyleSettings::equals(const QmlJSCodeStyleSettings &rhs) const
{
return lineLength == rhs.lineLength;
}
QmlJSCodeStyleSettings QmlJSCodeStyleSettings::currentGlobalCodeStyle()
{
QmlJSCodeStylePreferences *QmlJSCodeStylePreferences = QmlJSToolsSettings::globalCodeStyle();
QTC_ASSERT(QmlJSCodeStylePreferences, return QmlJSCodeStyleSettings());
return QmlJSCodeStylePreferences->currentCodeStyleSettings();
}
TextEditor::TabSettings QmlJSCodeStyleSettings::currentGlobalTabSettings()
{
QmlJSCodeStylePreferences *QmlJSCodeStylePreferences = QmlJSToolsSettings::globalCodeStyle();
QTC_ASSERT(QmlJSCodeStylePreferences, return TextEditor::TabSettings());
return QmlJSCodeStylePreferences->currentTabSettings();
}
} // namespace QmlJSTools

View File

@@ -0,0 +1,58 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "qmljstools_global.h"
#include <utils/optional.h>
#include <QVariantMap>
namespace TextEditor { class TabSettings; }
namespace QmlJSTools {
class QMLJSTOOLS_EXPORT QmlJSCodeStyleSettings
{
public:
QmlJSCodeStyleSettings();
int lineLength = 80;
QVariantMap toMap() const;
void fromMap(const QVariantMap &map);
bool equals(const QmlJSCodeStyleSettings &rhs) const;
bool operator==(const QmlJSCodeStyleSettings &s) const { return equals(s); }
bool operator!=(const QmlJSCodeStyleSettings &s) const { return !equals(s); }
static QmlJSCodeStyleSettings currentGlobalCodeStyle();
static TextEditor::TabSettings currentGlobalTabSettings();
};
} // namespace CppEditor
Q_DECLARE_METATYPE(QmlJSTools::QmlJSCodeStyleSettings)

View File

@@ -29,11 +29,11 @@
#include "qmljstoolssettings.h" #include "qmljstoolssettings.h"
#include "qmljsindenter.h" #include "qmljsindenter.h"
#include "qmljsqtstylecodeformatter.h" #include "qmljsqtstylecodeformatter.h"
#include "qmljscodestylepreferences.h"
#include <texteditor/fontsettings.h> #include <texteditor/fontsettings.h>
#include <texteditor/snippets/snippetprovider.h> #include <texteditor/snippets/snippetprovider.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <texteditor/simplecodestylepreferences.h>
#include <texteditor/displaysettings.h> #include <texteditor/displaysettings.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
#include <texteditor/codestyleeditor.h> #include <texteditor/codestyleeditor.h>
@@ -70,13 +70,18 @@ QmlJSCodeStylePreferencesWidget::~QmlJSCodeStylePreferencesWidget()
delete m_ui; delete m_ui;
} }
void QmlJSCodeStylePreferencesWidget::setPreferences(ICodeStylePreferences *preferences) void QmlJSCodeStylePreferencesWidget::setPreferences(QmlJSCodeStylePreferences *preferences)
{ {
m_preferences = preferences; m_preferences = preferences;
m_ui->tabPreferencesWidget->setPreferences(preferences); m_ui->tabPreferencesWidget->setPreferences(preferences);
m_ui->codeStylePreferencesWidget->setPreferences(preferences);
if (m_preferences) if (m_preferences)
{
connect(m_preferences, &ICodeStylePreferences::currentTabSettingsChanged, connect(m_preferences, &ICodeStylePreferences::currentTabSettingsChanged,
this, &QmlJSCodeStylePreferencesWidget::slotSettingsChanged); this, &QmlJSCodeStylePreferencesWidget::slotSettingsChanged);
connect(m_preferences, &QmlJSCodeStylePreferences::currentCodeStyleSettingsChanged,
this, &QmlJSCodeStylePreferencesWidget::slotSettingsChanged);
}
updatePreview(); updatePreview();
} }
@@ -134,15 +139,16 @@ QmlJSCodeStyleSettingsPage::QmlJSCodeStyleSettingsPage()
QWidget *QmlJSCodeStyleSettingsPage::widget() QWidget *QmlJSCodeStyleSettingsPage::widget()
{ {
if (!m_widget) { if (!m_widget) {
SimpleCodeStylePreferences *originalTabPreferences QmlJSCodeStylePreferences *originalPreferences
= QmlJSToolsSettings::globalCodeStyle(); = QmlJSToolsSettings::globalCodeStyle();
m_pageTabPreferences = new SimpleCodeStylePreferences(m_widget); m_preferences = new QmlJSCodeStylePreferences(m_widget);
m_pageTabPreferences->setDelegatingPool(originalTabPreferences->delegatingPool()); m_preferences->setDelegatingPool(originalPreferences->delegatingPool());
m_pageTabPreferences->setTabSettings(originalTabPreferences->tabSettings()); m_preferences->setCodeStyleSettings(originalPreferences->codeStyleSettings());
m_pageTabPreferences->setCurrentDelegate(originalTabPreferences->currentDelegate()); m_preferences->setTabSettings(originalPreferences->tabSettings());
m_pageTabPreferences->setId(originalTabPreferences->id()); m_preferences->setCurrentDelegate(originalPreferences->currentDelegate());
m_preferences->setId(originalPreferences->id());
m_widget = new CodeStyleEditor(TextEditorSettings::codeStyleFactory(QmlJSTools::Constants::QML_JS_SETTINGS_ID), m_widget = new CodeStyleEditor(TextEditorSettings::codeStyleFactory(QmlJSTools::Constants::QML_JS_SETTINGS_ID),
m_pageTabPreferences); m_preferences);
} }
return m_widget; return m_widget;
} }
@@ -152,14 +158,18 @@ void QmlJSCodeStyleSettingsPage::apply()
if (m_widget) { if (m_widget) {
QSettings *s = Core::ICore::settings(); QSettings *s = Core::ICore::settings();
SimpleCodeStylePreferences *originalTabPreferences = QmlJSToolsSettings::globalCodeStyle(); QmlJSCodeStylePreferences *originalPreferences = QmlJSToolsSettings::globalCodeStyle();
if (originalTabPreferences->tabSettings() != m_pageTabPreferences->tabSettings()) { if (originalPreferences->codeStyleSettings() != m_preferences->codeStyleSettings()) {
originalTabPreferences->setTabSettings(m_pageTabPreferences->tabSettings()); originalPreferences->setCodeStyleSettings(m_preferences->codeStyleSettings());
originalTabPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s); originalPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
} }
if (originalTabPreferences->currentDelegate() != m_pageTabPreferences->currentDelegate()) { if (originalPreferences->tabSettings() != m_preferences->tabSettings()) {
originalTabPreferences->setCurrentDelegate(m_pageTabPreferences->currentDelegate()); originalPreferences->setTabSettings(m_preferences->tabSettings());
originalTabPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s); originalPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
}
if (originalPreferences->currentDelegate() != m_preferences->currentDelegate()) {
originalPreferences->setCurrentDelegate(m_preferences->currentDelegate());
originalPreferences->toSettings(QLatin1String(QmlJSTools::Constants::QML_JS_SETTINGS_ID), s);
} }
} }
} }

View File

@@ -37,12 +37,13 @@ QT_END_NAMESPACE
namespace TextEditor { namespace TextEditor {
class FontSettings; class FontSettings;
class TabSettings;
class CodeStyleEditor; class CodeStyleEditor;
class ICodeStylePreferences;
} }
namespace QmlJSTools { namespace QmlJSTools {
class QmlJSCodeStylePreferences;
class QmlJSCodeStyleSettings;
namespace Internal { namespace Internal {
namespace Ui { class QmlJSCodeStyleSettingsPage; } namespace Ui { class QmlJSCodeStyleSettingsPage; }
@@ -55,7 +56,7 @@ public:
explicit QmlJSCodeStylePreferencesWidget(QWidget *parent = nullptr); explicit QmlJSCodeStylePreferencesWidget(QWidget *parent = nullptr);
~QmlJSCodeStylePreferencesWidget() override; ~QmlJSCodeStylePreferencesWidget() override;
void setPreferences(TextEditor::ICodeStylePreferences *preferences); void setPreferences(QmlJSCodeStylePreferences* preferences);
private: private:
void decorateEditor(const TextEditor::FontSettings &fontSettings); void decorateEditor(const TextEditor::FontSettings &fontSettings);
@@ -63,7 +64,7 @@ private:
void slotSettingsChanged(); void slotSettingsChanged();
void updatePreview(); void updatePreview();
TextEditor::ICodeStylePreferences *m_preferences = nullptr; QmlJSCodeStylePreferences *m_preferences = nullptr;
Ui::QmlJSCodeStyleSettingsPage *m_ui; Ui::QmlJSCodeStyleSettingsPage *m_ui;
}; };
@@ -78,7 +79,7 @@ public:
void finish() override; void finish() override;
private: private:
TextEditor::ICodeStylePreferences *m_pageTabPreferences = nullptr; QmlJSCodeStylePreferences *m_preferences = nullptr;
QPointer<TextEditor::CodeStyleEditor> m_widget; QPointer<TextEditor::CodeStyleEditor> m_widget;
}; };

View File

@@ -24,7 +24,20 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1" rowspan="2"> <item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>267</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1" rowspan="3">
<widget class="TextEditor::SnippetEditorWidget" name="previewTextEdit"> <widget class="TextEditor::SnippetEditorWidget" name="previewTextEdit">
<property name="plainText"> <property name="plainText">
<string notr="true">import QtQuick 1.0 <string notr="true">import QtQuick 1.0
@@ -48,17 +61,7 @@ Rectangle {
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="1" column="0">
<spacer name="verticalSpacer"> <widget class="QmlJSTools::QmlJSCodeStylePreferencesWidget" name="codeStylePreferencesWidget" native="true"/>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>267</height>
</size>
</property>
</spacer>
</item> </item>
</layout> </layout>
</widget> </widget>
@@ -74,6 +77,12 @@ Rectangle {
<extends>QPlainTextEdit</extends> <extends>QPlainTextEdit</extends>
<header location="global">texteditor/snippets/snippeteditor.h</header> <header location="global">texteditor/snippets/snippeteditor.h</header>
</customwidget> </customwidget>
<customwidget>
<class>QmlJSTools::QmlJSCodeStylePreferencesWidget</class>
<extends>QWidget</extends>
<header location="global">qmljscodestylepreferenceswidget.h</header>
<container>1</container>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>

View File

@@ -0,0 +1,70 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "qmljscodestylesettingswidget.h"
#include "ui_qmljscodestylesettingswidget.h"
#include "qmljscodestylesettings.h"
#include <QTextStream>
namespace QmlJSTools {
QmlJSCodeStyleSettingsWidget::QmlJSCodeStyleSettingsWidget(QWidget *parent) :
QGroupBox(parent),
ui(new Internal::Ui::QmlJSCodeStyleSettingsWidget)
{
ui->setupUi(this);
auto spinValueChanged = QOverload<int>::of(&QSpinBox::valueChanged);
connect(ui->lineLengthSpinBox, spinValueChanged,
this, &QmlJSCodeStyleSettingsWidget::slotSettingsChanged);
}
QmlJSCodeStyleSettingsWidget::~QmlJSCodeStyleSettingsWidget()
{
delete ui;
}
void QmlJSCodeStyleSettingsWidget::setCodeStyleSettings(const QmlJSCodeStyleSettings& s)
{
QSignalBlocker blocker(this);
ui->lineLengthSpinBox->setValue(s.lineLength);
}
QmlJSCodeStyleSettings QmlJSCodeStyleSettingsWidget::codeStyleSettings() const
{
QmlJSCodeStyleSettings set;
set.lineLength = ui->lineLengthSpinBox->value();
return set;
}
void QmlJSCodeStyleSettingsWidget::slotSettingsChanged()
{
emit settingsChanged(codeStyleSettings());
}
} // namespace TextEditor

View File

@@ -0,0 +1,66 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "qmljstools_global.h"
#include <QGroupBox>
namespace QmlJSTools {
class QmlJSCodeStyleSettings;
namespace Internal { namespace Ui { class QmlJSCodeStyleSettingsWidget; } }
class QMLJSTOOLS_EXPORT QmlJSCodeStyleSettingsWidget : public QGroupBox
{
Q_OBJECT
public:
enum CodingStyleLink {
CppLink,
QtQuickLink
};
explicit QmlJSCodeStyleSettingsWidget(QWidget *parent = nullptr);
~QmlJSCodeStyleSettingsWidget() override;
QmlJSCodeStyleSettings codeStyleSettings() const;
void setCodingStyleWarningVisible(bool visible);
void setCodeStyleSettings(const QmlJSCodeStyleSettings& s);
signals:
void settingsChanged(const QmlJSCodeStyleSettings &);
private:
void slotSettingsChanged();
void codingStyleLinkActivated(const QString &linkString);
Internal::Ui::QmlJSCodeStyleSettingsWidget *ui;
};
} // namespace QmlJSTools

View File

@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QmlJSTools::Internal::QmlJSCodeStyleSettingsWidget</class>
<widget class="QGroupBox" name="QmlJSTools::Internal::QmlJSCodeStyleSettingsWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>254</width>
<height>203</height>
</rect>
</property>
<property name="windowTitle">
<string notr="true"/>
</property>
<property name="title">
<string>Qml JS Code Style</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="lineLengthLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&amp;Indent size:</string>
</property>
<property name="buddy">
<cstring>lineLengthSpinBox</cstring>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="lineLengthSpinBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>999</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>lineLengthSpinBox</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>

View File

@@ -25,10 +25,10 @@
#include "qmljstoolssettings.h" #include "qmljstoolssettings.h"
#include "qmljstoolsconstants.h" #include "qmljstoolsconstants.h"
#include "qmljscodestylepreferences.h"
#include "qmljscodestylepreferencesfactory.h" #include "qmljscodestylepreferencesfactory.h"
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
#include <texteditor/simplecodestylepreferences.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <texteditor/codestylepool.h> #include <texteditor/codestylepool.h>
@@ -44,7 +44,7 @@ namespace QmlJSTools {
const char idKey[] = "QmlJSGlobal"; const char idKey[] = "QmlJSGlobal";
static SimpleCodeStylePreferences *m_globalCodeStyle = nullptr; static QmlJSCodeStylePreferences *m_globalCodeStyle = nullptr;
QmlJSToolsSettings::QmlJSToolsSettings() QmlJSToolsSettings::QmlJSToolsSettings()
{ {
@@ -59,7 +59,7 @@ QmlJSToolsSettings::QmlJSToolsSettings()
TextEditorSettings::registerCodeStylePool(Constants::QML_JS_SETTINGS_ID, pool); TextEditorSettings::registerCodeStylePool(Constants::QML_JS_SETTINGS_ID, pool);
// global code style settings // global code style settings
m_globalCodeStyle = new SimpleCodeStylePreferences(this); m_globalCodeStyle = new QmlJSCodeStylePreferences(this);
m_globalCodeStyle->setDelegatingPool(pool); m_globalCodeStyle->setDelegatingPool(pool);
m_globalCodeStyle->setDisplayName(tr("Global", "Settings")); m_globalCodeStyle->setDisplayName(tr("Global", "Settings"));
m_globalCodeStyle->setId(idKey); m_globalCodeStyle->setId(idKey);
@@ -68,7 +68,7 @@ QmlJSToolsSettings::QmlJSToolsSettings()
// built-in settings // built-in settings
// Qt style // Qt style
auto qtCodeStyle = new SimpleCodeStylePreferences; auto qtCodeStyle = new QmlJSCodeStylePreferences;
qtCodeStyle->setId("qt"); qtCodeStyle->setId("qt");
qtCodeStyle->setDisplayName(tr("Qt")); qtCodeStyle->setDisplayName(tr("Qt"));
qtCodeStyle->setReadOnly(true); qtCodeStyle->setReadOnly(true);
@@ -78,6 +78,9 @@ QmlJSToolsSettings::QmlJSToolsSettings()
qtTabSettings.m_indentSize = 4; qtTabSettings.m_indentSize = 4;
qtTabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent; qtTabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent;
qtCodeStyle->setTabSettings(qtTabSettings); qtCodeStyle->setTabSettings(qtTabSettings);
QmlJSCodeStyleSettings qtQmlJSSetings;
qtQmlJSSetings.lineLength = 80;
qtCodeStyle->setCodeStyleSettings(qtQmlJSSetings);
pool->addCodeStyle(qtCodeStyle); pool->addCodeStyle(qtCodeStyle);
// default delegate for global preferences // default delegate for global preferences
@@ -148,7 +151,7 @@ QmlJSToolsSettings::~QmlJSToolsSettings()
m_globalCodeStyle = nullptr; m_globalCodeStyle = nullptr;
} }
SimpleCodeStylePreferences *QmlJSToolsSettings::globalCodeStyle() QmlJSCodeStylePreferences *QmlJSToolsSettings::globalCodeStyle()
{ {
return m_globalCodeStyle; return m_globalCodeStyle;
} }

View File

@@ -29,9 +29,8 @@
#include <QObject> #include <QObject>
namespace TextEditor { class SimpleCodeStylePreferences; }
namespace QmlJSTools { namespace QmlJSTools {
class QmlJSCodeStylePreferences;
/** /**
* This class provides a central place for cpp tools settings. * This class provides a central place for cpp tools settings.
@@ -44,7 +43,7 @@ public:
explicit QmlJSToolsSettings(); explicit QmlJSToolsSettings();
~QmlJSToolsSettings() override; ~QmlJSToolsSettings() override;
static TextEditor::SimpleCodeStylePreferences *globalCodeStyle(); static QmlJSCodeStylePreferences *globalCodeStyle();
}; };
} // namespace QmlJSTools } // namespace QmlJSTools

View File

@@ -14,7 +14,7 @@
<string/> <string/>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<property name="margin"> <property name="margin" stdset="0">
<number>0</number> <number>0</number>
</property> </property>
<item row="0" column="0"> <item row="0" column="0">