Skip to content

Commit

Permalink
[filesystem] Fix missing "out of sync error"
Browse files Browse the repository at this point in the history
which should be shown if one try to save pending editor changes to a file which was modified in background.

Fixes #7102
  • Loading branch information
AlexTugarev authored and akosyakov committed Feb 12, 2020
1 parent 46a2d51 commit 574bf69
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
15 changes: 12 additions & 3 deletions packages/core/src/common/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ import { ApplicationError } from './application-error';
export interface Resource extends Disposable {
readonly uri: URI;
readContents(options?: { encoding?: string }): Promise<string>;
/**
* Use `Resource.save` to apply changes.
*/
saveContents?(content: string, options?: { encoding?: string }): Promise<void>;
saveContentChanges?(changes: TextDocumentContentChangeEvent[], options?: { encoding?: string }): Promise<void>;
/**
* Use `Resource.save` to apply changes.
*
* Returns whether incremental changes can be applied to the current state.
* If not then `saveContents` should be used.
*/
saveContentChanges?(changes: TextDocumentContentChangeEvent[], options?: { encoding?: string }): Promise<void | boolean>;
readonly onDidChangeContents?: Event<void>;
guessEncoding?(): Promise<string | undefined>
}
Expand All @@ -55,12 +64,12 @@ export namespace Resource {
return false;
}
try {
await resource.saveContentChanges(context.changes, context.options);
const result = await resource.saveContentChanges(context.changes, context.options);
return typeof result === 'boolean' ? result : true;
} catch (e) {
console.error(e);
return false;
}
return true;
}
export function shouldSaveContent({ content, changes }: SaveContext): boolean {
if (!changes) {
Expand Down
25 changes: 20 additions & 5 deletions packages/filesystem/src/browser/file-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,33 @@ export class FileResource implements Resource {
}
}
protected async doSaveContents(content: string, options?: { encoding?: string }): Promise<FileStat> {
const stat = await this.getFileStat();
const stat = this.stat || await this.getFileStat();
if (stat) {
return this.fileSystem.setContent(stat, content, options);
try {
return await this.fileSystem.setContent(stat, content, options);
} catch (e) {
if (FileSystemError.FileNotFound.is(e)) {

This comment has been minimized.

Copy link
@akosyakov

akosyakov Feb 12, 2020

Member

could be inverted to avoid duplication of createFile line

return this.fileSystem.createFile(this.uriString, { content, ...options });
}
throw e;
}
}
return this.fileSystem.createFile(this.uriString, { content, ...options });
}

async saveContentChanges(changes: TextDocumentContentChangeEvent[], options?: { encoding?: string, overwriteEncoding?: string }): Promise<void> {
async saveContentChanges(changes: TextDocumentContentChangeEvent[], options?: { encoding?: string, overwriteEncoding?: string }): Promise<boolean> {
if (!this.stat) {
throw new Error(this.uriString + ' has not been read yet');
return false;
}
try {
this.stat = await this.fileSystem.updateContent(this.stat, changes, options);
return true;
} catch (e) {
if (FileSystemError.FileNotFound.is(e)) {
return false;
}
throw e;
}
this.stat = await this.fileSystem.updateContent(this.stat, changes, options);
}

async guessEncoding(): Promise<string | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion packages/filesystem/src/node/node-filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class FileSystemNode implements FileSystem {
if (stat.isDirectory) {
throw FileSystemError.FileIsDirectory(file.uri, 'Cannot set the content.');
}
if (!this.checkInSync(file, stat)) {
if (!(await this.isInSync(file, stat))) {
throw this.createOutOfSyncError(file, stat);
}
if (contentChanges.length === 0 && !(options && options.overwriteEncoding)) {
Expand Down

0 comments on commit 574bf69

Please sign in to comment.