@@ -29,6 +29,8 @@ import { ILanguageModelIgnoredFilesService } from '../../common/ignoredFiles.js'
2929import { getPromptsTypeForLanguageId } from '../../common/promptSyntax/promptTypes.js' ;
3030import { IChatWidget , IChatWidgetService } from '../chat.js' ;
3131import { IChatContextService } from '../contextContrib/chatContextService.js' ;
32+ import { ITextModel } from '../../../../../editor/common/model.js' ;
33+ import { IRange } from '../../../../../editor/common/core/range.js' ;
3234
3335export class ChatImplicitContextContribution extends Disposable implements IWorkbenchContribution {
3436 static readonly ID = 'chat.implicitContext' ;
@@ -210,18 +212,22 @@ export class ChatImplicitContextContribution extends Disposable implements IWork
210212 const selection = codeEditor ?. getSelection ( ) ;
211213 const visibleRanges = codeEditor ?. getVisibleRanges ( ) || [ ] ;
212214 newValue = activeCell . uri ;
213- if ( isEqual ( codeEditor ?. getModel ( ) ?. uri , activeCell . uri ) ) {
215+ const cellModel = codeEditor ?. getModel ( ) ;
216+ if ( cellModel && isEqual ( cellModel . uri , activeCell . uri ) ) {
214217 if ( selection && ! selection . isEmpty ( ) ) {
215218 newValue = { uri : activeCell . uri , range : selection } satisfies Location ;
216219 isSelection = true ;
217220 } else if ( visibleRanges . length > 0 ) {
218- // Merge visible ranges. Maybe the reference value could actually be an array of Locations?
219- // Something like a Location with an array of Ranges?
220- let range = visibleRanges [ 0 ] ;
221- visibleRanges . slice ( 1 ) . forEach ( r => {
222- range = range . plusRange ( r ) ;
223- } ) ;
224- newValue = { uri : activeCell . uri , range } satisfies Location ;
221+ // If the entire cell is visible, just use the cell URI, no need to specify range.
222+ if ( ! isEntireCellVisible ( cellModel , visibleRanges ) ) {
223+ // Merge visible ranges. Maybe the reference value could actually be an array of Locations?
224+ // Something like a Location with an array of Ranges?
225+ let range = visibleRanges [ 0 ] ;
226+ visibleRanges . slice ( 1 ) . forEach ( r => {
227+ range = range . plusRange ( r ) ;
228+ } ) ;
229+ newValue = { uri : activeCell . uri , range } satisfies Location ;
230+ }
225231 }
226232 }
227233 } else {
@@ -267,6 +273,13 @@ export class ChatImplicitContextContribution extends Disposable implements IWork
267273 }
268274}
269275
276+ function isEntireCellVisible ( cellModel : ITextModel , visibleRanges : IRange [ ] ) : boolean {
277+ if ( visibleRanges . length === 1 && visibleRanges [ 0 ] . startLineNumber === 1 && visibleRanges [ 0 ] . startColumn === 1 && visibleRanges [ 0 ] . endLineNumber === cellModel . getLineCount ( ) && visibleRanges [ 0 ] . endColumn === cellModel . getLineMaxColumn ( visibleRanges [ 0 ] . endLineNumber ) ) {
278+ return true ;
279+ }
280+ return false ;
281+ }
282+
270283export class ChatImplicitContext extends Disposable implements IChatRequestImplicitVariableEntry {
271284 get id ( ) {
272285 if ( URI . isUri ( this . value ) ) {
0 commit comments