-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sandeep Somavarapu
authored and
Sandeep Somavarapu
committed
Sep 12, 2019
1 parent
2b5da56
commit 889c7a8
Showing
6 changed files
with
192 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
111 changes: 111 additions & 0 deletions
111
src/vs/workbench/contrib/userData/browser/userDataPreviewEditorContribution.ts
This file contains 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,111 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { Disposable, MutableDisposable } from 'vs/base/common/lifecycle'; | ||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; | ||
import * as editorCommon from 'vs/editor/common/editorCommon'; | ||
import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; | ||
import { FloatingClickWidget } from 'vs/workbench/browser/parts/editor/editorWidgets'; | ||
import { localize } from 'vs/nls'; | ||
import { IUserDataSyncService, SETTINGS_PREVIEW_RESOURCE } from 'vs/workbench/services/userData/common/userData'; | ||
import { isEqual } from 'vs/base/common/resources'; | ||
import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; | ||
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles'; | ||
import { INotificationService } from 'vs/platform/notification/common/notification'; | ||
import { EditorOption } from 'vs/editor/common/config/editorOptions'; | ||
|
||
export class AcceptChangesController extends Disposable implements editorCommon.IEditorContribution { | ||
|
||
private static readonly ID = 'editor.contrib.sync.acceptChanges'; | ||
|
||
static get(editor: ICodeEditor): AcceptChangesController { | ||
return editor.getContribution<AcceptChangesController>(AcceptChangesController.ID); | ||
} | ||
|
||
private readonly acceptChangesWidgetRenderer: MutableDisposable<AcceptChangesWidgetRenderer>; | ||
|
||
constructor( | ||
private editor: ICodeEditor, | ||
@IInstantiationService private readonly instantiationService: IInstantiationService | ||
) { | ||
super(); | ||
|
||
this.acceptChangesWidgetRenderer = this._register(new MutableDisposable<AcceptChangesWidgetRenderer>()); | ||
this._register(this.editor.onDidChangeModel(() => this.update())); | ||
this.update(); | ||
} | ||
|
||
getId(): string { | ||
return AcceptChangesController.ID; | ||
} | ||
|
||
private update(): void { | ||
if (this.isInterestingEditorModel()) { | ||
if (!this.acceptChangesWidgetRenderer.value) { | ||
this.acceptChangesWidgetRenderer.value = this.instantiationService.createInstance(AcceptChangesWidgetRenderer, this.editor); | ||
} | ||
} else { | ||
this.acceptChangesWidgetRenderer.clear(); | ||
} | ||
} | ||
|
||
private isInterestingEditorModel(): boolean { | ||
const model = this.editor.getModel(); | ||
if (!model) { | ||
return false; | ||
} | ||
return isEqual(model.uri, SETTINGS_PREVIEW_RESOURCE, false); | ||
} | ||
} | ||
|
||
export class AcceptChangesWidgetRenderer extends Disposable { | ||
|
||
constructor( | ||
private readonly editor: ICodeEditor, | ||
@IInstantiationService instantiationService: IInstantiationService, | ||
@IUserDataSyncService private readonly userDataSyncService: IUserDataSyncService, | ||
@IEditorService private readonly editorService: IEditorService, | ||
@ITextFileService private readonly textFileService: ITextFileService, | ||
@INotificationService private readonly notificationService: INotificationService | ||
) { | ||
super(); | ||
|
||
const floatingClickWidget = this._register(instantiationService.createInstance(FloatingClickWidget, editor, localize('Accept', "Accept & Sync"), null)); | ||
this._register(floatingClickWidget.onClick(() => this.acceptChanges())); | ||
floatingClickWidget.render(); | ||
} | ||
|
||
private async acceptChanges(): Promise<void> { | ||
// Do not accept if editor is readonly | ||
if (this.editor.getOption(EditorOption.readOnly)) { | ||
return; | ||
} | ||
|
||
const model = this.editor.getModel(); | ||
if (model) { | ||
// Disable updating | ||
this.editor.updateOptions({ readOnly: true }); | ||
// Save the preview | ||
await this.textFileService.save(model.uri); | ||
|
||
try { | ||
// Apply Preview | ||
await this.userDataSyncService.apply(model.uri); | ||
} catch (error) { | ||
this.notificationService.error(error); | ||
// Enable updating | ||
this.editor.updateOptions({ readOnly: false }); | ||
return; | ||
} | ||
|
||
// Close all preview editors | ||
const editorInputs = this.editorService.editors.filter(input => isEqual(input.getResource(), model.uri)); | ||
for (const input of editorInputs) { | ||
input.dispose(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains 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 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.