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:
@@ -101,6 +101,11 @@ void BaseTextDocument::setSupportsUtf8Bom(bool value)
|
||||
d->m_supportsUtf8Bom = value;
|
||||
}
|
||||
|
||||
void BaseTextDocument::setLineTerminationMode(Utils::TextFileFormat::LineTerminationMode mode)
|
||||
{
|
||||
d->m_format.lineTerminationMode = mode;
|
||||
}
|
||||
|
||||
/*!
|
||||
Autodetects format and reads in the text file specified by \a fileName.
|
||||
*/
|
||||
@@ -149,6 +154,11 @@ bool BaseTextDocument::supportsUtf8Bom() const
|
||||
return d->m_supportsUtf8Bom;
|
||||
}
|
||||
|
||||
Utils::TextFileFormat::LineTerminationMode BaseTextDocument::lineTerminationMode() const
|
||||
{
|
||||
return d->m_format.lineTerminationMode;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the format obtained from the last call to \c read().
|
||||
*/
|
||||
|
||||
@@ -48,6 +48,7 @@ public:
|
||||
void setCodec(const QTextCodec *);
|
||||
void switchUtf8Bom();
|
||||
bool supportsUtf8Bom() const;
|
||||
Utils::TextFileFormat::LineTerminationMode lineTerminationMode() const;
|
||||
|
||||
ReadResult read(const QString &fileName, QStringList *plainTextList, QString *errorString);
|
||||
ReadResult read(const QString &fileName, QString *plainText, QString *errorString);
|
||||
@@ -59,6 +60,7 @@ public:
|
||||
bool write(const QString &fileName, const Utils::TextFileFormat &format, const QString &data, QString *errorMessage) const;
|
||||
|
||||
void setSupportsUtf8Bom(bool value);
|
||||
void setLineTerminationMode(Utils::TextFileFormat::LineTerminationMode mode);
|
||||
|
||||
private:
|
||||
Internal::TextDocumentPrivate *d;
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "extraencodingsettings.h"
|
||||
#include "behaviorsettingswidget.h"
|
||||
|
||||
#include <utils/settingsutils.h>
|
||||
|
||||
@@ -71,3 +72,9 @@ bool ExtraEncodingSettings::equals(const ExtraEncodingSettings &s) const
|
||||
{
|
||||
return m_utf8BomSetting == s.m_utf8BomSetting;
|
||||
}
|
||||
|
||||
QStringList ExtraEncodingSettings::lineTerminationModeNames()
|
||||
{
|
||||
return {BehaviorSettingsWidget::tr("Unix (LF)"),
|
||||
BehaviorSettingsWidget::tr("Windows (CRLF)")};
|
||||
}
|
||||
|
||||
@@ -49,6 +49,8 @@ public:
|
||||
|
||||
bool equals(const ExtraEncodingSettings &s) const;
|
||||
|
||||
static QStringList lineTerminationModeNames();
|
||||
|
||||
enum Utf8BomSetting {
|
||||
AlwaysAdd = 0,
|
||||
OnlyKeep = 1,
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -381,6 +381,8 @@ public:
|
||||
void unfold();
|
||||
void selectEncoding();
|
||||
void updateTextCodecLabel();
|
||||
void selectLineEnding(int index);
|
||||
void updateTextLineEndingLabel();
|
||||
|
||||
void gotoBlockStart();
|
||||
void gotoBlockEnd();
|
||||
|
||||
Reference in New Issue
Block a user