aboutsummaryrefslogtreecommitdiffstats
path: root/sources
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2020-11-25 13:32:49 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2020-11-26 06:20:31 +0000
commit44b03e3ded688aabe8352e8bda66df8677acc8a4 (patch)
tree1be3180596e386c93d49682db0b39b5dc8e07680 /sources
parent9af1a02b440dce0afb97298aef3ad337f46a7e62 (diff)
shiboken6/TextStream: Fix output of empty aligned fields
Change-Id: I1cc8c2b174dd7428def2a2b4ac7f84f5b6aa1df3 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources')
-rw-r--r--sources/shiboken6/ApiExtractor/tests/testcodeinjection.cpp4
-rw-r--r--sources/shiboken6/ApiExtractor/textstream.cpp8
-rw-r--r--sources/shiboken6/ApiExtractor/textstream.h5
3 files changed, 16 insertions, 1 deletions
diff --git a/sources/shiboken6/ApiExtractor/tests/testcodeinjection.cpp b/sources/shiboken6/ApiExtractor/tests/testcodeinjection.cpp
index 9e8909a66..d5c8971cf 100644
--- a/sources/shiboken6/ApiExtractor/tests/testcodeinjection.cpp
+++ b/sources/shiboken6/ApiExtractor/tests/testcodeinjection.cpp
@@ -139,7 +139,8 @@ void TestCodeInjections::testTextStream()
}
str << "}\n\n// A table\n|"
<< AlignedField("bla", 40, QTextStream::AlignRight) << "|\n|"
- << AlignedField("bla", 40, QTextStream::AlignLeft) << "|\n";
+ << AlignedField("bla", 40, QTextStream::AlignLeft) << "|\n|"
+ << AlignedField(QString(), 40, QTextStream::AlignLeft) << "|\n";
static const char expected[] = R"(void foo(int a, int b) {
if (a == b)
@@ -153,6 +154,7 @@ static const char expected[] = R"(void foo(int a, int b) {
// A table
| bla|
|bla |
+| |
)";
QCOMPARE(str.toString(), QLatin1String(expected));
diff --git a/sources/shiboken6/ApiExtractor/textstream.cpp b/sources/shiboken6/ApiExtractor/textstream.cpp
index 74b43b6da..55dc89290 100644
--- a/sources/shiboken6/ApiExtractor/textstream.cpp
+++ b/sources/shiboken6/ApiExtractor/textstream.cpp
@@ -65,6 +65,14 @@ void TextStream::outdent(int n)
Q_ASSERT(m_indentation >= 0);
}
+qint64 TextStream::pos() const
+{
+ // QTextStream::pos() only works for QIODevice, be a bit smarter
+ if (auto s = m_str.string())
+ return s->size();
+ return m_str.pos();
+}
+
void TextStream::putRepetitiveChars(char c, int count)
{
if (count > 0) {
diff --git a/sources/shiboken6/ApiExtractor/textstream.h b/sources/shiboken6/ApiExtractor/textstream.h
index 18be47ba5..991965094 100644
--- a/sources/shiboken6/ApiExtractor/textstream.h
+++ b/sources/shiboken6/ApiExtractor/textstream.h
@@ -74,6 +74,7 @@ public:
void outdent(int n = 1);
// QTextStream API
+ qint64 pos() const;
QTextStream::FieldAlignment fieldAlignment() const
{ return m_str.fieldAlignment(); }
void setFieldAlignment(QTextStream::FieldAlignment al)
@@ -166,7 +167,11 @@ public:
const auto oldFieldAlignment = s.fieldAlignment();
s.setFieldWidth(m_fieldWidth);
s.setFieldAlignment(m_alignment);
+ const auto oldPos = s.pos();
s << m_value;
+ // Ensure something is written when an empty string is encountered
+ if (oldPos == s.pos())
+ s << ' ';
s.setFieldAlignment(oldFieldAlignment);
s.setFieldWidth(oldFieldWidth);
}