@vscode/web-editors 0.0.2-127 → 0.0.2-129

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ //#region src/utils/event.ts
6
+ var Emitter = class {
7
+ _listeners = /* @__PURE__ */ new Set();
8
+ event = (listener) => {
9
+ this._listeners.add(listener);
10
+ return { dispose: () => this._listeners.delete(listener) };
11
+ };
12
+ fire(value) {
13
+ for (const listener of this._listeners) try {
14
+ listener(value);
15
+ } catch (e) {
16
+ console.error("Emitter listener threw:", e);
17
+ }
18
+ }
19
+ dispose() {
20
+ this._listeners.clear();
21
+ }
22
+ };
23
+ //#endregion
24
+ export { Emitter as t };
25
+
26
+ //# sourceMappingURL=event-BrSLCUGr.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-BrSLCUGr.js","names":[],"sources":["../../src/utils/event.ts"],"sourcesContent":["export interface IDisposable {\n dispose(): void;\n}\n\nexport type EventListener<T> = (event: T) => void;\nexport type Event<T> = (listener: EventListener<T>) => IDisposable;\n\nexport class Emitter<T> {\n private readonly _listeners = new Set<EventListener<T>>();\n\n public readonly event: Event<T> = (listener) => {\n this._listeners.add(listener);\n return { dispose: () => this._listeners.delete(listener) };\n };\n\n public fire(value: T): void {\n for (const listener of this._listeners) {\n try {\n listener(value);\n } catch (e) {\n console.error(\"Emitter listener threw:\", e);\n }\n }\n }\n\n public dispose(): void {\n this._listeners.clear();\n }\n}\n"],"mappings":";;;;;AAOA,IAAa,UAAb,MAAwB;CACpB,6BAA8B,IAAI,IAAsB;CAExD,SAAmC,aAAa;EAC5C,KAAK,WAAW,IAAI,QAAQ;EAC5B,OAAO,EAAE,eAAe,KAAK,WAAW,OAAO,QAAQ,EAAE;CAC7D;CAEA,KAAY,OAAgB;EACxB,KAAK,MAAM,YAAY,KAAK,YACxB,IAAI;GACA,SAAS,KAAK;EAClB,SAAS,GAAG;GACR,QAAQ,MAAM,2BAA2B,CAAC;EAC9C;CAER;CAEA,UAAuB;EACnB,KAAK,WAAW,MAAM;CAC1B;AACJ"}
@@ -0,0 +1,44 @@
1
+ //#region src/utils/event.d.ts
2
+ interface IDisposable {
3
+ dispose(): void;
4
+ }
5
+ type EventListener<T> = (event: T) => void;
6
+ type Event<T> = (listener: EventListener<T>) => IDisposable;
7
+ //#endregion
8
+ //#region src/documentSync/syncedTextDocument.d.ts
9
+ /**
10
+ * A text document that changes independently of the editor, for example a
11
+ * VS Code `TextDocument` (see `vsCodeTextDocument` in `@vscode/web-editors/vscode`).
12
+ */
13
+ interface SyncedTextDocument {
14
+ /** Changes with every change of the text. */
15
+ readonly version: number;
16
+ getText(): string;
17
+ /**
18
+ * Applies `edits` unless the document changed since `version`. Resolves
19
+ * whether it applied them. The document may apply them differently, for
20
+ * example with its own line breaks; its change event shows the result.
21
+ *
22
+ * The change event for the edits must fire before the returned promise
23
+ * resolves.
24
+ */
25
+ applyEdits(version: number, edits: readonly DocumentTextEdit[]): Promise<boolean>;
26
+ /**
27
+ * Fires after the text changed (not for other changes such as the dirty
28
+ * state). {@link getText} and {@link version} are those after the change.
29
+ */
30
+ readonly onDidChangeText: Event<void>;
31
+ }
32
+ /**
33
+ * Replaces `length` characters at `offset` with `newText`. Unlike the
34
+ * protocol's `TextEdit`, the offsets of a batch all refer to the text before
35
+ * the batch; the edits are ascending and don't overlap.
36
+ */
37
+ interface DocumentTextEdit {
38
+ readonly offset: number;
39
+ readonly length: number;
40
+ readonly newText: string;
41
+ }
42
+ //#endregion
43
+ export { IDisposable as i, SyncedTextDocument as n, Event as r, DocumentTextEdit as t };
44
+ //# sourceMappingURL=syncedTextDocument-EBcq_UIM.d.ts.map
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * Copyright (c) Microsoft Corporation. All rights reserved.
3
3
  * Licensed under the MIT License. See License.txt in the project root for license information.
