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
23#include "qqmlformatsettings_p.h"
24#include <bitset>
25
26QT_BEGIN_NAMESPACE
27
28enum QQmlFormatOptionLineEndings {
29 Native,
30 Windows,
31 Unix,
32 OldMacOs,
33};
34
35class QQmlFormatOptions
36{
37public:
38 QQmlFormatOptions();
39
40 using LineEndings = QQmlJS::Dom::LineWriterOptions::LineEndings;
41 using AttributesSequence = QQmlJS::Dom::LineWriterOptions::AttributesSequence;
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 void setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule rule)
87 {
88 m_options.semicolonRule = rule;
89 }
90
91 QQmlJS::Dom::LineWriterOptions::SemicolonRule semicolonRule() const
92 {
93 return m_options.semicolonRule;
94 }
95
96 QQmlJS::Dom::LineWriterOptions optionsForCode(const QString &code) const
97 {
98 QQmlJS::Dom::LineWriterOptions result = m_options;
99 result.lineEndings = lineEndings(endings: m_newline, code);
100 return result;
101 }
102
103 static QQmlFormatOptionLineEndings parseEndings(const QString &endings);
104
105 QQmlFormatOptionLineEndings newline() const { return m_newline; }
106 void setNewline(const QQmlFormatOptionLineEndings &endings) { m_newline = endings; }
107
108 QStringList files() const { return m_files; }
109 void setFiles(const QStringList &newFiles) { m_files = newFiles; }
110 QStringList arguments() const { return m_arguments; }
111 void setArguments(const QStringList &newArguments) { m_arguments = newArguments; }
112 bool isVerbose() const { return m_verbose; }
113 void setIsVerbose(bool newVerbose) { m_verbose = newVerbose; }
114 bool isValid() const { return m_errors.isEmpty(); }
115 bool isInplace() const { return m_inplace; }
116 void setIsInplace(bool newInplace) { m_inplace = newInplace; }
117 bool forceEnabled() const { return m_force; }
118 void setForceEnabled(bool newForce) { m_force = newForce; }
119 bool ignoreSettingsEnabled() const { return m_ignoreSettings; }
120 void setIgnoreSettingsEnabled(bool newIgnoreSettings) { m_ignoreSettings = newIgnoreSettings; }
121 bool writeDefaultSettingsEnabled() const { return m_writeDefaultSettings; }
122 void setWriteDefaultSettingsEnabled(bool newWriteDefaultSettings)
123 {
124 m_writeDefaultSettings = newWriteDefaultSettings;
125 }
126
127 bool indentWidthSet() const { return m_indentWidthSet; }
128 void setIndentWidthSet(bool newIndentWidthSet) { m_indentWidthSet = newIndentWidthSet; }
129 QStringList errors() const { return m_errors; }
130 void addError(const QString &newError) { m_errors.append(t: newError); };
131
132 void applySettings(const QQmlFormatSettings &settings);
133 static QQmlFormatOptions buildCommandLineOptions(const QStringList &args);
134 QQmlFormatOptions optionsForFile(const QString &fileName, QQmlFormatSettings *settings) const;
135
136 // Set of options that can be also passed by settings file.
137 // We need to know if the option was set by command line
138 enum Settings {
139 UseTabs = 0,
140 IndentWidth,
141 MaxColumnWidth,
142 NormalizeOrder,
143 NewlineType,
144 ObjectsSpacing,
145 FunctionsSpacing,
146 SortImports,
147 SemicolonRule,
148 SettingsCount
149 };
150
151private:
152 // Command line options have the precedence over the values in the .ini file.
153 // Mark them if they are set by command line then don't override the options
154 // with the values in the .ini file.
155 void mark(Settings setting) { m_settingBits.set(position: setting, val: true); }
156 bool isMarked(Settings setting) const { return m_settingBits.test(position: setting); }
157
158private:
159 QQmlJS::Dom::LineWriterOptions m_options;
160
161 QQmlFormatOptionLineEndings m_newline = Native;
162
163 QStringList m_files;
164 QStringList m_arguments;
165 QStringList m_errors;
166
167 bool m_verbose = false;
168 bool m_inplace = false;
169 bool m_force = false;
170 bool m_ignoreSettings = false;
171 bool m_writeDefaultSettings = false;
172 bool m_indentWidthSet = false;
173 std::bitset<SettingsCount> m_settingBits;
174};
175
176QT_END_NAMESPACE
177
178#endif // QQMLFORMATOPTIONS_P_H
179

source code of qtdeclarative/src/qmlformat/qqmlformatoptions_p.h