@@ -7,7 +7,7 @@ import { assertFn, checkAdjacentItems } from 'vs/base/common/assert';
77import { CharCode } from 'vs/base/common/charCode' ;
88import { Position } from 'vs/editor/common/core/position' ;
99import { Range } from 'vs/editor/common/core/range' ;
10- import { SequenceFromIntArray , OffsetRange , SequenceDiff , ISequence } from 'vs/editor/common/diff/algorithms/diffAlgorithm' ;
10+ import { OffsetRange , SequenceDiff , ISequence } from 'vs/editor/common/diff/algorithms/diffAlgorithm' ;
1111import { DynamicProgrammingDiffing } from 'vs/editor/common/diff/algorithms/dynamicProgrammingDiffing' ;
1212import { optimizeSequenceDiffs } from 'vs/editor/common/diff/algorithms/joinSequenceDiffs' ;
1313import { MyersDiffAlgorithm } from 'vs/editor/common/diff/algorithms/myersDiffAlgorithm' ;
@@ -34,10 +34,10 @@ export class StandardLinesDiffComputer implements ILinesDiffComputer {
3434 const srcDocLines = originalLines . map ( ( l ) => getOrCreateHash ( l . trim ( ) ) ) ;
3535 const tgtDocLines = modifiedLines . map ( ( l ) => getOrCreateHash ( l . trim ( ) ) ) ;
3636
37- const sequence1 = new SequenceFromIntArray ( srcDocLines ) ;
38- const sequence2 = new SequenceFromIntArray ( tgtDocLines ) ;
37+ const sequence1 = new LineSequence ( srcDocLines , originalLines ) ;
38+ const sequence2 = new LineSequence ( tgtDocLines , modifiedLines ) ;
3939
40- const lineAlignments = ( ( ) => {
40+ let lineAlignments = ( ( ) => {
4141 if ( sequence1 . length + sequence2 . length < 1500 ) {
4242 // Use the improved algorithm for small files
4343 return this . dynamicProgrammingDiffing . compute (
@@ -58,6 +58,8 @@ export class StandardLinesDiffComputer implements ILinesDiffComputer {
5858 ) ;
5959 } ) ( ) ;
6060
61+ lineAlignments = optimizeSequenceDiffs ( sequence1 , sequence2 , lineAlignments ) ;
62+
6163 const alignments : RangeMapping [ ] = [ ] ;
6264
6365 const scanForWhitespaceChanges = ( equalLinesCount : number ) => {
@@ -182,6 +184,35 @@ function* group<T>(items: Iterable<T>, shouldBeGrouped: (item1: T, item2: T) =>
182184 }
183185}
184186
187+ export class LineSequence implements ISequence {
188+ constructor (
189+ private readonly trimmedHash : number [ ] ,
190+ private readonly lines : string [ ]
191+ ) { }
192+
193+ getElement ( offset : number ) : number {
194+ return this . trimmedHash [ offset ] ;
195+ }
196+
197+ get length ( ) : number {
198+ return this . trimmedHash . length ;
199+ }
200+
201+ getBoundaryScore ( length : number ) : number {
202+ const indentationBefore = length === 0 ? 0 : getIndentation ( this . lines [ length - 1 ] ) ;
203+ const indentationAfter = length === this . lines . length ? 0 : getIndentation ( this . lines [ length ] ) ;
204+ return 1000 - ( indentationBefore + indentationAfter ) ;
205+ }
206+ }
207+
208+ function getIndentation ( str : string ) : number {
209+ let i = 0 ;
210+ while ( i < str . length && ( str . charCodeAt ( i ) === CharCode . Space || str . charCodeAt ( i ) === CharCode . Tab ) ) {
211+ i ++ ;
212+ }
213+ return i ;
214+ }
215+
185216class Slice implements ISequence {
186217 private readonly elements : Int32Array ;
187218 private readonly firstCharOnLineOffsets : Int32Array ;
0 commit comments