add syntax highlighting (WIP)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user