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