Skip to main content

Module fuzzy_match

Module fuzzy_match 

Source
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§

FuzzyMatch
A match result: byte offset in source and the matched byte length.
ReindentPlan
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. A replace_all over thousands of matches would otherwise parse the whole file thousands of times.

Enums§

IndentShift
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 needle in haystack using 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 oldString line 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 oldString was found only by a loose pass: what the match ignored, and whether newString was shifted to the file’s indentation. This is the only place the wording lives; the reply formatters render the notice field 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. notice is the reply line that tells the agent the match was loose (see loose_match_notice); reindented says whether any replacement was shifted to the file’s indentation.
shorten_diagnostic_line
line as is when it has at most NEAREST_MISS_LINE_CHARS characters, 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 byte line_start starts 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 by str::lines), whether it begins inside a multi-line string literal of text, parsed with lang. 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. None when the boundaries cannot be determined: the parse fails, or it has errors while string delimiters are present.