@@ -684,16 +684,119 @@ function selectedDiff(git) {
684684 return [ committed && `# Committed against ${ git . baseRef || 'base' } \n${ committed } ` , working && `# Working tree\n${ working } ` ] . filter ( Boolean ) . join ( '\n\n' ) ;
685685}
686686
687+ /*
688+ * Adapted from MyAppDesk/GitCito src/main/grounding.ts at
689+ * 0bab066640ea4d73f4f7e5a580644031f125c1f3 (MIT). Splitting the unified
690+ * diff into bounded hunks gives the browser the same stable path/line units
691+ * used by Persephone's owner-side evidence resolver.
692+ */
693+ function buildDiffEvidence ( diff , options = { } ) {
694+ const maxBytes = options . maxBytes ?? 24000 ;
695+ const maxHunks = options . maxHunks ?? 40 ;
696+ const maxHunkBytes = options . maxHunkBytes ?? 4000 ;
697+ const items = [ ] ;
698+ let omitted = 0 ;
699+ let used = 0 ;
700+ let path = '' ;
701+ let scope = '' ;
702+ let head = null ;
703+ let body = [ ] ;
704+ let startLine = 0 ;
705+ let endLine = 0 ;
706+ let newLine = 0 ;
707+ let oldLine = 0 ;
708+ let sawNew = false ;
709+
710+ const flush = ( ) => {
711+ if ( head === null ) return ;
712+ let text = [ head , ...body ] . join ( '\n' ) ;
713+ if ( text . length > maxHunkBytes ) text = `${ text . slice ( 0 , maxHunkBytes ) } \n…(hunk truncated)` ;
714+ head = null ;
715+ body = [ ] ;
716+ if ( items . length >= maxHunks || used + text . length > maxBytes ) {
717+ omitted += 1 ;
718+ return ;
719+ }
720+ used += text . length ;
721+ items . push ( { id : `E${ items . length + 1 } ` , path, scope, startLine, endLine : Math . max ( startLine , endLine ) , text } ) ;
722+ } ;
723+
724+ for ( const line of String ( diff || '' ) . split ( '\n' ) ) {
725+ if ( line . startsWith ( '# ' ) ) {
726+ flush ( ) ;
727+ scope = line . slice ( 2 ) . trim ( ) ;
728+ continue ;
729+ }
730+ const file = line . match ( / ^ d i f f - - g i t a \/ ( .+ ?) b \/ ( .+ ) $ / ) ;
731+ if ( file ) {
732+ flush ( ) ;
733+ path = file [ 2 ] ;
734+ continue ;
735+ }
736+ if ( line . startsWith ( '+++ ' ) ) {
737+ const target = line . slice ( 4 ) . trim ( ) ;
738+ if ( target !== '/dev/null' ) path = target . replace ( / ^ b \/ / , '' ) ;
739+ continue ;
740+ }
741+ if ( line . startsWith ( '--- ' ) ) {
742+ const source = line . slice ( 4 ) . trim ( ) ;
743+ if ( ! path && source !== '/dev/null' ) path = source . replace ( / ^ a \/ / , '' ) ;
744+ continue ;
745+ }
746+ const hunk = line . match ( / ^ @ @ - ( \d + ) (?: , ( \d + ) ) ? \+ ( \d + ) (?: , ( \d + ) ) ? @ @ / ) ;
747+ if ( hunk ) {
748+ flush ( ) ;
749+ oldLine = Number ( hunk [ 1 ] ) ;
750+ newLine = Number ( hunk [ 3 ] ) ;
751+ sawNew = Number ( hunk [ 4 ] ?? '1' ) > 0 ;
752+ startLine = sawNew ? newLine : oldLine ;
753+ endLine = startLine ;
754+ head = line ;
755+ continue ;
756+ }
757+ if ( head === null ) continue ;
758+ body . push ( line ) ;
759+ if ( line . startsWith ( '+' ) ) {
760+ endLine = newLine ;
761+ newLine += 1 ;
762+ } else if ( line . startsWith ( '-' ) ) {
763+ if ( ! sawNew ) endLine = oldLine ;
764+ oldLine += 1 ;
765+ } else if ( line . startsWith ( ' ' ) || line === '' ) {
766+ endLine = sawNew ? newLine : oldLine ;
767+ newLine += 1 ;
768+ oldLine += 1 ;
769+ }
770+ }
771+ flush ( ) ;
772+ return { items, omitted } ;
773+ }
774+
687775function renderDiff ( source , fileFilter = '' ) {
688- let active = ! fileFilter ;
689- const lines = source . split ( '\n' ) ;
776+ const evidence = buildDiffEvidence ( source , {
777+ maxBytes : 250000 ,
778+ maxHunks : 400 ,
779+ maxHunkBytes : 20000 ,
780+ } ) ;
781+ const hunks = fileFilter ? evidence . items . filter ( ( item ) => item . path === fileFilter ) : evidence . items ;
782+ let lines = hunks . length
783+ ? hunks . flatMap ( ( item ) => [
784+ `# ${ item . scope ? `${ item . scope } · ` : '' } ${ item . id } · ${ item . path } :${ item . startLine } ${ item . endLine > item . startLine ? `-${ item . endLine } ` : '' } ` ,
785+ ...item . text . split ( '\n' ) ,
786+ ] )
787+ : String ( source || '' ) . split ( '\n' ) ;
788+ if ( ! hunks . length && fileFilter ) {
789+ let active = false ;
790+ lines = lines . filter ( ( line ) => {
791+ if ( line . startsWith ( 'diff --git ' ) ) {
792+ const match = line . match ( / ^ d i f f - - g i t a \/ ( .+ ) b \/ ( .+ ) $ / ) ;
793+ active = match ?. [ 1 ] === fileFilter || match ?. [ 2 ] === fileFilter ;
794+ }
795+ return active || line . startsWith ( '# ' ) ;
796+ } ) ;
797+ }
690798 const rendered = [ ] ;
691799 for ( const line of lines ) {
692- if ( line . startsWith ( 'diff --git ' ) ) {
693- const match = line . match ( / ^ d i f f - - g i t a \/ ( .+ ) b \/ ( .+ ) $ / ) ;
694- active = ! fileFilter || match ?. [ 1 ] === fileFilter || match ?. [ 2 ] === fileFilter ;
695- }
696- if ( ! active && ! line . startsWith ( '# ' ) ) continue ;
697800 let kind = 'context' ;
698801 if ( line . startsWith ( '@@' ) ) kind = 'hunk' ;
699802 else if ( line . startsWith ( '+++' ) || line . startsWith ( '---' ) || line . startsWith ( 'diff --git' ) || line . startsWith ( 'index ' ) ) kind = 'meta' ;
@@ -702,6 +805,9 @@ function renderDiff(source, fileFilter = '') {
702805 else if ( line . startsWith ( '# ' ) ) kind = 'scope' ;
703806 rendered . push ( `<span class="${ kind } "><i>${ esc ( diffMarker ( line ) ) } </i><code>${ esc ( line || ' ' ) } </code></span>` ) ;
704807 }
808+ if ( evidence . omitted > 0 ) {
809+ rendered . push ( `<span class="meta"><i> </i><code>${ esc ( `${ evidence . omitted } further hunk(s) omitted` ) } </code></span>` ) ;
810+ }
705811 return `<pre class="dio-roboomp-diff">${ rendered . join ( '' ) } </pre>` ;
706812}
707813
@@ -826,29 +932,34 @@ function paletteEntries() {
826932 return entries ;
827933}
828934
829- function fuzzyScore ( value , query ) {
830- const source = String ( value || '' ) . toLowerCase ( ) ;
831- const needle = String ( query || '' ) . trim ( ) . toLowerCase ( ) ;
832- if ( ! needle ) return 0 ;
833- const exact = source . indexOf ( needle ) ;
834- if ( exact >= 0 ) return 10000 - exact * 10 - source . length ;
835- let cursor = 0 ;
836- let gap = 0 ;
837- for ( const character of needle ) {
838- const found = source . indexOf ( character , cursor ) ;
839- if ( found < 0 ) return - 1 ;
840- gap += found - cursor ;
841- cursor = found + 1 ;
935+ /* Adapted from GitCito's CommandPalette fuzzyScore at the pinned MIT revision. */
936+ function fuzzyScore ( query , text ) {
937+ if ( ! query ) return 0 ;
938+ const q = String ( query ) . toLowerCase ( ) ;
939+ const t = String ( text ) . toLowerCase ( ) ;
940+ let score = 0 ;
941+ let ti = 0 ;
942+ let previousMatch = - 2 ;
943+ for ( let qi = 0 ; qi < q . length ; qi += 1 ) {
944+ const character = q [ qi ] ;
945+ const found = t . indexOf ( character , ti ) ;
946+ if ( found === - 1 ) return null ;
947+ score += 1 ;
948+ if ( found === previousMatch + 1 ) score += 4 ;
949+ if ( found === 0 || / [ \s / _ \- . ] / . test ( t [ found - 1 ] ) ) score += 3 ;
950+ score -= Math . min ( found - ti , 3 ) * 0.3 ;
951+ previousMatch = found ;
952+ ti = found + 1 ;
842953 }
843- return 1000 - gap - source . length ;
954+ return score ;
844955}
845956
846957function paletteResultsMarkup ( query ) {
847958 const needle = String ( query || '' ) . trim ( ) ;
848959 if ( ! needle ) return '' ;
849960 const matches = paletteEntries ( )
850- . map ( ( entry ) => ( { ...entry , score : fuzzyScore ( `${ entry . label } ${ entry . detail } ${ entry . reference } ` , needle ) } ) )
851- . filter ( ( entry ) => entry . score >= 0 )
961+ . map ( ( entry ) => ( { ...entry , score : fuzzyScore ( needle , `${ entry . label } ${ entry . detail } ${ entry . reference } ` ) } ) )
962+ . filter ( ( entry ) => entry . score !== null )
852963 . sort ( ( left , right ) => right . score - left . score || left . label . localeCompare ( right . label ) )
853964 . slice ( 0 , 12 ) ;
854965 return matches . map ( ( entry ) => `<button type="button" data-robo-palette-item="${ esc ( entry . type ) } " data-robo-palette-reference="${ esc ( entry . reference ) } "><span><strong>${ esc ( entry . label ) } </strong><small>${ esc ( entry . detail ) } </small></span><em>${ esc ( entry . type ) } </em></button>` ) . join ( '' ) || '<p>No loaded repository item matches.</p>' ;
0 commit comments