Replaces the first contiguous MoonBit token sequence in input that matches
old, scanning from left to right, with new.
Parameters and return value
- old: A required named argument specifying the code fragment to find.
Leading and trailing whitespace and comments do not affect match locations.
At least one token must remain after ignored items are removed. This is not
a regular expression, and wildcards are not supported.
- new: A required named argument specifying the text to insert verbatim.
An empty string deletes the matched range. No lexical or syntactic validation
is performed, and indentation, line breaks, and escapes are not adjusted.
No separators are added.
- input: A positional argument specifying the source text to edit, either
complete source code or a string slice.
Returns a String consisting of the original text before the matched range,
new, and the original text after the matched range. Only the supplied view
is processed; underlying string content outside the view is not included.
The arguments are not modified. Each call replaces at most one occurrence,
leaving other matches unchanged. If no match is found, NotFound is raised
instead of returning the original text as a successful result.
Matching rules
old and input are tokenized using ordinary MoonBit lexical rules. Code
fragments need not form complete syntax. Comparisons ignore whitespace, line
breaks, comments, and automatically inserted semicolons between tokens.
Explicit ; tokens must match: for example, a b does not match a;b.
Each token's kind and original text must match exactly, except that trailing
lexer-recognized whitespace in attribute tokens is ignored, including Unicode
whitespace. Matching is case-sensitive, and literal values are not normalized:
foo does not match foobar, 1 does not match 01, and 1.0 does not match
1.00.
Individual tokens such as strings, interpolated strings, and attributes are
compared as a whole, with no recursive search inside them. Their internal
whitespace must also match. Compound tokens such as .field include the
leading dot, so field does not match .field, and replacing .field also
replaces the dot.
The replacement range starts at the beginning of the first matched token and
ends at the end of the last matched token. Whitespace and comments inside
this range are removed; text outside it is preserved verbatim.
Attributes retain their original lexer spans, so their ignored trailing
whitespace is removed with the attribute. ASCII line endings (LF, CRLF, or CR)
after a final attribute are outside its token and are preserved.
The result is not tokenized again after insertion. It may contain lexical
errors or join adjacent tokens together. Any required spaces or other
separators must be supplied explicitly in new.
Errors
Checks and raises LEditError in the following order, without using lexical
recovery tokens or falling back to text matching:
- OldLexError: old contains a lexical error.
- EmptyOld: old is lexically valid but contains no tokens to match.
- InputLexError: The entire input contains a lexical error, including
errors after the first match.
- NotFound: Validation succeeds, but no contiguous token sequence matches old.
Lexical errors report only the first lexer diagnostic in the relevant view.
Offsets count UTF-16 code units from the start of that view; see LEditError
for details.
Examples
Match across whitespace and comments, replace only the first occurrence, and
preserve formatting outside the matched range:
test "replace the first token match" {
let result = @lexical_edit.lexical_edit(
old="a + b",
new="sum",
" a // inside\n + b // after\n a+b",
)
assert_eq(result, " sum // after\n a+b")
}
An empty replacement deletes the matched range without merging spaces outside it:
test "delete tokens without trimming surrounding text" {
assert_eq(
@lexical_edit.lexical_edit(old=" + b", new="", "a + b + b"),
"a + b",
)
}