Skip to content

Commit f63104b

Browse files
committed
Refine caret handling after replacement:
• If there is a `|` character in the text, the caret is ALWAYS placed here • If an indent is found in the text, place the cursor at the FIRST occurrence of it
1 parent 7f4e2e2 commit f63104b

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

WebEdit/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ internal static void ReplaceTag()
269269
value = parser.Unescape(value);
270270
scintillaGateway.ReplaceSel(Tags.UserDefinedInsertionPoint().Replace(value, string.Empty));
271271

272-
if (parser.FindAndReplace(selStart) || !Tags.UserDefinedInsertionPoint().IsMatch(value))
272+
if ((parser.FindAndReplace(selStart) && value.IndexOf('|') == -1) || !Tags.UserDefinedInsertionPoint().IsMatch(value))
273273
return;
274274

275275
scintillaGateway.SetSelectionEnd(position + value.Substring(0, value.IndexOf('|')).Length - selectedText.Length);

WebEdit/Tags.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ internal bool FindAndReplace(long startPos)
6262
ScintillaGateway sci = new(Utils.GetCurrentScintilla());
6363
bool didReplace = false, didIndent = false;
6464

65+
// track the caret position resulting from the FIRST '\i' replacement
66+
long firstIndentCaret = -1;
67+
6568
Dictionary<string, Action> replacements = new()
6669
{
6770
{"\\f", PasteFileContents},
@@ -82,14 +85,21 @@ internal bool FindAndReplace(long startPos)
8285
long selStart = sci.GetTargetStart() + seqStart;
8386
sci.SetSelection(selStart, selStart + sci.CodePage.GetByteCount(seq));
8487
replaceFunc();
88+
89+
// capture the caret position produced by the first '\i' replacement
90+
if (seq == "\\i" && firstIndentCaret == -1)
91+
firstIndentCaret = sci.GetSelectionEnd();
92+
8593
sci.SetTargetRange(sci.GetSelectionEnd(), sci.GetTextLength());
8694
seqStart = sci.GetTargetText().IndexOf(seq);
8795
}
8896
}
8997

9098
if (didIndent)
9199
{
92-
sci.SetCurrentPos(sci.GetSelectionEnd());
100+
// move caret to the position recorded for the first '\i' replacement;
101+
// fall back to current selection end if something went wrong.
102+
sci.SetCurrentPos(firstIndentCaret > -1 ? firstIndentCaret : sci.GetSelectionEnd());
93103
sci.ClearSelectionToCursor();
94104
}
95105

@@ -150,7 +160,7 @@ private void PasteFileContents()
150160
}
151161

152162
/// <summary>
153-
/// Matches a single, unescaped '|'.
163+
/// Matches a single, unescaped '|'
154164
/// </summary>
155165
[GeneratedRegex(@"(?<!\\)\|")]
156166
internal static partial Regex UserDefinedInsertionPoint();

0 commit comments

Comments
 (0)