Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Feb 22, 2022
1 parent 32b031e commit e240ada
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 51 deletions.
8 changes: 2 additions & 6 deletions src/vs/workbench/api/browser/mainThreadDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { ITextModel, shouldSynchronizeModel } from 'vs/editor/common/model';
import { IModelService } from 'vs/editor/common/services/model';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { IFileService, FileOperation } from 'vs/platform/files/common/files';
import { MainThreadDocumentsAndEditors } from 'vs/workbench/api/browser/mainThreadDocumentsAndEditors';
import { ExtHostContext, ExtHostDocumentsShape, MainThreadDocumentsShape } from 'vs/workbench/api/common/extHost.protocol';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
Expand Down Expand Up @@ -120,7 +119,6 @@ export class MainThreadDocuments extends Disposable implements MainThreadDocumen
private readonly _modelReferenceCollection: BoundModelReferenceCollection;

constructor(
documentsAndEditors: MainThreadDocumentsAndEditors,
extHostContext: IExtHostContext,
@IModelService private readonly _modelService: IModelService,
@ITextFileService private readonly _textFileService: ITextFileService,
Expand All @@ -137,8 +135,6 @@ export class MainThreadDocuments extends Disposable implements MainThreadDocumen

this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostDocuments);

this._register(documentsAndEditors.onDocumentAdd(models => models.forEach(this._onModelAdded, this)));
this._register(documentsAndEditors.onDocumentRemove(urls => urls.forEach(this._onModelRemoved, this)));
this._register(_modelService.onModelLanguageChanged(this._onModelModeChanged, this));

