Path: blob/main/src/vs/workbench/api/common/extHostDocumentsAndEditors.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as assert from '../../../base/common/assert.js';6import * as vscode from 'vscode';7import { Emitter, Event } from '../../../base/common/event.js';8import { dispose } from '../../../base/common/lifecycle.js';9import { URI } from '../../../base/common/uri.js';10import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';11import { ExtHostDocumentsAndEditorsShape, IDocumentsAndEditorsDelta, MainContext } from './extHost.protocol.js';12import { ExtHostDocumentData } from './extHostDocumentData.js';13import { IExtHostRpcService } from './extHostRpcService.js';14import { ExtHostTextEditor } from './extHostTextEditor.js';15import * as typeConverters from './extHostTypeConverters.js';16import { ILogService } from '../../../platform/log/common/log.js';17import { ResourceMap } from '../../../base/common/map.js';18import { Schemas } from '../../../base/common/network.js';19import { Iterable } from '../../../base/common/iterator.js';20import { Lazy } from '../../../base/common/lazy.js';2122class Reference<T> {23private _count = 0;24constructor(readonly value: T) { }25ref() {26this._count++;27}28unref() {29return --this._count === 0;30}31}3233export class ExtHostDocumentsAndEditors implements ExtHostDocumentsAndEditorsShape {3435readonly _serviceBrand: undefined;3637private _activeEditorId: string | null = null;3839private readonly _editors = new Map<string, ExtHostTextEditor>();40private readonly _documents = new ResourceMap<Reference<ExtHostDocumentData>>();4142private readonly _onDidAddDocuments = new Emitter<readonly ExtHostDocumentData[]>();43private readonly _onDidRemoveDocuments = new Emitter<readonly ExtHostDocumentData[]>();44private readonly _onDidChangeVisibleTextEditors = new Emitter<readonly vscode.TextEditor[]>();45private readonly _onDidChangeActiveTextEditor = new Emitter<vscode.TextEditor | undefined>();4647readonly onDidAddDocuments: Event<readonly ExtHostDocumentData[]> = this._onDidAddDocuments.event;48readonly onDidRemoveDocuments: Event<readonly ExtHostDocumentData[]> = this._onDidRemoveDocuments.event;49readonly onDidChangeVisibleTextEditors: Event<readonly vscode.TextEditor[]> = this._onDidChangeVisibleTextEditors.event;50readonly onDidChangeActiveTextEditor: Event<vscode.TextEditor | undefined> = this._onDidChangeActiveTextEditor.event;5152constructor(53@IExtHostRpcService private readonly _extHostRpc: IExtHostRpcService,54@ILogService private readonly _logService: ILogService55) { }5657$acceptDocumentsAndEditorsDelta(delta: IDocumentsAndEditorsDelta): void {58this.acceptDocumentsAndEditorsDelta(delta);59}6061acceptDocumentsAndEditorsDelta(delta: IDocumentsAndEditorsDelta): void {6263const removedDocuments: ExtHostDocumentData[] = [];64const addedDocuments: ExtHostDocumentData[] = [];65const removedEditors: ExtHostTextEditor[] = [];6667if (delta.removedDocuments) {68for (const uriComponent of delta.removedDocuments) {69const uri = URI.revive(uriComponent);70const data = this._documents.get(uri);71if (data?.unref()) {72this._documents.delete(uri);73removedDocuments.push(data.value);74}75}76}7778if (delta.addedDocuments) {79for (const data of delta.addedDocuments) {80const resource = URI.revive(data.uri);81let ref = this._documents.get(resource);8283// double check -> only notebook cell documents should be84// referenced/opened more than once...85if (ref) {86if (resource.scheme !== Schemas.vscodeNotebookCell && resource.scheme !== Schemas.vscodeInteractiveInput) {87throw new Error(`document '${resource} already exists!'`);88}89}90if (!ref) {91ref = new Reference(new ExtHostDocumentData(92this._extHostRpc.getProxy(MainContext.MainThreadDocuments),93resource,94data.lines,95data.EOL,96data.versionId,97data.languageId,98data.isDirty,99data.encoding100));101this._documents.set(resource, ref);102addedDocuments.push(ref.value);103}104105ref.ref();106}107}108109if (delta.removedEditors) {110for (const id of delta.removedEditors) {111const editor = this._editors.get(id);112this._editors.delete(id);113if (editor) {114removedEditors.push(editor);115}116}117}118119if (delta.addedEditors) {120for (const data of delta.addedEditors) {121const resource = URI.revive(data.documentUri);122assert.ok(this._documents.has(resource), `document '${resource}' does not exist`);123assert.ok(!this._editors.has(data.id), `editor '${data.id}' already exists!`);124125const documentData = this._documents.get(resource)!.value;126const editor = new ExtHostTextEditor(127data.id,128this._extHostRpc.getProxy(MainContext.MainThreadTextEditors),129this._logService,130new Lazy(() => documentData.document),131data.selections.map(typeConverters.Selection.to),132data.options,133data.visibleRanges.map(range => typeConverters.Range.to(range)),134typeof data.editorPosition === 'number' ? typeConverters.ViewColumn.to(data.editorPosition) : undefined135);136this._editors.set(data.id, editor);137}138}139140if (delta.newActiveEditor !== undefined) {141assert.ok(delta.newActiveEditor === null || this._editors.has(delta.newActiveEditor), `active editor '${delta.newActiveEditor}' does not exist`);142this._activeEditorId = delta.newActiveEditor;143}144145dispose(removedDocuments);146dispose(removedEditors);147148// now that the internal state is complete, fire events149if (delta.removedDocuments) {150this._onDidRemoveDocuments.fire(removedDocuments);151}152if (delta.addedDocuments) {153this._onDidAddDocuments.fire(addedDocuments);154}155156if (delta.removedEditors || delta.addedEditors) {157this._onDidChangeVisibleTextEditors.fire(this.allEditors().map(editor => editor.value));158}159if (delta.newActiveEditor !== undefined) {160this._onDidChangeActiveTextEditor.fire(this.activeEditor());161}162}163164getDocument(uri: URI): ExtHostDocumentData | undefined {165return this._documents.get(uri)?.value;166}167168allDocuments(): Iterable<ExtHostDocumentData> {169return Iterable.map(this._documents.values(), ref => ref.value);170}171172getEditor(id: string): ExtHostTextEditor | undefined {173return this._editors.get(id);174}175176activeEditor(): vscode.TextEditor | undefined;177activeEditor(internal: true): ExtHostTextEditor | undefined;178activeEditor(internal?: true): vscode.TextEditor | ExtHostTextEditor | undefined {179if (!this._activeEditorId) {180return undefined;181}182const editor = this._editors.get(this._activeEditorId);183if (internal) {184return editor;185} else {186return editor?.value;187}188}189190allEditors(): ExtHostTextEditor[] {191return [...this._editors.values()];192}193}194195export interface IExtHostDocumentsAndEditors extends ExtHostDocumentsAndEditors { }196export const IExtHostDocumentsAndEditors = createDecorator<IExtHostDocumentsAndEditors>('IExtHostDocumentsAndEditors');197198199