| 1 | // Copyright 2025 Florian Zenker ([email protected]) |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package indentheuristic is an implementation of the indentation heuristic by Michael Haggerty |
| 16 | // (https://github.com/mhagger/diff-slider-tools). |
| 17 | // |
| 18 | // The idea behind the heuristic is that, since there's usually not a single solution, we can |
| 19 | // locally vary the solution we found to improve the aesthetics. We can vary the solution around the |
| 20 | // following degrees of freedom: |
| 21 | // |
| 22 | // a. A deletion of a line X, followed by an insertion of line Y is the same as an insertion of |
| 23 | // line Y followed by a deletion of line X. |
| 24 | // b. A deletion of a line X, followed by zero or more deletions, followed by a match Y with |
| 25 | // X == Y allows us to swap the deletion and the match. The same is true for insertions. The |
| 26 | // same is true the other way around (match of line X, followed by zero or more deletions, |
| 27 | // followed by a deletion of line Y with X == Y). |
| 28 | // |
| 29 | // The heuristics use these degrees of freedom to achieve the following goals: |
| 30 | // |
| 31 | // 1. Group deletions and insertions using (we do this implicitly by how we construct the diff |
| 32 | // from the result slices). |
| 33 | // 2. Make deletion and insertion groups as large as possible by merging adjacent groups if |
| 34 | // using. |
| 35 | // 3. If possible align deletions and insertions such that deletions are followed by insertions |
| 36 | // without a matching line in between using. |
| 37 | // 4. If it's not possible to align deletions and insertions, shift beginnings and ends of |
| 38 | // deletions / insertion groups to a line that has significance to humans based on indention at |
| 39 | // the line and around the line. |
| 40 | // |
| 41 | // The most intricate piece of these heuristics are in (4) which is based on human rated diffs. |
| 42 | package indentheuristicscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/ |
| 43 | |
| 44 | import ( |
| 45 | "cmpscip-go gomod github.com/golang/go/src go1.24.13 cmp/" |
| 46 | |
| 47 | "znkr.io/diff/internal/byteviewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/" |
| 48 | ) |
| 49 | |
| 50 | // Never move a group more than this many lines. |
| 51 | const maxSlidingscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/maxSliding. = 100 |
| 52 | |
| 53 | // We don't care if a line is indented more than this and clamp the value to maxIndent. That way, |
| 54 | // we don't overflow an int and avoid unnecessary work on input that's not human readable text. |
| 55 | const maxIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/maxIndent. = 200 |
| 56 | |
| 57 | // Don't consider more than this number of consecutive blank lines. This is to bound the work |
| 58 | // and avoid integer overflows. |
| 59 | const maxBlanksscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/maxBlanks. = 20 |
| 60 | |
| 61 | const startOfFilePenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/startOfFilePenalty. = 1 // No no-blank lines before the split |
| 62 | const endOfFilePenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/endOfFilePenalty. = 21 // No non-blank lines after the split |
| 63 | const totalBlankWeightscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/totalBlankWeight. = -30 // Weight for number of blank lines around the split |
| 64 | const postBlankWeightscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/postBlankWeight. = 6 // Weight for number of blank lines after the split |
| 65 | const relativeIndentPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeIndentPenalty. = -4 // Indented more than predecessor |
| 66 | const relativeIndentWithBlankPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeIndentWithBlankPenalty. = 10 // Indented more than predecessor, with blank lines |
| 67 | const relativeOutdentPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeOutdentPenalty. = 24 // Indented less than predecessor |
| 68 | const relativeOutdentWithBlankPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeOutdentWithBlankPenalty. = 17 // Indented less than predecessor, with blank lines |
| 69 | const relativeDedentPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeDedentPenalty. = 23 // Indented less than predecessor but not less than successor |
| 70 | const relativeDedentWithBlankPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeDedentWithBlankPenalty. = 17 // Indented less than predecessor but not less than successor, with blank lines |
| 71 | |
| 72 | // We only consider whether the sum of the effective indents for splits are less than (-1), equal |
| 73 | // to (0), or greater than (+1) each other. The resulting value is multiplied by the following |
| 74 | // weight and combined with the penalty to determine the better of two scores. |
| 75 | const indentWeightscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/indentWeight. = 60 |
| 76 | |
| 77 | // Apply applies the indent heuristics to rx and ry. |
| 78 | func Applyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/Apply().(x, y []byteviewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/.ByteViewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/ByteView#, rx, ry []bool) { |
| 79 | apply0scip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/apply0().(x, y, rx, ry) // for deletions |
| 80 | apply0scip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/apply0().(y, x, ry, rx) // for insertions |
| 81 | } |
| 82 | |
| 83 | // apply0 applies the indentation heuristics to r. |
| 84 | func apply0scip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/apply0().(lines, lineso []byteviewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/.ByteViewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/ByteView#, r, ro []bool) { |
| 85 | s, so := newScannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/newScanner().(lines, r), newScannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/newScanner().(lineso, ro) |
| 86 | for s.nextGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#nextGroup().() { |
| 87 | if !so.nextGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#nextGroup().() { |
| 88 | panic("scanner sync broken") |
| 89 | } |
| 90 | |
| 91 | if s.groupLenscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#groupLen().() == 0 { |
| 92 | continue |
| 93 | } |
| 94 | |
| 95 | matchingEnd := -1 // End of group that aligns with other input. |
| 96 | minEnd := s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. // Highest line that the group can be shifted to. |
| 97 | grpLen := 0 |
| 98 | for grpLen != s.groupLenscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#groupLen().() { |
| 99 | grpLen = s.groupLenscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#groupLen().() |
| 100 | matchingEnd = -1 |
| 101 | |
| 102 | // Slide up as much as possible and merge with adjacent groups. |
| 103 | for s.slideGroupUpscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#slideGroupUp().() { |
| 104 | if !so.prevGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#prevGroup().() { |
| 105 | panic("scanner sync broken") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | minEnd = s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. |
| 110 | if so.groupLenscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#groupLen().() > 0 { |
| 111 | matchingEnd = s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. |
| 112 | } |
| 113 | |
| 114 | // Slide down as much as possible and merge with adjacent groups. |
| 115 | for s.slideGroupDownscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#slideGroupDown().() { |
| 116 | if !so.nextGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#nextGroup().() { |
| 117 | panic("scanner sync broken") |
| 118 | } |
| 119 | if so.groupLenscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#groupLen().() > 0 { |
| 120 | matchingEnd = s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | switch { |
| 126 | case minEnd == s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.: |
| 127 | // no shifting possible |
| 128 | case matchingEnd != -1: |
| 129 | // found a matching group, align with it |
| 130 | for so.groupLenscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#groupLen().() == 0 { |
| 131 | if !s.slideGroupUpscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#slideGroupUp().() { |
| 132 | panic("match disappeared") |
| 133 | } |
| 134 | if !so.prevGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#prevGroup().() { |
| 135 | panic("scanner sync broken") |
| 136 | } |
| 137 | } |
| 138 | default: |
| 139 | // The group can be shifted around somewhat, we can use the possible shift range to |
| 140 | // apply heuristics that make the diff easier to read. Right now, the group is shifted |
| 141 | // to its lowest position, so we only have to consider upward shifts. |
| 142 | |
| 143 | bestShift := -1 |
| 144 | var bestScore shiftScorescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore# |
| 145 | for shift := max(minEnd, s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.-grpLen-1, s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.-maxSlidingscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/maxSliding.); shift <= s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.; shift++ { |
| 146 | score := shiftScorescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#{} |
| 147 | score.addscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#add().(measureShiftscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measureShift().(lines, shift)) |
| 148 | score.addscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#add().(measureShiftscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measureShift().(lines, shift-grpLen)) |
| 149 | if bestShift == -1 || score.cmpscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#cmp().(bestScore) <= 0 { |
| 150 | bestShift = shift |
| 151 | bestScore = score |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | for s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. > bestShift { |
| 156 | if !s.slideGroupUpscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#slideGroupUp().() { |
| 157 | panic("best shift not found") |
| 158 | } |
| 159 | if !so.prevGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#prevGroup().() { |
| 160 | panic("scanner sync broken") |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if so.nextGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#nextGroup().() { |
| 167 | panic("scanner sync broken") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | type scannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner# struct { |
| 172 | startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start. int // First changed line of the current group if non-empty, or unchanged line if empty. |
| 173 | endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. int // First unchanged line after the group. For an empty group, start == end. |
| 174 | linesscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#lines. []byteviewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/.ByteViewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/ByteView# |
| 175 | rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r. []bool |
| 176 | } |
| 177 | |
| 178 | func newScannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/newScanner().(lines []byteviewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/.ByteViewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/ByteView#, r []bool) *scannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner# { |
| 179 | return &scannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#{ |
| 180 | startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.: -1, |
| 181 | endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.: -1, |
| 182 | linesscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#lines.: lines, |
| 183 | rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.: r, |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // groupLen returns the length of the current group. |
| 188 | func (s *scannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#) groupLenscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#groupLen().() int { return s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. - s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start. } |
| 189 | |
| 190 | // nextGroup moves s to the nextGroup (possibly empty) group and returns true. Returns false if |
| 191 | // the end is reached. |
| 192 | func (s *scannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#) nextGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#nextGroup().() bool { |
| 193 | if s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. == len(s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.)-1 { |
| 194 | return false |
| 195 | } |
| 196 | s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start., s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. = s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.+1, s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.+1 |
| 197 | for s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. < len(s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.)-1 && s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.[s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.] { |
| 198 | s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.++ |
| 199 | } |
| 200 | return true |
| 201 | } |
| 202 | |
| 203 | // prevGroup moves g to the previous (possibly empty) group and return true. Returns true if the |
| 204 | // beginning is reached. |
| 205 | func (s *scannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#) prevGroupscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#prevGroup().() bool { |
| 206 | if s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start. == 0 { |
| 207 | return false |
| 208 | } |
| 209 | s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start., s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. = s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-1, s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-1 |
| 210 | for s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start. > 0 && s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.[s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-1] { |
| 211 | s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-- |
| 212 | } |
| 213 | return true |
| 214 | } |
| 215 | |
| 216 | // slideGroupDown tried to slide g down by one. If the slide up connects g with another group at below |
| 217 | // it, it merges the two groups. Returns true if sliding up was possible and false if the group |
| 218 | // could not be slid up. |
| 219 | func (s *scannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#) slideGroupDownscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#slideGroupDown().() bool { |
| 220 | if s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. < len(s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.)-1 && s.linesscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#lines.[s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.] == s.linesscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#lines.[s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.] { |
| 221 | s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.[s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.], s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.[s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.] = false, true |
| 222 | s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.++ |
| 223 | s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.++ |
| 224 | for s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end. < len(s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.)-1 && s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.[s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.] { |
| 225 | s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.++ |
| 226 | } |
| 227 | return true |
| 228 | } else { |
| 229 | return false |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // slideGroupUp tries to slide g up by one. If the slide up connects g with another group above it, it |
| 234 | // merges the two groups. Returns true if sliding up was possible and false if the group could not |
| 235 | // be slid up. |
| 236 | func (s *scannerscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#) slideGroupUpscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#slideGroupUp().() bool { |
| 237 | if s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start. > 0 && s.linesscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#lines.[s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-1] == s.linesscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#lines.[s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.-1] { |
| 238 | s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.[s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-1], s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.[s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.-1] = true, false |
| 239 | s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-- |
| 240 | s.endscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#end.-- |
| 241 | for s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start. > 0 && s.rscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#r.[s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-1] { |
| 242 | s.startscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/scanner#start.-- |
| 243 | } |
| 244 | return true |
| 245 | } else { |
| 246 | return false |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | type measurescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure# struct { |
| 251 | endOfFilescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#endOfFile. bool |
| 252 | indentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#indent. int |
| 253 | preBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preBlank. int |
| 254 | preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. int |
| 255 | postBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postBlank. int |
| 256 | postIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postIndent. int |
| 257 | } |
| 258 | |
| 259 | func measureShiftscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measureShift().(lines []byteviewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/.ByteViewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/ByteView#, shift int) measurescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure# { |
| 260 | m := measurescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#{} |
| 261 | if shift >= len(lines) { |
| 262 | m.endOfFilescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#endOfFile. = true |
| 263 | m.indentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#indent. = -1 |
| 264 | } else { |
| 265 | m.indentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#indent. = getIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/getIndent().(lines[shift]) |
| 266 | } |
| 267 | |
| 268 | m.preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. = -1 |
| 269 | for i := shift - 1; i >= 0; i-- { |
| 270 | m.preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. = getIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/getIndent().(lines[i]) |
| 271 | if m.preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. != -1 { |
| 272 | break |
| 273 | } |
| 274 | m.preBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preBlank.++ |
| 275 | if m.preBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preBlank. == maxBlanksscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/maxBlanks. { |
| 276 | m.preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. = 0 |
| 277 | break |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | m.postIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postIndent. = -1 |
| 282 | for i := shift + 1; i < len(lines); i++ { |
| 283 | m.postIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postIndent. = getIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/getIndent().(lines[i]) |
| 284 | if m.postIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postIndent. != -1 { |
| 285 | break |
| 286 | } |
| 287 | m.postBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postBlank.++ |
| 288 | if m.postBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postBlank. == maxBlanksscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/maxBlanks. { |
| 289 | m.postIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postIndent. = 0 |
| 290 | break |
| 291 | } |
| 292 | } |
| 293 | return m |
| 294 | } |
| 295 | |
| 296 | func getIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/getIndent().(line byteviewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/.ByteViewscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/ByteView#) int { |
| 297 | indent := 0 |
| 298 | for c := range line.Bytesscip-go gomod znkr.io/diff . `znkr.io/diff/internal/byteview`/ByteView#Bytes().() { |
| 299 | switch c { |
| 300 | case ' ': |
| 301 | indent++ |
| 302 | case '\t': |
| 303 | indent += 8 - indent%8 |
| 304 | case '\n', '\v', '\r': |
| 305 | // Ignore other whitespace. |
| 306 | default: |
| 307 | return indent |
| 308 | } |
| 309 | if indent >= maxIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/maxIndent. { |
| 310 | return maxIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/maxIndent. |
| 311 | } |
| 312 | } |
| 313 | return -1 // only whitespace |
| 314 | } |
| 315 | |
| 316 | type shiftScorescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore# struct { |
| 317 | effectiveIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#effectiveIndent. int // smaller is better |
| 318 | penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. int // smaller is better |
| 319 | } |
| 320 | |
| 321 | func (s *shiftScorescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#) addscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#add().(m measurescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#) { |
| 322 | if m.preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. == 1 && m.preBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preBlank. == 0 { |
| 323 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += startOfFilePenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/startOfFilePenalty. |
| 324 | } |
| 325 | if m.endOfFilescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#endOfFile. { |
| 326 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += endOfFilePenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/endOfFilePenalty. |
| 327 | } |
| 328 | |
| 329 | postBlank := 0 |
| 330 | if m.indentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#indent. == -1 { |
| 331 | postBlank = 1 + m.postBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postBlank. |
| 332 | } |
| 333 | totalBlank := m.preBlankscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preBlank. + postBlank |
| 334 | |
| 335 | // Penalties based on nearby blank lines |
| 336 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += totalBlankWeightscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/totalBlankWeight. * totalBlank |
| 337 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += postBlankWeightscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/postBlankWeight. * postBlank |
| 338 | |
| 339 | indent := m.indentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#indent. |
| 340 | if indent == -1 { |
| 341 | indent = m.postIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postIndent. |
| 342 | } |
| 343 | |
| 344 | s.effectiveIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#effectiveIndent. += indent |
| 345 | |
| 346 | if indent == -1 || m.preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. == -1 { |
| 347 | // No additional adjustment needed. |
| 348 | } else if indent > m.preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. { |
| 349 | // The line is indented more than its predecessors. |
| 350 | if totalBlank != 0 { |
| 351 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += relativeIndentWithBlankPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeIndentWithBlankPenalty. |
| 352 | } else { |
| 353 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. = relativeIndentPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeIndentPenalty. |
| 354 | } |
| 355 | } else if indent == m.preIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#preIndent. { |
| 356 | // Same indentation as previous line, no adjustments need. |
| 357 | } else { |
| 358 | // Line is indented more than its predecessor. It could be the block terminator of the |
| 359 | // previous block, but it could also be the start of a new block (e.g., an "else" block, or |
| 360 | // maybe the previous block didn't have a block terminator). Try to distinguish those cases |
| 361 | // based on what comes next. |
| 362 | if m.postIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postIndent. != -1 && m.postIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/measure#postIndent. > indent { |
| 363 | // The following line is indented more. So it's likely that this line is the start of a |
| 364 | // block. |
| 365 | if totalBlank != 0 { |
| 366 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += relativeOutdentWithBlankPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeOutdentWithBlankPenalty. |
| 367 | } else { |
| 368 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += relativeOutdentPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeOutdentPenalty. |
| 369 | } |
| 370 | } else { |
| 371 | if totalBlank != 0 { |
| 372 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += relativeDedentWithBlankPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeDedentWithBlankPenalty. |
| 373 | } else { |
| 374 | s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. += relativeDedentPenaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/relativeDedentPenalty. |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | func (s *shiftScorescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#) cmpscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#cmp().(t shiftScorescip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#) int { |
| 381 | return indentWeightscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/indentWeight.*cmpscip-go gomod github.com/golang/go/src go1.24.13 cmp/.Comparescip-go gomod github.com/golang/go/src go1.24.13 cmp/Compare().(s.effectiveIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#effectiveIndent., t.effectiveIndentscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#effectiveIndent.) + s.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. - t.penaltyscip-go gomod znkr.io/diff . `znkr.io/diff/internal/indentheuristic`/shiftScore#penalty. |
| 382 | } |