-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSyntaxManager.cpp
More file actions
130 lines (114 loc) · 3.82 KB
/
Copy pathSyntaxManager.cpp
File metadata and controls
130 lines (114 loc) · 3.82 KB
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
#include "SyntaxManager.h"
#include "Syntax.h"
#include <QDir>
#include <QFile>
#include <QDebug>
void SyntaxManager::initializeUserSyntaxConfig()
{
QString userSyntaxDir = QDir::homePath() + "/.config/codeastra/syntax";
QDir dir(userSyntaxDir);
if (!dir.exists())
{
qDebug() << "[Setup] First run detected. Creating syntax config directory:" << userSyntaxDir;
if (!dir.mkpath("."))
{
qWarning() << "[Setup] Failed to create user syntax config directory:" << userSyntaxDir;
return;
}
// List of default syntax files to copy
QDir defaultDir(":/resources/syntax/");
QStringList defaultSyntaxFiles = defaultDir.entryList({"*.yaml", "*.yml"}, QDir::Files);
for (const QString &fileName : defaultSyntaxFiles)
{
QString resourcePath = ":/resources/syntax/" + fileName;
QString destPath = userSyntaxDir + "/" + fileName;
QFile resFile(resourcePath);
if (resFile.copy(destPath))
{
qDebug() << "[Setup] Copied default config:" << fileName;
}
else
{
qWarning() << "[Setup] Failed to copy:" << resourcePath << "to" << destPath;
}
}
}
else
{
qDebug() << "[Setup] User syntax directory already exists. Skipping first-run config.";
}
}
std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createSyntaxHighlighter(const QString &extension, QTextDocument *doc)
{
QString baseDir;
if (qEnvironmentVariableIsSet("CONFIG_DIR"))
{
baseDir = qEnvironmentVariable("CONFIG_DIR");
}
else
{
QString userSyntaxDir = QDir::homePath() + "/.config/codeastra/syntax";
if (QDir(userSyntaxDir).exists())
{
baseDir = userSyntaxDir;
}
else
{
baseDir = "config";
}
}
#ifdef DEBUG
qDebug() << "[SyntaxManager] Using config directory:" << baseDir;
#endif
QDir syntaxDir(baseDir);
QStringList yamlFilePath = syntaxDir.entryList({"*.yaml", "*.yml"}, QDir::Files);
std::vector<YAML::Node> config;
// Iterate over all YAML files and store their contents as separate nodes
for (const QString &fileName : yamlFilePath)
{
QFile file(syntaxDir.filePath(fileName));
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
YAML::Node fileConfig = YAML::Load(file.readAll().toStdString());
file.close();
#ifdef DEBUG
qDebug() << "[SyntaxManager] Loaded config for extension:" << extension << "from:" << file.fileName();
#endif
config.push_back(fileConfig);
}
else
{
qWarning() << "[SyntaxManager] Failed to open syntax config for extension:" << extension << "at:" << yamlFilePath;
return nullptr;
}
}
return createHighlighter(doc, config, extension);
}
std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createHighlighter(QTextDocument *doc, const std::vector<YAML::Node> &config, const QString &extension)
{
#ifdef DEBUG
qDebug() << "[SyntaxManager] Creating highlighter for extension:" << extension;
#endif
for (const auto &node : config)
{
if (node["extensions"])
{
for (const auto &ext : node["extensions"])
{
std::string extensionInConfig = ext.as<std::string>();
if (extensionInConfig == extension.toStdString())
{
return std::make_unique<Syntax>(doc, node);
}
}
}
else
{
qDebug() << "[SyntaxManager] No extensions key in YAML config.";
}
}
#ifdef DEBUG
qDebug() << "[SyntaxManager] No matching highlighter found for extension:" << extension;
#endif
return nullptr;
}