this._register(_textFileService.files.onDidSave(e => {
Expand Down Expand Up @@ -184,7 +180,7 @@ export class MainThreadDocuments extends Disposable implements MainThreadDocumen
return !!model && shouldSynchronizeModel(model);
}

private _onModelAdded(model: ITextModel): void {
handleModelAdded(model: ITextModel): void {
// Same filter as in mainThreadEditorsTracker
if (!shouldSynchronizeModel(model)) {
// don't synchronize too large models
Expand All @@ -202,7 +198,7 @@ export class MainThreadDocuments extends Disposable implements MainThreadDocumen
this._proxy.$acceptModelLanguageChanged(model.uri, model.getLanguageId());
}

private _onModelRemoved(modelUrl: URI): void {
handleModelRemoved(modelUrl: URI): void {
if (!this._modelIsSynced.has(modelUrl)) {
return;
}
Expand Down
36 changes: 12 additions & 24 deletions src/vs/workbench/api/browser/mainThreadDocumentsAndEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Emitter, Event } from 'vs/base/common/event';
import { Event } from 'vs/base/common/event';
import { IDisposable, combinedDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { ICodeEditor, isCodeEditor, isDiffEditor, IActiveCodeEditor } from 'vs/editor/browser/editorBrowser';
Expand Down Expand Up @@ -279,18 +279,9 @@ export class MainThreadDocumentsAndEditors {
private readonly _toDispose = new DisposableStore();
private readonly _proxy: ExtHostDocumentsAndEditorsShape;
private readonly _mainThreadDocuments: MainThreadDocuments;
private readonly _mainThreadEditors: MainThreadTextEditors;
private readonly _textEditors = new Map<string, MainThreadTextEditor>();

private readonly _onTextEditorAdd = new Emitter<MainThreadTextEditor[]>();
private readonly _onTextEditorRemove = new Emitter<string[]>();
private readonly _onDocumentAdd = new Emitter<ITextModel[]>();
private readonly _onDocumentRemove = new Emitter<URI[]>();

readonly onTextEditorAdd: Event<MainThreadTextEditor[]> = this._onTextEditorAdd.event;
readonly onTextEditorRemove: Event<string[]> = this._onTextEditorRemove.event;
readonly onDocumentAdd: Event<ITextModel[]> = this._onDocumentAdd.event;
readonly onDocumentRemove: Event<URI[]> = this._onDocumentRemove.event;

constructor(
extHostContext: IExtHostContext,
@IModelService private readonly _modelService: IModelService,
Expand All @@ -310,19 +301,14 @@ export class MainThreadDocumentsAndEditors {
) {
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostDocumentsAndEditors);

this._mainThreadDocuments = this._toDispose.add(new MainThreadDocuments(this, extHostContext, this._modelService, this._textFileService, fileService, textModelResolverService, environmentService, uriIdentityService, workingCopyFileService, pathService));
this._mainThreadDocuments = this._toDispose.add(new MainThreadDocuments(extHostContext, this._modelService, this._textFileService, fileService, textModelResolverService, environmentService, uriIdentityService, workingCopyFileService, pathService));
extHostContext.set(MainContext.MainThreadDocuments, this._mainThreadDocuments);

const mainThreadTextEditors = this._toDispose.add(new MainThreadTextEditors(this, extHostContext, codeEditorService, bulkEditService, this._editorService, this._editorGroupService));
extHostContext.set(MainContext.MainThreadTextEditors, mainThreadTextEditors);
this._mainThreadEditors = this._toDispose.add(new MainThreadTextEditors(this, extHostContext, codeEditorService, bulkEditService, this._editorService, this._editorGroupService));
extHostContext.set(MainContext.MainThreadTextEditors, this._mainThreadEditors);

// It is expected that the ctor of the state computer calls our `_onDelta`.
this._toDispose.add(new MainThreadDocumentAndEditorStateComputer(delta => this._onDelta(delta), _modelService, codeEditorService, this._editorService, paneCompositeService));

this._toDispose.add(this._onTextEditorAdd);
this._toDispose.add(this._onTextEditorRemove);
this._toDispose.add(this._onDocumentAdd);
this._toDispose.add(this._onDocumentRemove);
}

dispose(): void {
Expand Down Expand Up @@ -383,11 +369,13 @@ export class MainThreadDocumentsAndEditors {
if (!empty) {
// first update ext host
this._proxy.$acceptDocumentsAndEditorsDelta(extHostDelta);
// second update dependent state listener
this._onDocumentRemove.fire(removedDocuments);
this._onDocumentAdd.fire(delta.addedDocuments);
this._onTextEditorRemove.fire(removedEditors);
this._onTextEditorAdd.fire(addedEditors);

// second update dependent document/editor states
removedDocuments.forEach(this._mainThreadDocuments.handleModelRemoved, this._mainThreadDocuments);
delta.addedDocuments.forEach(this._mainThreadDocuments.handleModelAdded, this._mainThreadDocuments);

removedEditors.forEach(this._mainThreadEditors.handleTextEditorRemoved, this._mainThreadEditors);
addedEditors.forEach(this._mainThreadEditors.handleTextEditorAdded, this._mainThreadEditors);
}
}

Expand Down
42 changes: 21 additions & 21 deletions src/vs/workbench/api/browser/mainThreadEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { ISingleEditOperation } from 'vs/editor/common/core/editOperation';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { ITextEditorOptions, IResourceEditorInput, EditorActivation, EditorResolution } from 'vs/platform/editor/common/editor';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { MainThreadDocumentsAndEditors } from 'vs/workbench/api/browser/mainThreadDocumentsAndEditors';
import { MainThreadTextEditor } from 'vs/workbench/api/browser/mainThreadEditor';
import { ExtHostContext, ExtHostEditorsShape, IApplyEditsOptions, ITextDocumentShowOptions, ITextEditorConfigurationUpdate, ITextEditorPositionData, IUndoStopOptions, MainThreadTextEditorsShape, TextEditorRevealType, IWorkspaceEditDto, WorkspaceEditType } from 'vs/workbench/api/common/extHost.protocol';
import { editorGroupToColumn, columnToEditorGroup, EditorGroupColumn } from 'vs/workbench/services/editor/common/editorGroupColumn';
Expand All @@ -30,6 +29,7 @@ import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
import { ILineChange } from 'vs/editor/common/diff/diffComputer';
import { IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers';
import { IEditorPane } from 'vs/workbench/common/editor';

export function reviveWorkspaceEditDto2(data: IWorkspaceEditDto | undefined): ResourceEdit[] {
if (!data?.edits) {
Expand All @@ -49,20 +49,24 @@ export function reviveWorkspaceEditDto2(data: IWorkspaceEditDto | undefined): Re
return result;
}

export interface IMainThreadEditorLocator {
getEditor(id: string): MainThreadTextEditor | undefined;
findTextEditorIdFor(editorPane: IEditorPane): string | undefined;
}

export class MainThreadTextEditors implements MainThreadTextEditorsShape {

private static INSTANCE_COUNT: number = 0;

private readonly _instanceId: string;
private readonly _proxy: ExtHostEditorsShape;
private readonly _documentsAndEditors: MainThreadDocumentsAndEditors;
private readonly _toDispose = new DisposableStore();
private _textEditorsListenersMap: { [editorId: string]: IDisposable[] };
private _editorPositionData: ITextEditorPositionData | null;
private _registeredDecorationTypes: { [decorationType: string]: boolean };

constructor(
documentsAndEditors: MainThreadDocumentsAndEditors,
private readonly _editorLocator: IMainThreadEditorLocator,
extHostContext: IExtHostContext,
@ICodeEditorService private readonly _codeEditorService: ICodeEditorService,
@IBulkEditService private readonly _bulkEditService: IBulkEditService,
Expand All @@ -71,14 +75,10 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
) {
this._instanceId = String(++MainThreadTextEditors.INSTANCE_COUNT);
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostEditors);
this._documentsAndEditors = documentsAndEditors;

this._textEditorsListenersMap = Object.create(null);
this._editorPositionData = null;

this._toDispose.add(documentsAndEditors.onTextEditorAdd(editors => editors.forEach(this._onTextEditorAdd, this)));
this._toDispose.add(documentsAndEditors.onTextEditorRemove(editors => editors.forEach(this._onTextEditorRemove, this)));

this._toDispose.add(this._editorService.onDidVisibleEditorsChange(() => this._updateActiveAndVisibleTextEditors()));
this._toDispose.add(this._editorGroupService.onDidRemoveGroup(() => this._updateActiveAndVisibleTextEditors()));
this._toDispose.add(this._editorGroupService.onDidMoveGroup(() => this._updateActiveAndVisibleTextEditors()));
Expand All @@ -98,7 +98,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
this._registeredDecorationTypes = Object.create(null);
}

private _onTextEditorAdd(textEditor: MainThreadTextEditor): void {
handleTextEditorAdded(textEditor: MainThreadTextEditor): void {
const id = textEditor.getId();
const toDispose: IDisposable[] = [];
toDispose.push(textEditor.onPropertiesChanged((data) => {
Expand All @@ -108,7 +108,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
this._textEditorsListenersMap[id] = toDispose;
}

private _onTextEditorRemove(id: string): void {
handleTextEditorRemoved(id: string): void {
dispose(this._textEditorsListenersMap[id]);
delete this._textEditorsListenersMap[id];
}
Expand All @@ -126,7 +126,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
private _getTextEditorPositionData(): ITextEditorPositionData {
const result: ITextEditorPositionData = Object.create(null);
for (let editorPane of this._editorService.visibleEditorPanes) {
const id = this._documentsAndEditors.findTextEditorIdFor(editorPane);
const id = this._editorLocator.findTextEditorIdFor(editorPane);
if (id) {
result[id] = editorGroupToColumn(this._editorGroupService, editorPane.group);
}
Expand Down Expand Up @@ -158,11 +158,11 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
if (!editor) {
return undefined;
}
return this._documentsAndEditors.findTextEditorIdFor(editor);
return this._editorLocator.findTextEditorIdFor(editor);
}

async $tryShowEditor(id: string, position?: EditorGroupColumn): Promise<void> {
const mainThreadEditor = this._documentsAndEditors.getEditor(id);
const mainThreadEditor = this._editorLocator.getEditor(id);
if (mainThreadEditor) {
const model = mainThreadEditor.getModel();
await this._editorService.openEditor({
Expand All @@ -174,7 +174,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
}

async $tryHideEditor(id: string): Promise<void> {
const mainThreadEditor = this._documentsAndEditors.getEditor(id);
const mainThreadEditor = this._editorLocator.getEditor(id);
if (mainThreadEditor) {
const editorPanes = this._editorService.visibleEditorPanes;
for (let editorPane of editorPanes) {
Expand All @@ -187,7 +187,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
}

$trySetSelections(id: string, selections: ISelection[]): Promise<void> {
const editor = this._documentsAndEditors.getEditor(id);
const editor = this._editorLocator.getEditor(id);
if (!editor) {
return Promise.reject(disposed(`TextEditor(${id})`));
}
Expand All @@ -197,7 +197,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {

$trySetDecorations(id: string, key: string, ranges: IDecorationOptions[]): Promise<void> {
key = `${this._instanceId}-${key}`;
const editor = this._documentsAndEditors.getEditor(id);
const editor = this._editorLocator.getEditor(id);
if (!editor) {
return Promise.reject(disposed(`TextEditor(${id})`));
}
Expand All @@ -207,7 +207,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {

$trySetDecorationsFast(id: string, key: string, ranges: number[]): Promise<void> {
key = `${this._instanceId}-${key}`;
const editor = this._documentsAndEditors.getEditor(id);
const editor = this._editorLocator.getEditor(id);
if (!editor) {
return Promise.reject(disposed(`TextEditor(${id})`));
}
Expand All @@ -216,7 +216,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
}

$tryRevealRange(id: string, range: IRange, revealType: TextEditorRevealType): Promise<void> {
const editor = this._documentsAndEditors.getEditor(id);
const editor = this._editorLocator.getEditor(id);
if (!editor) {
return Promise.reject(disposed(`TextEditor(${id})`));
}
Expand All @@ -225,7 +225,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
}

$trySetOptions(id: string, options: ITextEditorConfigurationUpdate): Promise<void> {
const editor = this._documentsAndEditors.getEditor(id);
const editor = this._editorLocator.getEditor(id);
if (!editor) {
return Promise.reject(disposed(`TextEditor(${id})`));
}
Expand All @@ -234,7 +234,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
}

$tryApplyEdits(id: string, modelVersionId: number, edits: ISingleEditOperation[], opts: IApplyEditsOptions): Promise<boolean> {
const editor = this._documentsAndEditors.getEditor(id);
const editor = this._editorLocator.getEditor(id);
if (!editor) {
return Promise.reject(disposed(`TextEditor(${id})`));
}
Expand All @@ -247,7 +247,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
}

$tryInsertSnippet(id: string, template: string, ranges: readonly IRange[], opts: IUndoStopOptions): Promise<boolean> {
const editor = this._documentsAndEditors.getEditor(id);
const editor = this._editorLocator.getEditor(id);
if (!editor) {
return Promise.reject(disposed(`TextEditor(${id})`));
}
Expand All @@ -267,7 +267,7 @@ export class MainThreadTextEditors implements MainThreadTextEditorsShape {
}

$getDiffInformation(id: string): Promise<ILineChange[]> {
const editor = this._documentsAndEditors.getEditor(id);
const editor = this._editorLocator.getEditor(id);

if (!editor) {
return Promise.reject(new Error('No such TextEditor'));
Expand Down

0 comments on commit e240ada

Please sign in to comment.