Skip to content

Commit

Permalink
Assume utf8 for nvim strings, remove QTextCodec
Browse files Browse the repository at this point in the history
In the (very) early days of neovim, it was possible to have non utf8
strings, but this was removed so we don't need to support it.
  • Loading branch information
equalsraf authored and equalsraf committed Dec 12, 2022
1 parent 24883e7 commit cf83a00
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 72 deletions.
45 changes: 4 additions & 41 deletions src/msgpackiodevice.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <QAbstractSocket>
#include <QTextCodec>
#include <QSocketNotifier>

// read/write
Expand Down Expand Up @@ -45,7 +44,7 @@ MsgpackIODevice* MsgpackIODevice::fromStdinOut(QObject *parent)
}

MsgpackIODevice::MsgpackIODevice(QIODevice *dev, QObject *parent)
:QObject(parent), m_reqid(0), m_dev(dev), m_encoding(0), m_reqHandler(0), m_error(NoError)
:QObject(parent), m_reqid(0), m_dev(dev), m_reqHandler(0), m_error(NoError)
{
qRegisterMetaType<MsgpackError>("MsgpackError");
msgpack_unpacker_init(&m_uk, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
Expand Down Expand Up @@ -82,31 +81,6 @@ bool MsgpackIODevice::isOpen()
}
}

/** The encoding used by the MsgpackIODevice::encode and MsgpackIODevice::decode methods @see setEncoding */
QByteArray MsgpackIODevice::encoding() const
{
if (m_encoding) {
return m_encoding->name();
}
return QByteArray();
}

/**
* Set encoding for strings in this RPC, if it not set we
* fall back to utf8
*
* \see QTextCode
*/
bool MsgpackIODevice::setEncoding(const QByteArray& name)
{
m_encoding = QTextCodec::codecForName(name);
if (!m_encoding) {
setError(UnsupportedEncoding, QString("Unsupported encoding (%1)").arg(QString::fromLatin1(name)));
return false;
}
return true;
}

int MsgpackIODevice::msgpack_write_to_stdout(void* data, const char* buf, unsigned long int len)
{
MsgpackIODevice *c = static_cast<MsgpackIODevice*>(data);
Expand Down Expand Up @@ -854,30 +828,19 @@ bool MsgpackIODevice::decodeMsgpack(const msgpack_object& in, QPoint& out)
}

/**
* Convert string to the proper encoding
* \see MsgpackIODevice::setEncoding
* Convert string to the proper encoding to send to neovim (utf8)
*/
QByteArray MsgpackIODevice::encode(const QString& str)
{
if (m_encoding) {
return m_encoding->fromUnicode(str);
} else {
qWarning() << "Encoding String into MsgpackIODevice without an encoding (defaulting to utf8)";
return str.toUtf8();
}
return str.toUtf8();
}

/**
* Decode byte array as string, from Neovim's encoding
*/
QString MsgpackIODevice::decode(const QByteArray& data)
{
if (m_encoding) {
return m_encoding->toUnicode(data);
} else {
qWarning() << "Decoding String from MsgpackIODevice without an encoding (defaulting to utf8)";
return QString::fromUtf8(data);
}
return QString::fromUtf8(data);
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/msgpackiodevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <QIODevice>
#include <QHash>
#include <QTextCodec>
#include <QVariant>
#include <msgpack.h>

Expand All @@ -15,7 +14,6 @@ class MsgpackIODevice: public QObject
{
Q_OBJECT
Q_PROPERTY(MsgpackError error READ errorCause NOTIFY error)
Q_PROPERTY(QByteArray encoding READ encoding WRITE setEncoding NOTIFY encodingChanged)
public:
enum MsgpackError {
NoError=0,
Expand All @@ -34,7 +32,6 @@ class MsgpackIODevice: public QObject
MsgpackError errorCause() const {return m_error;};

QByteArray encoding() const;
bool setEncoding(const QByteArray&);

quint32 msgId();
MsgpackRequest* startRequestUnchecked(const QString& method, quint32 argcount);
Expand Down Expand Up @@ -106,7 +103,6 @@ protected slots:

quint32 m_reqid;
QIODevice *m_dev;
QTextCodec *m_encoding;
msgpack_packer m_pk;
msgpack_unpacker m_uk;
QHash<quint32, MsgpackRequest*> m_requests;
Expand Down
1 change: 0 additions & 1 deletion src/neovimconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <QObject>
#include <QAbstractSocket>
#include <QProcess>
#include <QTextCodec>
#include "msgpackiodevice.h"
#include "auto/neovimapi0.h"
#include "auto/neovimapi1.h"
Expand Down
10 changes: 2 additions & 8 deletions src/neovimconnectorhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,8 @@ void NeovimConnectorHelper::handleMetadata(quint32 msgid, quint64, const QVarian
m_c->m_api_supported = api_level;

if (m_c->errorCause() == NeovimConnector::NoError) {
// Neovim is always utf8, but this was not the case in the early days of nvim
// these days it should always be utf8
if (m_c->m_dev->setEncoding("utf8")) {
m_c->m_ready = true;
emit m_c->ready();
} else {
qWarning() << "Unable to set encoding to utf8";
}
m_c->m_ready = true;
emit m_c->ready();
} else {
qWarning() << "Error retrieving metadata" << m_c->errorString();
}
Expand Down
18 changes: 0 additions & 18 deletions test/tst_msgpackiodevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,9 @@ private slots:
}

void defaultValues() {
QVERIFY(one->encoding().isEmpty());
QCOMPARE(one->errorCause(), MsgpackIODevice::NoError);
}

void setEncoding() {
QCOMPARE(one->setEncoding("utf8"), true);
QCOMPARE(one->errorCause(), MsgpackIODevice::NoError);
QVERIFY(!one->encoding().isEmpty());
}

void setInvalidEncoding() {
QSignalSpy onError(one, SIGNAL(error(MsgpackError)));
QVERIFY(onError.isValid());

// Ignore qWarn
QCOMPARE(one->setEncoding("invalid-encoding"), false);

QVERIFY(SPYWAIT(onError));
QCOMPARE(one->errorCause(), MsgpackIODevice::UnsupportedEncoding);
}

/**
* These errors are not fatal but increase coverage
*/
Expand Down

0 comments on commit cf83a00

Please sign in to comment.