← Back to Pull requests

enhance search API #3658

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Feb 08, 2025 03:09 UTC
Author

This PR is based on #3575 ~~and therefore a draft at present~~. It has the following components:

  • additional methods for searching text, for example methods that also return matched capturing groups,

  • a new type RegexpGroup that combines a regexp with its padded versions as used in #3575,

  • process Deltas in ExecuteTextEvent in reverse order. This makes replaceall easier to implement,

  • new functions LocVoid() and Loc.IsVoid() to deal with unused submatches.
  • The new types and functions are as follows (UPDATED):

    // NewRegexpGroup creates a RegexpGroup from a string
    func NewRegexpGroup(s string) (RegexpGroup, error)
    
    // FindDown returns a slice containing the start and end positions
    // of the first match of `rgrp` between `start` and `end`, or nil
    // if no match exists.
    func (b *Buffer) FindDown(rgrp RegexpGroup, start, end Loc) []Loc
    
    // FindDownSubmatch returns a slice containing the start and end positions
    // of the first match of `rgrp` between `start` and `end` plus those
    // of all submatches (capturing groups), or nil if no match exists.
    func (b *Buffer) FindDownSubmatch(rgrp RegexpGroup, start, end Loc) []Loc
    
    // FindUp returns a slice containing the start and end positions
    // of the last match of `rgrp` between `start` and `end`, or nil
    // if no match exists.
    func (b *Buffer) FindUp(rgrp RegexpGroup, start, end Loc) []Loc
    
    // FindUpSubmatch returns a slice containing the start and end positions
    // of the last match of `rgrp` between `start` and `end` plus those
    // of all submatches (capturing groups), or nil if no match exists.
    func (b *Buffer) FindUpSubmatch(rgrp RegexpGroup, start, end Loc) []Loc
    
    // FindAllFunc calls the function `f` once for each match between `start`
    // and `end` of the regexp given by `s`. The argument of `f` is the slice
    // containing the start and end positions of the match. FindAllFunc returns
    // the number of matches plus any error that occured when compiling the regexp.
    func (b *Buffer) FindAllFunc(s string, start, end Loc, f func([]Loc)) (int, error)
    
    // FindAll returns a slice containing the start and end positions of all
    // matches between `start` and `end` of the regexp given by `s`, plus any
    // error that occured when compiling the regexp. If no match is found, the
    // slice returned is nil.
    func (b *Buffer) FindAll(s string, start, end Loc) ([][]Loc, error)
    
    // FindAllSubmatchFunc calls the function `f` once for each match between
    // `start` and `end` of the regexp given by `s`. The argument of `f` is the
    // slice containing the start and end positions of the match and all submatches
    // (capturing groups). FindAllSubmatch Func returns the number of matches plus
    // any error that occured when compiling the regexp.
    func (b *Buffer) FindAllSubmatchFunc(s string, start, end Loc, f func([]Loc)) (int, error)
    
    // FindAllSubmatch returns a slice containing the start and end positions of
    // all matches and all submatches (capturing groups) between `start` and `end`
    // of the regexp given by `s`, plus any error that occured when compiling
    // the regexp. If no match is found, the slice returned is nil.
    func (b *Buffer) FindAllSubmatch(s string, start, end Loc) ([][]Loc, error)
    
    // ReplaceAll replaces all matches of the regexp `s` in the given area. The
    // new text is obtained from `template` by replacing each variable with the
    // corresponding submatch as in `Regexp.Expand`. The function returns the
    // number of replacements made, the new end position and any error that
    // occured during regexp compilation
    func (b *Buffer) ReplaceAll(s string, start, end Loc, template []byte) (int, Loc, error)
    
    // ReplaceAllLiteral replaces all matches of the regexp `s` with `repl` in
    // the given area. The function returns the number of replacements made, the
    // new end position and any error that occured during regexp compilation
    func (b *Buffer) ReplaceAllLiteral(s string, start, end Loc, repl []byte) (int, Loc, error)
    
    // ReplaceAllFunc replaces all matches of the regexp `s` with `repl(match)`
    // in the given area, where `match` is the slice containing start and end
    // positions of the match. The function returns the number of replacements
    // made, the new end position and any error that occured during regexp
    // compilation
    func (b *Buffer) ReplaceAllFunc(s string, start, end Loc, repl func(match []Loc) []byte) (int, Loc, error)
    
    // ReplaceAllSubmatchFunc replaces all matches of the regexp `s` with
    // `repl(match)` in the given area, where `match` is the slice containing
    // start and end positions of the match and all submatches. The function
    // returns the number of replacements made, the new end position and any
    // error that occured during regexp compilation
    func (b *Buffer) ReplaceAllSubmatchFunc(s string, start, end Loc, repl func(match []Loc) []byte) (int, Loc, error)
    
    // MatchedStrings converts a slice containing start and end positions of
    // matches or submatches to a slice containing the corresponding strings.
    func (b *Buffer) MatchedStrings(locs []Loc) ([]string)
    
    // LocVoid returns a Loc strictly smaller then any valid buffer location
    func LocVoid() Loc
    
    // IsVoid returns true if the location l is void
    func (l Loc) IsVoid() bool
    

    The method FindNext is kept. ReplaceRegex is removed in favor of ReplaceAll. The latter is easier to use in Lua scripts.

    Currently the simple search functions (FindDown etc.) take a RegexpGroup as argument to avoid recompiling the regexps. In contrast, FindAll, ReplaceAll and friends take a string argument. Many other variants would be possible. Also, the new search functions ignore the ignorecase setting of the buffer and don't wrap around when they hit the end of the search region. I think they are more useful this way in Lua scripts.

    You will see that many new internal functions use callback functions. This avoids code duplication. (One has to somehow switch between (regexp.Regexp).FindIndex() and (regexp.Regexp).FindSubmatchIndex() in the innermost function that searches each line of the buffer.)

    As said before, many details could be modified, but overall I think these functions are very useful for writing scripts. Please let me know what you think.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: matthias314 Review duration: 927530m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.59) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 650 lines diff across 8 files
    0.59
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3658"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 59% (MEDIUM) · Higher = larger blast radius
    59%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.9% · Lower = rushed or shallow review
    40.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 34.8% · Safe to merge
    34.8%