add syntax highlighting (WIP)

This commit is contained in:
MihailRis
2024-12-04 22:13:17 +03:00
parent ce1e9f76cf
commit ed3865964b
16 changed files with 168 additions and 43 deletions
+23 -2
View File
@@ -5,6 +5,7 @@
#include <algorithm>
#include "Label.hpp"
#include "devtools/syntax_highlighting.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/Font.hpp"
@@ -65,11 +66,10 @@ void TextBox::draw(const DrawContext* pctx, Assets* assets) {
lcoord.y -= 2;
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->setColor(glm::vec4(1.0f));
if (editable && int((Window::time() - caretLastMove) * 2) % 2 == 0) {
uint line = label->getLineByTextIndex(caret);
uint lcaret = caret - label->getTextLineOffset(line);
batch->setColor(glm::vec4(1.0f));
int width = font->calcWidth(input, lcaret);
batch->rect(lcoord.x + width, lcoord.y+label->getLineYOffset(line), 2, lineHeight);
}
@@ -529,10 +529,21 @@ void TextBox::stepDefaultUp(bool shiftPressed, bool breakSelection) {
}
}
void TextBox::refreshSyntax() {
if (!syntax.empty()) {
if (auto styles = devtools::syntax_highlight(
syntax, util::wstr2str_utf8(input)
)) {
label->setStyles(std::move(styles));
}
}
}
void TextBox::onInput() {
if (subconsumer) {
subconsumer(input);
}
refreshSyntax();
}
void TextBox::performEditingKeyboardEvents(keycode key) {
@@ -710,6 +721,7 @@ const std::wstring& TextBox::getText() const {
void TextBox::setText(const std::wstring& value) {
this->input = value;
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
refreshSyntax();
}
const std::wstring& TextBox::getPlaceholder() const {
@@ -789,3 +801,12 @@ void TextBox::setShowLineNumbers(bool flag) {
bool TextBox::isShowLineNumbers() const {
return showLineNumbers;
}
void TextBox::setSyntax(const std::string& lang) {
syntax = lang;
if (syntax.empty()) {
label->setStyles(nullptr);
} else {
refreshSyntax();
}
}