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// Qt-Security score:significant
4
5#ifndef QQMLFORMATOPTIONS_P_H
6#define QQMLFORMATOPTIONS_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qstring.h>
20#include <QtQmlDom/private/qqmldomoutwriter_p.h>
21#include <QtQmlDom/private/qqmldomlinewriter_p.h>
22#include <QtQmlToolingSettings/private/qqmltoolingsettings_p.h>
23
25#include <bitset>
26
27QT_BEGIN_NAMESPACE
28
29enum QQmlFormatOptionLineEndings {
30 Native,
31 Windows,
32 Unix,
33 OldMacOs,
34};
35
37{
38public:
40
43
44 static LineEndings detectLineEndings(const QString &code);
45
46 static LineEndings lineEndings(QQmlFormatOptionLineEndings endings, const QString &code)
47 {
48 switch (endings) {
49 case Native:
50 return detectLineEndings(code);
51 case OldMacOs:
52 return LineEndings::OldMacOs;
53 case Windows:
54 return LineEndings::Windows;
55 case Unix:
56 return LineEndings::Unix;
57 }
58 Q_UNREACHABLE_RETURN(LineEndings::Unix);
59 }
60
61 bool tabsEnabled() const { return m_options.formatOptions.useTabs; }
62 void setTabsEnabled(bool tabs) { m_options.formatOptions.useTabs = tabs; }
63 bool normalizeEnabled() const
64 {
65 return m_options.attributesSequence == AttributesSequence::Normalize;
66 }
67 void setNormalizeEnabled(bool normalize)
68 {
69 m_options.attributesSequence =
70 (normalize ? AttributesSequence::Normalize : AttributesSequence::Preserve);
71 }
72 bool objectsSpacing() const { return m_options.objectsSpacing; }
73 void setObjectsSpacing(bool spacing) { m_options.objectsSpacing = spacing; }
74 bool functionsSpacing() const { return m_options.functionsSpacing; }
75 void setFunctionsSpacing(bool spacing) { m_options.functionsSpacing = spacing; }
76
77 bool groupAttributesTogether() const { return m_options.groupAttributesTogether; }
79 {
80 m_options.groupAttributesTogether = keep;
81 if (keep)
82 m_options.attributesSequence = AttributesSequence::Normalize;
83 }
84
85 bool sortImports() const { return m_options.sortImports; }
86 void setSortImports(bool sort) { m_options.sortImports = sort; }
87
88 bool singleLineEmptyObjects() const { return m_options.singleLineEmptyObjects; }
89 void setSingleLineEmptyObjects(bool singleLineEmptyObjects) { m_options.singleLineEmptyObjects = singleLineEmptyObjects; }
90
91 int indentWidth() const { return m_options.formatOptions.indentSize; }
92 void setIndentWidth(int width) { m_options.formatOptions.indentSize = width; }
93
94 int maxColumnWidth() const { return m_options.maxLineLength; }
95 void setMaxColumnWidth(int width) { m_options.maxLineLength = width; }
96 bool isMaxColumnWidthSet() const { return m_options.maxLineLength > 0; }
97
98 void setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule rule)
99 {
100 m_options.semicolonRule = rule;
101 }
102
104 {
105 return m_options.semicolonRule;
106 }
107
108 QQmlJS::Dom::LineWriterOptions optionsForCode(const QString &code) const
109 {
110 QQmlJS::Dom::LineWriterOptions result = m_options;
111 result.lineEndings = lineEndings(m_newline, code);
112 return result;
113 }
114
115 static QQmlFormatOptionLineEndings parseEndings(const QString &endings);
116
117 QQmlFormatOptionLineEndings newline() const { return m_newline; }
118 void setNewline(const QQmlFormatOptionLineEndings &endings) { m_newline = endings; }
119
120 QStringList files() const { return m_files; }
121 void setFiles(const QStringList &newFiles) { m_files = newFiles; }
122 QStringList arguments() const { return m_arguments; }
123 void setArguments(const QStringList &newArguments) { m_arguments = newArguments; }
124 bool isVerbose() const { return m_verbose; }
125 void setIsVerbose(bool newVerbose) { m_verbose = newVerbose; }
126 bool isValid() const { return m_errors.isEmpty(); }
127 bool isInplace() const { return m_inplace; }
128 void setIsInplace(bool newInplace) { m_inplace = newInplace; }
129 bool forceEnabled() const { return m_force; }
130 void setForceEnabled(bool newForce) { m_force = newForce; }
131 bool ignoreSettingsEnabled() const { return m_ignoreSettings; }
132 void setIgnoreSettingsEnabled(bool newIgnoreSettings) { m_ignoreSettings = newIgnoreSettings; }
133 bool writeDefaultSettingsEnabled() const { return m_writeDefaultSettings; }
134 void setWriteDefaultSettingsEnabled(bool newWriteDefaultSettings)
135 {
136 m_writeDefaultSettings = newWriteDefaultSettings;
137 }
138 bool outputOptionsEnabled() const { return m_outputOptions; }
139 void setOutputOptionsEnabled(bool newOutputOptions) { m_outputOptions = newOutputOptions; }
140
141 bool indentWidthSet() const { return m_indentWidthSet; }
142 void setIndentWidthSet(bool newIndentWidthSet) { m_indentWidthSet = newIndentWidthSet; }
143 bool dryRun() const { return m_dryRun; }
144 void setDryRun(bool newDryRun) { m_dryRun = newDryRun; }
145 QString settingsFile() const { return m_settingsFile; }
146 void setSettingsFile(const QString &newSettingsFile) { m_settingsFile = newSettingsFile; }
147 QStringList errors() const { return m_errors; }
148 void addError(const QString &newError) { m_errors.append(newError); };
149
150 void applySettings(const QQmlFormatSettings &settings);
151 static QQmlFormatOptions buildCommandLineOptions(const QStringList &args);
152 QQmlFormatOptions optionsForFile(const QString &fileName, QQmlFormatSettings *settings) const;
153
154 // Set of options that can be also passed by settings file.
155 // We need to know if the option was set by command line
171
172private:
173 // Command line options have the precedence over the values in the .ini file.
174 // Mark them if they are set by command line then don't override the options
175 // with the values in the .ini file.
176 void mark(Settings setting) { m_settingBits.set(setting, true); }
177 bool isMarked(Settings setting) const { return m_settingBits.test(setting); }
178
179private:
180 QQmlJS::Dom::LineWriterOptions m_options;
181
182 QQmlFormatOptionLineEndings m_newline = Native;
183
184 QStringList m_files;
185 QStringList m_arguments;
186 QStringList m_errors;
187
188 bool m_verbose = false;
189 bool m_inplace = false;
190 bool m_force = false;
191 bool m_ignoreSettings = false;
192 bool m_writeDefaultSettings = false;
193 bool m_outputOptions = false;
194 bool m_indentWidthSet = false;
195 std::bitset<SettingsCount> m_settingBits;
196 bool m_dryRun = false;
197 QString m_settingsFile;
198};
199
200QT_END_NAMESPACE
201
202#endif // QQMLFORMATOPTIONS_P_H
bool outputOptionsEnabled() const
void setArguments(const QStringList &newArguments)
void setTabsEnabled(bool tabs)
bool isMaxColumnWidthSet() const
void setSingleLineEmptyObjects(bool singleLineEmptyObjects)
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
void setOutputOptionsEnabled(bool newOutputOptions)
bool objectsSpacing() const
void setMaxColumnWidth(int width)
QStringList files() const
void setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule rule)
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
bool groupAttributesTogether() const
bool singleLineEmptyObjects() const
void setObjectsSpacing(bool spacing)
bool ignoreSettingsEnabled() const
void setSettingsFile(const QString &newSettingsFile)
void setIsInplace(bool newInplace)
bool functionsSpacing() const
void setWriteDefaultSettingsEnabled(bool newWriteDefaultSettings)
void setFiles(const QStringList &newFiles)
void setDryRun(bool newDryRun)
void setGroupAttributesTogether(bool keep)
QQmlJS::Dom::LineWriterOptions::SemicolonRule semicolonRule() const
void addError(const QString &newError)
QQmlFormatOptions optionsForFile(const QString &fileName, QQmlFormatSettings *settings) const
QString settingsFile() const
QQmlFormatOptionLineEndings newline() const
std::optional< QQmlJS::Dom::LineWriterOptions::SemicolonRule > parseSemicolonRule(const QString &value)