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 sortImports() const { return m_options.sortImports; }
78 void setSortImports(bool sort) { m_options.sortImports = sort; }
79
80 bool singleLineEmptyObjects() const { return m_options.singleLineEmptyObjects; }
81 void setSingleLineEmptyObjects(bool singleLineEmptyObjects) { m_options.singleLineEmptyObjects = singleLineEmptyObjects; }
82
83 int indentWidth() const { return m_options.formatOptions.indentSize; }
84 void setIndentWidth(int width) { m_options.formatOptions.indentSize = width; }
85
86 int maxColumnWidth() const { return m_options.maxLineLength; }
87 void setMaxColumnWidth(int width) { m_options.maxLineLength = width; }
88 bool isMaxColumnWidthSet() const { return m_options.maxLineLength > 0; }
89
90 void setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule rule)
91 {
92 m_options.semicolonRule = rule;
93 }
94
96 {
97 return m_options.semicolonRule;
98 }
99
100 QQmlJS::Dom::LineWriterOptions optionsForCode(const QString &code) const
101 {
102 QQmlJS::Dom::LineWriterOptions result = m_options;
103 result.lineEndings = lineEndings(m_newline, code);
104 return result;
105 }
106
107 static QQmlFormatOptionLineEndings parseEndings(const QString &endings);
108
109 QQmlFormatOptionLineEndings newline() const { return m_newline; }
110 void setNewline(const QQmlFormatOptionLineEndings &endings) { m_newline = endings; }
111
112 QStringList files() const { return m_files; }
113 void setFiles(const QStringList &newFiles) { m_files = newFiles; }
114 QStringList arguments() const { return m_arguments; }
115 void setArguments(const QStringList &newArguments) { m_arguments = newArguments; }
116 bool isVerbose() const { return m_verbose; }
117 void setIsVerbose(bool newVerbose) { m_verbose = newVerbose; }
118 bool isValid() const { return m_errors.isEmpty(); }
119 bool isInplace() const { return m_inplace; }
120 void setIsInplace(bool newInplace) { m_inplace = newInplace; }
121 bool forceEnabled() const { return m_force; }
122 void setForceEnabled(bool newForce) { m_force = newForce; }
123 bool ignoreSettingsEnabled() const { return m_ignoreSettings; }
124 void setIgnoreSettingsEnabled(bool newIgnoreSettings) { m_ignoreSettings = newIgnoreSettings; }
125 bool writeDefaultSettingsEnabled() const { return m_writeDefaultSettings; }
126 void setWriteDefaultSettingsEnabled(bool newWriteDefaultSettings)
127 {
128 m_writeDefaultSettings = newWriteDefaultSettings;
129 }
130 bool outputOptionsEnabled() const { return m_outputOptions; }
131 void setOutputOptionsEnabled(bool newOutputOptions) { m_outputOptions = newOutputOptions; }
132
133 bool indentWidthSet() const { return m_indentWidthSet; }
134 void setIndentWidthSet(bool newIndentWidthSet) { m_indentWidthSet = newIndentWidthSet; }
135 bool dryRun() const { return m_dryRun; }
136 void setDryRun(bool newDryRun) { m_dryRun = newDryRun; }
137 QString settingsFile() const { return m_settingsFile; }
138 void setSettingsFile(const QString &newSettingsFile) { m_settingsFile = newSettingsFile; }
139 QStringList errors() const { return m_errors; }
140 void addError(const QString &newError) { m_errors.append(newError); };
141
142 void applySettings(const QQmlFormatSettings &settings);
143 static QQmlFormatOptions buildCommandLineOptions(const QStringList &args);
144 QQmlFormatOptions optionsForFile(const QString &fileName, QQmlFormatSettings *settings) const;
145
146 // Set of options that can be also passed by settings file.
147 // We need to know if the option was set by command line
162
163private:
164 // Command line options have the precedence over the values in the .ini file.
165 // Mark them if they are set by command line then don't override the options
166 // with the values in the .ini file.
167 void mark(Settings setting) { m_settingBits.set(setting, true); }
168 bool isMarked(Settings setting) const { return m_settingBits.test(setting); }
169
170private:
171 QQmlJS::Dom::LineWriterOptions m_options;
172
173 QQmlFormatOptionLineEndings m_newline = Native;
174
175 QStringList m_files;
176 QStringList m_arguments;
177 QStringList m_errors;
178
179 bool m_verbose = false;
180 bool m_inplace = false;
181 bool m_force = false;
182 bool m_ignoreSettings = false;
183 bool m_writeDefaultSettings = false;
184 bool m_outputOptions = false;
185 bool m_indentWidthSet = false;
186 std::bitset<SettingsCount> m_settingBits;
187 bool m_dryRun = false;
188 QString m_settingsFile;
189};
190
191QT_END_NAMESPACE
192
193#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 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)
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)