Skip to content

Commit 2bab83a

Browse files
authored
Add option to control width of the line cursor (#41169)
* Add option to control width of the line cursor #41052 * Allow lineCursorWidth greater than 10 * Avoid linecontent read when not needed
1 parent c240df0 commit 2bab83a

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

src/vs/editor/browser/viewParts/viewCursors/viewCursor.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class ViewCursor {
3838
private readonly _domNode: FastDomNode<HTMLElement>;
3939

4040
private _cursorStyle: TextEditorCursorStyle;
41+
private _lineCursorWidth: number;
4142
private _lineHeight: number;
4243
private _typicalHalfwidthCharacterWidth: number;
4344

@@ -55,6 +56,7 @@ export class ViewCursor {
5556
this._cursorStyle = this._context.configuration.editor.viewInfo.cursorStyle;
5657
this._lineHeight = this._context.configuration.editor.lineHeight;
5758
this._typicalHalfwidthCharacterWidth = this._context.configuration.editor.fontInfo.typicalHalfwidthCharacterWidth;
59+
this._lineCursorWidth = Math.min(this._context.configuration.editor.viewInfo.lineCursorWidth, this._typicalHalfwidthCharacterWidth);
5860

5961
this._isVisible = true;
6062

@@ -103,13 +105,15 @@ export class ViewCursor {
103105
if (e.lineHeight) {
104106
this._lineHeight = this._context.configuration.editor.lineHeight;
105107
}
106-
if (e.viewInfo) {
107-
this._cursorStyle = this._context.configuration.editor.viewInfo.cursorStyle;
108-
}
109108
if (e.fontInfo) {
110109
Configuration.applyFontInfo(this._domNode, this._context.configuration.editor.fontInfo);
111110
this._typicalHalfwidthCharacterWidth = this._context.configuration.editor.fontInfo.typicalHalfwidthCharacterWidth;
112111
}
112+
if (e.viewInfo) {
113+
this._cursorStyle = this._context.configuration.editor.viewInfo.cursorStyle;
114+
this._lineCursorWidth = Math.min(this._context.configuration.editor.viewInfo.lineCursorWidth, this._typicalHalfwidthCharacterWidth);
115+
}
116+
113117
return true;
114118
}
115119

@@ -119,6 +123,8 @@ export class ViewCursor {
119123
}
120124

121125
private _prepareRender(ctx: RenderingContext): ViewCursorRenderData {
126+
let textContent = '';
127+
122128
if (this._cursorStyle === TextEditorCursorStyle.Line || this._cursorStyle === TextEditorCursorStyle.LineThin) {
123129
const visibleRange = ctx.visibleRangeForPosition(this._position);
124130
if (!visibleRange) {
@@ -127,12 +133,16 @@ export class ViewCursor {
127133
}
128134
let width: number;
129135
if (this._cursorStyle === TextEditorCursorStyle.Line) {
130-
width = dom.computeScreenAwareSize(2);
136+
width = dom.computeScreenAwareSize(this._lineCursorWidth > 0 ? this._lineCursorWidth : 2);
137+
if (this._lineCursorWidth > 2) {
138+
const lineContent = this._context.model.getLineContent(this._position.lineNumber);
139+
textContent = lineContent.charAt(this._position.column - 1);
140+
}
131141
} else {
132142
width = dom.computeScreenAwareSize(1);
133143
}
134144
const top = ctx.getVerticalOffsetForLineNumber(this._position.lineNumber) - ctx.bigNumbersDelta;
135-
return new ViewCursorRenderData(top, visibleRange.left, width, this._lineHeight, '');
145+
return new ViewCursorRenderData(top, visibleRange.left, width, this._lineHeight, textContent);
136146
}
137147

138148
const visibleRangeForCharacter = ctx.linesVisibleRangesForRange(new Range(this._position.lineNumber, this._position.column, this._position.lineNumber, this._position.column + 1), false);
@@ -145,7 +155,6 @@ export class ViewCursor {
145155
const range = visibleRangeForCharacter[0].ranges[0];
146156
const width = range.width < 1 ? this._typicalHalfwidthCharacterWidth : range.width;
147157

148-
let textContent = '';
149158
if (this._cursorStyle === TextEditorCursorStyle.Block) {
150159
const lineContent = this._context.model.getLineContent(this._position.lineNumber);
151160
textContent = lineContent.charAt(this._position.column - 1);

src/vs/editor/browser/viewParts/viewCursors/viewCursors.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
.monaco-editor .cursors-layer > .cursor {
1111
position: absolute;
1212
cursor: text;
13+
overflow: hidden;
1314
}
1415
.monaco-editor .cursors-layer > .cursor.secondary {
1516
opacity: 0.6;

src/vs/editor/common/config/commonEditorConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ const editorConfiguration: IConfigurationNode = {
511511
'default': editorOptions.cursorStyleToString(EDITOR_DEFAULTS.viewInfo.cursorStyle),
512512
'description': nls.localize('cursorStyle', "Controls the cursor style, accepted values are 'block', 'block-outline', 'line', 'line-thin', 'underline' and 'underline-thin'")
513513
},
514+
'editor.lineCursorWidth': {
515+
'type': 'integer',
516+
'default': EDITOR_DEFAULTS.viewInfo.lineCursorWidth,
517+
'description': nls.localize('lineCursorWidth', "Controls the width of the cursor when editor.cursorStyle is set to 'line'")
518+
},
514519
'editor.fontLigatures': {
515520
'type': 'boolean',
516521
'default': EDITOR_DEFAULTS.viewInfo.fontLigatures,

src/vs/editor/common/config/editorOptions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ export interface IEditorOptions {
256256
* Defaults to 'line'.
257257
*/
258258
cursorStyle?: string;
259+
/**
260+
* Control the width of the cursor when cursorStyle is set to 'line'
261+
*/
262+
lineCursorWidth?: number;
259263
/**
260264
* Enable font ligatures.
261265
* Defaults to false.
@@ -786,6 +790,7 @@ export interface InternalEditorViewOptions {
786790
readonly cursorBlinking: TextEditorCursorBlinkingStyle;
787791
readonly mouseWheelZoom: boolean;
788792
readonly cursorStyle: TextEditorCursorStyle;
793+
readonly lineCursorWidth: number;
789794
readonly hideCursorInOverviewRuler: boolean;
790795
readonly scrollBeyondLastLine: boolean;
791796
readonly smoothScrolling: boolean;
@@ -1054,6 +1059,7 @@ export class InternalEditorOptions {
10541059
&& a.cursorBlinking === b.cursorBlinking
10551060
&& a.mouseWheelZoom === b.mouseWheelZoom
10561061
&& a.cursorStyle === b.cursorStyle
1062+
&& a.lineCursorWidth === b.lineCursorWidth
10571063
&& a.hideCursorInOverviewRuler === b.hideCursorInOverviewRuler
10581064
&& a.scrollBeyondLastLine === b.scrollBeyondLastLine
10591065
&& a.smoothScrolling === b.smoothScrolling
@@ -1647,6 +1653,7 @@ export class EditorOptionsValidator {
16471653
cursorBlinking: _cursorBlinkingStyleFromString(opts.cursorBlinking, defaults.cursorBlinking),
16481654
mouseWheelZoom: _boolean(opts.mouseWheelZoom, defaults.mouseWheelZoom),
16491655
cursorStyle: _cursorStyleFromString(opts.cursorStyle, defaults.cursorStyle),
1656+
lineCursorWidth: _clampedInt(opts.lineCursorWidth, defaults.lineCursorWidth, 1, Number.MAX_VALUE),
16501657
hideCursorInOverviewRuler: _boolean(opts.hideCursorInOverviewRuler, defaults.hideCursorInOverviewRuler),
16511658
scrollBeyondLastLine: _boolean(opts.scrollBeyondLastLine, defaults.scrollBeyondLastLine),
16521659
smoothScrolling: _boolean(opts.smoothScrolling, defaults.smoothScrolling),
@@ -1749,6 +1756,7 @@ export class InternalEditorOptionsFactory {
17491756
cursorBlinking: opts.viewInfo.cursorBlinking,
17501757
mouseWheelZoom: opts.viewInfo.mouseWheelZoom,
17511758
cursorStyle: opts.viewInfo.cursorStyle,
1759+
lineCursorWidth: opts.viewInfo.lineCursorWidth,
17521760
hideCursorInOverviewRuler: opts.viewInfo.hideCursorInOverviewRuler,
17531761
scrollBeyondLastLine: opts.viewInfo.scrollBeyondLastLine,
17541762
smoothScrolling: opts.viewInfo.smoothScrolling,
@@ -2172,6 +2180,7 @@ export const EDITOR_DEFAULTS: IValidatedEditorOptions = {
21722180
cursorBlinking: TextEditorCursorBlinkingStyle.Blink,
21732181
mouseWheelZoom: false,
21742182
cursorStyle: TextEditorCursorStyle.Line,
2183+
lineCursorWidth: 2,
21752184
hideCursorInOverviewRuler: false,
21762185
scrollBeyondLastLine: true,
21772186
smoothScrolling: false,

src/vs/monaco.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,10 @@ declare module monaco.editor {
25382538
* Defaults to 'line'.
25392539
*/
25402540
cursorStyle?: string;
2541+
/**
2542+
* Control the width of the cursor when cursorStyle is set to 'line'
2543+
*/
2544+
lineCursorWidth?: number;
25412545
/**
25422546
* Enable font ligatures.
25432547
* Defaults to false.
@@ -3003,6 +3007,7 @@ declare module monaco.editor {
30033007
readonly cursorBlinking: TextEditorCursorBlinkingStyle;
30043008
readonly mouseWheelZoom: boolean;
30053009
readonly cursorStyle: TextEditorCursorStyle;
3010+
readonly lineCursorWidth: number;
30063011
readonly hideCursorInOverviewRuler: boolean;
30073012
readonly scrollBeyondLastLine: boolean;
30083013
readonly smoothScrolling: boolean;

0 commit comments

Comments
 (0)