-
Notifications
You must be signed in to change notification settings - Fork 6
Refactor Syntax Highlighting Config #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
9e5033a
c73f65b
61f015d
b314cdc
390cec7
b455dd7
83db2f3
30fb09e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,4 +32,3 @@ find_package(yaml-cpp REQUIRED CONFIG) | |
| # Add subdirectories | ||
| add_subdirectory(src) | ||
| add_subdirectory(tests) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| extensions: [tsx, ts, js] | ||
|
|
||
| keywords: | ||
| comment: | ||
| - regex: "//.*" | ||
| color: "#6A9955" # Green | ||
| - regex: "/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/" | ||
| color: "#6A9955" # Green | ||
|
|
||
| string: | ||
| - regex: "'(?:\\\\.|[^'\\\\])*'" | ||
| color: "#CE9178" # Reddish | ||
| - regex: "\"(?:\\\\.|[^\"\\\\])*\"" | ||
| color: "#CE9178" # Reddish | ||
| - regex: "`(?:\\\\.|[^`\\\\])*`" | ||
| color: "#DCDCAA" # Yellow for template literals | ||
|
|
||
| number: | ||
| - regex: "\\b\\d+(\\.\\d+)?\\b" | ||
| color: "#B5CEA8" # Light green | ||
|
|
||
| keyword: | ||
| - regex: "\\b(?:let|const|var|function|class|interface|extends|implements|public|private|protected|static|new|return|if|else|for|while|do|switch|case|break|continue|try|catch|finally|throw|typeof|instanceof|in|of|void|async|await|import|from|export|default|as|enum|type|declare|namespace|abstract|readonly|get|set|yield|with|super|this)\\b" | ||
| color: "#569CD6" # Blue | ||
|
|
||
| type: | ||
| - regex: "\\b(?:string|number|boolean|any|unknown|never|void|object|undefined|null|bigint|symbol)\\b" | ||
| color: "#4EC9B0" # Cyan | ||
|
|
||
| boolean: | ||
| - regex: "\\b(?:true|false)\\b" | ||
| color: "#569CD6" # Blue | ||
|
|
||
| operator: | ||
| - regex: "\\+|-|\\*|\\/|=|==|===|!=|!==|<|>|<=|>=|\\?|:|\\.|,|\\||&|\\^|~|!" | ||
| color: "#D4D4D4" # Light gray | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,44 +5,90 @@ | |
| #include <QFile> | ||
| #include <QDebug> | ||
|
|
||
| std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createSyntaxHighlighter(const QString &extension, QTextDocument *doc) | ||
| void SyntaxManager::initializeUserSyntaxConfig() | ||
| { | ||
| QString configPath = qgetenv("CONFIG_DIR"); | ||
| if (configPath.isEmpty()) | ||
| QString userSyntaxDir = QDir::homePath() + "/.config/codeastra/syntax"; | ||
| QDir dir(userSyntaxDir); | ||
|
Comment on lines
-10
to
+11
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we still provide a way to change the default config location? It could be a good idea to keep a universal location for these files for simplicity (and security) reasons as is done here now.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is the universal location. I took the same example as Neo Vim, where the config is located at |
||
|
|
||
| if (!dir.exists()) | ||
| { | ||
| configPath = "config"; | ||
| } | ||
| 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); | ||
|
|
||
| QDir syntaxDir(configPath); | ||
| 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."; | ||
| } | ||
| } | ||
|
|
||
| QStringList yamlFiles = syntaxDir.entryList({"*.yaml", "*.yml"}, QDir::Files); | ||
| qDebug() << "Directory being scanned: " << syntaxDir.absolutePath(); | ||
| std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createSyntaxHighlighter(const QString &extension, QTextDocument *doc) | ||
| { | ||
| QString baseDir; | ||
|
|
||
| if (syntaxDir.exists()) | ||
| if (qEnvironmentVariableIsSet("CONFIG_DIR")) | ||
| { | ||
| qDebug() << "Directory exists."; | ||
| baseDir = qEnvironmentVariable("CONFIG_DIR"); | ||
| } | ||
| else | ||
| { | ||
| qDebug() << "Directory does not exist."; | ||
| QString userSyntaxDir = QDir::homePath() + "/.config/codeastra/syntax"; | ||
| if (QDir(userSyntaxDir).exists()) | ||
| { | ||
| baseDir = userSyntaxDir; | ||
| } | ||
| else | ||
| { | ||
| baseDir = "config"; | ||
| } | ||
| } | ||
|
|
||
| // qDebug() << "[SyntaxManager] Using config directory:" << baseDir; | ||
|
|
||
| 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 : yamlFiles) | ||
| 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(); | ||
| qDebug() << "Loaded YAML from: " << file.fileName(); | ||
| // qDebug() << "[SyntaxManager] Loaded config for extension:" << extension << "from:" << file.fileName(); | ||
chrisdedman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| config.push_back(fileConfig); | ||
| } | ||
| else | ||
| { | ||
| qDebug() << "Failed to open file: " << file.fileName(); | ||
| qWarning() << "[SyntaxManager] Failed to open syntax config for extension:" << extension << "at:" << yamlFilePath; | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -51,7 +97,7 @@ std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createSyntaxHighlighter(const | |
|
|
||
| std::unique_ptr<QSyntaxHighlighter> SyntaxManager::createHighlighter(QTextDocument *doc, const std::vector<YAML::Node> &config, const QString &extension) | ||
| { | ||
| qDebug() << "Creating highlighter for extension:" << extension; | ||
| // qDebug() << "[SyntaxManager] Creating highlighter for extension:" << extension; | ||
chrisdedman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (const auto &node : config) | ||
| { | ||
| if (node["extensions"]) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.