11import Parser from "web-tree-sitter" ;
22import { Chunk , IDE , ILLM , Position , Range , RangeInFile } from ".." ;
33import { getAst } from "../autocomplete/util/ast" ;
4+ import { NEXT_EDIT_MODELS } from "../llm/constants" ;
45import { DocumentHistoryTracker } from "./DocumentHistoryTracker" ;
5- import {
6- NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN ,
7- NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN ,
8- } from "./constants" ;
6+ import { MODEL_WINDOW_SIZES } from "./constants" ;
97
108export enum EditableRegionStrategy {
119 Naive = "naive" ,
@@ -60,16 +58,16 @@ function naiveJump(ctx: any): RangeInFile[] | null {
6058
6159// Sliding splits the file using into sliding window.
6260function slidingJump ( ctx : any ) : RangeInFile [ ] | null {
63- const { fileLines, filepath } = ctx ;
64- if ( ! fileLines || ! filepath ) {
65- console . warn ( "Missing required context for naive jump" ) ;
61+ const { fileLines, filepath, modelName , currentCursorPos } = ctx ;
62+ if ( ! fileLines || ! filepath || ! modelName || ! currentCursorPos ) {
63+ console . warn ( "Missing required context for sliding jump" ) ;
6664 return null ;
6765 }
6866
69- const windowSize =
70- NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN +
71- NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN +
72- 1 ; // 1 for current line;
67+ const topMargin = MODEL_WINDOW_SIZES [ modelName as NEXT_EDIT_MODELS ] . topMargin ;
68+ const bottomMargin =
69+ MODEL_WINDOW_SIZES [ modelName as NEXT_EDIT_MODELS ] . bottomMargin ;
70+ const windowSize = topMargin + bottomMargin + 1 ; // 1 for current line
7371
7472 if ( fileLines . length <= windowSize ) {
7573 return [
@@ -86,29 +84,74 @@ function slidingJump(ctx: any): RangeInFile[] | null {
8684 ] ;
8785 }
8886
89- const slidingStep = Math . max ( 1 , Math . floor ( windowSize / 2 ) ) ;
9087 const ranges : RangeInFile [ ] = [ ] ;
88+ const cursorLine = currentCursorPos . line ;
9189
92- for (
93- let startLine = 0 ;
94- startLine < fileLines . length ;
95- startLine += slidingStep
96- ) {
97- const endLine = Math . min ( startLine + windowSize - 1 , fileLines . length - 1 ) ;
90+ // Create the first window centered around the cursor position
91+ const firstWindowStart = Math . max ( 0 , cursorLine - topMargin ) ;
92+ const firstWindowEnd = Math . min (
93+ fileLines . length - 1 ,
94+ cursorLine + bottomMargin ,
95+ ) ;
9896
99- ranges . push ( {
100- filepath,
101- range : {
102- start : { line : startLine , character : 0 } ,
103- end : {
104- line : endLine ,
105- character : fileLines [ endLine ] . length ,
106- } ,
97+ ranges . push ( {
98+ filepath,
99+ range : {
100+ start : { line : firstWindowStart , character : 0 } ,
101+ end : {
102+ line : firstWindowEnd ,
103+ character : fileLines [ firstWindowEnd ] . length ,
107104 } ,
108- } ) ;
105+ } ,
106+ } ) ;
107+
108+ // Alternating pattern: down once, up once, repeat
109+ const slidingStep = Math . max ( 1 , Math . floor ( windowSize / 2 ) ) ;
110+ let currentStartDown = firstWindowEnd + 1 ;
111+ let currentStartUp = firstWindowStart - slidingStep ;
112+ while ( currentStartDown < fileLines . length || currentStartUp >= 0 ) {
113+ // Go down once
114+ if ( currentStartDown < fileLines . length ) {
115+ const windowStart = currentStartDown ;
116+ const windowEnd = Math . min (
117+ windowStart + windowSize - 1 ,
118+ fileLines . length - 1 ,
119+ ) ;
120+
121+ ranges . push ( {
122+ filepath,
123+ range : {
124+ start : { line : windowStart , character : 0 } ,
125+ end : {
126+ line : windowEnd ,
127+ character : fileLines [ windowEnd ] . length ,
128+ } ,
129+ } ,
130+ } ) ;
131+
132+ currentStartDown += slidingStep ;
133+ }
134+
135+ // Go up once
136+ if ( currentStartUp >= 0 ) {
137+ const windowStart = Math . max ( 0 , currentStartUp ) ;
138+ const windowEnd = Math . min (
139+ windowStart + windowSize - 1 ,
140+ fileLines . length - 1 ,
141+ ) ;
142+
143+ ranges . push ( {
144+ filepath,
145+ range : {
146+ start : { line : windowStart , character : 0 } ,
147+ end : {
148+ line : windowEnd ,
149+ character : fileLines [ windowEnd ] . length ,
150+ } ,
151+ } ,
152+ } ) ;
109153
110- if ( endLine >= fileLines . length - 1 ) {
111- break ;
154+ currentStartUp -= slidingStep ;
112155 }
113156 }
114157
0 commit comments