Debian Codegraph @

node/@xterm/xterm/src/common/services/OscLinkService.ts

raw
1 /**
2 * Copyright (c) 2022 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5 import { IBufferService, IOscLinkService } from 'common/services/Services';
6 import { IMarker, IOscLinkData } from 'common/Types';
7
8 export class OscLinkService implements IOscLinkService {
9 public serviceBrand: any;
10
11 private _nextId = 1;
12
13 /**
14 * A map of the link key to link entry. This is used to add additional lines to links with ids.
15 */
16 private _entriesWithId: Map<string, IOscLinkEntryWithId> = new Map();
17
18 /**
19 * A map of the link id to the link entry. The "link id" (number) which is the numberic
20 * representation of a unique link should not be confused with "id" (string) which comes in with
21 * `id=` in the OSC link's properties.
22 */
23 private _dataByLinkId: Map<number, IOscLinkEntryNoId | IOscLinkEntryWithId> = new Map();
24
25 constructor(
26 @IBufferService private readonly _bufferService: IBufferService
27 ) {
28 }
29
30 public registerLink(data: IOscLinkData): number {
31 const buffer = this._bufferService.buffer;
32
33 // Links with no id will only ever be registered a single time
34 if (data.id === undefined) {
35 const marker = buffer.addMarker(buffer.ybase + buffer.y);
36 const entry: IOscLinkEntryNoId = {
37 data,
38 id: this._nextId++,
39 lines: [marker]
40 };
41 marker.onDispose(() => this._removeMarkerFromLink(entry, marker));
42 this._dataByLinkId.set(entry.id, entry);
43 return entry.id;
44 }
45
46 // Add the line to the link if it already exists
47 const castData = data as Required<IOscLinkData>;
48 const key = this._getEntryIdKey(castData);
49 const match = this._entriesWithId.get(key);
50 if (match) {
51 this.addLineToLink(match.id, buffer.ybase + buffer.y);
52 return match.id;
53 }
54
55 // Create the link
56 const marker = buffer.addMarker(buffer.ybase + buffer.y);
57 const entry: IOscLinkEntryWithId = {
58 id: this._nextId++,
59 key: this._getEntryIdKey(castData),
60 data: castData,
61 lines: [marker]
62 };
63 marker.onDispose(() => this._removeMarkerFromLink(entry, marker));
64 this._entriesWithId.set(entry.key, entry);
65 this._dataByLinkId.set(entry.id, entry);
66 return entry.id;
67 }
68
69 public addLineToLink(linkId: number, y: number): void {
70 const entry = this._dataByLinkId.get(linkId);
71 if (!entry) {
72 return;
73 }
74 if (entry.lines.every(e => e.line !== y)) {
75 const marker = this._bufferService.buffer.addMarker(y);
76 entry.lines.push(marker);
77 marker.onDispose(() => this._removeMarkerFromLink(entry, marker));
78 }
79 }
80
81 public getLinkData(linkId: number): IOscLinkData | undefined {
82 return this._dataByLinkId.get(linkId)?.data;
83 }
84
85 private _getEntryIdKey(linkData: Required<IOscLinkData>): string {
86 return `${linkData.id};;${linkData.uri}`;
87 }
88
89 private _removeMarkerFromLink(entry: IOscLinkEntryNoId | IOscLinkEntryWithId, marker: IMarker): void {
90 const index = entry.lines.indexOf(marker);
91 if (index === -1) {
92 return;
93 }
94 entry.lines.splice(index, 1);
95 if (entry.lines.length === 0) {
96 if (entry.data.id !== undefined) {
97 this._entriesWithId.delete((entry as IOscLinkEntryWithId).key);
98 }
99 this._dataByLinkId.delete(entry.id);
100 }
101 }
102 }
103
104 interface IOscLinkEntry<T extends IOscLinkData> {
105 data: T;
106 id: number;
107 lines: IMarker[];
108 }
109
110 interface IOscLinkEntryNoId extends IOscLinkEntry<IOscLinkData> {
111 }
112
113 interface IOscLinkEntryWithId extends IOscLinkEntry<Required<IOscLinkData>> {
114 key: string;
115 }