From 5df8ec78aad2ec651ed1e4ba314b8ff5069dcbfc Mon Sep 17 00:00:00 2001 From: paxcut <53811119+paxcut@users.noreply.github.com> Date: Sun, 20 Jul 2025 13:10:45 -0700 Subject: [PATCH] improv: removing tabs (#2345) These changes are part of an effort aimed at removing tabs from ImHex that started some time ago. Here text preprocessing is removed from all the places were it was done before and moved to the places where files are read that go in the pattern editor with two notable exceptions. 1) Pattern import reads patterns in order to present a list that can be filtered.That can safely ignore preprocessing since only needs to get information needed to filter. 2) The pattern editor can incorporate text from the clipboard so that needs to be preprocessed as well. find/replace is unable to add tabs or carriage returns so this should cover all angles. --- lib/external/pattern_language | 2 +- .../ColorTextEditor/source/TextEditor.cpp | 20 ++++++++----------- .../text_highlighting/pattern_language.cpp | 4 ++-- .../content/views/view_pattern_editor.cpp | 10 +++++----- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 7cbc1ac7f..a3f3c1f40 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 7cbc1ac7f860ad52a99359770c9f224a05d9a3ad +Subproject commit a3f3c1f40ed4860f1c163269647c3dfd37078eb2 diff --git a/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp b/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp index 4e2f3b79e..31d3ce24b 100644 --- a/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp +++ b/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp @@ -237,7 +237,7 @@ void TextEditor::DeleteRange(const Coordinates &aStart, const Coordinates &aEnd) } void TextEditor::AppendLine(const std::string &aValue) { - auto text = PreprocessText(aValue); + auto text = ReplaceStrings(aValue, "\000", "."); if (text.empty()) return; if (isEmpty()) { @@ -254,15 +254,14 @@ void TextEditor::AppendLine(const std::string &aValue) { int TextEditor::InsertTextAt(Coordinates & /* inout */ aWhere, const std::string &aValue) { - auto text = PreprocessText(aValue); - if (text.empty()) + if (aValue.empty()) return 0; auto start = SanitizeCoordinates(aWhere); auto &line = mLines[start.mLine]; - auto stringVector = SplitString(text, "\n", false); + auto stringVector = SplitString(aValue, "\n", false); auto lineCount = (int)stringVector.size(); aWhere.mLine += lineCount - 1; - aWhere.mColumn += stringVector[lineCount-1].size(); + aWhere.mColumn += GetStringCharacterCount(stringVector[lineCount-1]); stringVector[lineCount - 1].append(line.substr(GetCharacterIndex(start))); if (GetCharacterIndex(start) < (int)line.size()) line.erase(line.begin() + GetCharacterIndex(start),-1); @@ -1448,8 +1447,7 @@ void TextEditor::SetText(const std::string &aText, bool aUndo) { mLines.resize(1); mLines[0] = Line(); mLines[0].mColorized = false; - std::string text = PreprocessText(aText); - for (auto chr : text) { + for (auto chr : aText) { if (chr == '\n') { mLines.push_back(Line()); mLines.back().mColorized = false; @@ -1457,7 +1455,7 @@ void TextEditor::SetText(const std::string &aText, bool aUndo) { mLines.back().push_back(Glyph(chr)); } if (!mReadOnly && aUndo) { - u.mAdded = text; + u.mAdded = aText; u.mAddedStart = Coordinates(0, 0); u.mAddedEnd = Coordinates((int) mLines.size() - 1, GetLineMaxColumn((int) mLines.size() - 1)); } @@ -1482,7 +1480,7 @@ void TextEditor::SetTextLines(const std::vector &aLines) { auto lineCount = aLines.size(); mLines.resize(lineCount); for (size_t i = 0; i < lineCount; i++) { - mLines[i].SetLine(PreprocessText(aLines[i])); + mLines[i].SetLine(aLines[i]); mLines[i].mColorized = false; } } @@ -1745,9 +1743,8 @@ void TextEditor::InsertText(const char *aValue) { auto pos = GetActualCursorCoordinates(); auto start = std::min(pos, mState.mSelectionStart); - auto text = PreprocessText(aValue); - InsertTextAt(pos, text); + InsertTextAt(pos, aValue); mLines[pos.mLine].mColorized = false; SetSelection(pos, pos); @@ -2374,7 +2371,6 @@ std::string TextEditor::ReplaceTabsWithSpaces(const std::string& string, uint32_ std::string TextEditor::PreprocessText(const std::string &code) { std::string result = ReplaceStrings(code, "\r\n", "\n"); result = ReplaceStrings(result, "\r", "\n"); - result = ReplaceStrings(result, "\000", "."); result = ReplaceTabsWithSpaces(result, 4); return result; diff --git a/plugins/builtin/source/content/text_highlighting/pattern_language.cpp b/plugins/builtin/source/content/text_highlighting/pattern_language.cpp index 5ab651a63..0382d7119 100644 --- a/plugins/builtin/source/content/text_highlighting/pattern_language.cpp +++ b/plugins/builtin/source/content/text_highlighting/pattern_language.cpp @@ -1958,7 +1958,7 @@ namespace hex::plugin::builtin { m_lines.clear(); if (m_text.empty()) - m_text = m_viewPatternEditor->getTextEditor().PreprocessText(m_viewPatternEditor->getTextEditor().GetText()); + m_text = m_viewPatternEditor->getTextEditor().GetText(); m_lines = hex::splitString(m_text, "\n"); m_lines.push_back(""); @@ -2252,7 +2252,7 @@ namespace hex::plugin::builtin { auto lastToken = m_tokens.back(); m_tokens.push_back(lastToken); - m_text = m_viewPatternEditor->getTextEditor().PreprocessText( m_viewPatternEditor->getTextEditor().GetText()); + m_text = m_viewPatternEditor->getTextEditor().GetText(); if (m_text.empty() || m_text == "\n") return; diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 72bfd1eed..dab143957 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -1741,7 +1741,7 @@ namespace hex::plugin::builtin { void ViewPatternEditor::loadPatternFile(const std::fs::path &path, prv::Provider *provider) { wolv::io::File file(path, wolv::io::File::Mode::Read); if (file.isValid()) { - auto code = file.readString(); + auto code = wolv::util::preprocessText(file.readString()); this->evaluatePattern(code, provider); m_textEditor.get(provider).SetText(code, true); @@ -1940,7 +1940,7 @@ namespace hex::plugin::builtin { RequestSetPatternLanguageCode::subscribe(this, [this](const std::string &code) { auto provider = ImHexApi::Provider::get(); - m_textEditor.get(provider).SetText(code); + m_textEditor.get(provider).SetText(wolv::util::preprocessText(code)); m_sourceCode.get(provider) = code; m_hasUnevaluatedChanges.get(provider) = true; m_textHighlighter.m_needsToUpdateColors = false; @@ -1990,7 +1990,7 @@ namespace hex::plugin::builtin { } if (newProvider != nullptr) { - m_textEditor.get(newProvider).SetText(m_sourceCode.get(newProvider)); + m_textEditor.get(newProvider).SetText(wolv::util::preprocessText(m_sourceCode.get(newProvider))); m_textEditor.get(newProvider).SetCursorPosition(m_cursorPosition.get(newProvider)); TextEditor::Selection selection = m_selection.get(newProvider); m_textEditor.get(newProvider).SetSelection(selection.mStart, selection.mEnd); @@ -2136,7 +2136,7 @@ namespace hex::plugin::builtin { wolv::io::File file(path, wolv::io::File::Mode::Read); if (file.isValid()) { - RequestSetPatternLanguageCode::post(file.readString()); + RequestSetPatternLanguageCode::post(wolv::util::preprocessText(file.readString())); return true; } else { return false; @@ -2226,7 +2226,7 @@ namespace hex::plugin::builtin { .basePath = "pattern_source_code.hexpat", .required = false, .load = [this](prv::Provider *provider, const std::fs::path &basePath, const Tar &tar) { - const auto sourceCode = tar.readString(basePath); + const auto sourceCode = wolv::util::preprocessText(tar.readString(basePath)); m_sourceCode.get(provider) = sourceCode;