forked from qt-creator/qt-creator
Add option to configure file line ending for an open document
Add into the text editor toolbar a combo box which allows to change the line endings for an open document. The supported line endings are LF and CRLF. Change-Id: I029ba7867e0087b162edba7aba1bd1d1e966fd69 Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -90,6 +90,7 @@
|
||||
#include <QApplication>
|
||||
#include <QClipboard>
|
||||
#include <QCoreApplication>
|
||||
#include <QComboBox>
|
||||
#include <QDebug>
|
||||
#include <QFutureWatcher>
|
||||
#include <QGridLayout>
|
||||
@@ -624,6 +625,9 @@ public:
|
||||
FixedSizeClickLabel *m_fileEncodingLabel = nullptr;
|
||||
QAction *m_fileEncodingLabelAction = nullptr;
|
||||
|
||||
QComboBox *m_fileLineEnding = nullptr;
|
||||
QAction *m_fileLineEndingAction = nullptr;
|
||||
|
||||
bool m_contentsChanged = false;
|
||||
bool m_lastCursorChangeWasInteresting = false;
|
||||
|
||||
@@ -825,6 +829,11 @@ TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent)
|
||||
m_cursorPositionLabel->setContentsMargins(spacing, 0, spacing, 0);
|
||||
m_toolBarWidget->layout()->addWidget(m_cursorPositionLabel);
|
||||
|
||||
m_fileLineEnding = new QComboBox();
|
||||
m_fileLineEnding->addItems(ExtraEncodingSettings::lineTerminationModeNames());
|
||||
m_fileLineEnding->setContentsMargins(spacing, 0, spacing, 0);
|
||||
m_fileLineEndingAction = m_toolBar->addWidget(m_fileLineEnding);
|
||||
|
||||
m_fileEncodingLabel = new FixedSizeClickLabel;
|
||||
m_fileEncodingLabel->setContentsMargins(spacing, 0, spacing, 0);
|
||||
m_fileEncodingLabelAction = m_toolBar->addWidget(m_fileEncodingLabel);
|
||||
@@ -1063,6 +1072,13 @@ void TextEditorWidgetPrivate::ctor(const QSharedPointer<TextDocument> &doc)
|
||||
connect(m_document->document(), &QTextDocument::modificationChanged,
|
||||
q, &TextEditorWidget::updateTextCodecLabel);
|
||||
q->updateTextCodecLabel();
|
||||
|
||||
connect(m_fileLineEnding, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
q, &TextEditorWidget::selectLineEnding);
|
||||
connect(m_document->document(), &QTextDocument::modificationChanged,
|
||||
q, &TextEditorWidget::updateTextLineEndingLabel);
|
||||
q->updateTextLineEndingLabel();
|
||||
|
||||
}
|
||||
|
||||
TextEditorWidget::~TextEditorWidget()
|
||||
@@ -1318,6 +1334,21 @@ void TextEditorWidget::selectEncoding()
|
||||
}
|
||||
}
|
||||
|
||||
void TextEditorWidget::selectLineEnding(int index)
|
||||
{
|
||||
QTC_CHECK(index >= 0);
|
||||
const auto newMode = Utils::TextFileFormat::LineTerminationMode(index);
|
||||
if (d->m_document->lineTerminationMode() != newMode) {
|
||||
d->m_document->setLineTerminationMode(newMode);
|
||||
d->q->document()->setModified(true);
|
||||
}
|
||||
}
|
||||
|
||||
void TextEditorWidget::updateTextLineEndingLabel()
|
||||
{
|
||||
d->m_fileLineEnding->setCurrentIndex(d->m_document->lineTerminationMode());
|
||||
}
|
||||
|
||||
void TextEditorWidget::updateTextCodecLabel()
|
||||
{
|
||||
QString text = QString::fromLatin1(d->m_document->codec()->name());
|
||||
|
||||
Reference in New Issue
Block a user