-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtcreator-sphinx-plugin.cpp
206 lines (172 loc) · 7.12 KB
/
qtcreator-sphinx-plugin.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
#include "qtcreator-sphinx-plugin.h"
#include "qtcreator-sphinx-pluginconstants.h"
#include "editor/SphinxCodeStylePreferencesFactory.h"
#include "editor/SphinxEditor.h"
#include "editor/SphinxEditorFactory.h"
#include "editor/SphinxEditorWidget.h"
#include "tests/tst_completion.h"
#include "tests/tst_formatter.h"
#include "tests/tst_rightpane.h"
#include <texteditor/codestylepool.h>
#include <texteditor/simplecodestylepreferences.h>
#include <texteditor/snippets/snippetprovider.h>
#include <texteditor/texteditorsettings.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <QAction>
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
#include <QtCore/QTranslator>
#include <QtTest/QTest>
#ifdef WITH_TESTS
QTEST_APPLESS_MAIN(qtc::plugin::sphinx::Plugin)
#endif
namespace qtc::plugin::sphinx {
namespace Internal {
PluginPrivate::PluginPrivate() {}
} // namespace Internal
Plugin::Plugin()
{
// Create your members
}
Plugin::~Plugin()
{
// Unregister objects from the plugin manager's object pool
// Delete members
delete d;
d = nullptr;
}
bool Plugin::initialize(const QStringList &arguments, QString *errorString)
{
// Register objects in the plugin manager's object pool
// Load settings
// Add actions to menus
// Connect to other plugins' signals
// In the initialize function, a plugin can be sure that the plugins it
// depends on have initialized their members.
Q_UNUSED(arguments)
Q_UNUSED(errorString)
initializeToolsSettings();
d = new Internal::PluginPrivate();
createActions();
TextEditor::SnippetProvider::registerGroup(Constants::SnippetGroupId,
tr("Sphinx", "SnippetProvider"),
&EditorFactory::decorateEditor);
const QString locale = Core::ICore::userInterfaceLanguage();
if (!locale.isEmpty()) {
auto qtr = new QTranslator(this);
const QString creatorTrPath = Core::ICore::resourcePath("translations").toString();
const QString trFile = QLatin1String(Constants::SnippetGroupId) + "_" + locale;
if (qtr->load(trFile, creatorTrPath)) {
QCoreApplication::installTranslator(qtr);
}
}
return true;
}
void Plugin::initializeToolsSettings()
{
mSettings.load();
// code style factory
auto factory = new CodeStylePreferencesFactory;
TextEditor::TextEditorSettings::registerCodeStyleFactory(factory);
// code style pool
auto pool = new TextEditor::CodeStylePool(factory, this);
TextEditor::TextEditorSettings::registerCodeStylePool(Constants::SettingsGeneralId, pool);
// global code style settings
auto globalCodeStyle = new TextEditor::SimpleCodeStylePreferences(this);
globalCodeStyle->setDelegatingPool(pool);
globalCodeStyle->setDisplayName(tr("Global", "Settings"));
globalCodeStyle->setId("SphinxGlobal");
pool->addCodeStyle(globalCodeStyle);
TextEditor::TextEditorSettings::registerCodeStyle(Constants::SettingsGeneralId, globalCodeStyle);
// built-in settings
// Sphinx style
auto sphinxyCodeStyle = new TextEditor::SimpleCodeStylePreferences;
sphinxyCodeStyle->setId("sphinx");
sphinxyCodeStyle->setDisplayName(tr("Sphinx"));
sphinxyCodeStyle->setReadOnly(true);
TextEditor::TabSettings tabSettings;
tabSettings.m_tabPolicy = TextEditor::TabSettings::SpacesOnlyTabPolicy;
tabSettings.m_tabSize = mSettings.indentSize();
tabSettings.m_indentSize = mSettings.indentSize();
tabSettings.m_continuationAlignBehavior = TextEditor::TabSettings::ContinuationAlignWithIndent;
sphinxyCodeStyle->setTabSettings(tabSettings);
pool->addCodeStyle(sphinxyCodeStyle);
// default delegate for global preferences
globalCodeStyle->setCurrentDelegate(sphinxyCodeStyle);
pool->loadCustomCodeStyles();
// load global settings (after built-in settings are added to the pool)
globalCodeStyle->fromSettings(Constants::SettingsGeneralId, Core::ICore::settings());
// mimetypes to be handled
TextEditor::TextEditorSettings::registerMimeTypeForLanguageId(Constants::MimeType,
Constants::SettingsGeneralId);
mOptionsPage = new OptionsPage(&mSettings, this);
}
void Plugin::extensionsInitialized()
{
// Retrieve objects from the plugin manager's object pool
// In the extensionsInitialized function, a plugin can be sure that all
// plugins that depend on it are completely initialized.
}
ExtensionSystem::IPlugin::ShutdownFlag Plugin::aboutToShutdown()
{
mSettings.save();
// Save settings
// Disconnect from signals that are not needed during shutdown
// Hide UI (if you add UI that is not in the main window directly)
return SynchronousShutdown;
}
QVector<QObject *> Plugin::createTestObjects() const
{
#ifdef WITH_TESTS
QVector<QObject *> tests;
tests << new TestFormatter << new TestCompletion << new TestRightPane;
return tests;
#else
return {};
#endif
}
void Plugin::createActions()
{
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::MENU_ID);
menu->menu()->setTitle(tr("Sphinx Editor"));
Core::Command *cmd = nullptr;
QAction *action = nullptr;
const auto &formatActions = mFormatActions.formatActions();
for (int idx = 0; idx < FormatActions::FORMAT::LAST_FORMAT; idx++) {
action = formatActions[idx];
cmd = Core::ActionManager::registerAction(action,
qPrintable(action->objectName()),
Core::Context(Core::Constants::C_EDIT_MODE));
cmd->setDefaultKeySequence(action->shortcut());
menu->addAction(cmd);
}
menu->addSeparator();
const auto &commentActions = mFormatActions.commentActions();
for (int idx = 0; idx < FormatActions::COMMENTS::LAST_COMMENT; idx++) {
action = commentActions[idx];
cmd = Core::ActionManager::registerAction(action,
qPrintable(action->objectName()),
Core::Context(Core::Constants::C_EDIT_MODE));
cmd->setDefaultKeySequence(action->shortcut());
menu->addAction(cmd);
}
menu->addSeparator();
// const auto &spaceActions = mFormatActions.spaceActions();
const auto §ionActions = mFormatActions.sectionActions();
for (int idx = 0; idx < FormatActions::SECTIONS::LAST_SECTION; idx++) {
action = sectionActions[idx];
cmd = Core::ActionManager::registerAction(action,
qPrintable(action->objectName()),
Core::Context(Core::Constants::C_EDIT_MODE));
cmd->setDefaultKeySequence(action->shortcut());
menu->addAction(cmd);
}
Core::ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu);
}
} // namespace qtc::plugin::sphinx