Skip to content

Commit 1676342

Browse files
authored
Various fixes for pattern editor (WerWolv#2561)
- Fix for vertical scroll bar being too far to the left. - Fix constructor not initializing from const char pointer properly - maxcolumn not being set for console text lines causing crashes on empty pattern evaluation - A replacement using replace all is now undone in one step. - Find/replace no longer need to have enter or return key to accept text. You can use arrows or shortcuts. - More efficient search replace implementation with plans to add even faster. - Tooltips added to find/replace window - Providers now save both horizontal and vertical scroll positions when switching to another one and restore them when switching back. This is independent to the cursor position which is also saved. - Pattern editor no longer takes focus when changing providers via a tab click. This has the effect that menus won't change by just clicking on a tab. - Small fixes and code refactoring.
1 parent 62732de commit 1676342

File tree

8 files changed

+352
-186
lines changed

8 files changed

+352
-186
lines changed

plugins/builtin/include/content/views/view_pattern_editor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ namespace hex::plugin::builtin {
153153
std::mutex m_logMutex;
154154

155155
PerProvider<ui::TextEditor::Coordinates> m_cursorPosition;
156+
PerProvider<ImVec2> m_scroll;
157+
PerProvider<ImVec2> m_consoleScroll;
156158

157159
PerProvider<ui::TextEditor::Coordinates> m_consoleCursorPosition;
158-
PerProvider<bool> m_cursorNeedsUpdate;
159-
PerProvider<bool> m_consoleCursorNeedsUpdate;
160160
PerProvider<ui::TextEditor::Range> m_selection;
161161
PerProvider<ui::TextEditor::Range> m_consoleSelection;
162162
PerProvider<size_t> m_consoleLongestLineLength;

plugins/builtin/romfs/lang/en_US.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,9 @@
10481048
"hex.builtin.view.pattern_data.virtual_files.no_virtual_files": "Visualize regions of data as a virtual folder structure by adding them using the hex::core::add_virtual_file function.",
10491049
"hex.builtin.view.pattern_editor.no_env_vars": "The content of Environment Variables created here can be accessed from Pattern scripts using the std::env function.",
10501050
"hex.builtin.view.pattern_editor.no_results": "no results",
1051+
"hex.builtin.view.pattern_editor.match_case_tooltip": "Match Case Alt-C",
1052+
"hex.builtin.view.pattern_editor.whole_word_tooltip": "Whole Word Alt-W",
1053+
"hex.builtin.view.pattern_editor.regex_tooltip": "Regex Alt-R",
10511054
"hex.builtin.view.pattern_editor.of": "of",
10521055
"hex.builtin.view.pattern_editor.open_pattern": "Open pattern",
10531056
"hex.builtin.view.pattern_editor.replace_hint": "Replace",

plugins/builtin/source/content/views/view_pattern_editor.cpp

Lines changed: 85 additions & 65 deletions
Large diffs are not rendered by default.

plugins/ui/include/ui/text_editor.hpp

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,23 @@ namespace hex::ui {
114114
Coordinates m_cursorPosition;
115115
};
116116

117+
class UndoRecord;
118+
class UndoAction;
119+
using UndoBuffer = std::vector<UndoAction>;
120+
using UndoRecords = std::vector<UndoRecord>;
121+
117122
class FindReplaceHandler {
118123
public:
119124
FindReplaceHandler();
120125
using Matches = std::vector<EditorState>;
121126
Matches &getMatches() { return m_matches; }
122-
bool findNext(TextEditor *editor);
127+
bool findNext(TextEditor *editor, u64 &byteIndex);
123128
u32 findMatch(TextEditor *editor, i32 index);
124129
bool replace(TextEditor *editor, bool right);
125130
bool replaceAll(TextEditor *editor);
126131
std::string &getFindWord() { return m_findWord; }
127132

128-
void setFindWord(TextEditor *editor, const std::string &findWord) {
129-
if (findWord != m_findWord) {
130-
findAllMatches(editor, findWord);
131-
m_findWord = findWord;
132-
}
133-
}
133+
void setFindWord(TextEditor *editor, const std::string &findWord);
134134

135135
std::string &getReplaceWord() { return m_replaceWord; }
136136
void setReplaceWord(const std::string &replaceWord) { m_replaceWord = replaceWord; }
@@ -139,37 +139,16 @@ namespace hex::ui {
139139
u32 findPosition(TextEditor *editor, Coordinates pos, bool isNext);
140140
bool getMatchCase() const { return m_matchCase; }
141141

142-
void setMatchCase(TextEditor *editor, bool matchCase) {
143-
if (matchCase != m_matchCase) {
144-
m_matchCase = matchCase;
145-
m_optionsChanged = true;
146-
findAllMatches(editor, m_findWord);
147-
}
148-
}
142+
void setMatchCase(TextEditor *editor, bool matchCase);
149143

150144
bool getWholeWord() const { return m_wholeWord; }
151-
void setWholeWord(TextEditor *editor, bool wholeWord) {
152-
if (wholeWord != m_wholeWord) {
153-
m_wholeWord = wholeWord;
154-
m_optionsChanged = true;
155-
findAllMatches(editor, m_findWord);
156-
}
157-
}
145+
void setWholeWord(TextEditor *editor, bool wholeWord);
158146

159147
bool getFindRegEx() const { return m_findRegEx; }
160-
void setFindRegEx(TextEditor *editor, bool findRegEx) {
161-
if (findRegEx != m_findRegEx) {
162-
m_findRegEx = findRegEx;
163-
m_optionsChanged = true;
164-
findAllMatches(editor, m_findWord);
165-
}
166-
}
167-
168-
void resetMatches() {
169-
m_matches.clear();
170-
m_findWord = "";
171-
}
148+
void setFindRegEx(TextEditor *editor, bool findRegEx);
172149

150+
void resetMatches();
151+
UndoRecords m_undoBuffer;
173152
private:
174153
std::string m_findWord;
175154
std::string m_replaceWord;
@@ -299,7 +278,7 @@ namespace hex::ui {
299278
enum class LinePart { Chars, Utf8, Colors, Flags };
300279

301280
Line() : m_chars(""), m_colors(""), m_flags(""), m_colorized(false), m_lineMaxColumn(-1) {}
302-
explicit Line(const char *line) { Line(std::string(line)); }
281+
explicit Line(const char *line) : Line(std::string(line)) {}
303282
explicit Line(const std::string &line) : m_chars(line), m_colors(std::string(line.size(), 0x00)), m_flags(std::string(line.size(), 0x00)), m_colorized(false), m_lineMaxColumn(maxColumn()) {}
304283
Line(const Line &line) : m_chars(std::string(line.m_chars)), m_colors(std::string(line.m_colors)), m_flags(std::string(line.m_flags)), m_colorized(line.m_colorized), m_lineMaxColumn(line.m_lineMaxColumn) {}
305284
Line(Line &&line) noexcept : m_chars(std::move(line.m_chars)), m_colors(std::move(line.m_colors)), m_flags(std::move(line.m_flags)), m_colorized(line.m_colorized), m_lineMaxColumn(line.m_lineMaxColumn) {}
@@ -400,11 +379,11 @@ namespace hex::ui {
400379
UndoRecord() {}
401380
~UndoRecord() {}
402381
UndoRecord( const std::string &added,
403-
const TextEditor::Range addedRange,
382+
const Range addedRange,
404383
const std::string &removed,
405-
const TextEditor::Range removedRange,
406-
TextEditor::EditorState &before,
407-
TextEditor::EditorState &after);
384+
const Range removedRange,
385+
EditorState &before,
386+
EditorState &after);
408387

409388
void undo(TextEditor *editor);
410389
void redo(TextEditor *editor);
@@ -417,7 +396,18 @@ namespace hex::ui {
417396
EditorState m_after;
418397
};
419398

420-
typedef std::vector<UndoRecord> UndoBuffer;
399+
class UndoAction {
400+
public:
401+
UndoAction() {}
402+
~UndoAction() {}
403+
explicit UndoAction(const UndoRecords &records) : m_records(records) {}
404+
void undo(TextEditor *editor);
405+
void redo(TextEditor *editor);
406+
private:
407+
UndoRecords m_records;
408+
};
409+
410+
421411

422412
struct MatchedBracket {
423413
bool m_active = false;
@@ -511,8 +501,8 @@ namespace hex::ui {
511501
void backspace();
512502
bool canUndo();
513503
bool canRedo() const;
514-
void undo(i32 steps = 1);
515-
void redo(i32 steps = 1);
504+
void undo();
505+
void redo();
516506
void copy();
517507
void cut();
518508
void paste();
@@ -559,8 +549,10 @@ namespace hex::ui {
559549
void moveEnd(bool select = false);
560550
void moveToMatchedBracket(bool select = false);
561551
void setScrollY();
552+
void setScroll(ImVec2 scroll);
553+
ImVec2 getScroll() const { return m_scroll; }
562554
Coordinates getCursorPosition() { return setCoordinates(m_state.m_cursorPosition); }
563-
void setCursorPosition(const Coordinates &position);
555+
void setCursorPosition(const Coordinates &position, bool scrollToCursor = true);
564556
void setCursorPosition();
565557
private:
566558
Coordinates setCoordinates(const Coordinates &value);
@@ -591,8 +583,8 @@ namespace hex::ui {
591583
void clearRaiseContextMenu() { m_raiseContextMenu = false; }
592584
TextEditor *getSourceCodeEditor();
593585
bool isEmpty() const;
586+
void addUndo(UndoRecords &value);
594587
private:
595-
void addUndo(UndoRecord &value);
596588
TextEditor::PaletteIndex getColorIndexFromFlags(Line::Flags flags);
597589
void handleKeyboardInputs();
598590
void handleMouseInputs();
@@ -662,7 +654,9 @@ namespace hex::ui {
662654
std::vector<std::string> m_defines;
663655
TextEditor *m_sourceCodeEditor = nullptr;
664656
float m_shiftedScrollY = 0;
657+
ImVec2 m_scroll=ImVec2(0, 0);
665658
float m_scrollYIncrement = 0.0F;
659+
bool m_setScroll = false;
666660
bool m_setScrollY = false;
667661
float m_numberOfLinesDisplayed = 0;
668662
float m_lastClick = -1.0F;

0 commit comments

Comments
 (0)