From 845630ad239c4b37ff37e49ef5bb969a8946744b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 10 May 2023 13:50:56 +0200 Subject: Move the shiboken-generator source around THIS COMMIT WAS GENERATED BY A SCRIPT Task-number: PYSIDE-962 Task-number: PYSIDE-1587 Change-Id: I58b05c3d05606efb6303193f2d7f907a0ab5741b Reviewed-by: Friedemann Kleint --- .../ApiExtractor/codesniphelpers.cpp | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 sources/shiboken6_generator/ApiExtractor/codesniphelpers.cpp (limited to 'sources/shiboken6_generator/ApiExtractor/codesniphelpers.cpp') diff --git a/sources/shiboken6_generator/ApiExtractor/codesniphelpers.cpp b/sources/shiboken6_generator/ApiExtractor/codesniphelpers.cpp new file mode 100644 index 000000000..9d16cb44f --- /dev/null +++ b/sources/shiboken6_generator/ApiExtractor/codesniphelpers.cpp @@ -0,0 +1,77 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include "codesniphelpers.h" + +#include + +#include + +static inline int firstNonBlank(QStringView s) +{ + const auto it = std::find_if(s.cbegin(), s.cend(), + [] (QChar c) { return !c.isSpace(); }); + return int(it - s.cbegin()); +} + +static inline bool isEmpty(QStringView s) +{ + return s.isEmpty() + || std::all_of(s.cbegin(), s.cend(), + [] (QChar c) { return c.isSpace(); }); +} + +QString CodeSnipHelpers::dedent(const QString &code) +{ + if (code.isEmpty()) + return code; + // Right trim if indent=0, or trim if single line + if (!code.at(0).isSpace() || !code.contains(u'\n')) + return code.trimmed(); + const auto lines = QStringView{code}.split(u'\n'); + int spacesToRemove = std::numeric_limits::max(); + for (const auto &line : lines) { + if (!isEmpty(line)) { + const int nonSpacePos = firstNonBlank(line); + if (nonSpacePos < spacesToRemove) + spacesToRemove = nonSpacePos; + if (spacesToRemove == 0) + return code; + } + } + QString result; + for (const auto &line : lines) { + if (!isEmpty(line) && spacesToRemove < line.size()) + result += line.mid(spacesToRemove).toString(); + result += u'\n'; + } + return result; +} + +QString CodeSnipHelpers::fixSpaces(QString code) +{ + code.remove(u'\r'); + // Check for XML \nbla... + if (code.startsWith(u"\n ")) + code.remove(0, 1); + while (!code.isEmpty() && code.back().isSpace()) + code.chop(1); + code = dedent(code); + if (!code.isEmpty() && !code.endsWith(u'\n')) + code.append(u'\n'); + return code; +} + +// Prepend a line to the code, observing indentation +void CodeSnipHelpers::prependCode(QString *code, QString firstLine) +{ + while (!code->isEmpty() && code->front() == u'\n') + code->remove(0, 1); + if (!code->isEmpty() && code->front().isSpace()) { + const int indent = firstNonBlank(*code); + firstLine.prepend(QString(indent, u' ')); + } + if (!firstLine.endsWith(u'\n')) + firstLine += u'\n'; + code->prepend(firstLine); +} -- cgit v1.2.3