Path: blob/main/src/vs/editor/browser/viewParts/currentLineHighlight/currentLineHighlight.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import './currentLineHighlight.css';6import { DynamicViewOverlay } from '../../view/dynamicViewOverlay.js';7import { editorLineHighlight, editorLineHighlightBorder } from '../../../common/core/editorColorRegistry.js';8import { RenderingContext } from '../../view/renderingContext.js';9import { ViewContext } from '../../../common/viewModel/viewContext.js';10import * as viewEvents from '../../../common/viewEvents.js';11import * as arrays from '../../../../base/common/arrays.js';12import { registerThemingParticipant } from '../../../../platform/theme/common/themeService.js';13import { Selection } from '../../../common/core/selection.js';14import { EditorOption } from '../../../common/config/editorOptions.js';15import { isHighContrast } from '../../../../platform/theme/common/theme.js';16import { Position } from '../../../common/core/position.js';1718export abstract class AbstractLineHighlightOverlay extends DynamicViewOverlay {19private readonly _context: ViewContext;20protected _renderLineHighlight: 'none' | 'gutter' | 'line' | 'all';21protected _wordWrap: boolean;22protected _contentLeft: number;23protected _contentWidth: number;24protected _selectionIsEmpty: boolean;25protected _renderLineHighlightOnlyWhenFocus: boolean;26protected _focused: boolean;27/**28* Unique sorted list of view line numbers which have cursors sitting on them.29*/30private _cursorLineNumbers: number[];31private _selections: Selection[];32private _renderData: string[] | null;3334constructor(context: ViewContext) {35super();36this._context = context;3738const options = this._context.configuration.options;39const layoutInfo = options.get(EditorOption.layoutInfo);40this._renderLineHighlight = options.get(EditorOption.renderLineHighlight);41this._renderLineHighlightOnlyWhenFocus = options.get(EditorOption.renderLineHighlightOnlyWhenFocus);42this._wordWrap = layoutInfo.isViewportWrapping;43this._contentLeft = layoutInfo.contentLeft;44this._contentWidth = layoutInfo.contentWidth;45this._selectionIsEmpty = true;46this._focused = false;47this._cursorLineNumbers = [1];48this._selections = [new Selection(1, 1, 1, 1)];49this._renderData = null;5051this._context.addEventHandler(this);52}5354public override dispose(): void {55this._context.removeEventHandler(this);56super.dispose();57}5859private _readFromSelections(): boolean {60let hasChanged = false;6162const lineNumbers = new Set<number>();63for (const selection of this._selections) {64lineNumbers.add(selection.positionLineNumber);65}66const cursorsLineNumbers = Array.from(lineNumbers);67cursorsLineNumbers.sort((a, b) => a - b);68if (!arrays.equals(this._cursorLineNumbers, cursorsLineNumbers)) {69this._cursorLineNumbers = cursorsLineNumbers;70hasChanged = true;71}7273const selectionIsEmpty = this._selections.every(s => s.isEmpty());74if (this._selectionIsEmpty !== selectionIsEmpty) {75this._selectionIsEmpty = selectionIsEmpty;76hasChanged = true;77}7879return hasChanged;80}8182// --- begin event handlers83public override onThemeChanged(e: viewEvents.ViewThemeChangedEvent): boolean {84return this._readFromSelections();85}86public override onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {87const options = this._context.configuration.options;88const layoutInfo = options.get(EditorOption.layoutInfo);89this._renderLineHighlight = options.get(EditorOption.renderLineHighlight);90this._renderLineHighlightOnlyWhenFocus = options.get(EditorOption.renderLineHighlightOnlyWhenFocus);91this._wordWrap = layoutInfo.isViewportWrapping;92this._contentLeft = layoutInfo.contentLeft;93this._contentWidth = layoutInfo.contentWidth;94return true;95}96public override onCursorStateChanged(e: viewEvents.ViewCursorStateChangedEvent): boolean {97this._selections = e.selections;98return this._readFromSelections();99}100public override onFlushed(e: viewEvents.ViewFlushedEvent): boolean {101return true;102}103public override onLinesDeleted(e: viewEvents.ViewLinesDeletedEvent): boolean {104return true;105}106public override onLinesInserted(e: viewEvents.ViewLinesInsertedEvent): boolean {107return true;108}109public override onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {110return e.scrollWidthChanged || e.scrollTopChanged;111}112public override onZonesChanged(e: viewEvents.ViewZonesChangedEvent): boolean {113return true;114}115public override onFocusChanged(e: viewEvents.ViewFocusChangedEvent): boolean {116if (!this._renderLineHighlightOnlyWhenFocus) {117return false;118}119120this._focused = e.isFocused;121return true;122}123// --- end event handlers124125public prepareRender(ctx: RenderingContext): void {126if (!this._shouldRenderThis()) {127this._renderData = null;128return;129}130const visibleStartLineNumber = ctx.visibleRange.startLineNumber;131const visibleEndLineNumber = ctx.visibleRange.endLineNumber;132133// initialize renderData134const renderData: string[] = [];135for (let lineNumber = visibleStartLineNumber; lineNumber <= visibleEndLineNumber; lineNumber++) {136const lineIndex = lineNumber - visibleStartLineNumber;137renderData[lineIndex] = '';138}139140if (this._wordWrap) {141// do a first pass to render wrapped lines142const renderedLineWrapped = this._renderOne(ctx, false);143for (const cursorLineNumber of this._cursorLineNumbers) {144145const coordinatesConverter = this._context.viewModel.coordinatesConverter;146const modelLineNumber = coordinatesConverter.convertViewPositionToModelPosition(new Position(cursorLineNumber, 1)).lineNumber;147const firstViewLineNumber = coordinatesConverter.convertModelPositionToViewPosition(new Position(modelLineNumber, 1)).lineNumber;148const lastViewLineNumber = coordinatesConverter.convertModelPositionToViewPosition(new Position(modelLineNumber, this._context.viewModel.model.getLineMaxColumn(modelLineNumber))).lineNumber;149150const firstLine = Math.max(firstViewLineNumber, visibleStartLineNumber);151const lastLine = Math.min(lastViewLineNumber, visibleEndLineNumber);152for (let lineNumber = firstLine; lineNumber <= lastLine; lineNumber++) {153const lineIndex = lineNumber - visibleStartLineNumber;154renderData[lineIndex] = renderedLineWrapped;155}156}157}158159// do a second pass to render exact lines160const renderedLineExact = this._renderOne(ctx, true);161for (const cursorLineNumber of this._cursorLineNumbers) {162if (cursorLineNumber < visibleStartLineNumber || cursorLineNumber > visibleEndLineNumber) {163continue;164}165const lineIndex = cursorLineNumber - visibleStartLineNumber;166renderData[lineIndex] = renderedLineExact;167}168169this._renderData = renderData;170}171172public render(startLineNumber: number, lineNumber: number): string {173if (!this._renderData) {174return '';175}176const lineIndex = lineNumber - startLineNumber;177if (lineIndex >= this._renderData.length) {178return '';179}180return this._renderData[lineIndex];181}182183protected _shouldRenderInMargin(): boolean {184return (185(this._renderLineHighlight === 'gutter' || this._renderLineHighlight === 'all')186&& (!this._renderLineHighlightOnlyWhenFocus || this._focused)187);188}189190protected _shouldRenderInContent(): boolean {191return (192(this._renderLineHighlight === 'line' || this._renderLineHighlight === 'all')193&& this._selectionIsEmpty194&& (!this._renderLineHighlightOnlyWhenFocus || this._focused)195);196}197198protected abstract _shouldRenderThis(): boolean;199protected abstract _shouldRenderOther(): boolean;200protected abstract _renderOne(ctx: RenderingContext, exact: boolean): string;201}202203/**204* Emphasizes the current line by drawing a border around it.205*/206export class CurrentLineHighlightOverlay extends AbstractLineHighlightOverlay {207208protected _renderOne(ctx: RenderingContext, exact: boolean): string {209const className = 'current-line' + (this._shouldRenderInMargin() ? ' current-line-both' : '') + (exact ? ' current-line-exact' : '');210return `<div class="${className}" style="width:${Math.max(ctx.scrollWidth, this._contentWidth)}px;"></div>`;211}212protected _shouldRenderThis(): boolean {213return this._shouldRenderInContent();214}215protected _shouldRenderOther(): boolean {216return this._shouldRenderInMargin();217}218}219220/**221* Emphasizes the current line margin/gutter by drawing a border around it.222*/223export class CurrentLineMarginHighlightOverlay extends AbstractLineHighlightOverlay {224protected _renderOne(ctx: RenderingContext, exact: boolean): string {225const className = 'current-line' + (this._shouldRenderInMargin() ? ' current-line-margin' : '') + (this._shouldRenderOther() ? ' current-line-margin-both' : '') + (this._shouldRenderInMargin() && exact ? ' current-line-exact-margin' : '');226return `<div class="${className}" style="width:${this._contentLeft}px"></div>`;227}228protected _shouldRenderThis(): boolean {229return true;230}231protected _shouldRenderOther(): boolean {232return this._shouldRenderInContent();233}234}235236registerThemingParticipant((theme, collector) => {237const lineHighlight = theme.getColor(editorLineHighlight);238if (lineHighlight) {239collector.addRule(`.monaco-editor .view-overlays .current-line { background-color: ${lineHighlight}; }`);240collector.addRule(`.monaco-editor .margin-view-overlays .current-line-margin { background-color: ${lineHighlight}; border: none; }`);241}242if (!lineHighlight || lineHighlight.isTransparent() || theme.defines(editorLineHighlightBorder)) {243const lineHighlightBorder = theme.getColor(editorLineHighlightBorder);244if (lineHighlightBorder) {245collector.addRule(`.monaco-editor .view-overlays .current-line-exact { border: 2px solid ${lineHighlightBorder}; }`);246collector.addRule(`.monaco-editor .margin-view-overlays .current-line-exact-margin { border: 2px solid ${lineHighlightBorder}; }`);247if (isHighContrast(theme.type)) {248collector.addRule(`.monaco-editor .view-overlays .current-line-exact { border-width: 1px; }`);249collector.addRule(`.monaco-editor .margin-view-overlays .current-line-exact-margin { border-width: 1px; }`);250}251}252}253});254255256