|
| 1 | +package ioutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +type MaybeStartLineDiagnostic struct { |
| 9 | + RequestedLine int `json:"requestedLine"` |
| 10 | + Tolerance int `json:"tolerance"` |
| 11 | + ClosestDistance int `json:"closestDistance"` |
| 12 | + ClosestStartLines []int `json:"closestStartLines,omitempty"` |
| 13 | + Applied bool `json:"applied"` |
| 14 | +} |
| 15 | + |
| 16 | +type blockMatchCandidateDiagnostic struct { |
| 17 | + StartLine int `json:"startLine"` |
| 18 | + EndLine int `json:"endLine"` |
| 19 | + BeforeLines []string `json:"beforeLines,omitempty"` |
| 20 | + MatchLines []string `json:"matchLines"` |
| 21 | + AfterLines []string `json:"afterLines,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +type blockMatchDiagnostic struct { |
| 25 | + CandidateStartLines []int `json:"candidateStartLines,omitempty"` |
| 26 | + AdditionalCandidatesOmitted int `json:"additionalCandidatesOmitted,omitempty"` |
| 27 | + SampleCandidates []blockMatchCandidateDiagnostic `json:"sampleCandidates,omitempty"` |
| 28 | + MaybeStartLine *MaybeStartLineDiagnostic `json:"maybeStartLine,omitempty"` |
| 29 | +} |
| 30 | + |
| 31 | +func OneBasedLineNumbers(idxs []int) []int { |
| 32 | + if len(idxs) == 0 { |
| 33 | + return nil |
| 34 | + } |
| 35 | + out := make([]int, len(idxs)) |
| 36 | + for i, idx := range idxs { |
| 37 | + out[i] = idx + 1 |
| 38 | + } |
| 39 | + return out |
| 40 | +} |
| 41 | + |
| 42 | +// NarrowMatchIndicesByMaybeStartLine applies a soft start-line hint. |
| 43 | +// |
| 44 | +// Behavior: |
| 45 | +// - if maybeStartLine is nil or invalid, no narrowing is applied |
| 46 | +// - if there is a unique closest candidate within tolerance, returns only that candidate |
| 47 | +// - otherwise returns the original candidates and emits diagnostics explaining the closest set |
| 48 | +func NarrowMatchIndicesByMaybeStartLine( |
| 49 | + idxs []int, |
| 50 | + maybeStartLine *int, |
| 51 | + tolerance int, |
| 52 | +) ([]int, *MaybeStartLineDiagnostic) { |
| 53 | + if len(idxs) == 0 || maybeStartLine == nil || *maybeStartLine <= 0 { |
| 54 | + return idxs, nil |
| 55 | + } |
| 56 | + if tolerance < 0 { |
| 57 | + tolerance = 0 |
| 58 | + } |
| 59 | + |
| 60 | + hint := *maybeStartLine |
| 61 | + minDist := -1 |
| 62 | + closest := make([]int, 0, 1) |
| 63 | + |
| 64 | + for _, idx := range idxs { |
| 65 | + dist := idx + 1 - hint |
| 66 | + if dist < 0 { |
| 67 | + dist = -dist |
| 68 | + } |
| 69 | + if minDist == -1 || dist < minDist { |
| 70 | + minDist = dist |
| 71 | + closest = closest[:0] |
| 72 | + closest = append(closest, idx) |
| 73 | + continue |
| 74 | + } |
| 75 | + if dist == minDist { |
| 76 | + closest = append(closest, idx) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + diag := &MaybeStartLineDiagnostic{ |
| 81 | + RequestedLine: hint, |
| 82 | + Tolerance: tolerance, |
| 83 | + ClosestDistance: minDist, |
| 84 | + ClosestStartLines: OneBasedLineNumbers(closest), |
| 85 | + } |
| 86 | + |
| 87 | + if len(closest) == 1 && minDist <= tolerance { |
| 88 | + diag.Applied = true |
| 89 | + return []int{closest[0]}, diag |
| 90 | + } |
| 91 | + |
| 92 | + return idxs, diag |
| 93 | +} |
| 94 | + |
| 95 | +func BuildBlockMatchDiagnosticJSON( |
| 96 | + lines []string, |
| 97 | + matchIdxs []int, |
| 98 | + matchWidth int, |
| 99 | + hint *MaybeStartLineDiagnostic, |
| 100 | + maxCandidates int, |
| 101 | + contextLines int, |
| 102 | +) string { |
| 103 | + diag := blockMatchDiagnostic{ |
| 104 | + CandidateStartLines: OneBasedLineNumbers(matchIdxs), |
| 105 | + MaybeStartLine: hint, |
| 106 | + } |
| 107 | + |
| 108 | + if maxCandidates < 0 { |
| 109 | + maxCandidates = 0 |
| 110 | + } |
| 111 | + if contextLines < 0 { |
| 112 | + contextLines = 0 |
| 113 | + } |
| 114 | + if matchWidth < 1 { |
| 115 | + matchWidth = 1 |
| 116 | + } |
| 117 | + |
| 118 | + limit := len(matchIdxs) |
| 119 | + if maxCandidates > 0 && limit > maxCandidates { |
| 120 | + limit = maxCandidates |
| 121 | + diag.AdditionalCandidatesOmitted = len(matchIdxs) - maxCandidates |
| 122 | + } |
| 123 | + |
| 124 | + if limit > 0 && len(lines) > 0 { |
| 125 | + diag.SampleCandidates = make([]blockMatchCandidateDiagnostic, 0, limit) |
| 126 | + for _, idx := range matchIdxs[:limit] { |
| 127 | + if idx < 0 || idx >= len(lines) { |
| 128 | + continue |
| 129 | + } |
| 130 | + |
| 131 | + matchEndExclusive := min(idx+matchWidth, len(lines)) |
| 132 | + |
| 133 | + beforeStart := max(idx-contextLines, 0) |
| 134 | + |
| 135 | + afterEnd := min(matchEndExclusive+contextLines, len(lines)) |
| 136 | + |
| 137 | + diag.SampleCandidates = append(diag.SampleCandidates, blockMatchCandidateDiagnostic{ |
| 138 | + StartLine: idx + 1, |
| 139 | + EndLine: matchEndExclusive, |
| 140 | + BeforeLines: cloneStringSlice(lines[beforeStart:idx]), |
| 141 | + MatchLines: cloneStringSlice(lines[idx:matchEndExclusive]), |
| 142 | + AfterLines: cloneStringSlice(lines[matchEndExclusive:afterEnd]), |
| 143 | + }) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + b, err := json.Marshal(diag) |
| 148 | + if err != nil { |
| 149 | + return fmt.Sprintf("candidateStartLines=%v", OneBasedLineNumbers(matchIdxs)) |
| 150 | + } |
| 151 | + return string(b) |
| 152 | +} |
| 153 | + |
| 154 | +func cloneStringSlice(in []string) []string { |
| 155 | + if len(in) == 0 { |
| 156 | + return nil |
| 157 | + } |
| 158 | + out := make([]string, len(in)) |
| 159 | + copy(out, in) |
| 160 | + return out |
| 161 | +} |
0 commit comments