forked from qt-creator/qt-creator
Texteditor: Add ability to get margin from indenter
And implement it in the clang-format plugin to use the ColumnLimit from the style. This would be quite useful if you're working on different projects with a different ColumnLimit. This way you get an visual representation and do not only have to rely on clang-format. Change-Id: Ib0258e3fba6f45f0f46ce612f806527a47868ad9 Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
committed by
Björn Schäpers
parent
069efa9483
commit
d9cd74f06d
@@ -679,6 +679,11 @@ bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<int> ClangFormat::ClangFormatBaseIndenter::margin() const
|
||||||
|
{
|
||||||
|
return styleForFile().ColumnLimit;
|
||||||
|
}
|
||||||
|
|
||||||
void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor,
|
void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor,
|
||||||
const TextEditor::TabSettings & /*tabSettings*/,
|
const TextEditor::TabSettings & /*tabSettings*/,
|
||||||
int cursorPositionInEditor)
|
int cursorPositionInEditor)
|
||||||
|
@@ -66,6 +66,8 @@ public:
|
|||||||
|
|
||||||
bool isElectricCharacter(const QChar &ch) const override;
|
bool isElectricCharacter(const QChar &ch) const override;
|
||||||
|
|
||||||
|
std::optional<int> margin() const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual clang::format::FormatStyle styleForFile() const;
|
virtual clang::format::FormatStyle styleForFile() const;
|
||||||
virtual bool formatCodeInsteadOfIndent() const { return false; }
|
virtual bool formatCodeInsteadOfIndent() const { return false; }
|
||||||
|
@@ -372,6 +372,14 @@ void EditorConfiguration::setShowWrapColumn(bool onoff)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EditorConfiguration::setUseIndenter(bool onoff)
|
||||||
|
{
|
||||||
|
if (d->m_marginSettings.m_useIndenter != onoff) {
|
||||||
|
d->m_marginSettings.m_useIndenter = onoff;
|
||||||
|
emit marginSettingsChanged(d->m_marginSettings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void EditorConfiguration::setWrapColumn(int column)
|
void EditorConfiguration::setWrapColumn(int column)
|
||||||
{
|
{
|
||||||
if (d->m_marginSettings.m_marginColumn != column) {
|
if (d->m_marginSettings.m_marginColumn != column) {
|
||||||
|
@@ -94,6 +94,7 @@ public:
|
|||||||
void setMarginSettings(const TextEditor::MarginSettings &settings);
|
void setMarginSettings(const TextEditor::MarginSettings &settings);
|
||||||
|
|
||||||
void setShowWrapColumn(bool onoff);
|
void setShowWrapColumn(bool onoff);
|
||||||
|
void setUseIndenter(bool onoff);
|
||||||
void setWrapColumn(int column);
|
void setWrapColumn(int column);
|
||||||
|
|
||||||
void setTextCodec(QTextCodec *textCodec);
|
void setTextCodec(QTextCodec *textCodec);
|
||||||
|
@@ -55,6 +55,8 @@ EditorSettingsWidget::EditorSettingsWidget(Project *project) : QWidget(), m_proj
|
|||||||
|
|
||||||
connect(m_ui.showWrapColumn, &QAbstractButton::toggled,
|
connect(m_ui.showWrapColumn, &QAbstractButton::toggled,
|
||||||
config, &EditorConfiguration::setShowWrapColumn);
|
config, &EditorConfiguration::setShowWrapColumn);
|
||||||
|
connect(m_ui.useIndenter, &QAbstractButton::toggled,
|
||||||
|
config, &EditorConfiguration::setUseIndenter);
|
||||||
connect(m_ui.wrapColumn, QOverload<int>::of(&QSpinBox::valueChanged),
|
connect(m_ui.wrapColumn, QOverload<int>::of(&QSpinBox::valueChanged),
|
||||||
config, &EditorConfiguration::setWrapColumn);
|
config, &EditorConfiguration::setWrapColumn);
|
||||||
|
|
||||||
@@ -73,6 +75,7 @@ EditorSettingsWidget::EditorSettingsWidget(Project *project) : QWidget(), m_proj
|
|||||||
void EditorSettingsWidget::settingsToUi(const EditorConfiguration *config)
|
void EditorSettingsWidget::settingsToUi(const EditorConfiguration *config)
|
||||||
{
|
{
|
||||||
m_ui.showWrapColumn->setChecked(config->marginSettings().m_showMargin);
|
m_ui.showWrapColumn->setChecked(config->marginSettings().m_showMargin);
|
||||||
|
m_ui.useIndenter->setChecked(config->marginSettings().m_useIndenter);
|
||||||
m_ui.wrapColumn->setValue(config->marginSettings().m_marginColumn);
|
m_ui.wrapColumn->setValue(config->marginSettings().m_marginColumn);
|
||||||
m_ui.behaviorSettingsWidget->setCodeStyle(config->codeStyle());
|
m_ui.behaviorSettingsWidget->setCodeStyle(config->codeStyle());
|
||||||
m_ui.globalSelector->setCurrentIndex(config->useGlobalSettings() ? 0 : 1);
|
m_ui.globalSelector->setCurrentIndex(config->useGlobalSettings() ? 0 : 1);
|
||||||
|
@@ -81,6 +81,16 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="useIndenter">
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>If available, use a different margin. For example, the ColumnLimit from the clang-format plugin.</string>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Use context specific margin</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<spacer name="horizontalSpacer_2">
|
<spacer name="horizontalSpacer_2">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
|
@@ -89,6 +89,7 @@ void DisplaySettingsWidget::settingsFromUI(DisplaySettings &displaySettings,
|
|||||||
displaySettings.m_displayLineNumbers = m_ui.displayLineNumbers->isChecked();
|
displaySettings.m_displayLineNumbers = m_ui.displayLineNumbers->isChecked();
|
||||||
displaySettings.m_textWrapping = m_ui.enableTextWrapping->isChecked();
|
displaySettings.m_textWrapping = m_ui.enableTextWrapping->isChecked();
|
||||||
marginSettings.m_showMargin = m_ui.showWrapColumn->isChecked();
|
marginSettings.m_showMargin = m_ui.showWrapColumn->isChecked();
|
||||||
|
marginSettings.m_useIndenter = m_ui.useIndenter->isChecked();
|
||||||
marginSettings.m_marginColumn = m_ui.wrapColumn->value();
|
marginSettings.m_marginColumn = m_ui.wrapColumn->value();
|
||||||
displaySettings.m_visualizeWhitespace = m_ui.visualizeWhitespace->isChecked();
|
displaySettings.m_visualizeWhitespace = m_ui.visualizeWhitespace->isChecked();
|
||||||
displaySettings.m_displayFoldingMarkers = m_ui.displayFoldingMarkers->isChecked();
|
displaySettings.m_displayFoldingMarkers = m_ui.displayFoldingMarkers->isChecked();
|
||||||
@@ -121,6 +122,7 @@ void DisplaySettingsWidget::settingsToUI()
|
|||||||
m_ui.displayLineNumbers->setChecked(displaySettings.m_displayLineNumbers);
|
m_ui.displayLineNumbers->setChecked(displaySettings.m_displayLineNumbers);
|
||||||
m_ui.enableTextWrapping->setChecked(displaySettings.m_textWrapping);
|
m_ui.enableTextWrapping->setChecked(displaySettings.m_textWrapping);
|
||||||
m_ui.showWrapColumn->setChecked(marginSettings.m_showMargin);
|
m_ui.showWrapColumn->setChecked(marginSettings.m_showMargin);
|
||||||
|
m_ui.useIndenter->setChecked(marginSettings.m_useIndenter);
|
||||||
m_ui.wrapColumn->setValue(marginSettings.m_marginColumn);
|
m_ui.wrapColumn->setValue(marginSettings.m_marginColumn);
|
||||||
m_ui.visualizeWhitespace->setChecked(displaySettings.m_visualizeWhitespace);
|
m_ui.visualizeWhitespace->setChecked(displaySettings.m_visualizeWhitespace);
|
||||||
m_ui.displayFoldingMarkers->setChecked(displaySettings.m_displayFoldingMarkers);
|
m_ui.displayFoldingMarkers->setChecked(displaySettings.m_displayFoldingMarkers);
|
||||||
|
@@ -56,6 +56,16 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="useIndenter">
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>If available, use a different margin. For example, the ColumnLimit from the clang-format plugin.</string>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Use context specific margin</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<spacer name="horizontalSpacer">
|
<spacer name="horizontalSpacer">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
|
@@ -120,6 +120,8 @@ public:
|
|||||||
int cursorPositionInEditor = -1)
|
int cursorPositionInEditor = -1)
|
||||||
= 0;
|
= 0;
|
||||||
|
|
||||||
|
virtual std::optional<int> margin() const { return std::nullopt; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QTextDocument *m_doc;
|
QTextDocument *m_doc;
|
||||||
Utils::FilePath m_fileName;
|
Utils::FilePath m_fileName;
|
||||||
|
@@ -32,11 +32,13 @@
|
|||||||
static const char showWrapColumnKey[] = "ShowMargin";
|
static const char showWrapColumnKey[] = "ShowMargin";
|
||||||
static const char wrapColumnKey[] = "MarginColumn";
|
static const char wrapColumnKey[] = "MarginColumn";
|
||||||
static const char groupPostfix[] = "MarginSettings";
|
static const char groupPostfix[] = "MarginSettings";
|
||||||
|
static const char useIndenterColumnKey[] = "UseIndenter";
|
||||||
|
|
||||||
using namespace TextEditor;
|
using namespace TextEditor;
|
||||||
|
|
||||||
MarginSettings::MarginSettings()
|
MarginSettings::MarginSettings()
|
||||||
: m_showMargin(false)
|
: m_showMargin(false)
|
||||||
|
, m_useIndenter(false)
|
||||||
, m_marginColumn(80)
|
, m_marginColumn(80)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -48,6 +50,7 @@ void MarginSettings::toSettings(const QString &category, QSettings *s) const
|
|||||||
group.insert(0, category);
|
group.insert(0, category);
|
||||||
s->beginGroup(group);
|
s->beginGroup(group);
|
||||||
s->setValue(QLatin1String(showWrapColumnKey), m_showMargin);
|
s->setValue(QLatin1String(showWrapColumnKey), m_showMargin);
|
||||||
|
s->setValue(QLatin1String(useIndenterColumnKey), m_useIndenter);
|
||||||
s->setValue(QLatin1String(wrapColumnKey), m_marginColumn);
|
s->setValue(QLatin1String(wrapColumnKey), m_marginColumn);
|
||||||
s->endGroup();
|
s->endGroup();
|
||||||
}
|
}
|
||||||
@@ -62,24 +65,28 @@ void MarginSettings::fromSettings(const QString &category, const QSettings *s)
|
|||||||
*this = MarginSettings(); // Assign defaults
|
*this = MarginSettings(); // Assign defaults
|
||||||
|
|
||||||
m_showMargin = s->value(group + QLatin1String(showWrapColumnKey), m_showMargin).toBool();
|
m_showMargin = s->value(group + QLatin1String(showWrapColumnKey), m_showMargin).toBool();
|
||||||
|
m_useIndenter = s->value(group + QLatin1String(useIndenterColumnKey), m_useIndenter).toBool();
|
||||||
m_marginColumn = s->value(group + QLatin1String(wrapColumnKey), m_marginColumn).toInt();
|
m_marginColumn = s->value(group + QLatin1String(wrapColumnKey), m_marginColumn).toInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MarginSettings::toMap(const QString &prefix, QVariantMap *map) const
|
void MarginSettings::toMap(const QString &prefix, QVariantMap *map) const
|
||||||
{
|
{
|
||||||
map->insert(prefix + QLatin1String(showWrapColumnKey), m_showMargin);
|
map->insert(prefix + QLatin1String(showWrapColumnKey), m_showMargin);
|
||||||
|
map->insert(prefix + QLatin1String(useIndenterColumnKey), m_useIndenter);
|
||||||
map->insert(prefix + QLatin1String(wrapColumnKey), m_marginColumn);
|
map->insert(prefix + QLatin1String(wrapColumnKey), m_marginColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MarginSettings::fromMap(const QString &prefix, const QVariantMap &map)
|
void MarginSettings::fromMap(const QString &prefix, const QVariantMap &map)
|
||||||
{
|
{
|
||||||
m_showMargin = map.value(prefix + QLatin1String(showWrapColumnKey), m_showMargin).toBool();
|
m_showMargin = map.value(prefix + QLatin1String(showWrapColumnKey), m_showMargin).toBool();
|
||||||
|
m_useIndenter = map.value(prefix + QLatin1String(useIndenterColumnKey), m_useIndenter).toBool();
|
||||||
m_marginColumn = map.value(prefix + QLatin1String(wrapColumnKey), m_marginColumn).toInt();
|
m_marginColumn = map.value(prefix + QLatin1String(wrapColumnKey), m_marginColumn).toInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MarginSettings::equals(const MarginSettings &other) const
|
bool MarginSettings::equals(const MarginSettings &other) const
|
||||||
{
|
{
|
||||||
return m_showMargin == other.m_showMargin
|
return m_showMargin == other.m_showMargin
|
||||||
|
&& m_useIndenter == other.m_useIndenter
|
||||||
&& m_marginColumn == other.m_marginColumn
|
&& m_marginColumn == other.m_marginColumn
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
@@ -49,6 +49,7 @@ public:
|
|||||||
bool equals(const MarginSettings &other) const;
|
bool equals(const MarginSettings &other) const;
|
||||||
|
|
||||||
bool m_showMargin;
|
bool m_showMargin;
|
||||||
|
bool m_useIndenter;
|
||||||
int m_marginColumn;
|
int m_marginColumn;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -1478,6 +1478,7 @@ void TextEditorWidget::openFinishedSuccessfully()
|
|||||||
moveCursor(QTextCursor::Start);
|
moveCursor(QTextCursor::Start);
|
||||||
d->updateCannotDecodeInfo();
|
d->updateCannotDecodeInfo();
|
||||||
updateTextCodecLabel();
|
updateTextCodecLabel();
|
||||||
|
updateVisualWrapColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
TextDocumentPtr TextEditorWidget::textDocumentPtr() const
|
TextDocumentPtr TextEditorWidget::textDocumentPtr() const
|
||||||
@@ -7438,8 +7439,8 @@ void TextEditorWidget::setDisplaySettings(const DisplaySettings &ds)
|
|||||||
|
|
||||||
void TextEditorWidget::setMarginSettings(const MarginSettings &ms)
|
void TextEditorWidget::setMarginSettings(const MarginSettings &ms)
|
||||||
{
|
{
|
||||||
setVisibleWrapColumn(ms.m_showMargin ? ms.m_marginColumn : 0);
|
|
||||||
d->m_marginSettings = ms;
|
d->m_marginSettings = ms;
|
||||||
|
updateVisualWrapColumn();
|
||||||
|
|
||||||
viewport()->update();
|
viewport()->update();
|
||||||
extraArea()->update();
|
extraArea()->update();
|
||||||
@@ -8261,6 +8262,24 @@ bool TextEditorWidget::inFindScope(int selectionStart, int selectionEnd)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextEditorWidget::updateVisualWrapColumn()
|
||||||
|
{
|
||||||
|
auto calcMargin = [this]() {
|
||||||
|
const auto &ms = d->m_marginSettings;
|
||||||
|
|
||||||
|
if (!ms.m_showMargin) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (ms.m_useIndenter) {
|
||||||
|
if (auto margin = d->m_document->indenter()->margin()) {
|
||||||
|
return *margin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ms.m_marginColumn;
|
||||||
|
};
|
||||||
|
setVisibleWrapColumn(calcMargin());
|
||||||
|
}
|
||||||
|
|
||||||
void TextEditorWidget::setBlockSelection(bool on)
|
void TextEditorWidget::setBlockSelection(bool on)
|
||||||
{
|
{
|
||||||
if (d->m_inBlockSelectionMode == on)
|
if (d->m_inBlockSelectionMode == on)
|
||||||
|
@@ -621,6 +621,8 @@ private:
|
|||||||
friend class Internal::TextEditorWidgetPrivate;
|
friend class Internal::TextEditorWidgetPrivate;
|
||||||
friend class Internal::TextEditorOverlay;
|
friend class Internal::TextEditorOverlay;
|
||||||
friend class RefactorOverlay;
|
friend class RefactorOverlay;
|
||||||
|
|
||||||
|
void updateVisualWrapColumn();
|
||||||
};
|
};
|
||||||
|
|
||||||
class TEXTEDITOR_EXPORT TextEditorLinkLabel : public Utils::ElidingLabel
|
class TEXTEDITOR_EXPORT TextEditorLinkLabel : public Utils::ElidingLabel
|
||||||
|
Reference in New Issue
Block a user