Skip to content

Commit

Permalink
fix: fixes for issue #1950. This PR depends on PR #27 on libwolv repo…
Browse files Browse the repository at this point in the history
…. It uses function added there to remove tabs if they exist on the provider. Additionally, any pasted tab will be translated to spaces automatically. This code was tested successfully using the pattern posted in the issue.
  • Loading branch information
paxcut committed Nov 5, 2024
1 parent 592f613 commit a724627
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ int TextEditor::InsertTextAt(Coordinates & /* inout */ aWhere, const char *aValu
if (*aValue == '\r') {
// skip
++aValue;
} else if (*aValue == '\t') {
auto &line = mLines[aWhere.mLine];
auto c = GetCharacterColumn(aWhere.mLine, cindex);
auto r = c % mTabSize;
auto d = mTabSize - r;
auto i = d;
while (i-- > 0)
line.insert(line.begin() + cindex++, Glyph(' ', PaletteIndex::Default));

cindex += d;
aWhere.mColumn += d;
aValue++;
} else if (*aValue == '\n') {
if (cindex < (int)mLines[aWhere.mLine].size()) {
auto &newLine = InsertLine(aWhere.mLine + 1);
Expand Down
2 changes: 2 additions & 0 deletions plugins/builtin/include/content/views/view_pattern_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ namespace hex::plugin::builtin {
}

public:
std::string preprocessText(const std::string &code);

struct VirtualFile {
std::fs::path path;
std::vector<u8> data;
Expand Down
18 changes: 14 additions & 4 deletions plugins/builtin/source/content/views/view_pattern_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,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 = preprocessText(file.readString());

this->evaluatePattern(code, provider);
m_textEditor.SetText(code);
Expand Down Expand Up @@ -1815,6 +1815,15 @@ namespace hex::plugin::builtin {
});
}

std::string ViewPatternEditor::preprocessText(const std::string &code) {
std::string result = wolv::util::replaceStrings(code, "\r\n", "\n");
result = wolv::util::replaceStrings(result, "\r", "\n");
result = wolv::util::replaceTabsWithSpaces(result, 4);
while (result.ends_with('\n'))
result.pop_back();
return result;
}

void ViewPatternEditor::registerEvents() {
RequestPatternEditorSelectionChange::subscribe(this, [this](u32 line, u32 column) {
const TextEditor::Coordinates coords = { int(line) - 1, int(column) };
Expand All @@ -1835,8 +1844,9 @@ namespace hex::plugin::builtin {
});

RequestSetPatternLanguageCode::subscribe(this, [this](const std::string &code) {
m_textEditor.SetText(code);
m_sourceCode.set(ImHexApi::Provider::get(), code);
const std::string preprocessed = preprocessText(code);
m_textEditor.SetText(preprocessed);
m_sourceCode.set(ImHexApi::Provider::get(), preprocessed);
m_hasUnevaluatedChanges = true;
});

Expand Down Expand Up @@ -2081,7 +2091,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 = preprocessText(tar.readString(basePath));

m_sourceCode.set(provider, sourceCode);

Expand Down

0 comments on commit a724627

Please sign in to comment.