Skip to content

Commit

Permalink
Fix for regex action with keep selection
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Apr 14, 2024
1 parent 0d55969 commit 9fab147
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
47 changes: 20 additions & 27 deletions app/src/main/java/net/gsantner/markor/format/ActionButtonBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -492,43 +492,36 @@ public static void runRegexReplaceAction(final Editable editable, final ReplaceP

private static void runRegexReplaceAction(final Editable editable, final List<ReplacePattern> patterns) {

final int[] sel = TextViewUtils.getSelection(editable);
final TextViewUtils.ChunkedEditable text = TextViewUtils.ChunkedEditable.wrap(editable);
TextViewUtils.withKeepSelection(editable, (selStart, selEnd) -> {

// Offset of selection start from text end - used to restore selection
final int selEndOffset = text.length() - sel[1];
// Offset of selection start from line end - used to restore selection
final int selStartOffset = sel[1] == sel[0] ? selEndOffset : TextViewUtils.getLineEnd(text, sel[0]) - sel[0];
final TextViewUtils.ChunkedEditable text = TextViewUtils.ChunkedEditable.wrap(editable);
// Start of line on which sel begins
final int selStartStart = TextViewUtils.getLineStart(text, selStart);

// Start of line on which sel begins
final int selStartStart = TextViewUtils.getLineStart(text, sel[0]);
// Number of lines we will be modifying
final int lineCount = TextViewUtils.countChars(text, sel[0], sel[1], '\n')[0] + 1;
// Number of lines we will be modifying
final int lineCount = TextViewUtils.countChars(text, selStart, selEnd, '\n')[0] + 1;
int lineStart = selStartStart;

int lineStart = selStartStart;

for (int i = 0; i < lineCount; i++) {
for (int i = 0; i < lineCount; i++) {

int lineEnd = TextViewUtils.getLineEnd(text, lineStart);
final String line = TextViewUtils.toString(text, lineStart, lineEnd);
int lineEnd = TextViewUtils.getLineEnd(text, lineStart);
final String line = TextViewUtils.toString(text, lineStart, lineEnd);

for (final ReplacePattern pattern : patterns) {
if (pattern.matcher.reset(line).find()) {
if (!pattern.isSameReplace()) {
text.replace(lineStart, lineEnd, pattern.replace());
for (final ReplacePattern pattern : patterns) {
if (pattern.matcher.reset(line).find()) {
if (!pattern.isSameReplace()) {
text.replace(lineStart, lineEnd, pattern.replace());
}
break;
}
break;
}
}

lineStart = TextViewUtils.getLineEnd(text, lineStart) + 1;
}

text.applyChanges();
lineStart = TextViewUtils.getLineEnd(text, lineStart) + 1;
}

final int newSelEnd = text.length() - selEndOffset;
final int newSelStart = sel[0] == sel[1] ? newSelEnd : TextViewUtils.getLineEnd(text, selStartStart) - selStartOffset;
Selection.setSelection(editable, newSelStart, newSelEnd);
text.applyChanges();
});
}

protected void runSurroundAction(final String delim) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static void withKeepSelection(final Editable text, final GsCallback.a2<In
final int[] selStart = TextViewUtils.getLineOffsetFromIndex(text, sel[0]);
final int[] selEnd = TextViewUtils.getLineOffsetFromIndex(text, sel[1]);

action.callback(selStart[0], selEnd[0]);
action.callback(sel[0], sel[1]);

Selection.setSelection(text,
TextViewUtils.getIndexFromLineOffset(text, selStart),
Expand Down

0 comments on commit 9fab147

Please sign in to comment.