Skip to content
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

Implement onWillSaveTextDocument plugin api event handler. #4328

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion packages/monaco/src/browser/monaco-editor-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument {

autoSave: 'on' | 'off' = 'on';
autoSaveDelay: number = 500;
readonly onWillSaveLoopTimeOut = 1500;

protected model: monaco.editor.IModel;
protected readonly resolveModel: Promise<void>;
Expand Down Expand Up @@ -344,7 +345,7 @@ export class MonacoEditorModel implements ITextEditorModel, TextEditorDocument {
const timeoutPromise = new Promise((resolve, reject) => timeoutHandle = <any>setTimeout(() => {
didTimeout = true;
reject(new Error('onWillSave listener loop timeout'));
}, 1000));
}, this.onWillSaveLoopTimeOut));

const firing = this.onWillSaveModelEmitter.sequence(async listener => {
if (shouldStop()) {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/src/api/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ export interface ModelChangedEvent {
export interface DocumentsExt {
$acceptModelModeChanged(startUrl: UriComponents, oldModeId: string, newModeId: string): void;
$acceptModelSaved(strUrl: UriComponents): void;
$acceptModelWillSave(strUrl: UriComponents, reason: theia.TextDocumentSaveReason): Promise<SingleEditOperation[]>;
$acceptDirtyStateChanged(strUrl: UriComponents, isDirty: boolean): void;
$acceptModelChanged(strUrl: UriComponents, e: ModelChangedEvent, isDirty: boolean): void;
}
Expand Down
14 changes: 13 additions & 1 deletion packages/plugin-ext/src/main/browser/documents-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class DocumentsMainImpl implements DocumentsMain {
modelService: EditorModelService,
rpc: RPCProtocol,
private editorManger: EditorManager,
private openerService: OpenerService
private openerService: OpenerService,
olexii4 marked this conversation as resolved.
Show resolved Hide resolved
) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.DOCUMENTS_EXT);

Expand All @@ -53,6 +53,18 @@ export class DocumentsMainImpl implements DocumentsMain {
this.toDispose.push(modelService.onModelSaved(m => {
this.proxy.$acceptModelSaved(m.textEditorModel.uri);
}));
this.toDispose.push(modelService.onModelWillSave(onWillSaveModelEvent => {
onWillSaveModelEvent.waitUntil(new Promise<monaco.editor.IIdentifiedSingleEditOperation[]>(async resolve => {
const edits = await this.proxy.$acceptModelWillSave(onWillSaveModelEvent.model.textEditorModel.uri, onWillSaveModelEvent.reason);
const transformedEdits = edits.map((edit): monaco.editor.IIdentifiedSingleEditOperation =>
({
range: monaco.Range.lift(edit.range),
text: edit.text!,
forceMoveMarkers: edit.forceMoveMarkers
}));
resolve(transformedEdits);
}));
}));
this.toDispose.push(modelService.onModelDirtyChanged(m => {
this.proxy.$acceptDirtyStateChanged(m.textEditorModel.uri, m.dirty);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { Event, Emitter } from '@theia/core';
import { MonacoEditorModel } from '@theia/monaco/lib/browser/monaco-editor-model';
import { MonacoEditorModel, WillSaveMonacoModelEvent } from '@theia/monaco/lib/browser/monaco-editor-model';
import { injectable, inject } from 'inversify';
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
import { MonacoWorkspace } from '@theia/monaco/lib/browser/monaco-workspace';
Expand All @@ -25,6 +25,7 @@ export interface EditorModelService {
onModelRemoved: Event<MonacoEditorModel>;
onModelModeChanged: Event<{ model: MonacoEditorModel, oldModeId: string }>;

onModelWillSave: Event<WillSaveMonacoModelEvent>;
onModelDirtyChanged: Event<MonacoEditorModel>;
onModelSaved: Event<MonacoEditorModel>;

Expand All @@ -39,11 +40,13 @@ export class EditorModelServiceImpl implements EditorModelService {
private onModelRemovedEmitter = new Emitter<MonacoEditorModel>();
private modelDirtyEmitter = new Emitter<MonacoEditorModel>();
private modelSavedEmitter = new Emitter<MonacoEditorModel>();
private onModelWillSavedEmitter = new Emitter<WillSaveMonacoModelEvent>();

onModelDirtyChanged: Event<MonacoEditorModel> = this.modelDirtyEmitter.event;
onModelSaved: Event<MonacoEditorModel> = this.modelSavedEmitter.event;
onModelModeChanged = this.modelModeChangedEmitter.event;
onModelRemoved = this.onModelRemovedEmitter.event;
onModelWillSave = this.onModelWillSavedEmitter.event;

constructor(@inject(MonacoTextModelService) monacoModelService: MonacoTextModelService,
@inject(MonacoWorkspace) monacoWorkspace: MonacoWorkspace) {
Expand All @@ -70,6 +73,9 @@ export class EditorModelServiceImpl implements EditorModelService {
model.onDirtyChanged(_ => {
this.modelDirtyEmitter.fire(model);
});
model.onWillSaveModel(willSaveModelEvent => {
this.onModelWillSavedEmitter.fire(willSaveModelEvent);
});
}

get onModelAdded(): Event<MonacoEditorModel> {
Expand Down
34 changes: 33 additions & 1 deletion packages/plugin-ext/src/plugin/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { DocumentsExt, ModelChangedEvent, PLUGIN_RPC_CONTEXT, DocumentsMain } from '../api/plugin-api';
import { DocumentsExt, ModelChangedEvent, PLUGIN_RPC_CONTEXT, DocumentsMain, SingleEditOperation } from '../api/plugin-api';
import URI from 'vscode-uri';
import { UriComponents } from '../common/uri-components';
import { RPCProtocol } from '../api/rpc-protocol';
Expand All @@ -24,18 +24,21 @@ import { EditorsAndDocumentsExtImpl } from './editors-and-documents';
import * as Converter from './type-converters';
import { DisposableCollection } from '@theia/core/lib/common/disposable';
import { Range, TextDocumentShowOptions } from '../api/model';
import { TextEdit } from './types-impl';

export class DocumentsExtImpl implements DocumentsExt {
private toDispose = new DisposableCollection();
private _onDidAddDocument = new Emitter<theia.TextDocument>();
private _onDidRemoveDocument = new Emitter<theia.TextDocument>();
private _onDidChangeDocument = new Emitter<theia.TextDocumentChangeEvent>();
private _onDidSaveTextDocument = new Emitter<theia.TextDocument>();
private _onWillSaveTextDocument = new Emitter<theia.TextDocumentWillSaveEvent>();

readonly onDidAddDocument: Event<theia.TextDocument> = this._onDidAddDocument.event;
readonly onDidRemoveDocument: Event<theia.TextDocument> = this._onDidRemoveDocument.event;
readonly onDidChangeDocument: Event<theia.TextDocumentChangeEvent> = this._onDidChangeDocument.event;
readonly onDidSaveTextDocument: Event<theia.TextDocument> = this._onDidSaveTextDocument.event;
readonly onWillSaveTextDocument: Event<theia.TextDocumentWillSaveEvent> = this._onWillSaveTextDocument.event;

private proxy: DocumentsMain;
private loadingDocuments = new Map<string, Promise<DocumentDataExt | undefined>>();
Expand Down Expand Up @@ -78,6 +81,35 @@ export class DocumentsExtImpl implements DocumentsExt {
this._onDidSaveTextDocument.fire(data.document);
}
}
$acceptModelWillSave(strUrl: UriComponents, reason: theia.TextDocumentSaveReason): Promise<SingleEditOperation[]> {
return new Promise<SingleEditOperation[]>((resolve, reject) => {
const uri = URI.revive(strUrl);
const uriString = uri.toString();
const data = this.editorsAndDocuments.getDocument(uriString);
if (data) {
const onWillSaveEvent: theia.TextDocumentWillSaveEvent = {
document: data.document,
reason: reason,
/* tslint:disable:no-any */
waitUntil: async (editsPromise: PromiseLike<theia.TextEdit[] | any>) => {
const editsObjs = await editsPromise;
if (this.isTextEditArray(editsObjs)) {
const editOperations: SingleEditOperation[] = (editsObjs as theia.TextEdit[]).map(textEdit => Converter.fromTextEdit(textEdit));
resolve(editOperations);
} else {
resolve([]);
}
}
};
this._onWillSaveTextDocument.fire(onWillSaveEvent);
}
});
}

isTextEditArray(obj: any): obj is theia.TextEdit[] {
return Array.isArray(obj) && obj.every((elem: any) => TextEdit.isTextEdit(elem));
}

$acceptDirtyStateChanged(strUrl: UriComponents, isDirty: boolean): void {
const uri = URI.revive(strUrl);
const uriString = uri.toString();
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,7 @@ export function createAPIFactory(
return documents.onDidAddDocument(listener, thisArg, disposables);
},
onWillSaveTextDocument(listener, thisArg?, disposables?) {
// TODO to implement
return { dispose: () => { } };
return documents.onWillSaveTextDocument(listener, thisArg, disposables);
},
onDidSaveTextDocument(listener, thisArg?, disposables?) {
return documents.onDidSaveTextDocument(listener, thisArg, disposables);
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/types-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ export class TextEdit {
if (!thing) {
return false;
}
return Range.isRange((<TextEdit>thing))
return Range.isRange((<TextEdit>thing).range)
&& typeof (<TextEdit>thing).newText === 'string';
}

Expand Down