Loading...
Searching...
No Matches
CodeEditor.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_CODEEDITOR_H
5#define MALENA_CODEEDITOR_H
6
7#pragma once
8
12#include <cstddef>
13#include <memory>
14#include <string>
15#include <unordered_set>
16#include <vector>
17
18namespace ml
19{
20 // ── Languages ───────────────────────────────────────────────────────────────
22
23 // ── SyntaxToken ───────────────────────────────────────────────────────────────
26 {
27 std::size_t start = 0;
28 std::size_t end = 0;
30 };
31
32 // ── SyntaxHighlighter ─────────────────────────────────────────────────────────
38 {
39 public:
40 virtual ~SyntaxHighlighter() = default;
43 [[nodiscard]] virtual std::vector<SyntaxToken> tokenize(const std::string& text) const = 0;
44 };
45
46 // ── BasicSyntaxHighlighter ──────────────────────────────────────────────────
55 {
56 public:
58
60 [[nodiscard]] CodeLanguage language() const { return _lang; }
61
62 [[nodiscard]] std::vector<SyntaxToken> tokenize(const std::string& text) const override;
63
64 // Theme colors (override freely).
65 sf::Color keywordColor { 197, 134, 192 }; // purple
66 sf::Color stringColor { 206, 145, 120 }; // orange
67 sf::Color commentColor { 106, 153, 85 }; // green
68 sf::Color numberColor { 181, 206, 168 }; // light green
69
70 private:
71 CodeLanguage _lang;
72 std::unordered_set<std::string> _keywords;
73 bool _hashComments = false; // '#' line comments
74 };
75
76 // ── CodeEditor ──────────────────────────────────────────────────────────────
95 {
96 public:
98 static const sf::Font& monospaceFont();
99
100 explicit CodeEditor(const sf::Font& font = monospaceFont());
101
105
107 void setHighlighter(std::shared_ptr<SyntaxHighlighter> highlighter);
108
109 protected:
110 void onRebuildComplete() override;
111
112 private:
113 void applyHighlighting();
114
115 std::shared_ptr<SyntaxHighlighter> _highlighter;
116 BasicSyntaxHighlighter* _basic = nullptr; // non-owning alias when built-in
117 std::string _lastHighlighted;
118 bool _inHighlight = false;
119 };
120
121} // namespace ml
122
123#endif // MALENA_CODEEDITOR_H
Built-in generic highlighter for C-like and scripting languages.
Definition CodeEditor.h:55
std::vector< SyntaxToken > tokenize(const std::string &text) const override
CodeLanguage language() const
Definition CodeEditor.h:60
BasicSyntaxHighlighter(CodeLanguage lang=CodeLanguage::Cpp)
void setLanguage(CodeLanguage lang)
void onRebuildComplete() override
static const sf::Font & monospaceFont()
void setLanguage(CodeLanguage lang)
void setHighlighter(std::shared_ptr< SyntaxHighlighter > highlighter)
CodeEditor(const sf::Font &font=monospaceFont())
Pure tokenizer: maps source text to colored spans. No linting, no diagnostics — color only....
Definition CodeEditor.h:38
virtual std::vector< SyntaxToken > tokenize(const std::string &text) const =0
virtual ~SyntaxHighlighter()=default
TextArea(const sf::Font &font=FontManager<>::getDefault())
#define MALENA_API
Definition Component.h:22
CodeLanguage
Definition CodeEditor.h:21
const sf::Font * font
std::size_t start
Definition CodeEditor.h:27
sf::Color color
Definition CodeEditor.h:29
std::size_t end
Definition CodeEditor.h:28