Debian Codegraph @

node/@xterm/xterm/src/browser/selection/SelectionModel.ts

raw
1 /**
2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { IBufferService } from 'common/services/Services';
7
8 /**
9 * Represents a selection within the buffer. This model only cares about column
10 * and row coordinates, not wide characters.
11 */
12 export class SelectionModel {
13 /**
14 * Whether select all is currently active.
15 */
16 public isSelectAllActive: boolean = false;
17
18 /**
19 * The minimal length of the selection from the start position. When double
20 * clicking on a word, the word will be selected which makes the selection
21 * start at the start of the word and makes this variable the length.
22 */
23 public selectionStartLength: number = 0;
24
25 /**
26 * The [x, y] position the selection starts at.
27 */
28 public selectionStart: [number, number] | undefined;
29
30 /**
31 * The [x, y] position the selection ends at.
32 */
33 public selectionEnd: [number, number] | undefined;
34
35 constructor(
36 private _bufferService: IBufferService
37 ) {
38 }
39
40 /**
41 * Clears the current selection.
42 */
43 public clearSelection(): void {
44 this.selectionStart = undefined;
45 this.selectionEnd = undefined;
46 this.isSelectAllActive = false;
47 this.selectionStartLength = 0;
48 }
49
50 /**
51 * The final selection start, taking into consideration select all.
52 */
53 public get finalSelectionStart(): [number, number] | undefined {
54 if (this.isSelectAllActive) {
55 return [0, 0];
56 }
57
58 if (!this.selectionEnd || !this.selectionStart) {
59 return this.selectionStart;
60 }
61
62 return this.areSelectionValuesReversed() ? this.selectionEnd : this.selectionStart;
63 }
64
65 /**
66 * The final selection end, taking into consideration select all, double click
67 * word selection and triple click line selection.
68 */
69 public get finalSelectionEnd(): [number, number] | undefined {
70 if (this.isSelectAllActive) {
71 return [this._bufferService.cols, this._bufferService.buffer.ybase + this._bufferService.rows - 1];
72 }
73
74 if (!this.selectionStart) {
75 return undefined;
76 }
77
78 // Use the selection start + length if the end doesn't exist or they're reversed
79 if (!this.selectionEnd || this.areSelectionValuesReversed()) {
80 const startPlusLength = this.selectionStart[0] + this.selectionStartLength;
81 if (startPlusLength > this._bufferService.cols) {
82 // Ensure the trailing EOL isn't included when the selection ends on the right edge
83 if (startPlusLength % this._bufferService.cols === 0) {
84 return [this._bufferService.cols, this.selectionStart[1] + Math.floor(startPlusLength / this._bufferService.cols) - 1];
85 }
86 return [startPlusLength % this._bufferService.cols, this.selectionStart[1] + Math.floor(startPlusLength / this._bufferService.cols)];
87 }
88 return [startPlusLength, this.selectionStart[1]];
89 }
90
91 // Ensure the the word/line is selected after a double/triple click
92 if (this.selectionStartLength) {
93 // Select the larger of the two when start and end are on the same line
94 if (this.selectionEnd[1] === this.selectionStart[1]) {
95 // Keep the whole wrapped word/line selected if the content wraps multiple lines
96 const startPlusLength = this.selectionStart[0] + this.selectionStartLength;
97 if (startPlusLength > this._bufferService.cols) {
98 return [startPlusLength % this._bufferService.cols, this.selectionStart[1] + Math.floor(startPlusLength / this._bufferService.cols)];
99 }
100 return [Math.max(startPlusLength, this.selectionEnd[0]), this.selectionEnd[1]];
101 }
102 }
103 return this.selectionEnd;
104 }
105
106 /**
107 * Returns whether the selection start and end are reversed.
108 */
109 public areSelectionValuesReversed(): boolean {
110 const start = this.selectionStart;
111 const end = this.selectionEnd;
112 if (!start || !end) {
113 return false;
114 }
115 return start[1] > end[1] || (start[1] === end[1] && start[0] > end[0]);
116 }
117
118 /**
119 * Handle the buffer being trimmed, adjust the selection position.
120 * @param amount The amount the buffer is being trimmed.
121 * @returns Whether a refresh is necessary.
122 */
123 public handleTrim(amount: number): boolean {
124 // Adjust the selection position based on the trimmed amount.
125 if (this.selectionStart) {
126 this.selectionStart[1] -= amount;
127 }
128 if (this.selectionEnd) {
129 this.selectionEnd[1] -= amount;
130 }
131
132 // The selection has moved off the buffer, clear it.
133 if (this.selectionEnd && this.selectionEnd[1] < 0) {
134 this.clearSelection();
135 return true;
136 }
137
138 // If the selection start is trimmed, ensure the start column is 0.
139 if (this.selectionStart && this.selectionStart[1] < 0) {
140 this.selectionStart[1] = 0;
141 }
142 return false;
143 }
144 }