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.
This commit is contained in:
paxcut
2025-07-20 13:10:45 -07:00
committed by GitHub
parent cc6590d780
commit 5df8ec78aa
4 changed files with 16 additions and 20 deletions

View File

@@ -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<std::string> &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;

View File

@@ -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;

View File

@@ -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;