55#include < QFile>
66#include < QDebug>
77
8- std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createSyntaxHighlighter ( const QString &extension, QTextDocument *doc )
8+ void SyntaxManager::initializeUserSyntaxConfig ( )
99{
10- QString configPath = qgetenv (" CONFIG_DIR" );
11- if (configPath.isEmpty ())
10+ QString userSyntaxDir = QDir::homePath () + " /.config/codeastra/syntax" ;
11+ QDir dir (userSyntaxDir);
12+
13+ if (!dir.exists ())
1214 {
13- configPath = " config" ;
14- }
15+ qDebug () << " [Setup] First run detected. Creating syntax config directory:" << userSyntaxDir;
16+
17+ if (!dir.mkpath (" ." ))
18+ {
19+ qWarning () << " [Setup] Failed to create user syntax config directory:" << userSyntaxDir;
20+ return ;
21+ }
22+
23+ // List of default syntax files to copy
24+ QDir defaultDir (" :/resources/syntax/" );
25+ QStringList defaultSyntaxFiles = defaultDir.entryList ({" *.yaml" , " *.yml" }, QDir::Files);
1526
16- QDir syntaxDir (configPath);
27+ for (const QString &fileName : defaultSyntaxFiles)
28+ {
29+ QString resourcePath = " :/resources/syntax/" + fileName;
30+ QString destPath = userSyntaxDir + " /" + fileName;
31+
32+ QFile resFile (resourcePath);
33+ if (resFile.copy (destPath))
34+ {
35+ qDebug () << " [Setup] Copied default config:" << fileName;
36+ }
37+ else
38+ {
39+ qWarning () << " [Setup] Failed to copy:" << resourcePath << " to" << destPath;
40+ }
41+ }
42+ }
43+ else
44+ {
45+ qDebug () << " [Setup] User syntax directory already exists. Skipping first-run config." ;
46+ }
47+ }
1748
18- QStringList yamlFiles = syntaxDir.entryList ({" *.yaml" , " *.yml" }, QDir::Files);
19- qDebug () << " Directory being scanned: " << syntaxDir.absolutePath ();
49+ std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createSyntaxHighlighter (const QString &extension, QTextDocument *doc)
50+ {
51+ QString baseDir;
2052
21- if (syntaxDir. exists ( ))
53+ if (qEnvironmentVariableIsSet ( " CONFIG_DIR " ))
2254 {
23- qDebug () << " Directory exists. " ;
55+ baseDir = qEnvironmentVariable ( " CONFIG_DIR " ) ;
2456 }
2557 else
2658 {
27- qDebug () << " Directory does not exist." ;
59+ QString userSyntaxDir = QDir::homePath () + " /.config/codeastra/syntax" ;
60+ if (QDir (userSyntaxDir).exists ())
61+ {
62+ baseDir = userSyntaxDir;
63+ }
64+ else
65+ {
66+ baseDir = " config" ;
67+ }
2868 }
2969
70+ // qDebug() << "[SyntaxManager] Using config directory:" << baseDir;
71+
72+ QDir syntaxDir (baseDir);
73+ QStringList yamlFilePath = syntaxDir.entryList ({" *.yaml" , " *.yml" }, QDir::Files);
74+
3075 std::vector<YAML::Node> config;
3176 // Iterate over all YAML files and store their contents as separate nodes
32- for (const QString &fileName : yamlFiles )
77+ for (const QString &fileName : yamlFilePath )
3378 {
3479 QFile file (syntaxDir.filePath (fileName));
3580 if (file.open (QIODevice::ReadOnly | QIODevice::Text))
3681 {
3782 YAML::Node fileConfig = YAML::Load (file.readAll ().toStdString ());
3883 file.close ();
39- qDebug () << " Loaded YAML from: " << file.fileName ();
84+ // qDebug() << "[SyntaxManager] Loaded config for extension:" << extension << "from: " << file.fileName();
4085
4186 config.push_back (fileConfig);
4287 }
4388 else
4489 {
45- qDebug () << " Failed to open file: " << file.fileName ();
90+ qWarning () << " [SyntaxManager] Failed to open syntax config for extension:" << extension << " at:" << yamlFilePath;
91+ return nullptr ;
4692 }
4793 }
4894
@@ -51,7 +97,7 @@ std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createSyntaxHighlighter(const
5197
5298std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createHighlighter (QTextDocument *doc, const std::vector<YAML::Node> &config, const QString &extension)
5399{
54- qDebug () << " Creating highlighter for extension:" << extension;
100+ // qDebug() << "[SyntaxManager] Creating highlighter for extension:" << extension;
55101 for (const auto &node : config)
56102 {
57103 if (node[" extensions" ])
0 commit comments