forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Auto save native editor notebook #7831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DonJayamanne
merged 23 commits into
microsoft:master
from
DonJayamanne:autoSaveNotebook
Oct 10, 2019
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2c05a8b
Initial commit
DonJayamanne 76c7d4c
Oops
DonJayamanne edef45f
Oops again
DonJayamanne dda9986
Updates
DonJayamanne ea443f5
Misc changes
DonJayamanne c96f818
Oops
DonJayamanne 52be829
Bug fixes
DonJayamanne b3f5b50
Fixes
DonJayamanne b2df34e
Added tests
DonJayamanne f37a3af
Revert unnecessary formatting changes
DonJayamanne 83dc1f7
Added news entry
DonJayamanne 355b827
remove try catch
DonJayamanne cf22f3c
Fix sonar cloud issue
DonJayamanne 639347a
Skip sonar cloud message
DonJayamanne 2815fa0
Code review changes
DonJayamanne 1dd857f
Oops
DonJayamanne b71a3f2
Fixes
DonJayamanne 17cb1b1
Fixes
DonJayamanne 0acd884
Code review changes
DonJayamanne 182b01c
Sonar fixes
DonJayamanne aca10ed
Fix code review comments
DonJayamanne e44fe92
Address code review comments
DonJayamanne 0e366e7
Oops
DonJayamanne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added ability to auto-save chagnes made to the notebook. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/client/datascience/interactive-ipynb/autoSaveService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { inject, injectable } from 'inversify'; | ||
import { ConfigurationChangeEvent, Event, EventEmitter, TextEditor, Uri } from 'vscode'; | ||
import { IApplicationShell, IDocumentManager, IWorkspaceService } from '../../common/application/types'; | ||
import '../../common/extensions'; | ||
import { traceError } from '../../common/logger'; | ||
import { IFileSystem } from '../../common/platform/types'; | ||
import { IDisposable } from '../../common/types'; | ||
import { INotebookIdentity, InteractiveWindowMessages } from '../interactive-common/interactiveWindowTypes'; | ||
import { FileSettings, IInteractiveWindowListener, INotebookEditor, INotebookEditorProvider } from '../types'; | ||
|
||
// tslint:disable: no-any | ||
|
||
/** | ||
* Sends notifications to Notebooks to save the notebook. | ||
* Based on auto save settings, this class will regularly check for changes and send a save requet. | ||
* If window state changes or active editor changes, then notify notebooks (if auto save is configured to do so). | ||
* Monitor save and modified events on editor to determine its current dirty state. | ||
* | ||
* @export | ||
* @class AutoSaveService | ||
* @implements {IInteractiveWindowListener} | ||
*/ | ||
@injectable() | ||
export class AutoSaveService implements IInteractiveWindowListener { | ||
private postEmitter: EventEmitter<{ message: string; payload: any }> = new EventEmitter<{ message: string; payload: any }>(); | ||
private disposables: IDisposable[] = []; | ||
private notebookUri?: Uri; | ||
private timeout?: ReturnType<typeof setTimeout>; | ||
constructor( | ||
@inject(IApplicationShell) appShell: IApplicationShell, | ||
@inject(IDocumentManager) documentManager: IDocumentManager, | ||
@inject(INotebookEditorProvider) private readonly notebookProvider: INotebookEditorProvider, | ||
@inject(IFileSystem) private readonly fileSystem: IFileSystem, | ||
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService | ||
) { | ||
this.workspace.onDidChangeConfiguration(this.onSettingsChanded.bind(this), this, this.disposables); | ||
this.disposables.push(appShell.onDidChangeWindowState(this.onDidChangeWindowState.bind(this))); | ||
this.disposables.push(documentManager.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor.bind(this))); | ||
} | ||
|
||
public get postMessage(): Event<{ message: string; payload: any }> { | ||
return this.postEmitter.event; | ||
} | ||
|
||
public onMessage(message: string, payload?: any): void { | ||
if (message === InteractiveWindowMessages.NotebookIdentity) { | ||
this.notebookUri = Uri.parse((payload as INotebookIdentity).resource); | ||
} | ||
if (message === InteractiveWindowMessages.LoadAllCellsComplete) { | ||
const notebook = this.getNotebook(); | ||
if (!notebook) { | ||
traceError(`Received message ${message}, but there is no notebook for ${this.notebookUri ? this.notebookUri.fsPath : undefined}`); | ||
return; | ||
} | ||
this.disposables.push(notebook.modified(this.onNotebookModified, this, this.disposables)); | ||
this.disposables.push(notebook.saved(this.onNotebookSaved, this, this.disposables)); | ||
} | ||
} | ||
public dispose(): void | undefined { | ||
this.disposables.filter(item => !!item).forEach(item => item.dispose()); | ||
this.clearTimeout(); | ||
} | ||
private onNotebookModified(_: INotebookEditor) { | ||
// If we haven't started a timer, then start if necessary. | ||
if (!this.timeout) { | ||
this.setTimer(); | ||
} | ||
} | ||
private onNotebookSaved(_: INotebookEditor) { | ||
// If we haven't started a timer, then start if necessary. | ||
if (!this.timeout) { | ||
this.setTimer(); | ||
} | ||
} | ||
private getNotebook(): INotebookEditor | undefined { | ||
const uri = this.notebookUri; | ||
if (!uri) { | ||
return; | ||
} | ||
return this.notebookProvider.editors.find(item => this.fileSystem.arePathsSame(item.file.fsPath, uri.fsPath)); | ||
} | ||
private getAutoSaveSettings(): FileSettings { | ||
const filesConfig = this.workspace.getConfiguration('files', this.notebookUri); | ||
return { | ||
autoSave: filesConfig.get('autoSave', 'off'), | ||
autoSaveDelay: filesConfig.get('autoSaveDelay', 1000) | ||
}; | ||
} | ||
private onSettingsChanded(e: ConfigurationChangeEvent) { | ||
if (e.affectsConfiguration('files.autoSave') || e.affectsConfiguration('files.autoSaveDelay')) { | ||
// Reset the timer, as we may have increased it, turned it off or other. | ||
this.clearTimeout(); | ||
this.setTimer(); | ||
} | ||
} | ||
private setTimer() { | ||
const settings = this.getAutoSaveSettings(); | ||
if (!settings || settings.autoSave === 'off') { | ||
return; | ||
} | ||
if (settings && settings.autoSave === 'afterDelay') { | ||
// Add a timeout to save after n milli seconds. | ||
// Do not use setInterval, as that will cause all handlers to queue up. | ||
this.timeout = setTimeout(() => { | ||
this.save(); | ||
}, settings.autoSaveDelay); | ||
} | ||
} | ||
private clearTimeout() { | ||
if (this.timeout) { | ||
clearTimeout(this.timeout); | ||
this.timeout = undefined; | ||
} | ||
} | ||
private save() { | ||
this.clearTimeout(); | ||
const notebook = this.getNotebook(); | ||
if (notebook && notebook.isDirty) { | ||
// Notify webview to perform a save. | ||
this.postEmitter.fire({ message: InteractiveWindowMessages.DoSave, payload: undefined }); | ||
} else { | ||
this.setTimer(); | ||
} | ||
} | ||
private onDidChangeWindowState() { | ||
const settings = this.getAutoSaveSettings(); | ||
if (settings && settings.autoSave === 'onWindowChange') { | ||
this.save(); | ||
} | ||
} | ||
private onDidChangeActiveTextEditor(_e?: TextEditor) { | ||
const settings = this.getAutoSaveSettings(); | ||
if (settings && settings.autoSave === 'onFocusChange') { | ||
this.save(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.