Skip to content

Commit 3164594

Browse files
authored
Fix implicit context variables for notebook cells (microsoft#285858)
* Fix implicit context variables for notebook cells * Address comments
1 parent a45fa66 commit 3164594

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

src/vs/workbench/contrib/chat/browser/attachments/chatImplicitContext.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import { ILanguageModelIgnoredFilesService } from '../../common/ignoredFiles.js'
2929
import { getPromptsTypeForLanguageId } from '../../common/promptSyntax/promptTypes.js';
3030
import { IChatWidget, IChatWidgetService } from '../chat.js';
3131
import { IChatContextService } from '../contextContrib/chatContextService.js';
32+
import { ITextModel } from '../../../../../editor/common/model.js';
33+
import { IRange } from '../../../../../editor/common/core/range.js';
3234

3335
export 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+
270283
export class ChatImplicitContext extends Disposable implements IChatRequestImplicitVariableEntry {
271284
get id() {
272285
if (URI.isUri(this.value)) {

src/vs/workbench/contrib/chat/browser/attachments/implicitContextAttachment.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Disposable, DisposableStore } from '../../../../../base/common/lifecycl
1414
import { Schemas } from '../../../../../base/common/network.js';
1515
import { basename, dirname } from '../../../../../base/common/resources.js';
1616
import { URI } from '../../../../../base/common/uri.js';
17-
import { Location } from '../../../../../editor/common/languages.js';
17+
import { isLocation, Location } from '../../../../../editor/common/languages.js';
1818
import { ILanguageService } from '../../../../../editor/common/languages/language.js';
1919
import { IModelService } from '../../../../../editor/common/services/model.js';
2020
import { localize } from '../../../../../nls.js';
@@ -214,7 +214,11 @@ export class ImplicitContextAttachmentWidget extends Disposable {
214214
this.attachmentModel.addContext(context);
215215
} else {
216216
const file = URI.isUri(this.attachment.value) ? this.attachment.value : this.attachment.value.uri;
217-
this.attachmentModel.addFile(file);
217+
if (file.scheme === Schemas.vscodeNotebookCell && isLocation(this.attachment.value)) {
218+
this.attachmentModel.addFile(file, this.attachment.value.range);
219+
} else {
220+
this.attachmentModel.addFile(file);
221+
}
218222
}
219223
this.widgetRef()?.focusInput();
220224
}

0 commit comments

Comments
 (0)