Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlformatoptions_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQMLFORMATOPTIONS_P_H
5#define QQMLFORMATOPTIONS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qstring.h>
19#include <QtQmlDom/private/qqmldomoutwriter_p.h>
20#include <QtQmlDom/private/qqmldomlinewriter_p.h>
21#include <QtQmlToolingSettings/private/qqmltoolingsettings_p.h>
22
24#include <bitset>
25
26QT_BEGIN_NAMESPACE
27
28enum QQmlFormatOptionLineEndings {
29 Native,
30 Windows,
31 Unix,
32 OldMacOs,
33};
34
36{
37public:
39
42
43 static LineEndings detectLineEndings(const QString &code);
44
45 static LineEndings lineEndings(QQmlFormatOptionLineEndings endings, const QString &code)
46 {
47 switch (endings) {
48 case Native:
49 return detectLineEndings(code);
50 case OldMacOs:
51 return LineEndings::OldMacOs;
52 case Windows:
53 return LineEndings::Windows;
54 case Unix:
55 return LineEndings::Unix;
56 }
57 Q_UNREACHABLE_RETURN(LineEndings::Unix);
58 }
59
60 bool tabsEnabled() const { return m_options.formatOptions.useTabs; }
61 void setTabsEnabled(bool tabs) { m_options.formatOptions.useTabs = tabs; }
62 bool normalizeEnabled() const
63 {
64 return m_options.attributesSequence == AttributesSequence::Normalize;
65 }
66 void setNormalizeEnabled(bool normalize)
67 {
68 m_options.attributesSequence =
69 (normalize ? AttributesSequence::Normalize : AttributesSequence::Preserve);
70 }
71 bool objectsSpacing() const { return m_options.objectsSpacing; }
72 void setObjectsSpacing(bool spacing) { m_options.objectsSpacing = spacing; }
73 bool functionsSpacing() const { return m_options.functionsSpacing; }
74 void setFunctionsSpacing(bool spacing) { m_options.functionsSpacing = spacing; }
75
76 bool sortImports() const { return m_options.sortImports; }
77 void setSortImports(bool sort) { m_options.sortImports = sort; }
78
79 int indentWidth() const { return m_options.formatOptions.indentSize; }
80 void setIndentWidth(int width) { m_options.formatOptions.indentSize = width; }
81
82 int maxColumnWidth() const { return m_options.maxLineLength; }
83 void setMaxColumnWidth(int width) { m_options.maxLineLength = width; }
84 bool isMaxColumnWidthSet() const { return m_options.maxLineLength > 0; }
85
86 QQmlJS::Dom::LineWriterOptions optionsForCode(const QString &code) const
87 {
88 QQmlJS::Dom::LineWriterOptions result = m_options;
89 result.lineEndings = lineEndings(m_newline, code);
90 return result;
91 }
92
93 static QQmlFormatOptionLineEndings parseEndings(const QString &endings);
94
95 QQmlFormatOptionLineEndings newline() const { return m_newline; }
96 void setNewline(const QQmlFormatOptionLineEndings &endings) { m_newline = endings; }
97
98 QStringList files() const { return m_files; }
99 void setFiles(const QStringList &newFiles) { m_files = newFiles; }
100 QStringList arguments() const { return m_arguments; }
101 void setArguments(const QStringList &newArguments) { m_arguments = newArguments; }
102 bool isVerbose() const { return m_verbose; }
103 void setIsVerbose(bool newVerbose) { m_verbose = newVerbose; }
104 bool isValid() const { return m_errors.isEmpty(); }
105 bool isInplace() const { return m_inplace; }
106 void setIsInplace(bool newInplace) { m_inplace = newInplace; }
107 bool forceEnabled() const { return m_force; }
108 void setForceEnabled(bool newForce) { m_force = newForce; }
109 bool ignoreSettingsEnabled() const { return m_ignoreSettings; }
110 void setIgnoreSettingsEnabled(bool newIgnoreSettings) { m_ignoreSettings = newIgnoreSettings; }
111 bool writeDefaultSettingsEnabled() const { return m_writeDefaultSettings; }
112 void setWriteDefaultSettingsEnabled(bool newWriteDefaultSettings)
113 {
114 m_writeDefaultSettings = newWriteDefaultSettings;
115 }
116
117 bool indentWidthSet() const { return m_indentWidthSet; }
118 void setIndentWidthSet(bool newIndentWidthSet) { m_indentWidthSet = newIndentWidthSet; }
119 QStringList errors() const { return m_errors; }
120 void addError(const QString &newError) { m_errors.append(newError); };
121
122 void applySettings(const QQmlFormatSettings &settings);
123 static QQmlFormatOptions buildCommandLineOptions(const QStringList &args);
124 QQmlFormatOptions optionsForFile(const QString &fileName, QQmlFormatSettings *settings) const;
125
126 // Set of options that can be also passed by settings file.
127 // We need to know if the option was set by command line
139
140private:
141 // Command line options have the precedence over the values in the .ini file.
142 // Mark them if they are set by command line then don't override the options
143 // with the values in the .ini file.
144 void mark(Settings setting) { m_settingBits.set(setting, true); }
145 bool isMarked(Settings setting) const { return m_settingBits.test(setting); }
146
147private:
148 QQmlJS::Dom::LineWriterOptions m_options;
149
150 QQmlFormatOptionLineEndings m_newline = Native;
151
152 QStringList m_files;
153 QStringList m_arguments;
154 QStringList m_errors;
155
156 bool m_verbose = false;
157 bool m_inplace = false;
158 bool m_force = false;
159 bool m_ignoreSettings = false;
160 bool m_writeDefaultSettings = false;
161 bool m_indentWidthSet = false;
162 std::bitset<SettingsCount> m_settingBits;
163};
164
165QT_END_NAMESPACE
166
167#endif // QQMLFORMATOPTIONS_P_H
void setArguments(const QStringList &newArguments)
void setTabsEnabled(bool tabs)
bool isMaxColumnWidthSet() const
QStringList errors() const
static QQmlFormatOptions buildCommandLineOptions(const QStringList &args)
void setNewline(const QQmlFormatOptionLineEndings &endings)
void setNormalizeEnabled(bool normalize)
QQmlJS::Dom::LineWriterOptions optionsForCode(const QString &code) const
bool objectsSpacing() const
void setMaxColumnWidth(int width)
QStringList files() const
static LineEndings detectLineEndings(const QString &code)
void applySettings(const QQmlFormatSettings &settings)
void setIndentWidthSet(bool newIndentWidthSet)
static LineEndings lineEndings(QQmlFormatOptionLineEndings endings, const QString &code)
void setForceEnabled(bool newForce)
static QQmlFormatOptionLineEndings parseEndings(const QString &endings)
void setIgnoreSettingsEnabled(bool newIgnoreSettings)
void setIsVerbose(bool newVerbose)
void setSortImports(bool sort)
void setFunctionsSpacing(bool spacing)
void setIndentWidth(int width)
bool writeDefaultSettingsEnabled() const
QStringList arguments() const
bool normalizeEnabled() const
void setObjectsSpacing(bool spacing)
bool ignoreSettingsEnabled() const
void setIsInplace(bool newInplace)
bool functionsSpacing() const
void setWriteDefaultSettingsEnabled(bool newWriteDefaultSettings)
void setFiles(const QStringList &newFiles)
void addError(const QString &newError)
QQmlFormatOptions optionsForFile(const QString &fileName, QQmlFormatSettings *settings) const
QQmlFormatOptionLineEndings newline() const