Skip to content

Commit

Permalink
[prompt snippets]: cleanup changes in ChatDynamicVariableModel
Browse files Browse the repository at this point in the history
  • Loading branch information
legomushroom committed Dec 11, 2024
1 parent f29ed30 commit 7e8cc5e
Showing 1 changed file with 23 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ export class ChatDynamicVariableModel extends Disposable implements IChatWidgetC
) {
super();

this.updateDecorations = this.updateDecorations.bind(this);


this.updateDecorations = this.updateDecorations.bind(this);

this._register(widget.inputEditor.onDidChangeModelContent(e => {
e.changes.forEach(c => {
// Don't mutate entries in _variables, since they will be returned from the getter
Expand All @@ -79,7 +74,7 @@ export class ChatDynamicVariableModel extends Disposable implements IChatWidgetC
}

// dispose the reference if possible before dropping it off
if ('dispose' in ref) {
if ('dispose' in ref && typeof ref.dispose === 'function') {
ref.dispose();
}

Expand Down Expand Up @@ -113,9 +108,7 @@ export class ChatDynamicVariableModel extends Disposable implements IChatWidgetC
s = [];
}

this.disposeVariables();
this.disposeVariables();
this._variables = s;
this._variables = s.filter(isDynamicVariable);
this.updateDecorations();
}

Expand Down Expand Up @@ -147,40 +140,25 @@ export class ChatDynamicVariableModel extends Disposable implements IChatWidgetC
})));
}

private getHoverForReference(variable: IDynamicVariable): IMarkdownString | IMarkdownString[] {
const result: IMarkdownString[] = [];
const { data } = variable;

if (isLocation(data)) {
const prefix = variable.fullName ? ` ${variable.fullName}` : '';
const rangeString = `#${data.range.startLineNumber}-${data.range.endLineNumber}`;
return new MarkdownString(prefix + this.labelService.getUriLabel(data.uri, { relative: true }) + rangeString);
}

if (!URI.isUri(data)) {
return result;
private getHoverForReference(ref: IDynamicVariable): IMarkdownString | undefined {
const value = ref.data;
if (URI.isUri(value)) {
return new MarkdownString(this.labelService.getUriLabel(value, { relative: true }));
} else if (isLocation(value)) {
const prefix = ref.fullName ? ` ${ref.fullName}` : '';
const rangeString = `#${value.range.startLineNumber}-${value.range.endLineNumber}`;
return new MarkdownString(prefix + this.labelService.getUriLabel(value.uri, { relative: true }) + rangeString);
} else {
return undefined;
}

result.push(new MarkdownString(
`${this.labelService.getUriLabel(data, { relative: true })}`,
));

// if reference has nested child file references, include them in the label
for (const childUri of variable.validFileReferenceUris ?? []) {
result.push(new MarkdownString(
` • ${this.labelService.getUriLabel(childUri, { relative: true })}`,
));
}

return result;
}

/**
* Dispose all existing variables.
*/
private disposeVariables(): void {
for (const variable of this._variables) {
if ('dispose' in variable) {
if ('dispose' in variable && typeof variable.dispose === 'function') {
variable.dispose();
}
}
Expand All @@ -192,6 +170,16 @@ export class ChatDynamicVariableModel extends Disposable implements IChatWidgetC
}
}

/**
* Loose check to filter objects that are obviously missing data
*/
function isDynamicVariable(obj: any): obj is IDynamicVariable {
return obj &&
typeof obj.id === 'string' &&
Range.isIRange(obj.range) &&
'data' in obj;
}

ChatWidget.CONTRIBS.push(ChatDynamicVariableModel);

interface SelectAndInsertActionContext {
Expand Down

0 comments on commit 7e8cc5e

Please sign in to comment.