4
4
  *--------------------------------------------------------------------------------------------*/
5
+ import { i as IDisposable, n as SyncedTextDocument, r as Event, t as DocumentTextEdit } from "./chunks/syncedTextDocument-EBcq_UIM.js";
5
6
  import { HubRpcConnection, InterfaceClient, InterfaceDefinition, MemberMap } from "@vscode/hubrpc";
6
7
  import { MessageEndpoint, MessageLikeEvent, WindowMessageTransport } from "@vscode/hubrpc/web";
7
8
  import { HubAccessDuration, HubAccessRequest, HubAccessResult } from "@vscode/hubrpc/hub/common";
@@ -2324,13 +2325,6 @@ declare function encodeForEditor(text: string, contentType: ContentType): JsonVa
2324
2325
  */
2325
2326
  declare function decodeFromEditor(value: JsonValue, contentType: ContentType, options?: JsonFormatOptions): string;
2326
2327
  //#endregion
2327
- //#region src/utils/event.d.ts
2328
- interface IDisposable {
2329
- dispose(): void;
2330
- }
2331
- type EventListener<T> = (event: T) => void;
2332
- type Event<T> = (listener: EventListener<T>) => IDisposable;
2333
- //#endregion
2334
2328
  //#region src/hostTransport.d.ts
2335
2329
  /**
2336
2330
  * Optional message transport between a web editor and its host.
@@ -3008,6 +3002,9 @@ declare class TextOperationBuilder {
3008
3002
  * An operation that turns `oldText` into `newText` by replacing everything
3009
3003
  * between their common prefix and suffix. Linear time, so it stays fast for
3010
3004
  * large texts; separate changes become one replacement of the text between them.
3005
+ *
3006
+ * The replacement never starts or ends between `\r` and `\n`, so that text
3007
+ * inserted concurrently at its edges cannot split a `\r\n` line break.
3011
3008
  */
3012
3009
  declare function diffText(oldText: string, newText: string): TextOperation;
3013
3010
  //#endregion
