forked from editorconfig/editorconfig-qtcreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editorconfigplugin.cpp
146 lines (117 loc) · 4.9 KB
/
editorconfigplugin.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
/*
* Copyright 2016,2017 Herbert Graeber
*/
#include "editorconfigplugin.h"
#include "editorconfigdata.h"
#include "editorconfiglogging.h"
#include "editorconfigwizard.h"
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/idocument.h>
#include <coreplugin/iwizardfactory.h>
#include <texteditor/textdocument.h>
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/messagemanager.h>
#include <QtPlugin>
#include <QtDebug>
#include <QApplication>
using namespace ExtensionSystem;
using namespace EditorConfig::Internal;
EditorConfigPlugin::EditorConfigPlugin()
{
}
EditorConfigPlugin::~EditorConfigPlugin()
{
}
bool EditorConfigPlugin::initialize(const QStringList &arguments, QString *errorString)
{
Q_UNUSED(arguments)
Q_UNUSED(errorString)
QStringList uiLanguages;
uiLanguages = QLocale::system().uiLanguages();
QString overrideLanguage = PluginManager::globalSettings()->value(QLatin1String("General/OverrideLanguage")).toString();
if (!overrideLanguage.isEmpty())
uiLanguages.prepend(overrideLanguage);
QString creatorTrPath(QCoreApplication::applicationDirPath() + '/' + RELATIVE_DATA_PATH + "/translations");
foreach (QString locale, uiLanguages) {
Core::MessageManager::write(QString("Translation Path: \"%1\"").arg(creatorTrPath));
locale = QLocale(locale).name();
if (translator.load(QLatin1String("editorconfig_") + locale, creatorTrPath)) {
QApplication::instance()->installTranslator(&translator);
break;
} else if (locale == QLatin1String("C") /* overrideLanguage == "English" */) {
// use built-in
break;
} else if (locale.startsWith(QLatin1String("en")) /* "English" is built-in */) {
// use built-in
break;
}
}
Core::IWizardFactory::registerFactoryCreator([] {
return QList<Core::IWizardFactory *> {
new EditorConfigWizard
};
});
if (Core::EditorManager *editorManager = Core::EditorManager::instance()) {
connect(editorManager, SIGNAL(editorCreated(Core::IEditor*,QString)), SLOT(editorCreated(Core::IEditor*,QString)));
connect(editorManager, SIGNAL(editorAboutToClose(Core::IEditor*)), SLOT(editorAboutToClose(Core::IEditor*)));
}
return true;
}
void EditorConfigPlugin::extensionsInitialized()
{
}
ExtensionSystem::IPlugin::ShutdownFlag EditorConfigPlugin::aboutToShutdown()
{
return SynchronousShutdown;
}
void EditorConfigPlugin::editorCreated(Core::IEditor *editor, const QString &name) {
qCDebug(editorConfigLog) << "EditorConfig: created editor" << name;
if (TextEditor::BaseTextEditor *textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor)) {
if (TextEditor::TextDocument *textDocument = textEditor->textDocument()) {
documents.insert(
textDocument,
connect(textDocument, &TextEditor::TextDocument::tabSettingsChanged,
this, [=](){ this->overrideSettings(textDocument); }, Qt::QueuedConnection)
);
emit textDocument->tabSettingsChanged();
}
}
}
void EditorConfigPlugin::overrideSettings(TextEditor::TextDocument *textDocument)
{
if (!changingDocuments.contains(textDocument)) {
qCDebug(editorConfigLog) << "EditorConfig: overrideSettings for" << textDocument->filePath();
EditorConfigData data(textDocument->filePath().toString());
TextEditor::TabSettings tabSettings = textDocument->tabSettings();
TextEditor::StorageSettings storageSettings = textDocument->storageSettings();
const QTextCodec *codec = textDocument->codec();
changingDocuments.insert(textDocument);
if (data.overrideTabSettings(tabSettings))
textDocument->setTabSettings(tabSettings);
if (data.overrideStorageSettings(storageSettings))
textDocument->setStorageSettings(storageSettings);
if (data.overrideCodec(codec))
textDocument->setCodec(codec);
changingDocuments.remove(textDocument);
}
}
void EditorConfigPlugin::tabSettingsChanged()
{
if (TextEditor::TextDocument *textDocument = qobject_cast<TextEditor::TextDocument *>(sender())) {
qCDebug(editorConfigLog) << "EditorConfig: tabSettingsChanged" << textDocument->filePath();
overrideSettings(textDocument);
}
}
void EditorConfigPlugin::editorAboutToClose(Core::IEditor *editor)
{
if (TextEditor::BaseTextEditor *textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor)) {
if (TextEditor::TextDocument *textDocument = textEditor->textDocument()) {
qCDebug(editorConfigLog) << "EditorConfig: documentclosed" << textDocument->filePath();
if (auto connection = documents[textDocument]) {
textDocument->disconnect(connection);
documents.remove(textDocument);
}
}
}
}