eng - move the document content provider part out of document/editor management

This commit is contained in:
Johannes Rieken
2017-08-15 19:24:18 +02:00
parent 20a45eef3a
commit e909c33059
7 changed files with 188 additions and 118 deletions

View File

@@ -15,6 +15,7 @@ import pkg from 'vs/platform/node/package';
import { ExtHostFileSystemEventService } from 'vs/workbench/api/node/extHostFileSystemEventService';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/node/extHostDocumentsAndEditors';
import { ExtHostDocuments } from 'vs/workbench/api/node/extHostDocuments';
import { ExtHostDocumentContentProvider } from 'vs/workbench/api/node/extHostDocumentContentProviders';
import { ExtHostDocumentSaveParticipant } from 'vs/workbench/api/node/extHostDocumentSaveParticipant';
import { ExtHostConfiguration } from 'vs/workbench/api/node/extHostConfiguration';
import { ExtHostDiagnostics } from 'vs/workbench/api/node/extHostDiagnostics';
@@ -82,6 +83,7 @@ export function createApiFactory(
const extHostDebugService = col.define(ExtHostContext.ExtHostDebugService).set<ExtHostDebugService>(new ExtHostDebugService(threadService));
const extHostDocumentsAndEditors = col.define(ExtHostContext.ExtHostDocumentsAndEditors).set<ExtHostDocumentsAndEditors>(new ExtHostDocumentsAndEditors(threadService));
const extHostDocuments = col.define(ExtHostContext.ExtHostDocuments).set<ExtHostDocuments>(new ExtHostDocuments(threadService, extHostDocumentsAndEditors));
const extHostDocumentContentProviders = col.define(ExtHostContext.ExtHostDocumentContentProviders).set<ExtHostDocumentContentProvider>(new ExtHostDocumentContentProvider(threadService, extHostDocumentsAndEditors));
const extHostDocumentSaveParticipant = col.define(ExtHostContext.ExtHostDocumentSaveParticipant).set<ExtHostDocumentSaveParticipant>(new ExtHostDocumentSaveParticipant(extHostDocuments, threadService.get(MainContext.MainThreadWorkspace)));
const extHostEditors = col.define(ExtHostContext.ExtHostEditors).set<ExtHostEditors>(new ExtHostEditors(threadService, extHostDocumentsAndEditors));
const extHostCommands = col.define(ExtHostContext.ExtHostCommands).set<ExtHostCommands>(new ExtHostCommands(threadService, extHostHeapService));
@@ -428,9 +430,6 @@ export function createApiFactory(
});
});
},
registerTextDocumentContentProvider(scheme: string, provider: vscode.TextDocumentContentProvider) {
return extHostDocuments.registerTextDocumentContentProvider(scheme, provider);
},
onDidOpenTextDocument: (listener, thisArgs?, disposables?) => {
return extHostDocuments.onDidAddDocument(listener, thisArgs, disposables);
},
@@ -452,6 +451,9 @@ export function createApiFactory(
getConfiguration: (section?: string, resource?: vscode.Uri): vscode.WorkspaceConfiguration => {
return extHostConfiguration.getConfiguration(section, <URI>resource);
},
registerTextDocumentContentProvider(scheme: string, provider: vscode.TextDocumentContentProvider) {
return extHostDocumentContentProviders.registerTextDocumentContentProvider(scheme, provider);
},
registerTaskProvider: (type: string, provider: vscode.TaskProvider) => {
return extHostTask.registerTaskProvider(extension, provider);
},

View File

@@ -147,16 +147,18 @@ export abstract class MainThreadDiagnosticsShape {
$clear(owner: string): TPromise<any> { throw ni(); }
}
export abstract class MainThreadDocumentContentProvidersShape {
$registerTextContentProvider(handle: number, scheme: string): void { throw ni(); }
$unregisterTextContentProvider(handle: number): void { throw ni(); }
$onVirtualDocumentChange(uri: URI, value: ITextSource): void { throw ni(); }
}
export abstract class MainThreadDocumentsShape {
$tryCreateDocument(options?: { language?: string; content?: string; }): TPromise<any> { throw ni(); }
$tryOpenDocument(uri: URI): TPromise<any> { throw ni(); }
$registerTextContentProvider(handle: number, scheme: string): void { throw ni(); }
$onVirtualDocumentChange(uri: URI, value: ITextSource): void { throw ni(); }
$unregisterTextContentProvider(handle: number): void { throw ni(); }
$trySaveDocument(uri: URI): TPromise<boolean> { throw ni(); }
}
export interface ISelectionChangeEvent {
selections: Selection[];
source?: string;
@@ -390,6 +392,10 @@ export abstract class ExtHostDiagnosticsShape {
}
export abstract class ExtHostDocumentContentProvidersShape {
$provideTextDocumentContent(handle: number, uri: URI): TPromise<string> { throw ni(); }
}
export interface IModelAddedData {
url: URI;
versionId: number;
@@ -399,7 +405,6 @@ export interface IModelAddedData {
isDirty: boolean;
}
export abstract class ExtHostDocumentsShape {
$provideTextDocumentContent(handle: number, uri: URI): TPromise<string> { throw ni(); }
$acceptModelModeChanged(strURL: string, oldModeId: string, newModeId: string): void { throw ni(); }
$acceptModelSaved(strURL: string): void { throw ni(); }
$acceptDirtyStateChanged(strURL: string, isDirty: boolean): void { throw ni(); }
@@ -552,6 +557,7 @@ export const MainContext = {
MainThreadDebugService: createMainId<MainThreadDebugServiceShape>('MainThreadDebugService', MainThreadDebugServiceShape),
MainThreadDiagnostics: createMainId<MainThreadDiagnosticsShape>('MainThreadDiagnostics', MainThreadDiagnosticsShape),
MainThreadDocuments: createMainId<MainThreadDocumentsShape>('MainThreadDocuments', MainThreadDocumentsShape),
MainThreadDocumentContentProviders: createMainId<MainThreadDocumentContentProvidersShape>('MainThreadDocumentContentProviders', MainThreadDocumentContentProvidersShape),
MainThreadEditors: createMainId<MainThreadEditorsShape>('MainThreadEditors', MainThreadEditorsShape),
MainThreadErrors: createMainId<MainThreadErrorsShape>('MainThreadErrors', MainThreadErrorsShape),
MainThreadTreeViews: createMainId<MainThreadTreeViewsShape>('MainThreadTreeViews', MainThreadTreeViewsShape),
@@ -579,6 +585,7 @@ export const ExtHostContext = {
ExtHostDebugService: createExtId<ExtHostDebugServiceShape>('ExtHostDebugService', ExtHostDebugServiceShape),
ExtHostDocumentsAndEditors: createExtId<ExtHostDocumentsAndEditorsShape>('ExtHostDocumentsAndEditors', ExtHostDocumentsAndEditorsShape),
ExtHostDocuments: createExtId<ExtHostDocumentsShape>('ExtHostDocuments', ExtHostDocumentsShape),
ExtHostDocumentContentProviders: createExtId<ExtHostDocumentContentProvidersShape>('ExtHostDocumentContentProviders', ExtHostDocumentContentProvidersShape),
ExtHostDocumentSaveParticipant: createExtId<ExtHostDocumentSaveParticipantShape>('ExtHostDocumentSaveParticipant', ExtHostDocumentSaveParticipantShape),
ExtHostEditors: createExtId<ExtHostEditorsShape>('ExtHostEditors', ExtHostEditorsShape),
ExtHostTreeViews: createExtId<ExtHostTreeViewsShape>('ExtHostTreeViews', ExtHostTreeViewsShape),

View File

@@ -0,0 +1,90 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { onUnexpectedError } from 'vs/base/common/errors';
import * as editorCommon from 'vs/editor/common/editorCommon';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
import URI from 'vs/base/common/uri';
import { IDisposable } from 'vs/base/common/lifecycle';
import { Disposable } from 'vs/workbench/api/node/extHostTypes';
import { TPromise } from 'vs/base/common/winjs.base';
import * as vscode from 'vscode';
import { asWinJsPromise } from 'vs/base/common/async';
import { TextSource } from 'vs/editor/common/model/textSource';
import { MainContext, ExtHostDocumentContentProvidersShape, MainThreadDocumentContentProvidersShape } from './extHost.protocol';
import { ExtHostDocumentsAndEditors } from './extHostDocumentsAndEditors';
export class ExtHostDocumentContentProvider extends ExtHostDocumentContentProvidersShape {
private static _handlePool = 0;
private readonly _documentContentProviders = new Map<number, vscode.TextDocumentContentProvider>();
private readonly _proxy: MainThreadDocumentContentProvidersShape;
private readonly _documentsAndEditors: ExtHostDocumentsAndEditors;
constructor(threadService: IThreadService, documentsAndEditors: ExtHostDocumentsAndEditors) {
super();
this._proxy = threadService.get(MainContext.MainThreadDocumentContentProviders);
this._documentsAndEditors = documentsAndEditors;
}
dispose(): void {
// todo@joh
}
registerTextDocumentContentProvider(scheme: string, provider: vscode.TextDocumentContentProvider): vscode.Disposable {
if (scheme === 'file' || scheme === 'untitled') {
throw new Error(`scheme '${scheme}' already registered`);
}
const handle = ExtHostDocumentContentProvider._handlePool++;
this._documentContentProviders.set(handle, provider);
this._proxy.$registerTextContentProvider(handle, scheme);
let subscription: IDisposable;
if (typeof provider.onDidChange === 'function') {
subscription = provider.onDidChange(uri => {
if (this._documentsAndEditors.getDocument(uri.toString())) {
this.$provideTextDocumentContent(handle, <URI>uri).then(value => {
const document = this._documentsAndEditors.getDocument(uri.toString());
if (!document) {
// disposed in the meantime
return;
}
// create lines and compare
const textSource = TextSource.fromString(value, editorCommon.DefaultEndOfLine.CRLF);
// broadcast event when content changed
if (!document.equalLines(textSource)) {
return this._proxy.$onVirtualDocumentChange(<URI>uri, textSource);
}
}, onUnexpectedError);
}
});
}
return new Disposable(() => {
if (this._documentContentProviders.delete(handle)) {
this._proxy.$unregisterTextContentProvider(handle);
}
if (subscription) {
subscription.dispose();
subscription = undefined;
}
});
}
$provideTextDocumentContent(handle: number, uri: URI): TPromise<string> {
const provider = this._documentContentProviders.get(handle);
if (!provider) {
return TPromise.wrapError<string>(new Error(`unsupported uri-scheme: ${uri.scheme}`));
}
return asWinJsPromise(token => provider.provideTextDocumentContent(uri, token));
}
}

View File

@@ -4,17 +4,12 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import { onUnexpectedError } from 'vs/base/common/errors';
import * as editorCommon from 'vs/editor/common/editorCommon';
import Event, { Emitter } from 'vs/base/common/event';
import URI from 'vs/base/common/uri';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { Disposable } from 'vs/workbench/api/node/extHostTypes';
import * as TypeConverters from './extHostTypeConverters';
import { TPromise } from 'vs/base/common/winjs.base';
import * as vscode from 'vscode';
import { asWinJsPromise } from 'vs/base/common/async';
import { TextSource } from 'vs/editor/common/model/textSource';
import { MainContext, MainThreadDocumentsShape, ExtHostDocumentsShape, IMainContext } from './extHost.protocol';
import { ExtHostDocumentData, setWordDefinitionFor } from './extHostDocumentData';
import { ExtHostDocumentsAndEditors } from './extHostDocumentsAndEditors';
@@ -22,8 +17,6 @@ import { IModelChangedEvent } from 'vs/editor/common/model/mirrorModel';
export class ExtHostDocuments extends ExtHostDocumentsShape {
private static _handlePool: number = 0;
private _onDidAddDocument = new Emitter<vscode.TextDocument>();
private _onDidRemoveDocument = new Emitter<vscode.TextDocument>();
private _onDidChangeDocument = new Emitter<vscode.TextDocumentChangeEvent>();
@@ -38,8 +31,6 @@ export class ExtHostDocuments extends ExtHostDocumentsShape {
private _proxy: MainThreadDocumentsShape;
private _documentsAndEditors: ExtHostDocumentsAndEditors;
private _documentLoader = new Map<string, TPromise<ExtHostDocumentData>>();
private _documentContentProviders = new Map<number, vscode.TextDocumentContentProvider>();
constructor(mainContext: IMainContext, documentsAndEditors: ExtHostDocumentsAndEditors) {
super();
@@ -105,59 +96,6 @@ export class ExtHostDocuments extends ExtHostDocumentsShape {
return this._proxy.$tryCreateDocument(options);
}
public registerTextDocumentContentProvider(scheme: string, provider: vscode.TextDocumentContentProvider): vscode.Disposable {
if (scheme === 'file' || scheme === 'untitled') {
throw new Error(`scheme '${scheme}' already registered`);
}
const handle = ExtHostDocuments._handlePool++;
this._documentContentProviders.set(handle, provider);
this._proxy.$registerTextContentProvider(handle, scheme);
let subscription: IDisposable;
if (typeof provider.onDidChange === 'function') {
subscription = provider.onDidChange(uri => {
if (this._documentsAndEditors.getDocument(uri.toString())) {
this.$provideTextDocumentContent(handle, <URI>uri).then(value => {
const document = this._documentsAndEditors.getDocument(uri.toString());
if (!document) {
// disposed in the meantime
return;
}
// create lines and compare
const textSource = TextSource.fromString(value, editorCommon.DefaultEndOfLine.CRLF);
// broadcast event when content changed
if (!document.equalLines(textSource)) {
return this._proxy.$onVirtualDocumentChange(<URI>uri, textSource);
}
}, onUnexpectedError);
}
});
}
return new Disposable(() => {
if (this._documentContentProviders.delete(handle)) {
this._proxy.$unregisterTextContentProvider(handle);
}
if (subscription) {
subscription.dispose();
subscription = undefined;
}
});
}
public $provideTextDocumentContent(handle: number, uri: URI): TPromise<string> {
const provider = this._documentContentProviders.get(handle);
if (!provider) {
return TPromise.wrapError<string>(new Error(`unsupported uri-scheme: ${uri.scheme}`));
}
return asWinJsPromise(token => provider.provideTextDocumentContent(uri, token));
}
public $acceptModelModeChanged(strURL: string, oldModeId: string, newModeId: string): void {
let data = this._documentsAndEditors.getDocument(strURL);