Debian Codegraph @

node/@xterm/xterm/src/browser/Linkifier.ts

raw
1 /**
2 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { IBufferCellPosition, ILink, ILinkDecorations, ILinkWithState, ILinkifier2, ILinkifierEvent } from 'browser/Types';
7 import { Disposable, dispose, toDisposable } from 'vs/base/common/lifecycle';
8 import { IDisposable } from 'common/Types';
9 import { IBufferService } from 'common/services/Services';
10 import { ILinkProviderService, IMouseService, IRenderService } from './services/Services';
11 import { Emitter } from 'vs/base/common/event';
12 import { addDisposableListener } from 'vs/base/browser/dom';
13
14 export class Linkifier extends Disposable implements ILinkifier2 {
15 public get currentLink(): ILinkWithState | undefined { return this._currentLink; }
16 protected _currentLink: ILinkWithState | undefined;
17 private _mouseDownLink: ILinkWithState | undefined;
18 private _lastMouseEvent: MouseEvent | undefined;
19 private _linkCacheDisposables: IDisposable[] = [];
20 private _lastBufferCell: IBufferCellPosition | undefined;
21 private _isMouseOut: boolean = true;
22 private _wasResized: boolean = false;
23 private _activeProviderReplies: Map<Number, ILinkWithState[] | undefined> | undefined;
24 private _activeLine: number = -1;
25
26 private readonly _onShowLinkUnderline = this._register(new Emitter<ILinkifierEvent>());
27 public readonly onShowLinkUnderline = this._onShowLinkUnderline.event;
28 private readonly _onHideLinkUnderline = this._register(new Emitter<ILinkifierEvent>());
29 public readonly onHideLinkUnderline = this._onHideLinkUnderline.event;
30
31 constructor(
32 private readonly _element: HTMLElement,
33 @IMouseService private readonly _mouseService: IMouseService,
34 @IRenderService private readonly _renderService: IRenderService,
35 @IBufferService private readonly _bufferService: IBufferService,
36 @ILinkProviderService private readonly _linkProviderService: ILinkProviderService
37 ) {
38 super();
39 this._register(toDisposable(() => {
40 dispose(this._linkCacheDisposables);
41 this._linkCacheDisposables.length = 0;
42 this._lastMouseEvent = undefined;
43 // Clear out link providers as they could easily cause an embedder memory leak
44 this._activeProviderReplies?.clear();
45 }));
46 // Listen to resize to catch the case where it's resized and the cursor is out of the viewport.
47 this._register(this._bufferService.onResize(() => {
48 this._clearCurrentLink();
49 this._wasResized = true;
50 }));
51 this._register(addDisposableListener(this._element, 'mouseleave', () => {
52 this._isMouseOut = true;
53 this._clearCurrentLink();
54 }));
55 this._register(addDisposableListener(this._element, 'mousemove', this._handleMouseMove.bind(this)));
56 this._register(addDisposableListener(this._element, 'mousedown', this._handleMouseDown.bind(this)));
57 this._register(addDisposableListener(this._element, 'mouseup', this._handleMouseUp.bind(this)));
58 }
59
60 private _handleMouseMove(event: MouseEvent): void {
61 this._lastMouseEvent = event;
62
63 const position = this._positionFromMouseEvent(event, this._element, this._mouseService);
64 if (!position) {
65 return;
66 }
67 this._isMouseOut = false;
68
69 // Ignore the event if it's an embedder created hover widget
70 const composedPath = event.composedPath() as HTMLElement[];
71 for (let i = 0; i < composedPath.length; i++) {
72 const target = composedPath[i];
73 // Hit Terminal.element, break and continue
74 if (target.classList.contains('xterm')) {
75 break;
76 }
77 // It's a hover, don't respect hover event
78 if (target.classList.contains('xterm-hover')) {
79 return;
80 }
81 }
82
83 if (!this._lastBufferCell || (position.x !== this._lastBufferCell.x || position.y !== this._lastBufferCell.y)) {
84 this._handleHover(position);
85 this._lastBufferCell = position;
86 }
87 }
88
89 private _handleHover(position: IBufferCellPosition): void {
90 // TODO: This currently does not cache link provider results across wrapped lines, activeLine
91 // should be something like `activeRange: {startY, endY}`
92 // Check if we need to clear the link
93 if (this._activeLine !== position.y || this._wasResized) {
94 this._clearCurrentLink();
95 this._askForLink(position, false);
96 this._wasResized = false;
97 return;
98 }
99
100 // Check the if the link is in the mouse position
101 const isCurrentLinkInPosition = this._currentLink && this._linkAtPosition(this._currentLink.link, position);
102 if (!isCurrentLinkInPosition) {
103 this._clearCurrentLink();
104 this._askForLink(position, true);
105 }
106 }
107
108 private _askForLink(position: IBufferCellPosition, useLineCache: boolean): void {
109 if (!this._activeProviderReplies || !useLineCache) {
110 this._activeProviderReplies?.forEach(reply => {
111 reply?.forEach(linkWithState => {
112 if (linkWithState.link.dispose) {
113 linkWithState.link.dispose();
114 }
115 });
116 });
117 this._activeProviderReplies = new Map();
118 this._activeLine = position.y;
119 }
120 let linkProvided = false;
121
122 // There is no link cached, so ask for one
123 for (const [i, linkProvider] of this._linkProviderService.linkProviders.entries()) {
124 if (useLineCache) {
125 const existingReply = this._activeProviderReplies?.get(i);
126 // If there isn't a reply, the provider hasn't responded yet.
127
128 // TODO: If there isn't a reply yet it means that the provider is still resolving. Ensuring
129 // provideLinks isn't triggered again saves ILink.hover firing twice though. This probably
130 // needs promises to get fixed
131 if (existingReply) {
132 linkProvided = this._checkLinkProviderResult(i, position, linkProvided);
133 }
134 } else {
135 linkProvider.provideLinks(position.y, (links: ILink[] | undefined) => {
136 if (this._isMouseOut) {
137 return;
138 }
139 const linksWithState: ILinkWithState[] | undefined = links?.map(link => ({ link }));
140 this._activeProviderReplies?.set(i, linksWithState);
141 linkProvided = this._checkLinkProviderResult(i, position, linkProvided);
142
143 // If all providers have responded, remove lower priority links that intersect ranges of
144 // higher priority links
145 if (this._activeProviderReplies?.size === this._linkProviderService.linkProviders.length) {
146 this._removeIntersectingLinks(position.y, this._activeProviderReplies);
147 }
148 });
149 }
150 }
151 }
152
153 private _removeIntersectingLinks(y: number, replies: Map<Number, ILinkWithState[] | undefined>): void {
154 const occupiedCells = new Set<number>();
155 for (let i = 0; i < replies.size; i++) {
156 const providerReply = replies.get(i);
157 if (!providerReply) {
158 continue;
159 }
160 for (let i = 0; i < providerReply.length; i++) {
161 const linkWithState = providerReply[i];
162 const startX = linkWithState.link.range.start.y < y ? 0 : linkWithState.link.range.start.x;
163 const endX = linkWithState.link.range.end.y > y ? this._bufferService.cols : linkWithState.link.range.end.x;
164 for (let x = startX; x <= endX; x++) {
165 if (occupiedCells.has(x)) {
166 providerReply.splice(i--, 1);
167 break;
168 }
169 occupiedCells.add(x);
170 }
171 }
172 }
173 }
174
175 private _checkLinkProviderResult(index: number, position: IBufferCellPosition, linkProvided: boolean): boolean {
176 if (!this._activeProviderReplies) {
177 return linkProvided;
178 }
179
180 const links = this._activeProviderReplies.get(index);
181
182 // Check if every provider before this one has come back undefined
183 let hasLinkBefore = false;
184 for (let j = 0; j < index; j++) {
185 if (!this._activeProviderReplies.has(j) || this._activeProviderReplies.get(j)) {
186 hasLinkBefore = true;
187 }
188 }
189
190 // If all providers with higher priority came back undefined, then this provider's link for
191 // the position should be used
192 if (!hasLinkBefore && links) {
193 const linkAtPosition = links.find(link => this._linkAtPosition(link.link, position));
194 if (linkAtPosition) {
195 linkProvided = true;
196 this._handleNewLink(linkAtPosition);
197 }
198 }
199
200 // Check if all the providers have responded
201 if (this._activeProviderReplies.size === this._linkProviderService.linkProviders.length && !linkProvided) {
202 // Respect the order of the link providers
203 for (let j = 0; j < this._activeProviderReplies.size; j++) {
204 const currentLink = this._activeProviderReplies.get(j)?.find(link => this._linkAtPosition(link.link, position));
205 if (currentLink) {
206 linkProvided = true;
207 this._handleNewLink(currentLink);
208 break;
209 }
210 }
211 }
212
213 return linkProvided;
214 }
215
216 private _handleMouseDown(): void {
217 this._mouseDownLink = this._currentLink;
218 }
219
220 private _handleMouseUp(event: MouseEvent): void {
221 if (!this._currentLink) {
222 return;
223 }
224
225 const position = this._positionFromMouseEvent(event, this._element, this._mouseService);
226 if (!position) {
227 return;
228 }
229
230 if (this._mouseDownLink && linkEquals(this._mouseDownLink.link, this._currentLink.link) && this._linkAtPosition(this._currentLink.link, position)) {
231 this._currentLink.link.activate(event, this._currentLink.link.text);
232 }
233 }
234
235 private _clearCurrentLink(startRow?: number, endRow?: number): void {
236 if (!this._currentLink || !this._lastMouseEvent) {
237 return;
238 }
239
240 // If we have a start and end row, check that the link is within it
241 if (!startRow || !endRow || (this._currentLink.link.range.start.y >= startRow && this._currentLink.link.range.end.y <= endRow)) {
242 this._linkLeave(this._element, this._currentLink.link, this._lastMouseEvent);
243 this._currentLink = undefined;
244 dispose(this._linkCacheDisposables);
245 this._linkCacheDisposables.length = 0;
246 }
247 }
248
249 private _handleNewLink(linkWithState: ILinkWithState): void {
250 if (!this._lastMouseEvent) {
251 return;
252 }
253
254 const position = this._positionFromMouseEvent(this._lastMouseEvent, this._element, this._mouseService);
255
256 if (!position) {
257 return;
258 }
259
260 // Trigger hover if the we have a link at the position
261 if (this._linkAtPosition(linkWithState.link, position)) {
262 this._currentLink = linkWithState;
263 this._currentLink.state = {
264 decorations: {
265 underline: linkWithState.link.decorations === undefined ? true : linkWithState.link.decorations.underline,
266 pointerCursor: linkWithState.link.decorations === undefined ? true : linkWithState.link.decorations.pointerCursor
267 },
268 isHovered: true
269 };
270 this._linkHover(this._element, linkWithState.link, this._lastMouseEvent);
271
272 // Add listener for tracking decorations changes
273 linkWithState.link.decorations = {} as ILinkDecorations;
274 Object.defineProperties(linkWithState.link.decorations, {
275 pointerCursor: {
276 get: () => this._currentLink?.state?.decorations.pointerCursor,
277 set: v => {
278 if (this._currentLink?.state && this._currentLink.state.decorations.pointerCursor !== v) {
279 this._currentLink.state.decorations.pointerCursor = v;
280 if (this._currentLink.state.isHovered) {
281 this._element.classList.toggle('xterm-cursor-pointer', v);
282 }
283 }
284 }
285 },
286 underline: {
287 get: () => this._currentLink?.state?.decorations.underline,
288 set: v => {
289 if (this._currentLink?.state && this._currentLink?.state?.decorations.underline !== v) {
290 this._currentLink.state.decorations.underline = v;
291 if (this._currentLink.state.isHovered) {
292 this._fireUnderlineEvent(linkWithState.link, v);
293 }
294 }
295 }
296 }
297 });
298
299 // Listen to viewport changes to re-render the link under the cursor (only when the line the
300 // link is on changes)
301 this._linkCacheDisposables.push(this._renderService.onRenderedViewportChange(e => {
302 // Sanity check, this shouldn't happen in practice as this listener would be disposed
303 if (!this._currentLink) {
304 return;
305 }
306 // When start is 0 a scroll most likely occurred, make sure links above the fold also get
307 // cleared.
308 const start = e.start === 0 ? 0 : e.start + 1 + this._bufferService.buffer.ydisp;
309 const end = this._bufferService.buffer.ydisp + 1 + e.end;
310 // Only clear the link if the viewport change happened on this line
311 if (this._currentLink.link.range.start.y >= start && this._currentLink.link.range.end.y <= end) {
312 this._clearCurrentLink(start, end);
313 if (this._lastMouseEvent) {
314 // re-eval previously active link after changes
315 const position = this._positionFromMouseEvent(this._lastMouseEvent, this._element, this._mouseService!);
316 if (position) {
317 this._askForLink(position, false);
318 }
319 }
320 }
321 }));
322 }
323 }
324
325 protected _linkHover(element: HTMLElement, link: ILink, event: MouseEvent): void {
326 if (this._currentLink?.state) {
327 this._currentLink.state.isHovered = true;
328 if (this._currentLink.state.decorations.underline) {
329 this._fireUnderlineEvent(link, true);
330 }
331 if (this._currentLink.state.decorations.pointerCursor) {
332 element.classList.add('xterm-cursor-pointer');
333 }
334 }
335
336 if (link.hover) {
337 link.hover(event, link.text);
338 }
339 }
340
341 private _fireUnderlineEvent(link: ILink, showEvent: boolean): void {
342 const range = link.range;
343 const scrollOffset = this._bufferService.buffer.ydisp;
344 const event = this._createLinkUnderlineEvent(range.start.x - 1, range.start.y - scrollOffset - 1, range.end.x, range.end.y - scrollOffset - 1, undefined);
345 const emitter = showEvent ? this._onShowLinkUnderline : this._onHideLinkUnderline;
346 emitter.fire(event);
347 }
348
349 protected _linkLeave(element: HTMLElement, link: ILink, event: MouseEvent): void {
350 if (this._currentLink?.state) {
351 this._currentLink.state.isHovered = false;
352 if (this._currentLink.state.decorations.underline) {
353 this._fireUnderlineEvent(link, false);
354 }
355 if (this._currentLink.state.decorations.pointerCursor) {
356 element.classList.remove('xterm-cursor-pointer');
357 }
358 }
359
360 if (link.leave) {
361 link.leave(event, link.text);
362 }
363 }
364
365 /**
366 * Check if the buffer position is within the link
367 * @param link
368 * @param position
369 */
370 private _linkAtPosition(link: ILink, position: IBufferCellPosition): boolean {
371 const lower = link.range.start.y * this._bufferService.cols + link.range.start.x;
372 const upper = link.range.end.y * this._bufferService.cols + link.range.end.x;
373 const current = position.y * this._bufferService.cols + position.x;
374 return (lower <= current && current <= upper);
375 }
376
377 /**
378 * Get the buffer position from a mouse event
379 * @param event
380 */
381 private _positionFromMouseEvent(event: MouseEvent, element: HTMLElement, mouseService: IMouseService): IBufferCellPosition | undefined {
382 const coords = mouseService.getCoords(event, element, this._bufferService.cols, this._bufferService.rows);
383 if (!coords) {
384 return;
385 }
386
387 return { x: coords[0], y: coords[1] + this._bufferService.buffer.ydisp };
388 }
389
390 private _createLinkUnderlineEvent(x1: number, y1: number, x2: number, y2: number, fg: number | undefined): ILinkifierEvent {
391 return { x1, y1, x2, y2, cols: this._bufferService.cols, fg };
392 }
393 }
394
395 function linkEquals(a: ILink, b: ILink): boolean {
396 return (
397 a.text === b.text &&
398 a.range.start.x === b.range.start.x &&
399 a.range.start.y === b.range.start.y &&
400 a.range.end.x === b.range.end.x &&
401 a.range.end.y === b.range.end.y
402 );
403 }