Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 75 additions & 10 deletions packages/ai-chat/src/browser/change-set-file-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export class ChangeSetFileElement implements ChangeSetElement {
protected readonly toDispose = new DisposableCollection();
protected _state: ChangeSetElementState;

protected originalContent: string | undefined;
private _originalContent: string | undefined;
protected _initialized = false;
protected _initializationPromise: Promise<void> | undefined;

protected readonly onDidChangeEmitter = new Emitter<void>();
readonly onDidChange = this.onDidChangeEmitter.event;
Expand All @@ -85,12 +87,38 @@ export class ChangeSetFileElement implements ChangeSetElement {

@postConstruct()
init(): void {
this._initializationPromise = this.initializeAsync();
this.toDispose.push(this.onDidChangeEmitter);
}

protected async initializeAsync(): Promise<void> {
await this.obtainOriginalContent();
this.listenForOriginalFileChanges();
this._initialized = true;
}

/**
* Ensures that the element is fully initialized before proceeding.
* This includes loading the original content from the file system.
*/
async ensureInitialized(): Promise<void> {
if (this._initializationPromise) {
await this._initializationPromise;
}
}

/**
* Returns true if the element has been fully initialized.
*/
get isInitialized(): boolean {
return this._initialized;
}

protected async obtainOriginalContent(): Promise<void> {
this.originalContent = await this.changeSetFileService.read(this.uri);
this.readOnlyResource.update({ contents: this.originalContent ?? '' });
this._originalContent = await this.changeSetFileService.read(this.uri);
if (this._readOnlyResource) {
this.readOnlyResource.update({ contents: this._originalContent ?? '' });
}
}

protected getInMemoryUri(uri: URI): ConfigurableMutableReferenceResource {
Expand All @@ -103,7 +131,7 @@ export class ChangeSetFileElement implements ChangeSetElement {
// If we are applied, the tricky thing becomes the question what to revert to; otherwise, what to apply.
const newContent = await this.changeSetFileService.read(this.uri).catch(() => '');
this.readOnlyResource.update({ contents: newContent });
if (newContent === this.originalContent) {
if (newContent === this._originalContent) {
this.state = 'pending';
} else if (newContent === this.targetState) {
this.state = 'applied';
Expand All @@ -120,10 +148,19 @@ export class ChangeSetFileElement implements ChangeSetElement {
protected get readOnlyResource(): ConfigurableMutableReferenceResource {
if (!this._readOnlyResource) {
this._readOnlyResource = this.getInMemoryUri(ChangeSetFileElement.toReadOnlyUri(this.uri, this.elementProps.chatSessionId));
this._readOnlyResource.update({ autosaveable: false, readOnly: true });
this._readOnlyResource.update({
autosaveable: false,
readOnly: true,
contents: this._originalContent ?? ''
});
this.toDispose.push(this._readOnlyResource);
this.obtainOriginalContent();
this.listenForOriginalFileChanges();

// If not yet initialized, update the resource once initialization completes
if (!this._initialized && this._initializationPromise) {
this._initializationPromise.then(() => {
this._readOnlyResource?.update({ contents: this._originalContent ?? '' });
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will set the contents to the original value when we load the content without waiting for it here, won't we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but only if we were initialized before the readonly resource was accessed. I think this call here still makes sense.

}
return this._readOnlyResource;
}
Expand Down Expand Up @@ -180,22 +217,41 @@ export class ChangeSetFileElement implements ChangeSetElement {
return this.elementProps.data;
};

get originalContent(): string | undefined {
if (!this._initialized && this._initializationPromise) {
console.warn('Accessing originalContent before initialization is complete. Consider using async methods.');
}
return this._originalContent;
}

/**
* Gets the original content of the file asynchronously.
* Ensures initialization is complete before returning the content.
*/
async getOriginalContent(): Promise<string | undefined> {
await this.ensureInitialized();
return this._originalContent;
}

get targetState(): string {
return this.elementProps.targetState ?? '';
}

async open(): Promise<void> {
await this.ensureInitialized();
this.changeSetFileService.open(this);
}

async openChange(): Promise<void> {
await this.ensureInitialized();
this.changeSetFileService.openDiff(
this.readOnlyUri,
this.changedUri
);
}

async apply(contents?: string): Promise<void> {
await this.ensureInitialized();
if (!await this.confirm('Apply')) { return; }
if (!(await this.changeSetFileService.trySave(this.changedUri))) {
if (this.type === 'delete') {
Expand All @@ -214,16 +270,25 @@ export class ChangeSetFileElement implements ChangeSetElement {
}

onShow(): void {
this.changeResource.update({ contents: this.targetState, onSave: content => this.writeChanges(content) });
// Ensure we have the latest state when showing
if (!this._initialized && this._initializationPromise) {
this._initializationPromise.then(() => {
// Update the change resource with the correct target state after initialization
this.changeResource.update({ contents: this.targetState, onSave: content => this.writeChanges(content) });
});
} else {
this.changeResource.update({ contents: this.targetState, onSave: content => this.writeChanges(content) });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to wait for initialization to handle anything related to changeResource?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately yes, because writeChanges triggers the file system listener from listenForOriginalFileChanges, which uses the original content to determine the state of the element. I moved the check directly to listener now, I think it makes more sense to have it there directly.

}
}

async revert(): Promise<void> {
await this.ensureInitialized();
if (!await this.confirm('Revert')) { return; }
this.state = 'pending';
if (this.type === 'add') {
await this.changeSetFileService.delete(this.uri);
} else if (this.originalContent) {
await this.changeSetFileService.write(this.uri, this.originalContent);
} else if (this._originalContent) {
await this.changeSetFileService.write(this.uri, this._originalContent);
}
}

Expand Down
Loading