Skip to content

Commit

Permalink
fix(Studio): Clear selection when intersecting with a collapse
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Dec 14, 2024
1 parent b3a2a9c commit 3be25c7
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,11 +2104,30 @@ private void ToggleCollapse(int row) {
UserData = new CollapseAnchorData()
});

if (foldings.FirstOrDefault(f => f.MinRow == row) is { } fold &&
Document.Caret.Row >= fold.MinRow && Document.Caret.Row <= fold.MaxRow)
{
Document.Caret.Row = fold.MinRow;
Document.Caret = ClampCaret(Document.Caret);
if (foldings.FirstOrDefault(f => f.MinRow == row) is var fold) {
// Keep caret outside collapse
if (Document.Caret.Row >= fold.MinRow && Document.Caret.Row <= fold.MaxRow) {
Document.Caret.Row = fold.MinRow;
Document.Caret = ClampCaret(Document.Caret);
}

// Clear selection if it's inside the collapse
if (!Document.Selection.Empty) {
bool minInside = Document.Selection.Min.Row >= fold.MinRow && Document.Selection.Min.Row <= fold.MaxRow;
bool maxInside = Document.Selection.Max.Row >= fold.MinRow && Document.Selection.Max.Row <= fold.MaxRow;

if (minInside && maxInside) {
Document.Selection.Clear();
Document.Caret.Row = fold.MinRow;
Document.Caret = ClampCaret(Document.Caret);
} else if (minInside) {
Document.Caret = ClampCaret(Document.Selection.Max);
Document.Selection.Clear();
} else if (maxInside) {
Document.Caret = ClampCaret(Document.Selection.Min);
Document.Selection.Clear();
}
}
}
} else {
Document.RemoveAnchorsIf(anchor => anchor.Row == row && anchor.UserData is CollapseAnchorData);
Expand All @@ -2122,13 +2141,32 @@ private void SetCollapse(int row, bool collapse) {
UserData = new CollapseAnchorData()
});

if (foldings.FirstOrDefault(f => f.MinRow == row) is { } fold &&
Document.Caret.Row >= fold.MinRow && Document.Caret.Row <= fold.MaxRow)
{
Document.Caret.Row = fold.MinRow;
Document.Caret = ClampCaret(Document.Caret);
if (foldings.FirstOrDefault(f => f.MinRow == row) is var fold) {
// Keep caret outside collapse
if (Document.Caret.Row >= fold.MinRow && Document.Caret.Row <= fold.MaxRow) {
Document.Caret.Row = fold.MinRow;
Document.Caret = ClampCaret(Document.Caret);
}

// Clear selection if it's inside the collapse
if (!Document.Selection.Empty) {
bool minInside = Document.Selection.Min.Row >= fold.MinRow && Document.Selection.Min.Row <= fold.MaxRow;
bool maxInside = Document.Selection.Min.Row >= fold.MinRow && Document.Selection.Min.Row <= fold.MaxRow;

if (minInside && maxInside) {
Document.Selection.Clear();
Document.Caret.Row = fold.MinRow;
Document.Caret = ClampCaret(Document.Caret);
} else if (minInside) {
Document.Caret = ClampCaret(Document.Selection.Max);
Document.Selection.Clear();
} else if (maxInside) {
Document.Caret = ClampCaret(Document.Selection.Min);
Document.Selection.Clear();
}
}
}
} else {
} else if (!collapse) {
Document.RemoveAnchorsIf(anchor => anchor.Row == row && anchor.UserData is CollapseAnchorData);
}
}
Expand Down

0 comments on commit 3be25c7

Please sign in to comment.