Expand description
Fuzzy string matching for edit_match, inspired by opencode’s 4-pass approach.
When exact matching fails, progressively relaxes comparison: Pass 1: Exact match (str::find / match_indices) Pass 2: Trim trailing whitespace per line Pass 3: Trim both ends per line Pass 4: Normalize Unicode punctuation + trim Pass 5: Reflowed line wraps/joins with whitespace-normalized content
Structs§
- Fuzzy
Match - A match result: byte offset in source and the matched byte length.
- Reindent
Plan - Re-indents the replacement for each match of one find/replace (see
indentation_shift), sharing the work across matches. Finding which replacement lines sit inside multi-line string literals needs a parse of the edited file; a plan makes at most one such parse for all of its matches, and only when some match needs shifting and a string delimiter appears anywhere in the file, the matched lines or the replacement. Areplace_allover thousands of matches would otherwise parse the whole file thousands of times.
Enums§
- Indent
Shift - How a loose match’s replacement has to move to sit at the file’s indentation.
Constants§
- AMBIGUOUS_
OCCURRENCE_ LIST_ LIMIT - NEAREST_
MISS_ EXCERPT_ LINES - NEAREST_
MISS_ LINE_ CHARS - NEAREST_
MISS_ SIMILARITY_ FLOOR
Functions§
- describe_
searched_ text - How a not-found error names the text it searched for. One short line is
quoted as-is. Anything longer shows only its first line, cut to
[
SEARCHED_TEXT_ECHO_CHARS], and its size: the caller already has the text, and repeating a large one doubles its cost in the transcript. - find_
all_ fuzzy - Find all occurrences of
needleinhaystackusing progressive fuzzy matching. Returns matches in order of their byte position in the source. - indentation_
shift - The indentation shift for a match found by a pass that ignores leading
whitespace (3: trimmed lines, 4: trimmed lines with Unicode punctuation
normalised). Those passes pair each
oldStringline with one file line, so the difference in their leading whitespace is measurable. Other passes either keep leading whitespace exact (1, 2) or have no line pairing (5), and never shift. - loose_
match_ notice - The one-line reply notice for an edit whose
oldStringwas found only by a loose pass: what the match ignored, and whethernewStringwas shifted to the file’s indentation. This is the only place the wording lives; the reply formatters render thenoticefield as given. - render_
edit_ not_ found_ detail - render_
nearest_ miss_ detail - render_
occurrence_ listing - Render a bounded 1-based occurrence list for an ambiguity error.
- replacement_
detail - Describe fuzzy replacements only when the bytes being replaced contain
non-whitespace content different from the requested text.
noticeis the reply line that tells the agent the match was loose (seeloose_match_notice);reindentedsays whether any replacement was shifted to the file’s indentation. - shorten_
diagnostic_ line lineas is when it has at mostNEAREST_MISS_LINE_CHARScharacters, otherwise its first and last halves of that many characters around….- string_
literal_ at_ line_ start - The string literal (see [
is_string_literal_kind]) that the line beginning at byteline_startstarts inside, if any. Such a line is part of the string’s value: adding or removing its leading whitespace changes the string. - text_
lines_ inside_ strings - For each line of
text(as split bystr::lines), whether it begins inside a multi-line string literal oftext, parsed withlang. Code generators that re-indent a block (inlining a function body) use it to leave string contents alone. Text with no string delimiter is not parsed.Nonewhen the boundaries cannot be determined: the parse fails, or it has errors while string delimiters are present.