@@ -3042,5 +3039,98 @@ declare function mapThrough(operations: readonly (readonly ContentEdit[])[] | un
3042
3039
  */
3043
3040
  declare function diffContent(oldValue: JsonValue, newValue: JsonValue, path?: string[]): ContentEdit[];
3044
3041
  //#endregion
3045
- export { AppContext, type ConnectionInput, ContentEdit, ContentType, ContentVersion, EditorAssociationResult, EditorCapabilities, EditorRegistrationStatus, EditorScope, EmbeddedAppAccessOptions, EmbeddedAppDescriptor, EmbeddedAppHandle, EmbeddingContext, type Event, HostCapabilities, HostTransport, type IDisposable, JsonFormatOptions, JsonValue, type MessageEndpoint, type MessageLikeEvent, OT_EDIT_TRANSFORM, OtPeer, OtType, PresentationMode, PresentationPreferences, ScopeRegistration, TextComponent, TextEdit, TextOperation, TextOperationBuilder, VsCodeAppEmbeddingEventsProxy, VsCodeAppHostClient, VsCodeAppHostClientOptions, VsCodeAppHostProxy, WebEditorClient, WebEditorClientOptions, WebEditorClientState, WebEditorClientTransportProxy, WebEditorHost, WebEditorHostOptions, WebEditorHostProxy, WebEditorHostTransportProxy, WebEditorPresentationHostProxy, WebEditorPresentationProxy, WebEditorProxy, WebEditorUpdate, WindowMessageTransport, applyContentEdits, applyTextEdits, contentEditsOt, createWindowParentConnection, decodeFromEditor, diffContent, diffText, encodeForEditor, mapContentOffset, mapThrough, textOperationOt, transformContentEdits, vscodeAppEmbeddingEventsInterface, vscodeAppHostInterface, webEditorClientTransportInterface, webEditorHostInterface, webEditorHostTransportInterface, webEditorInterface, webEditorPresentationHostInterface, webEditorPresentationInterface };
3042
+ //#region src/documentSync/textDocumentSync.d.ts
3043
+ interface TextDocumentSyncOptions {
3044
+ /** Called for failed or rejected writes. Defaults to `console.error`. */
3045
+ onError?: (message: string, detail?: unknown) => void;
3046
+ }
3047
+ /** Options for the {@link WebEditorHost} of a connected editor. */
3048
+ type TextDocumentEditorOptions = Omit<WebEditorHostOptions, "connection" | "initialText" | "onError">;
3049
+ /**
3050
+ * Keeps a web editor's text and a {@link SyncedTextDocument} in sync while
3051
+ * both change.
3052
+ *
3053
+ * The editor text is the document text plus the editor's changes that are not
3054
+ * in the document yet. Editor changes are written one batch at a time. A
3055
+ * document change that is not such a write, for example an undo, a format on
3056
+ * save or another editor's edit, is transformed against the unwritten editor
3057
+ * changes and sent to the editor, so neither side's changes are lost. A write
3058
+ * the document rejects because it changed meanwhile is transformed the same
3059
+ * way and written again. If the document applies a write differently, for
3060
+ * example with its own line breaks, the editor receives the difference.
3061
+ *
3062
+ * Editors connect through a {@link WebEditorHost}, which merges the editor's
3063
+ * unsent edits with the changes sent to it (operational transformation).
3064
+ * Each (re)loaded editor connects anew.
3065
+ */
3066
+ declare class TextDocumentSync implements IDisposable {
3067
+ private readonly _document;
3068
+ private readonly _onError;
3069
+ /** The document text as last seen. */
3070
+ private _documentText;
3071
+ /** {@link _documentText} with {@link _writing} and {@link _unwritten} applied. */
3072
+ private _text;
3073
+ private _revision;
3074
+ private _writing;
3075
+ /**
3076
+ * Document changes seen while {@link _writing} is in flight. They are
3077
+ * settled when it completes: only then is it known whether the first one
3078
+ * is the write.
3079
+ */
3080
+ private readonly _changesDuringWrite;
3081
+ /** Editor changes after {@link _writing} that are not written yet. */
3082
+ private _unwritten;
3083
+ private _host;
3084
+ private _contentType;
3085
+ private readonly _tracked;
3086
+ private readonly _onDidChangeText;
3087
+ /** Fires after every change of {@link text}. */
3088
+ readonly onDidChangeText: Event<void>;
3089
+ private readonly _onDidSettle;
3090
+ private readonly _subscription;
3091
+ private _disposed;
3092
+ constructor(_document: SyncedTextDocument, options?: TextDocumentSyncOptions);
3093
+ /** The editor text: the document text with the editor changes that are not written yet. */
3094
+ get text(): string;
3095
+ /** Changes with every change of {@link text}. */
3096
+ get revision(): number;
3097
+ /** The version to send along with offsets into {@link text}, while an editor is connected. */
3098
+ get version(): ContentVersion | undefined;
3099
+ /**
3100
+ * Connects a (re)loaded editor, which receives the current text. Replaces
3101
+ * the previous editor's host, which is disposed along with its connection.
3102
+ */
3103
+ connect(connection: HubRpcConnection, options?: TextDocumentEditorOptions): WebEditorHost;
3104
+ /**
3105
+ * Follows `offset` in {@link text} through later changes, until disposed.
3106
+ * With `"after"` (the default), text inserted exactly at the offset ends up
3107
+ * before it.
3108
+ */
3109
+ track(offset: number, assoc?: "before" | "after"): TrackedOffset;
3110
+ /**
3111
+ * Tracks an offset into the text that the editor sent along with
3112
+ * `version`; `undefined` if it cannot be mapped. Call it when the editor's
3113
+ * message arrives.
3114
+ */
3115
+ trackFromEditor(version: ContentVersion, offset: number, assoc?: "before" | "after"): TrackedOffset | undefined;
3116
+ /**
3117
+ * Resolves once every editor change is in the document, so that
3118
+ * {@link text} equals the document text. Rejects if the sync is disposed.
3119
+ */
3120
+ drain(): Promise<void>;
3121
+ dispose(): void;
3122
+ private _acceptEditorChange;
3123
+ private _acceptDocumentChange;
3124
+ /** Someone else changed the document: merges the change with the editor changes that are not written yet. */
3125
+ private _acceptExternalChange;
3126
+ private _write;
3127
+ /** Applies a change that comes from the document to {@link text} and sends it to the editor. */
3128
+ private _applyFromDocument;
3129
+ private _applyToText;
3130
+ }
3131
+ interface TrackedOffset extends IDisposable {
3132
+ readonly offset: number;
3133
+ }
3134
+ //#endregion
3135
+ export { AppContext, type ConnectionInput, ContentEdit, ContentType, ContentVersion, DocumentTextEdit, EditorAssociationResult, EditorCapabilities, EditorRegistrationStatus, EditorScope, EmbeddedAppAccessOptions, EmbeddedAppDescriptor, EmbeddedAppHandle, EmbeddingContext, type Event, HostCapabilities, HostTransport, type IDisposable, JsonFormatOptions, JsonValue, type MessageEndpoint, type MessageLikeEvent, OT_EDIT_TRANSFORM, OtPeer, OtType, PresentationMode, PresentationPreferences, ScopeRegistration, SyncedTextDocument, TextComponent, TextDocumentEditorOptions, TextDocumentSync, TextDocumentSyncOptions, TextEdit, TextOperation, TextOperationBuilder, TrackedOffset, VsCodeAppEmbeddingEventsProxy, VsCodeAppHostClient, VsCodeAppHostClientOptions, VsCodeAppHostProxy, WebEditorClient, WebEditorClientOptions, WebEditorClientState, WebEditorClientTransportProxy, WebEditorHost, WebEditorHostOptions, WebEditorHostProxy, WebEditorHostTransportProxy, WebEditorPresentationHostProxy, WebEditorPresentationProxy, WebEditorProxy, WebEditorUpdate, WindowMessageTransport, applyContentEdits, applyTextEdits, contentEditsOt, createWindowParentConnection, decodeFromEditor, diffContent, diffText, encodeForEditor, mapContentOffset, mapThrough, textOperationOt, transformContentEdits, vscodeAppEmbeddingEventsInterface, vscodeAppHostInterface, webEditorClientTransportInterface, webEditorHostInterface, webEditorHostTransportInterface, webEditorInterface, webEditorPresentationHostInterface, webEditorPresentationInterface };
3046
3136
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  * Copyright (c) Microsoft Corporation. All rights reserved.
3
3
  * Licensed under the MIT License. See License.txt in the project root for license information.
4
4
  *--------------------------------------------------------------------------------------------*/
5
+ import { t as Emitter } from "./chunks/event-BrSLCUGr.js";
5
6
  import { HubRpcConnection, JsonRpcChannel, defineInterface, notificationType, requestType } from "@vscode/hubrpc";
6
7
  import { WindowMessageTransport, WindowMessageTransport as WindowMessageTransport$1 } from "@vscode/hubrpc/web";
7
8
  import { createManagedSigningChannel, findCoveringCapabilities, hubAccessInterface } from "@vscode/hubrpc/hub/common";
@@ -4449,25 +4450,6 @@ function decodeFromEditor(value, contentType, options = {}) {
4449
4450
  return JSON.stringify(value, void 0, indentation);
4450
4451
  }
4451
4452
  //#endregion
4452
- //#region src/utils/event.ts
4453
- var Emitter = class {
4454
- _listeners = /* @__PURE__ */ new Set();
4455
- event = (listener) => {
4456
- this._listeners.add(listener);
4457
- return { dispose: () => this._listeners.delete(listener) };
4458
- };
4459
- fire(value) {
4460
- for (const listener of this._listeners) try {
4461
- listener(value);
4462
- } catch (e) {
4463
- console.error("Emitter listener threw:", e);
4464
- }
4465
- }
4466
- dispose() {
4467
- this._listeners.clear();
4468
- }
4469
- };
4470
- //#endregion
4471
4453
  //#region src/sync/otPeer.ts
4472
4454
  /**
4473
4455
  * One of the two sides of a session that keeps a document in sync between
@@ -4891,19 +4873,29 @@ const identityOperation = new TextOperationBuilder().build();
4891
4873
  * An operation that turns `oldText` into `newText` by replacing everything
4892
4874
  * between their common prefix and suffix. Linear time, so it stays fast for
4893
4875
  * large texts; separate changes become one replacement of the text between them.
4876
+ *
4877
+ * The replacement never starts or ends between `\r` and `\n`, so that text
4878
+ * inserted concurrently at its edges cannot split a `\r\n` line break.
4894
4879
  */
4895
4880
  function diffText(oldText, newText) {
4896
4881
  const shorter = Math.min(oldText.length, newText.length);
4897
4882
  let start = 0;
4898
4883
  while (start < shorter && oldText.charCodeAt(start) === newText.charCodeAt(start)) start++;
4884
+ if (start > 0 && oldText.charCodeAt(start - 1) === CR && (oldText.charCodeAt(start) === LF || newText.charCodeAt(start) === LF)) start--;
4899
4885
  let oldEnd = oldText.length;
4900
4886
  let newEnd = newText.length;
4901
4887
  while (oldEnd > start && newEnd > start && oldText.charCodeAt(oldEnd - 1) === newText.charCodeAt(newEnd - 1)) {
4902
4888
  oldEnd--;
4903
4889
  newEnd--;
4904
4890
  }
4891
+ if (oldEnd < oldText.length && oldText.charCodeAt(oldEnd) === LF && (oldText.charCodeAt(oldEnd - 1) === CR || newText.charCodeAt(newEnd - 1) === CR)) {
4892
+ oldEnd++;
4893
+ newEnd++;
4894
+ }
4905
4895
  return new TextOperationBuilder().retain(start).insert(newText.slice(start, newEnd)).delete(oldEnd - start).build();
4906
4896
  }
4897
+ const CR = 13;
4898
+ const LF = 10;
4907
4899
  //#endregion
4908
4900
  //#region src/sync/contentOperation.ts
4909
4901
  /** {@link OtType} for lists of {@link ContentEdit}s, as exchanged by web editors. */
@@ -6053,6 +6045,249 @@ var VsCodeAppHostClient = class {
6053
6045
  }
6054
6046
  };
