fix issue where dynamic variables are checked properly when editing#297181
fix issue where dynamic variables are checked properly when editing#297181
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue where dynamic variables (file references like #file:) were not properly preserved when editing chat prompts. When a user edited a submitted prompt, the file references would be converted to plain text instead of remaining as active references.
Changes:
- Added event
onDidChangeActiveInputEditorto notify when the active input editor changes during editing - Implemented deduplication logic using range-based keys to prevent duplicate context entries
- Added dynamic variable restoration logic that recreates variable decorations when entering edit mode
- Added validation for offset ranges to prevent invalid dynamic variables
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/vs/workbench/contrib/chat/browser/widget/chatWidget.ts | Adds onDidChangeActiveInputEditor event, implements deduplication with range-based keys, and restores dynamic variables when entering edit mode |
| src/vs/workbench/contrib/chat/browser/chat.ts | Adds onDidChangeActiveInputEditor event to IChatWidget interface |
| src/vs/workbench/contrib/chat/browser/attachments/chatVariables.ts | Updates getDynamicVariables to convert attachment ranges from offset to editor positions with validation |
| src/vs/workbench/contrib/chat/browser/attachments/chatDynamicVariables.ts | Adds editor subscription management via MutableDisposable, implements range validation, and subscribes to editor changes |
Comments suppressed due to low confidence (4)
src/vs/workbench/contrib/chat/browser/attachments/chatVariables.ts:57
- The null check for editorModel is performed after trying to access it on line 43 (editorModel?.getValueLength()). If editorModel is null, the code should exit early before attempting to use it. Move this check to line 42, immediately after getting the model, before any calculations.
if (!editorModel) {
continue;
}
src/vs/workbench/contrib/chat/browser/attachments/chatDynamicVariables.ts:154
- When updateDecorations is called and there's no editor model (lines 150-154), the decorationData is cleared but this._variables is not cleared. This creates an inconsistency where _variables array may still contain references to dynamic variables that no longer have corresponding decorations.
This can lead to issues when the editor model becomes available again, as the old variables in _variables won't have matching decoration data. Consider also clearing this._variables when the model is null to maintain consistency.
const model = this.widget.inputEditor.getModel();
if (!model) {
this.decorationData = [];
return;
}
src/vs/workbench/contrib/chat/browser/widget/chatWidget.ts:1714
- The onDidChangeActiveInputEditor event is fired at line 1714, but this happens before setValue (line 1727) and before the dynamic variables are restored (lines 1729-1758). When the ChatDynamicVariableModel's onDidChangeActiveInputEditor handler fires at line 49-52 of chatDynamicVariables.ts, it calls updateDecorations which will operate on an empty or outdated editor model.
The sequence should be: 1) create/switch input editor, 2) set the value, 3) restore dynamic variables, 4) then fire the event to notify listeners that the editor is ready with its content. Consider moving the event firing to after line 1758 where dynamic variable restoration is complete.
this._onDidChangeActiveInputEditor.fire();
src/vs/workbench/contrib/chat/browser/attachments/chatDynamicVariables.ts:51
- When _subscribeToEditor is called during editor changes (line 50 in the onDidChangeActiveInputEditor handler), the existing variables in this._variables may refer to ranges in the old editor model. These ranges will be invalid in the new editor model context.
The code should clear this._variables and this.decorationData when switching to a new editor, or at minimum validate that the existing variables are still applicable to the new editor context. Currently, updateDecorations (line 51) will attempt to apply old variables to a new editor, which will likely fail or create invalid decorations.
this._register(widget.onDidChangeActiveInputEditor(() => {
this._subscribeToEditor();
this.updateDecorations();
fixes issue where dynamic variables are not properly checked and preserved.
fix #281766