Skip to content

Commit 74dc2bd

Browse files
committed
Update file contents that are changed by a non-post-save compile
1 parent 5404c39 commit 74dc2bd

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
495495
overwrite: boolean;
496496
}
497497
): void | Thenable<void> {
498+
const originalUriString = uri.toString();
499+
const originalUri = vscode.Uri.parse(originalUriString);
500+
this._needsUpdate.delete(originalUriString);
498501
uri = redirectDotvscodeRoot(uri, new vscode.FileSystemError("Server does not have a /_vscode web application"));
499502
if (uri.path.startsWith("/.")) {
500503
throw new vscode.FileSystemError("dot-folders are not supported by server");
@@ -635,13 +638,13 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
635638
// is part of the "write" operation from VS Code's point of view. This is required
636639
// to prevent concurreny issues when VS Code refreshs its internal representaton of
637640
// the file system while documents are being compiled.
638-
return this.compile(uri, entry, update);
641+
return this.compile(originalUri, entry, update);
639642
} else if (update) {
640643
// The file's contents may have changed as a result of the save,
641644
// so make sure VS Code updates the editor tab contents if needed
642-
this._needsUpdate.add(uri.toString());
645+
this._needsUpdate.add(originalUriString);
643646
} else if (!created) {
644-
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
647+
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: originalUri });
645648
}
646649
});
647650
}
@@ -865,6 +868,9 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
865868
*/
866869
public async compile(uri: vscode.Uri, file?: File, update?: boolean): Promise<void> {
867870
if (!uri || uri.scheme != FILESYSTEM_SCHEMA) return;
871+
const originalUriString = uri.toString();
872+
const originalUri = vscode.Uri.parse(originalUriString);
873+
this._needsUpdate.delete(originalUriString);
868874
uri = redirectDotvscodeRoot(uri, new vscode.FileSystemError("Server does not have a /_vscode web application"));
869875
const compileList: string[] = [];
870876
try {
@@ -912,17 +918,36 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
912918
})
913919
.catch(() => compileErrorMsg(conf))
914920
);
915-
if (update || (file && filesToUpdate.includes(file.fileName))) {
921+
if (file && (update || filesToUpdate.includes(file.fileName))) {
916922
// This file was just written and the write may have changed its contents or the
917923
// compilation changed its contents. Therefore, we must force VS Code to update it.
918-
this._needsUpdate.add(uri.toString());
924+
this._needsUpdate.add(originalUriString);
925+
}
926+
if (filesToUpdate.length) {
927+
// Force VS Code to refresh the active editor tab's contents if the file was changed
928+
// by compilation and is NOT the file that was saved if this is a post-save compilation
929+
const activeDoc = vscode.window.activeTextEditor?.document;
930+
if (activeDoc && !activeDoc.isDirty && !activeDoc.isClosed) {
931+
const activeDocUriString = activeDoc.uri.toString();
932+
if (
933+
!(file && activeDocUriString == originalUriString) &&
934+
filesToUpdate.some(
935+
(f) =>
936+
DocumentContentProvider.getUri(f, undefined, undefined, undefined, originalUri)?.toString() ==
937+
activeDocUriString
938+
)
939+
) {
940+
// Force VS Code to refresh the contents in the active editor tab
941+
vscode.commands.executeCommand("workbench.action.files.revert");
942+
}
943+
}
919944
}
920945
// Fire file changed events for all files changed by compilation
921946
this._fireSoon(
922947
...filesToUpdate.map((f) => {
923948
return {
924949
type: vscode.FileChangeType.Changed,
925-
uri: DocumentContentProvider.getUri(f, undefined, undefined, undefined, uri),
950+
uri: DocumentContentProvider.getUri(f, undefined, undefined, undefined, originalUri),
926951
};
927952
})
928953
);
@@ -940,7 +965,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
940965
).map((f: string) => {
941966
return {
942967
type: vscode.FileChangeType.Changed,
943-
uri: DocumentContentProvider.getUri(f, undefined, undefined, undefined, uri),
968+
uri: DocumentContentProvider.getUri(f, undefined, undefined, undefined, originalUri),
944969
};
945970
})
946971
);

0 commit comments

Comments
 (0)