yaze 0.3.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/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)
84 return mLine < o.mLine;
85 return mColumn < o.mColumn;
86 }
87
88 bool operator>(const Coordinates& o) const {
89 if (mLine != o.mLine)
90 return mLine > o.mLine;
91 return mColumn > o.mColumn;
92 }
93
94 bool operator<=(const Coordinates& o) const {
95 if (mLine != o.mLine)
96 return mLine < o.mLine;
97 return mColumn <= o.mColumn;
98 }
99
100 bool operator>=(const Coordinates& o) const {
101 if (mLine != o.mLine)
102 return mLine > o.mLine;
103 return mColumn >= o.mColumn;
104 }
105 };
106
111
112 typedef std::string String;
113 typedef std::unordered_map<std::string, Identifier> Identifiers;
114 typedef std::unordered_set<std::string> Keywords;
115 typedef std::map<int, std::string> ErrorMarkers;
116 typedef std::unordered_set<int> Breakpoints;
117 typedef std::array<ImU32, (unsigned)PaletteIndex::Max> Palette;
118 typedef uint8_t Char;
119
120 struct Glyph {
123 bool mComment : 1;
126
127 Glyph(Char aChar, PaletteIndex aColorIndex)
128 : mChar(aChar),
129 mColorIndex(aColorIndex),
130 mComment(false),
131 mMultiLineComment(false),
132 mPreprocessor(false) {}
133 };
134
135 typedef std::vector<Glyph> Line;
136 typedef std::vector<Line> Lines;
137
139 typedef std::pair<std::string, PaletteIndex> TokenRegexString;
140 typedef std::vector<TokenRegexString> TokenRegexStrings;
141 typedef bool (*TokenizeCallback)(const char* in_begin, const char* in_end,
142 const char*& out_begin,
143 const char*& out_end,
144 PaletteIndex& paletteIndex);
145
146 std::string mName;
153
155
157
159
161 : mPreprocChar('#'),
162 mAutoIndentation(true),
163 mTokenize(nullptr),
164 mCaseSensitive(true) {}
165
166 static const LanguageDefinition& CPlusPlus();
167 static const LanguageDefinition& HLSL();
168 static const LanguageDefinition& GLSL();
169 static const LanguageDefinition& C();
170 static const LanguageDefinition& SQL();
171 static const LanguageDefinition& AngelScript();
172 static const LanguageDefinition& Lua();
173 };
174
175 TextEditor();
176 ~TextEditor();
177
178 void SetLanguageDefinition(const LanguageDefinition& aLanguageDef);
182
183 const Palette& GetPalette() const { return mPaletteBase; }
184 void SetPalette(const Palette& aValue);
185
186 void SetErrorMarkers(const ErrorMarkers& aMarkers) {
187 mErrorMarkers = aMarkers;
188 }
189 void SetBreakpoints(const Breakpoints& aMarkers) { mBreakpoints = aMarkers; }
190
191 void Render(const char* aTitle, const ImVec2& aSize = ImVec2(),
192 bool aBorder = false);
193 void SetText(const std::string& aText);
194 std::string GetText() const;
195
196 void SetTextLines(const std::vector<std::string>& aLines);
197 std::vector<std::string> GetTextLines() const;
198
199 std::string GetSelectedText() const;
200 std::string GetCurrentLineText() const;
201
202 int GetTotalLines() const { return (int)mLines.size(); }
203 bool IsOverwrite() const { return mOverwrite; }
204
205 void SetReadOnly(bool aValue);
206 bool IsReadOnly() const { return mReadOnly; }
207 bool IsTextChanged() const { return mTextChanged; }
209
210 bool IsColorizerEnabled() const { return mColorizerEnabled; }
211 void SetColorizerEnable(bool aValue);
212
214 void SetCursorPosition(const Coordinates& aPosition);
215
216 inline void SetHandleMouseInputs(bool aValue) { mHandleMouseInputs = aValue; }
217 inline bool IsHandleMouseInputsEnabled() const {
219 }
220
221 inline void SetHandleKeyboardInputs(bool aValue) {
222 mHandleKeyboardInputs = aValue;
223 }
224 inline bool IsHandleKeyboardInputsEnabled() const {
226 }
227
228 inline void SetImGuiChildIgnored(bool aValue) { mIgnoreImGuiChild = aValue; }
229 inline bool IsImGuiChildIgnored() const { return mIgnoreImGuiChild; }
230
231 inline void SetShowWhitespaces(bool aValue) { mShowWhitespaces = aValue; }
232 inline bool IsShowingWhitespaces() const { return mShowWhitespaces; }
233
234 void SetTabSize(int aValue);
235 inline int GetTabSize() const { return mTabSize; }
236
237 void InsertText(const std::string& aValue);
238 void InsertText(const char* aValue);
239
240 void MoveUp(int aAmount = 1, bool aSelect = false);
241 void MoveDown(int aAmount = 1, bool aSelect = false);
242 void MoveLeft(int aAmount = 1, bool aSelect = false, bool aWordMode = false);
243 void MoveRight(int aAmount = 1, bool aSelect = false, bool aWordMode = false);
244 void MoveTop(bool aSelect = false);
245 void MoveBottom(bool aSelect = false);
246 void MoveHome(bool aSelect = false);
247 void MoveEnd(bool aSelect = false);
248
249 void SetSelectionStart(const Coordinates& aPosition);
250 void SetSelectionEnd(const Coordinates& aPosition);
251 void SetSelection(const Coordinates& aStart, const Coordinates& aEnd,
254 void SelectAll();
255 bool HasSelection() const;
256
257 void Copy();
258 void Cut();
259 void Paste();
260 void Delete();
261
262 bool CanUndo() const;
263 bool CanRedo() const;
264 void Undo(int aSteps = 1);
265 void Redo(int aSteps = 1);
266
267 static const Palette& GetDarkPalette();
268 static const Palette& GetLightPalette();
269 static const Palette& GetRetroBluePalette();
270
271 private:
272 typedef std::vector<std::pair<std::regex, PaletteIndex>> RegexList;
273
279
281 public:
284
285 UndoRecord(const std::string& aAdded,
286 const TextEditor::Coordinates aAddedStart,
287 const TextEditor::Coordinates aAddedEnd,
288
289 const std::string& aRemoved,
290 const TextEditor::Coordinates aRemovedStart,
291 const TextEditor::Coordinates aRemovedEnd,
292
295
296 void Undo(TextEditor* aEditor);
297 void Redo(TextEditor* aEditor);
298
299 std::string mAdded;
302
303 std::string mRemoved;
306
309 };
310
311 typedef std::vector<UndoRecord> UndoBuffer;
312
313 void ProcessInputs();
314 void Colorize(int aFromLine = 0, int aCount = -1);
315 void ColorizeRange(int aFromLine = 0, int aToLine = 0);
316 void ColorizeInternal();
317 float TextDistanceToLineStart(const Coordinates& aFrom) const;
318 void EnsureCursorVisible();
319 int GetPageSize() const;
320 std::string GetText(const Coordinates& aStart, const Coordinates& aEnd) const;
322 Coordinates SanitizeCoordinates(const Coordinates& aValue) const;
323 void Advance(Coordinates& aCoordinates) const;
324 void DeleteRange(const Coordinates& aStart, const Coordinates& aEnd);
325 int InsertTextAt(Coordinates& aWhere, const char* aValue);
326 void AddUndo(UndoRecord& aValue);
327 Coordinates ScreenPosToCoordinates(const ImVec2& aPosition) const;
328 Coordinates FindWordStart(const Coordinates& aFrom) const;
329 Coordinates FindWordEnd(const Coordinates& aFrom) const;
330 Coordinates FindNextWord(const Coordinates& aFrom) const;
331 int GetCharacterIndex(const Coordinates& aCoordinates) const;
332 int GetCharacterColumn(int aLine, int aIndex) const;
333 int GetLineCharacterCount(int aLine) const;
334 int GetLineMaxColumn(int aLine) const;
335 bool IsOnWordBoundary(const Coordinates& aAt) const;
336 void RemoveLine(int aStart, int aEnd);
337 void RemoveLine(int aIndex);
338 Line& InsertLine(int aIndex);
339 void EnterCharacter(ImWchar aChar, bool aShift);
340 void Backspace();
341 void DeleteSelection();
342 std::string GetWordUnderCursor() const;
343 std::string GetWordAt(const Coordinates& aCoords) const;
344 ImU32 GetGlyphColor(const Glyph& aGlyph) const;
345
347 void HandleMouseInputs();
348 void Render();
349
355
364 float mTextStart; // position (in pixels) where a code line starts relative
365 // to the left of the TextEditor.
374
379
385 std::string mLineBuffer;
386 uint64_t mStartTime;
387
389};
390
391#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:94
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:88
Coordinates(int aLine, int aColumn)
Definition text_editor.h:65
bool operator>=(const Coordinates &o) const
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()