6055
6047
  //#endregion
6056
- export { EmbeddedAppHandle, OT_EDIT_TRANSFORM, OtPeer, TextOperation, TextOperationBuilder, VsCodeAppHostClient, WebEditorClient, WebEditorHost, WindowMessageTransport, applyContentEdits, applyTextEdits, contentEditsOt, createWindowParentConnection, decodeFromEditor, diffContent, diffText, encodeForEditor, mapContentOffset, mapThrough, textOperationOt, transformContentEdits, vscodeAppEmbeddingEventsInterface, vscodeAppHostInterface, webEditorClientTransportInterface, webEditorHostInterface, webEditorHostTransportInterface, webEditorInterface, webEditorPresentationHostInterface, webEditorPresentationInterface };
6048
+ //#region src/documentSync/textDocumentSync.ts
6049
+ /**
6050
+ * Keeps a web editor's text and a {@link SyncedTextDocument} in sync while
6051
+ * both change.
6052
+ *
6053
+ * The editor text is the document text plus the editor's changes that are not
6054
+ * in the document yet. Editor changes are written one batch at a time. A
6055
+ * document change that is not such a write, for example an undo, a format on
6056
+ * save or another editor's edit, is transformed against the unwritten editor
6057
+ * changes and sent to the editor, so neither side's changes are lost. A write
6058
+ * the document rejects because it changed meanwhile is transformed the same
6059
+ * way and written again. If the document applies a write differently, for
6060
+ * example with its own line breaks, the editor receives the difference.
6061
+ *
6062
+ * Editors connect through a {@link WebEditorHost}, which merges the editor's
6063
+ * unsent edits with the changes sent to it (operational transformation).
6064
+ * Each (re)loaded editor connects anew.
6065
+ */
6066
+ var TextDocumentSync = class {
6067
+ _document;
6068
+ _onError;
6069
+ /** The document text as last seen. */
6070
+ _documentText;
6071
+ /** {@link _documentText} with {@link _writing} and {@link _unwritten} applied. */
6072
+ _text;
6073
+ _revision = 0;
6074
+ _writing;
6075
+ /**
6076
+ * Document changes seen while {@link _writing} is in flight. They are
6077
+ * settled when it completes: only then is it known whether the first one
6078
+ * is the write.
6079
+ */
6080
+ _changesDuringWrite = [];
6081
+ /** Editor changes after {@link _writing} that are not written yet. */
6082
+ _unwritten = TextOperation.identity;
6083
+ _host;
6084
+ _contentType = "text";
6085
+ _tracked = /* @__PURE__ */ new Set();
6086
+ _onDidChangeText = new Emitter();
6087
+ /** Fires after every change of {@link text}. */
6088
+ onDidChangeText = this._onDidChangeText.event;
6089
+ _onDidSettle = new Emitter();
6090
+ _subscription;
6091
+ _disposed = false;
6092
+ constructor(_document, options = {}) {
6093
+ this._document = _document;
6094
+ this._onError = options.onError ?? ((message, detail) => console.error("TextDocumentSync:", message, detail));
6095
+ this._documentText = _document.getText();
6096
+ this._text = this._documentText;
6097
+ this._subscription = _document.onDidChangeText(() => this._acceptDocumentChange());
6098
+ }
6099
+ /** The editor text: the document text with the editor changes that are not written yet. */
6100
+ get text() {
6101
+ return this._text;
6102
+ }
6103
+ /** Changes with every change of {@link text}. */
6104
+ get revision() {
6105
+ return this._revision;
6106
+ }
6107
+ /** The version to send along with offsets into {@link text}, while an editor is connected. */
6108
+ get version() {
6109
+ return this._host?.version;
6110
+ }
6111
+ /**
6112
+ * Connects a (re)loaded editor, which receives the current text. Replaces
6113
+ * the previous editor's host, which is disposed along with its connection.
6114
+ */
6115
+ connect(connection, options = {}) {
6116
+ this._host?.dispose();
6117
+ const host = new WebEditorHost({
6118
+ ...options,
6119
+ connection,
6120
+ initialText: this._text,
6121
+ onError: this._onError
6122
+ });
6123
+ this._host = host;
6124
+ this._contentType = options.contentType ?? "text";
6125
+ host.onDidChangeText(({ text, edits }) => {
6126
+ if (this._host === host) this._acceptEditorChange(text, edits);
6127
+ });
6128
+ return host;
6129
+ }
6130
+ /**
6131
+ * Follows `offset` in {@link text} through later changes, until disposed.
6132
+ * With `"after"` (the default), text inserted exactly at the offset ends up
6133
+ * before it.
6134
+ */
6135
+ track(offset, assoc = "after") {
6136
+ const tracked = new TrackedOffsetImpl(offset, assoc, this._tracked);
6137
+ this._tracked.add(tracked);
6138
+ return tracked;
6139
+ }
6140
+ /**
6141
+ * Tracks an offset into the text that the editor sent along with
6142
+ * `version`; `undefined` if it cannot be mapped. Call it when the editor's
6143
+ * message arrives.
6144
+ */
6145
+ trackFromEditor(version, offset, assoc) {
6146
+ const mapped = offset < 0 ? void 0 : this._host?.mapOffset(version, [], offset, assoc);
6147
+ return mapped === void 0 || mapped > this._text.length ? void 0 : this.track(mapped, assoc);
6148
+ }
6149
+ /**
6150
+ * Resolves once every editor change is in the document, so that
6151
+ * {@link text} equals the document text. Rejects if the sync is disposed.
6152
+ */
6153
+ async drain() {
6154
+ while (!this._disposed && (this._writing || !this._unwritten.isIdentity)) await new Promise((resolve) => {
6155
+ const listener = this._onDidSettle.event(() => {
6156
+ listener.dispose();
6157
+ resolve();
6158
+ });
6159
+ });
6160
+ if (this._disposed) throw new Error("The document sync was disposed");
6161
+ }
6162
+ dispose() {
6163
+ this._disposed = true;
6164
+ this._subscription.dispose();
6165
+ this._host?.dispose();
6166
+ this._host = void 0;
6167
+ this._tracked.clear();
6168
+ this._onDidSettle.fire();
6169
+ this._onDidSettle.dispose();
6170
+ this._onDidChangeText.dispose();
6171
+ }
6172
+ _acceptEditorChange(text, edits) {
6173
+ const operation = this._contentType === "text" && edits.length === 1 && edits[0].kind === "stringEdits" && edits[0].path.length === 0 ? TextOperation.fromTextEdits(edits[0].stringEdits) : diffText(this._text, text);
6174
+ this._applyToText(operation);
6175
+ this._unwritten = this._unwritten.compose(operation);
6176
+ this._write();
6177
+ }
6178
+ _acceptDocumentChange() {
6179
+ if (this._writing) {
6180
+ this._changesDuringWrite.push(this._document.getText());
6181
+ return;
6182
+ }
6183
+ this._acceptExternalChange(this._document.getText());
6184
+ this._write();
6185
+ }
6186
+ /** Someone else changed the document: merges the change with the editor changes that are not written yet. */
6187
+ _acceptExternalChange(documentText) {
6188
+ if (documentText === this._documentText) return;
6189
+ const [externalPrime, unwrittenPrime] = TextOperation.transform(diffText(this._documentText, documentText), this._unwritten);
6190
+ this._documentText = documentText;
6191
+ this._unwritten = unwrittenPrime;
6192
+ this._applyFromDocument(externalPrime);
6193
+ }
6194
+ async _write() {
6195
+ if (this._disposed || this._writing) return;
6196
+ if (this._unwritten.isIdentity) {
6197
+ this._onDidSettle.fire();
6198
+ return;
6199
+ }
6200
+ if (this._document.getText() !== this._documentText) return;
6201
+ const operation = this._unwritten;
6202
+ const write = {
6203
+ operation,
6204
+ version: this._document.version,
6205
+ expected: operation.apply(this._documentText)
6206
+ };
6207
+ this._writing = write;
6208
+ this._unwritten = TextOperation.identity;
6209
+ let applied = false;
6210
+ try {
6211
+ applied = await this._document.applyEdits(write.version, toDocumentEdits(operation));
6212
+ } catch (error) {
6213
+ this._onError("Failed to write the editor's changes to the document", error);
6214
+ }
6215
+ if (this._disposed) return;
6216
+ this._writing = void 0;
6217
+ const changes = this._changesDuringWrite.splice(0);
6218
+ if (applied) {
6219
+ const written = changes.shift() ?? this._document.getText();
6220
+ this._documentText = written;
6221
+ const [unwrittenPrime, adjustmentPrime] = TextOperation.transform(this._unwritten, diffText(write.expected, written));
6222
+ this._unwritten = unwrittenPrime;
6223
+ this._applyFromDocument(adjustmentPrime);
6224
+ } else if (changes.length === 0 && this._document.version === write.version) {
6225
+ this._onError("The document rejected the editor's changes");
6226
+ this._unwritten = TextOperation.identity;
6227
+ this._applyFromDocument(diffText(this._text, this._documentText));
6228
+ } else this._unwritten = operation.compose(this._unwritten);
6229
+ for (const documentText of changes) this._acceptExternalChange(documentText);
6230
+ this._write();
6231
+ }
6232
+ /** Applies a change that comes from the document to {@link text} and sends it to the editor. */
6233
+ _applyFromDocument(operation) {
6234
+ if (operation.isIdentity) return;
6235
+ this._applyToText(operation);
6236
+ if (this._contentType === "text") this._host?.applyEdits([{
6237
+ kind: "stringEdits",
6238
+ path: [],
6239
+ stringEdits: operation.toTextEdits()
6240
+ }]);
6241
+ else this._host?.setText(this._text);
6242
+ }
6243
+ _applyToText(operation) {
6244
+ if (operation.isIdentity) return;
6245
+ this._text = operation.apply(this._text);
6246
+ this._revision++;
6247
+ for (const tracked of this._tracked) tracked.offset = operation.mapOffset(tracked.offset, tracked.assoc);
6248
+ this._onDidChangeText.fire();
6249
+ }
6250
+ };
6251
+ var TrackedOffsetImpl = class {
6252
+ offset;
6253
+ assoc;
6254
+ _owner;
6255
+ constructor(offset, assoc, _owner) {
6256
+ this.offset = offset;
6257
+ this.assoc = assoc;
6258
+ this._owner = _owner;
6259
+ }
6260
+ dispose() {
6261
+ this._owner.delete(this);
6262
+ }
6263
+ };
6264
+ /** The edits of `operation`, with offsets into the text before it. */
6265
+ function toDocumentEdits(operation) {
6266
+ const edits = [];
6267
+ let offset = 0;
6268
+ let pending;
6269
+ for (const component of operation.components) if (component.kind === "retain") {
6270
+ if (pending) {
6271
+ edits.push(pending);
6272
+ pending = void 0;
6273
+ }
6274
+ offset += component.length;
6275
+ } else {
6276
+ pending ??= {
6277
+ offset,
6278
+ length: 0,
6279
+ newText: ""
6280
+ };
6281
+ if (component.kind === "insert") pending.newText += component.text;
6282
+ else {
6283
+ pending.length += component.length;
6284
+ offset += component.length;
6285
+ }
6286
+ }
6287
+ if (pending) edits.push(pending);
6288
+ return edits;
6289
+ }
6290
+ //#endregion
6291
+ export { EmbeddedAppHandle, OT_EDIT_TRANSFORM, OtPeer, TextDocumentSync, TextOperation, TextOperationBuilder, VsCodeAppHostClient, WebEditorClient, WebEditorHost, WindowMessageTransport, applyContentEdits, applyTextEdits, contentEditsOt, createWindowParentConnection, decodeFromEditor, diffContent, diffText, encodeForEditor, mapContentOffset, mapThrough, textOperationOt, transformContentEdits, vscodeAppEmbeddingEventsInterface, vscodeAppHostInterface, webEditorClientTransportInterface, webEditorHostInterface, webEditorHostTransportInterface, webEditorInterface, webEditorPresentationHostInterface, webEditorPresentationInterface };
6057
6292
 
6058
6293
  //# sourceMappingURL=index.js.map