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;
|
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.
|
Autodetects format and reads in the text file specified by \a fileName.
|
||||||
*/
|
*/
|
||||||
@@ -149,6 +154,11 @@ bool BaseTextDocument::supportsUtf8Bom() const
|
|||||||
return d->m_supportsUtf8Bom;
|
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().
|
Returns the format obtained from the last call to \c read().
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ public:
|
|||||||
void setCodec(const QTextCodec *);
|
void setCodec(const QTextCodec *);
|
||||||
void switchUtf8Bom();
|
void switchUtf8Bom();
|
||||||
bool supportsUtf8Bom() const;
|
bool supportsUtf8Bom() const;
|
||||||
|
Utils::TextFileFormat::LineTerminationMode lineTerminationMode() const;
|
||||||
|
|
||||||
ReadResult read(const QString &fileName, QStringList *plainTextList, QString *errorString);
|
ReadResult read(const QString &fileName, QStringList *plainTextList, QString *errorString);
|
||||||
ReadResult read(const QString &fileName, QString *plainText, 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;
|
bool write(const QString &fileName, const Utils::TextFileFormat &format, const QString &data, QString *errorMessage) const;
|
||||||
|
|
||||||
void setSupportsUtf8Bom(bool value);
|
void setSupportsUtf8Bom(bool value);
|
||||||
|
void setLineTerminationMode(Utils::TextFileFormat::LineTerminationMode mode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Internal::TextDocumentPrivate *d;
|
Internal::TextDocumentPrivate *d;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include "extraencodingsettings.h"
|
#include "extraencodingsettings.h"
|
||||||
|
#include "behaviorsettingswidget.h"
|
||||||
|
|
||||||
#include <utils/settingsutils.h>
|
#include <utils/settingsutils.h>
|
||||||
|
|
||||||
@@ -71,3 +72,9 @@ bool ExtraEncodingSettings::equals(const ExtraEncodingSettings &s) const
|
|||||||
{
|
{
|
||||||
return m_utf8BomSetting == s.m_utf8BomSetting;
|
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;
|
bool equals(const ExtraEncodingSettings &s) const;
|
||||||
|
|
||||||
|
static QStringList lineTerminationModeNames();
|
||||||
|
|
||||||
enum Utf8BomSetting {
|
enum Utf8BomSetting {
|
||||||
AlwaysAdd = 0,
|
AlwaysAdd = 0,
|
||||||
OnlyKeep = 1,
|
OnlyKeep = 1,
|
||||||
|
|||||||
@@ -90,6 +90,7 @@
|
|||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QClipboard>
|
#include <QClipboard>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <QComboBox>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QFutureWatcher>
|
#include <QFutureWatcher>
|
||||||
#include <QGridLayout>
|
#include <QGridLayout>
|
||||||
@@ -624,6 +625,9 @@ public:
|
|||||||
FixedSizeClickLabel *m_fileEncodingLabel = nullptr;
|
FixedSizeClickLabel *m_fileEncodingLabel = nullptr;
|
||||||
QAction *m_fileEncodingLabelAction = nullptr;
|
QAction *m_fileEncodingLabelAction = nullptr;
|
||||||
|
|
||||||
|
QComboBox *m_fileLineEnding = nullptr;
|
||||||
|
QAction *m_fileLineEndingAction = nullptr;
|
||||||
|
|
||||||
bool m_contentsChanged = false;
|
bool m_contentsChanged = false;
|
||||||
bool m_lastCursorChangeWasInteresting = false;
|
bool m_lastCursorChangeWasInteresting = false;
|
||||||
|
|
||||||
@@ -825,6 +829,11 @@ TextEditorWidgetPrivate::TextEditorWidgetPrivate(TextEditorWidget *parent)
|
|||||||
m_cursorPositionLabel->setContentsMargins(spacing, 0, spacing, 0);
|
m_cursorPositionLabel->setContentsMargins(spacing, 0, spacing, 0);
|
||||||
m_toolBarWidget->layout()->addWidget(m_cursorPositionLabel);
|
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 = new FixedSizeClickLabel;
|
||||||
m_fileEncodingLabel->setContentsMargins(spacing, 0, spacing, 0);
|
m_fileEncodingLabel->setContentsMargins(spacing, 0, spacing, 0);
|
||||||
m_fileEncodingLabelAction = m_toolBar->addWidget(m_fileEncodingLabel);
|
m_fileEncodingLabelAction = m_toolBar->addWidget(m_fileEncodingLabel);
|
||||||
@@ -1063,6 +1072,13 @@ void TextEditorWidgetPrivate::ctor(const QSharedPointer<TextDocument> &doc)
|
|||||||
connect(m_document->document(), &QTextDocument::modificationChanged,
|
connect(m_document->document(), &QTextDocument::modificationChanged,
|
||||||
q, &TextEditorWidget::updateTextCodecLabel);
|
q, &TextEditorWidget::updateTextCodecLabel);
|
||||||
q->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()
|
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()
|
void TextEditorWidget::updateTextCodecLabel()
|
||||||
{
|
{
|
||||||
QString text = QString::fromLatin1(d->m_document->codec()->name());
|
QString text = QString::fromLatin1(d->m_document->codec()->name());
|
||||||
|
|||||||
@@ -381,6 +381,8 @@ public:
|
|||||||
void unfold();
|
void unfold();
|
||||||
void selectEncoding();
|
void selectEncoding();
|
||||||
void updateTextCodecLabel();
|
void updateTextCodecLabel();
|
||||||
|
void selectLineEnding(int index);
|
||||||
|
void updateTextLineEndingLabel();
|
||||||
|
|
||||||
void gotoBlockStart();
|
void gotoBlockStart();
|
||||||
void gotoBlockEnd();
|
void gotoBlockEnd();
|
||||||
|
|||||||
Reference in New Issue
Block a user