fix: code folding was not restored when loading projects or opening/importing patterns

This commit is contained in:
paxcut
2026-01-28 08:04:35 -07:00
committed by paxcut
parent 219f588cbe
commit 77eff651ed
8 changed files with 72 additions and 90 deletions

View File

@@ -716,7 +716,7 @@ namespace hex::ui {
void removeLines(i32 start, i32 end);
void removeLine(i32 index);
float textDistanceToLineStart(const Coordinates &from);
std::string getText();
std::string getText(bool addHiddenLines = false);
void setCursorPosition();
void ensureSelectionNotFolded();
bool hasSelection();
@@ -747,6 +747,7 @@ namespace hex::ui {
void moveToStringIndex(i32 stringIndex, i32 &currentTokenId, Location &location);
void resetToTokenId(i32 &lineIndex, i32 &currentTokenId, Location &location);
i32 findNextDelimiter(bool openOnly);
void resetCodeFoldStates();
constexpr static u32 Normal = 0;
constexpr static u32 Not = 1;
@@ -947,7 +948,7 @@ namespace hex::ui {
void setLanguageDefinition(const LanguageDefinition &aLanguageDef) { m_lines.setLanguageDefinition(aLanguageDef); }
std::string getLineText(i32 line);
void setTextChanged(bool value) { m_lines.setTextChanged(value); }
std::string getText() { return m_lines.getText(); }
std::string getText(bool addHiddenLines = false) { return m_lines.getText(addHiddenLines); }
void addUndo(UndoRecords value) { m_lines.addUndo(value); }
bool isTextChanged() { return m_lines.isTextChanged(); }
void setHandleMouseInputs(bool value) { m_handleMouseInputs = value; }
@@ -984,6 +985,7 @@ namespace hex::ui {
void setScroll(ImVec2 scroll);
ImVec2 getScroll() const {return m_scroll;}
Coordinates getCursorPosition() { return m_lines.lineCoordinates(m_lines.m_state.m_cursorPosition); }
void setFocus(bool scrollTOCursor);
//Support
private:

View File

@@ -469,10 +469,10 @@ namespace hex::ui {
} else
codeFoldIndex++;
}
if (!m_hiddenLines.empty())
m_hiddenLines.clear();
if (closedFoldIncrements.empty())
return;
if (!m_hiddenLines.empty())
m_hiddenLines.clear();
std::string result = "//+-#:";
for (u32 i = 0; i < closedFoldIncrements.size(); ++i) {
result += std::to_string(closedFoldIncrements[i]);
@@ -489,12 +489,19 @@ namespace hex::ui {
void TextEditor::Lines::setCodeFoldState(CodeFoldState state) {
m_codeFoldState = state;
saveCodeFoldStates();
}
CodeFoldState TextEditor::Lines::getCodeFoldState() const {
return m_codeFoldState;
}
void TextEditor::Lines::resetCodeFoldStates() {
m_codeFoldState.clear();
for (auto key: m_codeFoldKeys)
m_codeFoldState[key] = true;
}
void TextEditor::Lines::applyCodeFoldStates() {
std::string commentLine;
@@ -505,8 +512,7 @@ namespace hex::ui {
}
}
if (commentLine.size() < 6 || !commentLine.starts_with("//+-#:")) {
for (auto key: m_codeFoldKeys)
m_codeFoldState[key] = true;
resetCodeFoldStates();
return;
}
auto states = commentLine.substr(6);
@@ -527,7 +533,7 @@ namespace hex::ui {
std::from_chars(stateStr.data(), stateStr.data() + stateStr.size(), value);
closedFoldIncrements[i] = value;
}
m_codeFoldState.clear();
resetCodeFoldStates();
auto codeFoldKeyIter = m_codeFoldKeys.begin();
i32 closeFold = 0;
for (auto closedFoldIncrement: closedFoldIncrements) {

View File

@@ -327,14 +327,8 @@ namespace hex::ui {
m_lines.m_unfoldedLines.resize(1);
m_lines.m_unfoldedLines[0].clear();
} else {
m_lines.m_hiddenLines.clear();
u64 i = 0;
while (vectorString[i].starts_with("//+-")) {
m_lines.m_hiddenLines.emplace_back(i, vectorString[i]), i++;
}
vectorString.erase(vectorString.begin(), vectorString.begin() + i);
lineCount = vectorString.size();
i = 0;
u64 i = 0;
m_lines.m_unfoldedLines.clear();
m_lines.m_unfoldedLines.resize(lineCount);
@@ -886,14 +880,21 @@ namespace hex::ui {
m_lines.refreshSearchResults();
}
std::string TextEditor::Lines::getText() {
std::string TextEditor::Lines::getText(bool includeHiddenLines) {
auto start = lineCoordinates(0, 0);
auto size = m_unfoldedLines.size();
auto line = m_unfoldedLines[size - 1];
auto end = Coordinates( size - 1, line.m_lineMaxColumn);
if (start == Invalid || end == Invalid)
return "";
return getRange(Range(start, end));
std::string result;
if (includeHiddenLines) {
for (const auto &hiddenLine: m_hiddenLines) {
result += hiddenLine.m_line + "\n";
}
}
result += getRange(Range(start, end));
return result;
}
StringVector TextEditor::getTextLines() const {

View File

@@ -432,6 +432,10 @@ namespace hex::ui {
}
}
void TextEditor::setFocus(bool scrollToCursor) {
m_lines.setFocusAtCoords(m_lines.m_state.m_cursorPosition, scrollToCursor);
}
void TextEditor::Lines::setFocusAtCoords(const Coordinates &coords, bool scrollToCursor) {
m_focusAtCoords = coords;
m_state.m_cursorPosition = coords;
@@ -460,7 +464,7 @@ namespace hex::ui {
if (std::abs(m_line) > maxLine)
return false;
auto maxColumn = lines.lineMaxColumn(m_line);
return std::abs(m_column) > maxColumn;
return std::abs(m_column) <= maxColumn;
}
TextEditor::Coordinates TextEditor::Coordinates::sanitize(Lines &lines) {

View File

@@ -1025,6 +1025,8 @@ namespace hex::ui {
}
void TextEditor::FoldedLine::loadSegments() {
if (m_keys.empty())
return;
m_foldedSegments.clear();
m_unfoldedSegments.clear();
i32 keyCount = (i32)m_keys.size();

View File

@@ -1320,15 +1320,13 @@ namespace hex::ui {
}
void TextEditor::Lines::removeHiddenLinesFromPattern() {
m_hiddenLines.clear();
i32 lineIndex = 0;
const auto totalLines = (i32)m_unfoldedLines.size();
while (lineIndex < totalLines && m_unfoldedLines[lineIndex].m_chars.starts_with("//+-"))
lineIndex++;
if (lineIndex > 0) {
m_hiddenLines.clear();
setSelection(Range(lineCoordinates(0, 0), lineCoordinates(lineIndex, 0)));
auto hiddenLinesText = getSelectedText();
auto lines = wolv::util::splitString(hiddenLinesText, "\n");
for (i32 i = 0; i < lineIndex; i++) {
HiddenLine hiddenLine(i, m_unfoldedLines[i].m_chars);
m_hiddenLines.emplace_back(std::move(hiddenLine));
@@ -1336,7 +1334,6 @@ namespace hex::ui {
}
deleteSelection();
}
setAllCodeFolds();
}
void TextEditor::Lines::addHiddenLinesToPattern() {
@@ -1348,6 +1345,8 @@ namespace hex::ui {
lineIndex = size() + hiddenLine.m_lineIndex;
else
lineIndex = hiddenLine.m_lineIndex;
if (m_unfoldedLines[lineIndex].m_chars.starts_with("//+-"))
m_unfoldedLines.erase(m_unfoldedLines.begin() + lineIndex);
insertLine(lineIndex, hiddenLine.m_line);
}
}