-
Notifications
You must be signed in to change notification settings - Fork 8
/
editorconfigdata.cpp
156 lines (130 loc) · 5.11 KB
/
editorconfigdata.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
/*
* Copyright 2021 Herbert Graeber
*/
#include "editorconfigdata.h"
#include <editorconfig/editorconfig.h>
#include <app/app_version.h>
#include <coreplugin/messagemanager.h>
#include <QtCore/QByteArray>
using namespace EditorConfig;
EditorConfigData::EditorConfigData(const QString &name, QObject *parent) :
QObject(parent), file_name(name)
{
QByteArray nativeName = QFile::encodeName(name);
editorconfig_handle handle = editorconfig_handle_init();
int ret = editorconfig_parse(nativeName.data(), handle);
if (ret == 0) {
int count = editorconfig_handle_get_name_value_count(handle);
for (int i = 0; i < count; i++) {
const char *name;
const char *value;
editorconfig_handle_get_name_value(handle, i, &name, &value);
m_data.insert(name, value);
}
editorconfig_handle_destroy(handle);
}
else if (ret > 0) {
#if IDE_VERSION_MAJOR >= 5 || IDE_VERSION_MAJOR == 4 && IDE_VERSION_MINOR >= 15
Core::MessageManager::writeFlashing(
QString::fromUtf8("editorconfig: Parse error in file \"%1\", line %2")
.arg(QString::fromUtf8(editorconfig_handle_get_err_file(handle))).arg(ret));
#else
Core::MessageManager::write(
QString::fromUtf8("editorconfig: Parse error in file \"%1\", line %2")
.arg(QString::fromUtf8(editorconfig_handle_get_err_file(handle))).arg(ret),
Core::MessageManager::Flash);
#endif
}
else {
#if IDE_VERSION_MAJOR >= 5 || IDE_VERSION_MAJOR == 4 && IDE_VERSION_MINOR >= 15
Core::MessageManager::writeFlashing(
QString::fromUtf8("editorconfig: %1")
.arg(QString::fromUtf8(editorconfig_get_error_msg(ret))));
}
#else
Core::MessageManager::writeFlashing(
QString::fromUtf8("editorconfig: %1")
.arg(QString::fromUtf8(editorconfig_get_error_msg(ret)))
Core::MessageManager::Flash);
#endif
}
bool EditorConfigData::overrideTabSettings(TextEditor::TabSettings &tabSettings) const {
bool changed = false;
QByteArray value;
bool ok;
value = m_data["tab_width"];
int tabSize = value.toInt(&ok);
if (ok && tabSize > 0 && tabSettings.m_tabSize != tabSize) {
message(tr("override tab width with %1").arg(tabSize));
tabSettings.m_tabSize = tabSize;
changed = true;
}
value = m_data["indent_size"];
int indentSize = value.toInt(&ok);
if (ok && indentSize > 0 && tabSettings.m_indentSize != indentSize) {
message(tr("override indent size with %1").arg(indentSize));
tabSettings.m_indentSize = indentSize;
changed = true;
}
value = m_data["indent_style"];
if (value == "tab") {
if (tabSettings.m_tabPolicy != TextEditor::TabSettings::TabsOnlyTabPolicy) {
message(tr("override indent style with 'tab'"));
tabSettings.m_tabPolicy = TextEditor::TabSettings::TabsOnlyTabPolicy;
changed = true;
}
}
else if (value == "space") {
if (tabSettings.m_tabPolicy != TextEditor::TabSettings::SpacesOnlyTabPolicy) {
message(tr("override indent style with 'space'"));
tabSettings.m_tabPolicy = TextEditor::TabSettings::SpacesOnlyTabPolicy;
changed = true;
}
}
return changed;
}
bool EditorConfigData::overrideStorageSettings(TextEditor::StorageSettings &storageSettings) const {
bool changed = false;
QByteArray value;
value = m_data["insert_final_newline"];
if (value == "true" && !storageSettings.m_addFinalNewLine) {
message(tr("override add final newline with 'true'"));
storageSettings.m_addFinalNewLine = true;
changed = true;
}
else if (value == "false" && storageSettings.m_addFinalNewLine) {
message(tr("override add final newline with 'false'"));
storageSettings.m_addFinalNewLine = false;
changed = true;
}
value = m_data["trim_trailing_whitespace"];
if (value == "true" && !storageSettings.m_cleanWhitespace) {
message(tr("override trim trailing whitespace with 'true'"));
storageSettings.m_cleanWhitespace = true;
changed = true;
}
else if (value == "false" && storageSettings.m_cleanWhitespace) {
message(tr("override trim trailing whitespace with 'false'"));
storageSettings.m_cleanWhitespace = false;
changed = true;
}
return changed;
}
bool EditorConfigData::overrideCodec(const QTextCodec *&codec) const {
bool changed = false;
QByteArray value = m_data["charset"];
QTextCodec *newCodec = QTextCodec::codecForName(value);
if (newCodec && codec != newCodec) {
message(tr("override charset with '%1'").arg(QString::fromLatin1(newCodec->name())));
codec = newCodec;
changed = true;
}
return changed;
}
void EditorConfigData::message(const QString &msg) const {
#if IDE_VERSION_MAJOR >= 5 || IDE_VERSION_MAJOR == 4 && IDE_VERSION_MINOR >= 15
Core::MessageManager::writeSilently(QStringLiteral("%1: %2").arg(file_name, msg));
#else
Core::MessageManager::write(QStringLiteral("%1: %2").arg(file_name, msg), Core::MessageManager::Silent);
#endif
}