This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvcreatorsettings.cpp
354 lines (264 loc) · 11.4 KB
/
vcreatorsettings.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "vcreatorsettings.h"
#include "vcreatorconstants.h"
#include "vcreatorindenter.h"
#include "vcreatorhighlighter.h"
#include <texteditor/simplecodestylepreferences.h>
#include <texteditor/simplecodestylepreferenceswidget.h>
#include <texteditor/snippets/snippeteditor.h>
#include <texteditor/codestyleeditor.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/textdocument.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/codestylepool.h>
#include <texteditor/displaysettings.h>
#include <coreplugin/icore.h>
#include <QWidget>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QFormLayout>
#include <utils/pathchooser.h>
using namespace TextEditor;
namespace VCreator {
namespace Internal {
static SimpleCodeStylePreferences *m_globalCodeStyle = nullptr;
class SettingsWidget final : public QWidget {
public:
explicit SettingsWidget(QWidget *parent = nullptr);
private:
QGroupBox *groupBox;
QVBoxLayout *verticalLayout;
QVBoxLayout *verticalLayout_2;
QFormLayout *formLayout;
QLabel *pathLabel;
Utils::PathChooser *pathWidget;
QSpacerItem *verticalSpacer;
};
class rCodeStylePreferencesWidget : public QWidget
{
QGridLayout *gridLayout;
TextEditor::SimpleCodeStylePreferencesWidget *tabPreferencesWidget;
TextEditor::SnippetEditorWidget *previewTextEdit;
QSpacerItem *verticalSpacer;
public:
rCodeStylePreferencesWidget(TextEditor::ICodeStylePreferences *preferences, QWidget *parent = nullptr);
~rCodeStylePreferencesWidget() = default;
private:
void decorateEditor(const TextEditor::FontSettings &fontSettings);
void setVisualizeWhitespace(bool on);
void updatePreview();
TextEditor::ICodeStylePreferences *m_preferences;
};
class CodeStyleSettingsWidget : public Core::IOptionsPageWidget
{
Q_DECLARE_TR_FUNCTIONS(CodeStyleSettings)
public:
CodeStyleSettingsWidget();
private:
void apply() final;
void finish() final;
TextEditor::SimpleCodeStylePreferences *m_vlangCodeStylePreferences;
};
class CodeStylePreferencesFactory : public TextEditor::ICodeStylePreferencesFactory
{
Q_DECLARE_TR_FUNCTIONS(VcreatorCodeStylePreferencesFactory)
public:
CodeStylePreferencesFactory();
Utils::Id languageId() override;
QString displayName() override;
TextEditor::ICodeStylePreferences *createCodeStyle() const override;
QWidget *createEditor(TextEditor::ICodeStylePreferences *preferences,
QWidget *parent) const override;
TextEditor::Indenter *createIndenter(QTextDocument *doc) const override;
QString snippetProviderGroupId() const override;
QString previewText() const override;
};
VlangSettings::VlangSettings(QObject *parent)
: QObject(parent)
{
auto factory = new CodeStylePreferencesFactory();
TextEditorSettings::registerCodeStyleFactory(factory);
// code style pool
auto pool = new CodeStylePool(factory, this);
TextEditorSettings::registerCodeStylePool(Constants::C_VLANGUAGE_ID, pool);
m_globalCodeStyle = new SimpleCodeStylePreferences();
m_globalCodeStyle->setDelegatingPool(pool);
m_globalCodeStyle->setDisplayName(tr("Global", "Settings"));
m_globalCodeStyle->setId(Constants::C_VLANGGLOBALCODESTYLE_ID);
pool->addCodeStyle(m_globalCodeStyle);
TextEditorSettings::registerCodeStyle(Constants::C_VLANGUAGE_ID, m_globalCodeStyle);
auto codeStyle = new SimpleCodeStylePreferences();
codeStyle->setId("vlang");
codeStyle->setDisplayName(tr("V"));
codeStyle->setReadOnly(true);
TabSettings tabSettings;
tabSettings.m_tabPolicy = TabSettings::TabsOnlyTabPolicy;
tabSettings.m_tabSize = 4;
tabSettings.m_indentSize = 4;
tabSettings.m_continuationAlignBehavior = TabSettings::ContinuationAlignWithIndent;
codeStyle->setTabSettings(tabSettings);
pool->addCodeStyle(codeStyle);
m_globalCodeStyle->setCurrentDelegate(codeStyle);
pool->loadCustomCodeStyles();
// load global settings (after built-in settings are added to the pool)
QSettings *s = Core::ICore::settings();
m_globalCodeStyle->fromSettings(QLatin1String(Constants::C_VLANGUAGE_ID), s);
TextEditorSettings::registerMimeTypeForLanguageId(Constants::C_VLANG_MIMETYPE,
Constants::C_VLANGUAGE_ID);
}
VlangSettings::~VlangSettings()
{
TextEditorSettings::unregisterCodeStyle(Constants::C_VLANGUAGE_ID);
TextEditorSettings::unregisterCodeStylePool(Constants::C_VLANGUAGE_ID);
TextEditorSettings::unregisterCodeStyleFactory(Constants::C_VLANGUAGE_ID);
delete m_globalCodeStyle;
m_globalCodeStyle = nullptr;
}
TextEditor::SimpleCodeStylePreferences *VlangSettings::globalCodeStyle()
{
return m_globalCodeStyle;
}
VlangSettingsPage::VlangSettingsPage()
{
setId(Constants::C_VLANGSETTINGSPAGE_ID);
setDisplayName(Constants::C_VLANGSETTINGSPAGE_DISPLAY);
setCategory(Constants::C_VLANGSETTINGSPAGE_CATEGORY);
setDisplayCategory("V");
setCategoryIconPath(":/v/images/settingscategory_v.png");
}
QWidget *VlangSettingsPage::widget()
{
if (!m_widget)
m_widget.reset(new SettingsWidget);
return m_widget.get();
}
void VlangSettingsPage::apply()
{
}
void VlangSettingsPage::finish()
{
m_widget.reset();
}
VlangCodeStyleSettingsPage::VlangCodeStyleSettingsPage()
{
setId(Constants::C_VLANGCODESTYLESETTINGSPAGE_ID);
setDisplayName(tr(Constants::C_VLANGCODESTYLESETTINGSPAGE_DISPLAY));
setCategory(Constants::C_VLANGCODESTYLESETTINGSPAGE_CATEGORY);
setDisplayCategory(CodeStyleSettingsWidget::tr("V"));
setCategoryIconPath(":/v/images/settingscategory_v.png");
setWidgetCreator([] { return new CodeStyleSettingsWidget; });
}
rCodeStylePreferencesWidget::rCodeStylePreferencesWidget(ICodeStylePreferences *preferences, QWidget *parent)
: QWidget(parent), m_preferences(preferences) {
resize(138, 112);
gridLayout = new QGridLayout(this);
tabPreferencesWidget = new TextEditor::SimpleCodeStylePreferencesWidget(this);
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(tabPreferencesWidget->sizePolicy().hasHeightForWidth());
tabPreferencesWidget->setSizePolicy(sizePolicy);
gridLayout->addWidget(tabPreferencesWidget, 0, 0, 1, 1);
previewTextEdit = new TextEditor::SnippetEditorWidget(this);
gridLayout->addWidget(previewTextEdit, 0, 1, 2, 1);
verticalSpacer = new QSpacerItem(20, 267, QSizePolicy::Minimum, QSizePolicy::Expanding);
gridLayout->addItem(verticalSpacer, 1, 0, 1, 1);
setWindowTitle(QString());
tabPreferencesWidget->setPreferences(preferences);
previewTextEdit->setPlainText(Constants::C_VCODESTYLEPREVIEWSNIPPET);
decorateEditor(TextEditorSettings::fontSettings());
connect(TextEditorSettings::instance(), &TextEditorSettings::fontSettingsChanged,
this, &rCodeStylePreferencesWidget::decorateEditor);
connect(m_preferences, &ICodeStylePreferences::currentTabSettingsChanged,
this, &rCodeStylePreferencesWidget::updatePreview);
setVisualizeWhitespace(true);
updatePreview();
}
void rCodeStylePreferencesWidget::decorateEditor(const FontSettings &fontSettings) {
previewTextEdit->textDocument()->setFontSettings(fontSettings);
previewTextEdit->textDocument()->setSyntaxHighlighter(new VlangHighlighter());
previewTextEdit->textDocument()->setIndenter(new VlangIndenter(previewTextEdit->textDocument()->document()));
}
void rCodeStylePreferencesWidget::setVisualizeWhitespace(bool on) {
DisplaySettings displaySettings = previewTextEdit->displaySettings();
displaySettings.m_visualizeWhitespace = on;
previewTextEdit->setDisplaySettings(displaySettings);
}
void rCodeStylePreferencesWidget::updatePreview() {
QTextDocument *doc = previewTextEdit->document();
const TabSettings &ts = m_preferences
? m_preferences->currentTabSettings()
: TextEditorSettings::codeStyle()->tabSettings();
previewTextEdit->textDocument()->setTabSettings(ts);
QTextBlock block = doc->firstBlock();
QTextCursor tc = previewTextEdit->textCursor();
tc.beginEditBlock();
while (block.isValid()) {
previewTextEdit->textDocument()->indenter()->indentBlock(block, QChar::Null, ts);
block = block.next();
}
tc.endEditBlock();
}
SettingsWidget::SettingsWidget(QWidget *parent)
: QWidget(parent)
{
verticalLayout_2 = new QVBoxLayout(this);
groupBox = new QGroupBox(this);
verticalLayout = new QVBoxLayout(groupBox);
formLayout = new QFormLayout();
pathLabel = new QLabel(groupBox);
formLayout->setWidget(0, QFormLayout::LabelRole, pathLabel);
pathWidget = new Utils::PathChooser(groupBox);
pathWidget->setExpectedKind(Utils::PathChooser::ExistingCommand);
formLayout->setWidget(0, QFormLayout::FieldRole, pathWidget);
verticalLayout->addLayout(formLayout);
verticalLayout_2->addWidget(groupBox);
verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
verticalLayout_2->addItem(verticalSpacer);
groupBox->setTitle(tr("V compiler"));
pathLabel->setText(tr("Path"));
}
CodeStylePreferencesFactory::CodeStylePreferencesFactory() {
}
Utils::Id CodeStylePreferencesFactory::languageId() {
return Constants::C_VLANGUAGE_ID;
}
QString CodeStylePreferencesFactory::displayName() {
return Constants::C_VLANGUAGE_NAME;
}
ICodeStylePreferences *CodeStylePreferencesFactory::createCodeStyle() const {
return new TextEditor::SimpleCodeStylePreferences();
}
QWidget *CodeStylePreferencesFactory::createEditor(ICodeStylePreferences *preferences, QWidget *parent) const {
auto result = new rCodeStylePreferencesWidget(preferences, parent);
result->layout()->setContentsMargins(0, 0, 0, 0);
return result;
}
Indenter *CodeStylePreferencesFactory::createIndenter(QTextDocument *doc) const {
return new VlangIndenter(doc);
}
QString CodeStylePreferencesFactory::snippetProviderGroupId() const {
return Constants::C_VLANGSNIPPETSGROUP_ID;
}
QString CodeStylePreferencesFactory::previewText() const {
return Constants::C_VCODESTYLEPREVIEWSNIPPET;
}
CodeStyleSettingsWidget::CodeStyleSettingsWidget()
{
auto originalTabPreferences = VlangSettings::globalCodeStyle();
m_vlangCodeStylePreferences = new SimpleCodeStylePreferences(this);
m_vlangCodeStylePreferences->setDelegatingPool(originalTabPreferences->delegatingPool());
m_vlangCodeStylePreferences->setTabSettings(originalTabPreferences->tabSettings());
m_vlangCodeStylePreferences->setCurrentDelegate(originalTabPreferences->currentDelegate());
m_vlangCodeStylePreferences->setId(originalTabPreferences->id());
auto factory = TextEditorSettings::codeStyleFactory(Constants::C_VLANGUAGE_ID);
auto editor = new CodeStyleEditor(factory, m_vlangCodeStylePreferences);
auto layout = new QVBoxLayout(this);
layout->addWidget(editor);
}
void CodeStyleSettingsWidget::apply() {}
void CodeStyleSettingsWidget::finish() {}
} // namespace Internal
} // namespace Vcreator