yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
text_editor.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_MODULES_TEXT_EDITOR_H
2#define YAZE_APP_GUI_MODULES_TEXT_EDITOR_H
3
4// Originally from ImGuiColorTextEdit/TextEditor.h
5
6#include <array>
7#include <map>
8#include <memory>
9#include <regex>
10#include <string>
11#include <unordered_map>
12#include <unordered_set>
13#include <vector>
14
15#include "imgui.h"
16
18 public:
43
44 enum class SelectionMode { Normal, Word, Line };
45
46 struct Breakpoint {
47 int mLine;
49 std::string mCondition;
50
51 Breakpoint() : mLine(-1), mEnabled(false) {}
52 };
53
54 // Represents a character coordinate from the user's point of view,
55 // i. e. consider an uniform grid (assuming fixed-width font) on the
56 // screen as it is rendered, and each cell has its own coordinate, starting
57 // from 0. Tabs are counted as [1..mTabSize] count empty spaces, depending on
58 // how many space is necessary to reach the next tab stop.
59 // For example, coordinate (1, 5) represents the character 'B' in a line
60 // "\tABC", when mTabSize = 4, because it is rendered as " ABC" on the
61 // screen.
62 struct Coordinates {
65 Coordinates(int aLine, int aColumn) : mLine(aLine), mColumn(aColumn) {
66 assert(aLine >= 0);
67 assert(aColumn >= 0);
68 }
70 static Coordinates invalid(-1, -1);
71 return invalid;
72 }
73
74 bool operator==(const Coordinates& o) const {
75 return mLine == o.mLine && mColumn == o.mColumn;
76 }
77
78 bool operator!=(const Coordinates& o) const {
79 return mLine != o.mLine || mColumn != o.mColumn;
80 }
81
82 bool operator<(const Coordinates& o) const {
83 if (mLine != o.mLine) return mLine < o.mLine;
84 return mColumn < o.mColumn;
85 }
86
87 bool operator>(const Coordinates& o) const {
88 if (mLine != o.mLine) return mLine > o.mLine;
89 return mColumn > o.mColumn;
90 }
91
92 bool operator<=(const Coordinates& o) const {
93 if (mLine != o.mLine) return mLine < o.mLine;
94 return mColumn <= o.mColumn;
95 }
96
97 bool operator>=(const Coordinates& o) const {
98 if (mLine != o.mLine) return mLine > o.mLine;
99 return mColumn >= o.mColumn;
100 }
101 };
102
107
108 typedef std::string String;
109 typedef std::unordered_map<std::string, Identifier> Identifiers;
110 typedef std::unordered_set<std::string> Keywords;
111 typedef std::map<int, std::string> ErrorMarkers;
112 typedef std::unordered_set<int> Breakpoints;
113 typedef std::array<ImU32, (unsigned)PaletteIndex::Max> Palette;
114 typedef uint8_t Char;
115
116 struct Glyph {
119 bool mComment : 1;
122
123 Glyph(Char aChar, PaletteIndex aColorIndex)
124 : mChar(aChar),
125 mColorIndex(aColorIndex),
126 mComment(false),
127 mMultiLineComment(false),
128 mPreprocessor(false) {}
129 };
130
131 typedef std::vector<Glyph> Line;
132 typedef std::vector<Line> Lines;
133
135 typedef std::pair<std::string, PaletteIndex> TokenRegexString;
136 typedef std::vector<TokenRegexString> TokenRegexStrings;
137 typedef bool (*TokenizeCallback)(const char* in_begin, const char* in_end,
138 const char*& out_begin,
139 const char*& out_end,
140 PaletteIndex& paletteIndex);
141
142 std::string mName;
149
151
153
155
157 : mPreprocChar('#'),
158 mAutoIndentation(true),
159 mTokenize(nullptr),
160 mCaseSensitive(true) {}
161
162 static const LanguageDefinition& CPlusPlus();
163 static const LanguageDefinition& HLSL();
164 static const LanguageDefinition& GLSL();
165 static const LanguageDefinition& C();
166 static const LanguageDefinition& SQL();
167 static const LanguageDefinition& AngelScript();
168 static const LanguageDefinition& Lua();
169 };
170
171 TextEditor();
172 ~TextEditor();
173
174 void SetLanguageDefinition(const LanguageDefinition& aLanguageDef);
178
179 const Palette& GetPalette() const { return mPaletteBase; }
180 void SetPalette(const Palette& aValue);
181
182 void SetErrorMarkers(const ErrorMarkers& aMarkers) {
183 mErrorMarkers = aMarkers;
184 }
185 void SetBreakpoints(const Breakpoints& aMarkers) { mBreakpoints = aMarkers; }
186
187 void Render(const char* aTitle, const ImVec2& aSize = ImVec2(),
188 bool aBorder = false);
189 void SetText(const std::string& aText);
190 std::string GetText() const;
191
192 void SetTextLines(const std::vector<std::string>& aLines);
193 std::vector<std::string> GetTextLines() const;
194
195 std::string GetSelectedText() const;
196 std::string GetCurrentLineText() const;
197
198 int GetTotalLines() const { return (int)mLines.size(); }
199 bool IsOverwrite() const { return mOverwrite; }
200
201 void SetReadOnly(bool aValue);
202 bool IsReadOnly() const { return mReadOnly; }
203 bool IsTextChanged() const { return mTextChanged; }
205
206 bool IsColorizerEnabled() const { return mColorizerEnabled; }
207 void SetColorizerEnable(bool aValue);
208
210 void SetCursorPosition(const Coordinates& aPosition);
211
212 inline void SetHandleMouseInputs(bool aValue) { mHandleMouseInputs = aValue; }
213 inline bool IsHandleMouseInputsEnabled() const {
215 }
216
217 inline void SetHandleKeyboardInputs(bool aValue) {
218 mHandleKeyboardInputs = aValue;
219 }
220 inline bool IsHandleKeyboardInputsEnabled() const {
222 }
223
224 inline void SetImGuiChildIgnored(bool aValue) { mIgnoreImGuiChild = aValue; }
225 inline bool IsImGuiChildIgnored() const { return mIgnoreImGuiChild; }
226
227 inline void SetShowWhitespaces(bool aValue) { mShowWhitespaces = aValue; }
228 inline bool IsShowingWhitespaces() const { return mShowWhitespaces; }
229
230 void SetTabSize(int aValue);
231 inline int GetTabSize() const { return mTabSize; }
232
233 void InsertText(const std::string& aValue);
234 void InsertText(const char* aValue);
235
236 void MoveUp(int aAmount = 1, bool aSelect = false);
237 void MoveDown(int aAmount = 1, bool aSelect = false);
238 void MoveLeft(int aAmount = 1, bool aSelect = false, bool aWordMode = false);
239 void MoveRight(int aAmount = 1, bool aSelect = false, bool aWordMode = false);
240 void MoveTop(bool aSelect = false);
241 void MoveBottom(bool aSelect = false);
242 void MoveHome(bool aSelect = false);
243 void MoveEnd(bool aSelect = false);
244
245 void SetSelectionStart(const Coordinates& aPosition);
246 void SetSelectionEnd(const Coordinates& aPosition);
247 void SetSelection(const Coordinates& aStart, const Coordinates& aEnd,
250 void SelectAll();
251 bool HasSelection() const;
252
253 void Copy();
254 void Cut();
255 void Paste();
256 void Delete();
257
258 bool CanUndo() const;
259 bool CanRedo() const;
260 void Undo(int aSteps = 1);
261 void Redo(int aSteps = 1);
262
263 static const Palette& GetDarkPalette();
264 static const Palette& GetLightPalette();
265 static const Palette& GetRetroBluePalette();
266
267 private:
268 typedef std::vector<std::pair<std::regex, PaletteIndex>> RegexList;
269
275
277 public:
280
281 UndoRecord(const std::string& aAdded,
282 const TextEditor::Coordinates aAddedStart,
283 const TextEditor::Coordinates aAddedEnd,
284
285 const std::string& aRemoved,
286 const TextEditor::Coordinates aRemovedStart,
287 const TextEditor::Coordinates aRemovedEnd,
288
291
292 void Undo(TextEditor* aEditor);
293 void Redo(TextEditor* aEditor);
294
295 std::string mAdded;
298
299 std::string mRemoved;
302
305 };
306
307 typedef std::vector<UndoRecord> UndoBuffer;
308
309 void ProcessInputs();
310 void Colorize(int aFromLine = 0, int aCount = -1);
311 void ColorizeRange(int aFromLine = 0, int aToLine = 0);
312 void ColorizeInternal();
313 float TextDistanceToLineStart(const Coordinates& aFrom) const;
314 void EnsureCursorVisible();
315 int GetPageSize() const;
316 std::string GetText(const Coordinates& aStart, const Coordinates& aEnd) const;
318 Coordinates SanitizeCoordinates(const Coordinates& aValue) const;
319 void Advance(Coordinates& aCoordinates) const;
320 void DeleteRange(const Coordinates& aStart, const Coordinates& aEnd);
321 int InsertTextAt(Coordinates& aWhere, const char* aValue);
322 void AddUndo(UndoRecord& aValue);
323 Coordinates ScreenPosToCoordinates(const ImVec2& aPosition) const;
324 Coordinates FindWordStart(const Coordinates& aFrom) const;
325 Coordinates FindWordEnd(const Coordinates& aFrom) const;
326 Coordinates FindNextWord(const Coordinates& aFrom) const;
327 int GetCharacterIndex(const Coordinates& aCoordinates) const;
328 int GetCharacterColumn(int aLine, int aIndex) const;
329 int GetLineCharacterCount(int aLine) const;
330 int GetLineMaxColumn(int aLine) const;
331 bool IsOnWordBoundary(const Coordinates& aAt) const;
332 void RemoveLine(int aStart, int aEnd);
333 void RemoveLine(int aIndex);
334 Line& InsertLine(int aIndex);
335 void EnterCharacter(ImWchar aChar, bool aShift);
336 void Backspace();
337 void DeleteSelection();
338 std::string GetWordUnderCursor() const;
339 std::string GetWordAt(const Coordinates& aCoords) const;
340 ImU32 GetGlyphColor(const Glyph& aGlyph) const;
341
343 void HandleMouseInputs();
344 void Render();
345
351
360 float mTextStart; // position (in pixels) where a code line starts relative
361 // to the left of the TextEditor.
370
375
381 std::string mLineBuffer;
382 uint64_t mStartTime;
383
385};
386
387#endif // YAZE_APP_GUI_MODULES_TEXT_EDITOR_H
void Redo(TextEditor *aEditor)
Coordinates mRemovedStart
void Undo(TextEditor *aEditor)
void HandleKeyboardInputs()
bool mScrollToCursor
void MoveBottom(bool aSelect=false)
int GetTabSize() const
bool mScrollToTop
void InsertText(const std::string &aValue)
ImU32 GetGlyphColor(const Glyph &aGlyph) const
bool mHandleMouseInputs
std::unordered_set< std::string > Keywords
bool IsCursorPositionChanged() const
bool IsShowingWhitespaces() const
std::vector< std::string > GetTextLines() const
float mLastClick
std::vector< Line > Lines
void SelectWordUnderCursor()
uint64_t mStartTime
UndoBuffer mUndoBuffer
static const Palette & GetLightPalette()
void HandleMouseInputs()
void MoveLeft(int aAmount=1, bool aSelect=false, bool aWordMode=false)
bool mOverwrite
void SetColorizerEnable(bool aValue)
void MoveHome(bool aSelect=false)
int GetLineMaxColumn(int aLine) const
ImVec2 mCharAdvance
void SetSelectionEnd(const Coordinates &aPosition)
static const Palette & GetDarkPalette()
void MoveEnd(bool aSelect=false)
std::string GetWordUnderCursor() const
Breakpoints mBreakpoints
int mColorRangeMax
static const Palette & GetRetroBluePalette()
int GetCharacterIndex(const Coordinates &aCoordinates) const
float mTextStart
Coordinates GetCursorPosition() const
float mLineSpacing
void EnsureCursorVisible()
void SetShowWhitespaces(bool aValue)
bool HasSelection() const
Coordinates FindWordStart(const Coordinates &aFrom) const
void Colorize(int aFromLine=0, int aCount=-1)
bool IsTextChanged() const
std::string GetWordAt(const Coordinates &aCoords) const
Coordinates FindNextWord(const Coordinates &aFrom) const
std::unordered_map< std::string, Identifier > Identifiers
void Render()
void SetReadOnly(bool aValue)
void Backspace()
std::string GetText() const
bool IsReadOnly() const
int mColorRangeMin
int GetCharacterColumn(int aLine, int aIndex) const
void SetSelectionStart(const Coordinates &aPosition)
void MoveTop(bool aSelect=false)
int GetTotalLines() const
RegexList mRegexList
void SetHandleKeyboardInputs(bool aValue)
bool IsOverwrite() const
std::string GetCurrentLineText() const
bool IsHandleMouseInputsEnabled() const
bool IsColorizerEnabled() const
const Palette & GetPalette() const
bool mColorizerEnabled
void AddUndo(UndoRecord &aValue)
void MoveUp(int aAmount=1, bool aSelect=false)
std::vector< Glyph > Line
void Undo(int aSteps=1)
Coordinates FindWordEnd(const Coordinates &aFrom) const
void DeleteSelection()
void SetPalette(const Palette &aValue)
std::map< int, std::string > ErrorMarkers
std::vector< std::pair< std::regex, PaletteIndex > > RegexList
void SetImGuiChildIgnored(bool aValue)
void RemoveLine(int aStart, int aEnd)
Palette mPalette
void SetBreakpoints(const Breakpoints &aMarkers)
void ProcessInputs()
void SetText(const std::string &aText)
std::string String
void SetTextLines(const std::vector< std::string > &aLines)
float TextDistanceToLineStart(const Coordinates &aFrom) const
Coordinates SanitizeCoordinates(const Coordinates &aValue) const
int GetPageSize() const
ErrorMarkers mErrorMarkers
bool mTextChanged
void ColorizeRange(int aFromLine=0, int aToLine=0)
EditorState mState
void SelectAll()
bool IsImGuiChildIgnored() const
std::string mLineBuffer
Line & InsertLine(int aIndex)
Coordinates mInteractiveEnd
bool mHandleKeyboardInputs
void EnterCharacter(ImWchar aChar, bool aShift)
Palette mPaletteBase
void ColorizeInternal()
void MoveDown(int aAmount=1, bool aSelect=false)
SelectionMode mSelectionMode
bool mWithinRender
Coordinates mInteractiveStart
void Advance(Coordinates &aCoordinates) const
bool mShowWhitespaces
void SetErrorMarkers(const ErrorMarkers &aMarkers)
bool mCursorPositionChanged
bool mIgnoreImGuiChild
bool IsOnWordBoundary(const Coordinates &aAt) const
Coordinates GetActualCursorCoordinates() const
void Redo(int aSteps=1)
const LanguageDefinition & GetLanguageDefinition() const
bool CanRedo() const
void SetCursorPosition(const Coordinates &aPosition)
std::unordered_set< int > Breakpoints
std::string GetSelectedText() const
int GetLineCharacterCount(int aLine) const
Coordinates ScreenPosToCoordinates(const ImVec2 &aPosition) const
void SetTabSize(int aValue)
void DeleteRange(const Coordinates &aStart, const Coordinates &aEnd)
bool mCheckComments
void SetHandleMouseInputs(bool aValue)
std::array< ImU32,(unsigned) PaletteIndex::Max > Palette
uint8_t Char
bool IsHandleKeyboardInputsEnabled() const
bool CanUndo() const
int InsertTextAt(Coordinates &aWhere, const char *aValue)
void SetSelection(const Coordinates &aStart, const Coordinates &aEnd, SelectionMode aMode=SelectionMode::Normal)
LanguageDefinition mLanguageDefinition
std::vector< UndoRecord > UndoBuffer
void SetLanguageDefinition(const LanguageDefinition &aLanguageDef)
void MoveRight(int aAmount=1, bool aSelect=false, bool aWordMode=false)
bool operator<=(const Coordinates &o) const
Definition text_editor.h:92
static Coordinates Invalid()
Definition text_editor.h:69
bool operator==(const Coordinates &o) const
Definition text_editor.h:74
bool operator<(const Coordinates &o) const
Definition text_editor.h:82
bool operator>(const Coordinates &o) const
Definition text_editor.h:87
Coordinates(int aLine, int aColumn)
Definition text_editor.h:65
bool operator>=(const Coordinates &o) const
Definition text_editor.h:97
bool operator!=(const Coordinates &o) const
Definition text_editor.h:78
Coordinates mSelectionStart
Coordinates mCursorPosition
PaletteIndex mColorIndex
Glyph(Char aChar, PaletteIndex aColorIndex)
static const LanguageDefinition & SQL()
TokenRegexStrings mTokenRegexStrings
static const LanguageDefinition & Lua()
static const LanguageDefinition & C()
static const LanguageDefinition & GLSL()
bool(* TokenizeCallback)(const char *in_begin, const char *in_end, const char *&out_begin, const char *&out_end, PaletteIndex &paletteIndex)
std::vector< TokenRegexString > TokenRegexStrings
static const LanguageDefinition & AngelScript()
static const LanguageDefinition & CPlusPlus()
std::pair< std::string, PaletteIndex > TokenRegexString
static const LanguageDefinition & HLSL()