Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/browser/viewParts/currentLineHighlight/currentLineHighlight.ts
3296 views
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
6
import './currentLineHighlight.css';
7
import { DynamicViewOverlay } from '../../view/dynamicViewOverlay.js';
8
import { editorLineHighlight, editorLineHighlightBorder } from '../../../common/core/editorColorRegistry.js';
9
import { RenderingContext } from '../../view/renderingContext.js';
10
import { ViewContext } from '../../../common/viewModel/viewContext.js';
11
import * as viewEvents from '../../../common/viewEvents.js';
12
import * as arrays from '../../../../base/common/arrays.js';
13
import { registerThemingParticipant } from '../../../../platform/theme/common/themeService.js';
14
import { Selection } from '../../../common/core/selection.js';
15
import { EditorOption } from '../../../common/config/editorOptions.js';
16
import { isHighContrast } from '../../../../platform/theme/common/theme.js';
17
import { Position } from '../../../common/core/position.js';
18
19
export abstract class AbstractLineHighlightOverlay extends DynamicViewOverlay {
20
private readonly _context: ViewContext;
21
protected _renderLineHighlight: 'none' | 'gutter' | 'line' | 'all';
22
protected _wordWrap: boolean;
23
protected _contentLeft: number;
24
protected _contentWidth: number;
25
protected _selectionIsEmpty: boolean;
26
protected _renderLineHighlightOnlyWhenFocus: boolean;
27
protected _focused: boolean;
28
/**
29
* Unique sorted list of view line numbers which have cursors sitting on them.
30
*/
31
private _cursorLineNumbers: number[];
32
private _selections: Selection[];
33
private _renderData: string[] | null;
34
35
constructor(context: ViewContext) {
36
super();
37
this._context = context;
38
39
const options = this._context.configuration.options;
40
const layoutInfo = options.get(EditorOption.layoutInfo);
41
this._renderLineHighlight = options.get(EditorOption.renderLineHighlight);
42
this._renderLineHighlightOnlyWhenFocus = options.get(EditorOption.renderLineHighlightOnlyWhenFocus);
43
this._wordWrap = layoutInfo.isViewportWrapping;
44
this._contentLeft = layoutInfo.contentLeft;
45
this._contentWidth = layoutInfo.contentWidth;
46
this._selectionIsEmpty = true;
47
this._focused = false;
48
this._cursorLineNumbers = [1];
49
this._selections = [new Selection(1, 1, 1, 1)];
50
this._renderData = null;
51
52
this._context.addEventHandler(this);
53
}
54
55
public override dispose(): void {
56
this._context.removeEventHandler(this);
57
super.dispose();
58
}
59
60
private _readFromSelections(): boolean {
61
let hasChanged = false;
62
63
const lineNumbers = new Set<number>();
64
for (const selection of this._selections) {
65
lineNumbers.add(selection.positionLineNumber);
66
}
67
const cursorsLineNumbers = Array.from(lineNumbers);
68
cursorsLineNumbers.sort((a, b) => a - b);
69
if (!arrays.equals(this._cursorLineNumbers, cursorsLineNumbers)) {
70
this._cursorLineNumbers = cursorsLineNumbers;
71
hasChanged = true;
72
}
73
74
const selectionIsEmpty = this._selections.every(s => s.isEmpty());
75
if (this._selectionIsEmpty !== selectionIsEmpty) {
76
this._selectionIsEmpty = selectionIsEmpty;
77
hasChanged = true;
78
}
79
80
return hasChanged;
81
}
82
83
// --- begin event handlers
84
public override onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {
85
return this._readFromSelections();
86
}
87
public override onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
88
const options = this._context.configuration.options;
89
const layoutInfo = options.get(EditorOption.layoutInfo);
90
this._renderLineHighlight = options.get(EditorOption.renderLineHighlight);
91
this._renderLineHighlightOnlyWhenFocus = options.get(EditorOption.renderLineHighlightOnlyWhenFocus);
92
this._wordWrap = layoutInfo.isViewportWrapping;
93
this._contentLeft = layoutInfo.contentLeft;
94
this._contentWidth = layoutInfo.contentWidth;
95
return true;
96
}
97
public override onCursorStateChanged(e: viewEvents.ViewCursorStateChangedEvent): boolean {
98
this._selections = e.selections;
99
return this._readFromSelections();
100
}
101
public override onFlushed(e: viewEvents.ViewFlushedEvent): boolean {
102
return true;
103
}
104
public override onLinesDeleted(e: viewEvents.ViewLinesDeletedEvent): boolean {
105
return true;
106
}
107
public override onLinesInserted(e: viewEvents.ViewLinesInsertedEvent): boolean {
108
return true;
109
}
110
public override onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
111
return e.scrollWidthChanged || e.scrollTopChanged;
112
}
113
public override onZonesChanged(e: viewEvents.ViewZonesChangedEvent): boolean {
114
return true;
115
}
116
public override onFocusChanged(e: viewEvents.ViewFocusChangedEvent): boolean {
117
if (!this._renderLineHighlightOnlyWhenFocus) {
118
return false;
119
}
120
121
this._focused = e.isFocused;
122
return true;
123
}
124
// --- end event handlers
125
126
public prepareRender(ctx: RenderingContext): void {
127
if (!this._shouldRenderThis()) {
128
this._renderData = null;
129
return;
130
}
131
const visibleStartLineNumber = ctx.visibleRange.startLineNumber;
132
const visibleEndLineNumber = ctx.visibleRange.endLineNumber;
133
134
// initialize renderData
135
const renderData: string[] = [];
136
for (let lineNumber = visibleStartLineNumber; lineNumber <= visibleEndLineNumber; lineNumber++) {
137
const lineIndex = lineNumber - visibleStartLineNumber;
138
renderData[lineIndex] = '';
139
}
140
141
if (this._wordWrap) {
142
// do a first pass to render wrapped lines
143
const renderedLineWrapped = this._renderOne(ctx, false);
144
for (const cursorLineNumber of this._cursorLineNumbers) {
145
146
const coordinatesConverter = this._context.viewModel.coordinatesConverter;
147
const modelLineNumber = coordinatesConverter.convertViewPositionToModelPosition(new Position(cursorLineNumber, 1)).lineNumber;
148
const firstViewLineNumber = coordinatesConverter.convertModelPositionToViewPosition(new Position(modelLineNumber, 1)).lineNumber;
149
const lastViewLineNumber = coordinatesConverter.convertModelPositionToViewPosition(new Position(modelLineNumber, this._context.viewModel.model.getLineMaxColumn(modelLineNumber))).lineNumber;
150
151
const firstLine = Math.max(firstViewLineNumber, visibleStartLineNumber);
152
const lastLine = Math.min(lastViewLineNumber, visibleEndLineNumber);
153
for (let lineNumber = firstLine; lineNumber <= lastLine; lineNumber++) {
154
const lineIndex = lineNumber - visibleStartLineNumber;
155
renderData[lineIndex] = renderedLineWrapped;
156
}
157
}
158
}
159
160
// do a second pass to render exact lines
161
const renderedLineExact = this._renderOne(ctx, true);
162
for (const cursorLineNumber of this._cursorLineNumbers) {
163
if (cursorLineNumber < visibleStartLineNumber || cursorLineNumber > visibleEndLineNumber) {
164
continue;
165
}
166
const lineIndex = cursorLineNumber - visibleStartLineNumber;
167
renderData[lineIndex] = renderedLineExact;
168
}
169
170
this._renderData = renderData;
171
}
172
173
public render(startLineNumber: number, lineNumber: number): string {
174
if (!this._renderData) {
175
return '';
176
}
177
const lineIndex = lineNumber - startLineNumber;
178
if (lineIndex >= this._renderData.length) {
179
return '';
180
}
181
return this._renderData[lineIndex];
182
}
183
184
protected _shouldRenderInMargin(): boolean {
185
return (
186
(this._renderLineHighlight === 'gutter' || this._renderLineHighlight === 'all')
187
&& (!this._renderLineHighlightOnlyWhenFocus || this._focused)
188
);
189
}
190
191
protected _shouldRenderInContent(): boolean {
192
return (
193
(this._renderLineHighlight === 'line' || this._renderLineHighlight === 'all')
194
&& this._selectionIsEmpty
195
&& (!this._renderLineHighlightOnlyWhenFocus || this._focused)
196
);
197
}
198
199
protected abstract _shouldRenderThis(): boolean;
200
protected abstract _shouldRenderOther(): boolean;
201
protected abstract _renderOne(ctx: RenderingContext, exact: boolean): string;
202
}
203
204
/**
205
* Emphasizes the current line by drawing a border around it.
206
*/
207
export class CurrentLineHighlightOverlay extends AbstractLineHighlightOverlay {
208
209
protected _renderOne(ctx: RenderingContext, exact: boolean): string {
210
const className = 'current-line' + (this._shouldRenderInMargin() ? ' current-line-both' : '') + (exact ? ' current-line-exact' : '');
211
return `<div class="${className}" style="width:${Math.max(ctx.scrollWidth, this._contentWidth)}px;"></div>`;
212
}
213
protected _shouldRenderThis(): boolean {
214
return this._shouldRenderInContent();
215
}
216
protected _shouldRenderOther(): boolean {
217
return this._shouldRenderInMargin();
218
}
219
}
220
221
/**
222
* Emphasizes the current line margin/gutter by drawing a border around it.
223
*/
224
export class CurrentLineMarginHighlightOverlay extends AbstractLineHighlightOverlay {
225
protected _renderOne(ctx: RenderingContext, exact: boolean): string {
226
const className = 'current-line' + (this._shouldRenderInMargin() ? ' current-line-margin' : '') + (this._shouldRenderOther() ? ' current-line-margin-both' : '') + (this._shouldRenderInMargin() && exact ? ' current-line-exact-margin' : '');
227
return `<div class="${className}" style="width:${this._contentLeft}px"></div>`;
228
}
229
protected _shouldRenderThis(): boolean {
230
return true;
231
}
232
protected _shouldRenderOther(): boolean {
233
return this._shouldRenderInContent();
234
}
235
}
236
237
registerThemingParticipant((theme, collector) => {
238
const lineHighlight = theme.getColor(editorLineHighlight);
239
if (lineHighlight) {
240
collector.addRule(`.monaco-editor .view-overlays .current-line { background-color: ${lineHighlight}; }`);
241
collector.addRule(`.monaco-editor .margin-view-overlays .current-line-margin { background-color: ${lineHighlight}; border: none; }`);
242
}
243
if (!lineHighlight || lineHighlight.isTransparent() || theme.defines(editorLineHighlightBorder)) {
244
const lineHighlightBorder = theme.getColor(editorLineHighlightBorder);
245
if (lineHighlightBorder) {
246
collector.addRule(`.monaco-editor .view-overlays .current-line-exact { border: 2px solid ${lineHighlightBorder}; }`);
247
collector.addRule(`.monaco-editor .margin-view-overlays .current-line-exact-margin { border: 2px solid ${lineHighlightBorder}; }`);
248
if (isHighContrast(theme.type)) {
249
collector.addRule(`.monaco-editor .view-overlays .current-line-exact { border-width: 1px; }`);
250
collector.addRule(`.monaco-editor .margin-view-overlays .current-line-exact-margin { border-width: 1px; }`);
251
}
252
}
253
}
254
});
255
256