diff options
| author | Sami Shalayel <sami.shalayel@qt.io> | 2024-09-13 19:18:28 +0200 |
|---|---|---|
| committer | Sami Shalayel <sami.shalayel@qt.io> | 2024-09-20 18:55:30 +0200 |
| commit | 09236b1101574c1a009d3cee0c979286f497e466 (patch) | |
| tree | 899f01e3445a700a70adfce623c0de7024851079 /src/qmlformat/qqmlformatoptions.cpp | |
| parent | 511f1f0fe508d28cebf8ec4fe8e026dac752f602 (diff) | |
qmlformat: extract settings logic into library
Move the code from tools/qmlformat.cpp that parses .qmlformat.ini files
into a newly created qmlformat library under src/qmlformat.
This would allow qmlls to reuse the same code.
Task-number: QTBUG-128866
Change-Id: Ibf7e52be744ea11c235d0b853ffa80f778c14a2b
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qmlformat/qqmlformatoptions.cpp')
| -rw-r--r-- | src/qmlformat/qqmlformatoptions.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/qmlformat/qqmlformatoptions.cpp b/src/qmlformat/qqmlformatoptions.cpp new file mode 100644 index 0000000000..14ea69d818 --- /dev/null +++ b/src/qmlformat/qqmlformatoptions.cpp @@ -0,0 +1,87 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qqmlformatoptions_p.h" + +QQmlFormatOptions::QQmlFormatOptions() +{ + m_options.updateOptions = QQmlJS::Dom::LineWriterOptions::Update::None; + + setTabsEnabled(false); + setNormalizeEnabled(false); + setObjectsSpacing(false); + setFunctionsSpacing(false); + setIndentWidth(4); +} + +QQmlFormatOptions::LineEndings QQmlFormatOptions::detectLineEndings(const QString &code) +{ + const QQmlJS::Dom::LineWriterOptions::LineEndings defaultEndings = +#if defined(Q_OS_WIN) + LineEndings::Windows; +#else + LineEndings::Unix; +#endif + // find out current line endings... + int newlineIndex = code.indexOf(QChar(u'\n')); + int crIndex = code.indexOf(QChar(u'\r')); + if (newlineIndex >= 0) { + if (crIndex >= 0) { + if (crIndex + 1 == newlineIndex) + return LineEndings::Windows; + + qWarning().noquote() << "Invalid line ending in file, using default"; + return defaultEndings; + } + return LineEndings::Unix; + } + if (crIndex >= 0) { + return LineEndings::OldMacOs; + } + + qWarning().noquote() << "Unknown line ending in file, using default"; + return defaultEndings; +} + +QQmlFormatOptionLineEndings QQmlFormatOptions::parseEndings(const QString &endings) +{ + if (endings == u"unix") + return Unix; + if (endings == u"windows") + return Windows; + if (endings == u"macos") + return OldMacOs; + if (endings == u"native") + return Native; + + qWarning().noquote() << "Unknown line ending type" << endings << ", using default"; +#if defined(Q_OS_WIN) + return Windows; +#else + return Unix; +#endif +} + +void QQmlFormatOptions::applySettings(const QQmlFormatSettings &settings) +{ + // Allow for tab settings to be overwritten by the command line + if (!indentWidthSet()) { + if (settings.isSet(QQmlFormatSettings::s_indentWidthSetting)) + setIndentWidth(settings.value(QQmlFormatSettings::s_indentWidthSetting).toInt()); + if (settings.isSet(QQmlFormatSettings::s_useTabsSetting)) + setTabsEnabled(settings.value(QQmlFormatSettings::s_useTabsSetting).toBool()); + } + + if (settings.isSet(QQmlFormatSettings::s_normalizeSetting)) + setNormalizeEnabled(settings.value(QQmlFormatSettings::s_normalizeSetting).toBool()); + + if (settings.isSet(QQmlFormatSettings::s_newlineSetting)) + setNewline(QQmlFormatOptions::parseEndings( + settings.value(QQmlFormatSettings::s_newlineSetting).toString())); + + if (settings.isSet(QQmlFormatSettings::s_objectsSpacingSetting)) + setObjectsSpacing(settings.value(QQmlFormatSettings::s_objectsSpacingSetting).toBool()); + + if (settings.isSet(QQmlFormatSettings::s_functionsSpacingSetting)) + setFunctionsSpacing(settings.value(QQmlFormatSettings::s_functionsSpacingSetting).toBool()); +} |
