Path: blob/main/src/vs/editor/browser/viewParts/whitespace/whitespace.ts
5297 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 './whitespace.css';6import { DynamicViewOverlay } from '../../view/dynamicViewOverlay.js';7import { Selection } from '../../../common/core/selection.js';8import { RenderingContext } from '../../view/renderingContext.js';9import { ViewContext } from '../../../common/viewModel/viewContext.js';10import * as viewEvents from '../../../common/viewEvents.js';11import { ViewLineRenderingData } from '../../../common/viewModel.js';12import { EditorOption } from '../../../common/config/editorOptions.js';13import { IEditorConfiguration } from '../../../common/config/editorConfiguration.js';14import * as strings from '../../../../base/common/strings.js';15import { CharCode } from '../../../../base/common/charCode.js';16import { Position } from '../../../common/core/position.js';17import { editorWhitespaces } from '../../../common/core/editorColorRegistry.js';18import { OffsetRange } from '../../../common/core/ranges/offsetRange.js';1920/**21* The whitespace overlay will visual certain whitespace depending on the22* current editor configuration (boundary, selection, etc.).23*/24export class WhitespaceOverlay extends DynamicViewOverlay {2526private readonly _context: ViewContext;27private _options: WhitespaceOptions;28private _selection: Selection[];29private _renderResult: string[] | null;3031constructor(context: ViewContext) {32super();33this._context = context;34this._options = new WhitespaceOptions(this._context.configuration);35this._selection = [];36this._renderResult = null;37this._context.addEventHandler(this);38}3940public override dispose(): void {41this._context.removeEventHandler(this);42this._renderResult = null;43super.dispose();44}4546// --- begin event handlers4748public override onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {49const newOptions = new WhitespaceOptions(this._context.configuration);50if (this._options.equals(newOptions)) {51return e.hasChanged(EditorOption.layoutInfo);52}53this._options = newOptions;54return true;55}56public override onCursorStateChanged(e: viewEvents.ViewCursorStateChangedEvent): boolean {57this._selection = e.selections;58if (this._options.renderWhitespace === 'selection') {59return true;60}61return false;62}63public override onDecorationsChanged(e: viewEvents.ViewDecorationsChangedEvent): boolean {64return true;65}66public override onFlushed(e: viewEvents.ViewFlushedEvent): boolean {67return true;68}69public override onLinesChanged(e: viewEvents.ViewLinesChangedEvent): boolean {70return true;71}72public override onLinesDeleted(e: viewEvents.ViewLinesDeletedEvent): boolean {73return true;74}75public override onLinesInserted(e: viewEvents.ViewLinesInsertedEvent): boolean {76return true;77}78public override onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {79return e.scrollTopChanged;80}81public override onZonesChanged(e: viewEvents.ViewZonesChangedEvent): boolean {82return true;83}84// --- end event handlers8586public prepareRender(ctx: RenderingContext): void {87if (this._options.renderWhitespace === 'none') {88this._renderResult = null;89return;90}9192this._renderResult = [];93for (let lineNumber = ctx.viewportData.startLineNumber; lineNumber <= ctx.viewportData.endLineNumber; lineNumber++) {94const lineIndex = lineNumber - ctx.viewportData.startLineNumber;95const lineData = this._context.viewModel.getViewLineRenderingData(lineNumber);9697let selectionsOnLine: OffsetRange[] | null = null;98if (this._options.renderWhitespace === 'selection') {99const selections = this._selection;100for (const selection of selections) {101102if (selection.endLineNumber < lineNumber || selection.startLineNumber > lineNumber) {103// Selection does not intersect line104continue;105}106107const startColumn = (selection.startLineNumber === lineNumber ? selection.startColumn : lineData.minColumn);108const endColumn = (selection.endLineNumber === lineNumber ? selection.endColumn : lineData.maxColumn);109110if (startColumn < endColumn) {111if (!selectionsOnLine) {112selectionsOnLine = [];113}114selectionsOnLine.push(new OffsetRange(startColumn - 1, endColumn - 1));115}116}117}118119this._renderResult[lineIndex] = this._applyRenderWhitespace(ctx, lineNumber, selectionsOnLine, lineData);120}121}122123private _applyRenderWhitespace(ctx: RenderingContext, lineNumber: number, selections: OffsetRange[] | null, lineData: ViewLineRenderingData): string {124if (lineData.hasVariableFonts) {125return '';126}127if (this._options.renderWhitespace === 'selection' && !selections) {128return '';129}130if (this._options.renderWhitespace === 'trailing' && lineData.continuesWithWrappedLine) {131return '';132}133const color = this._context.theme.getColor(editorWhitespaces);134const USE_SVG = this._options.renderWithSVG;135136const lineContent = lineData.content;137const len = (this._options.stopRenderingLineAfter === -1 ? lineContent.length : Math.min(this._options.stopRenderingLineAfter, lineContent.length));138const continuesWithWrappedLine = lineData.continuesWithWrappedLine;139const fauxIndentLength = lineData.minColumn - 1;140const onlyBoundary = (this._options.renderWhitespace === 'boundary');141const onlyTrailing = (this._options.renderWhitespace === 'trailing');142const lineHeight = ctx.getLineHeightForLineNumber(lineNumber);143const middotWidth = this._options.middotWidth;144const wsmiddotWidth = this._options.wsmiddotWidth;145const spaceWidth = this._options.spaceWidth;146const wsmiddotDiff = Math.abs(wsmiddotWidth - spaceWidth);147const middotDiff = Math.abs(middotWidth - spaceWidth);148149// U+2E31 - WORD SEPARATOR MIDDLE DOT150// U+00B7 - MIDDLE DOT151const renderSpaceCharCode = (wsmiddotDiff < middotDiff ? 0x2E31 : 0xB7);152153const canUseHalfwidthRightwardsArrow = this._options.canUseHalfwidthRightwardsArrow;154155let result: string = '';156157let lineIsEmptyOrWhitespace = false;158let firstNonWhitespaceIndex = strings.firstNonWhitespaceIndex(lineContent);159let lastNonWhitespaceIndex: number;160if (firstNonWhitespaceIndex === -1) {161lineIsEmptyOrWhitespace = true;162firstNonWhitespaceIndex = len;163lastNonWhitespaceIndex = len;164} else {165lastNonWhitespaceIndex = strings.lastNonWhitespaceIndex(lineContent);166}167168let currentSelectionIndex = 0;169let currentSelection = selections && selections[currentSelectionIndex];170let maxLeft = 0;171172for (let charIndex = fauxIndentLength; charIndex < len; charIndex++) {173const chCode = lineContent.charCodeAt(charIndex);174175if (currentSelection && currentSelection.endExclusive <= charIndex) {176currentSelectionIndex++;177currentSelection = selections && selections[currentSelectionIndex];178}179180if (chCode !== CharCode.Tab && chCode !== CharCode.Space) {181continue;182}183184if (onlyTrailing && !lineIsEmptyOrWhitespace && charIndex <= lastNonWhitespaceIndex) {185// If rendering only trailing whitespace, check that the charIndex points to trailing whitespace.186continue;187}188189if (onlyBoundary && charIndex >= firstNonWhitespaceIndex && charIndex <= lastNonWhitespaceIndex && chCode === CharCode.Space) {190// rendering only boundary whitespace191const prevChCode = (charIndex - 1 >= 0 ? lineContent.charCodeAt(charIndex - 1) : CharCode.Null);192const nextChCode = (charIndex + 1 < len ? lineContent.charCodeAt(charIndex + 1) : CharCode.Null);193if (prevChCode !== CharCode.Space && nextChCode !== CharCode.Space) {194continue;195}196}197198if (onlyBoundary && continuesWithWrappedLine && charIndex === len - 1) {199const prevCharCode = (charIndex - 1 >= 0 ? lineContent.charCodeAt(charIndex - 1) : CharCode.Null);200const isSingleTrailingSpace = (chCode === CharCode.Space && (prevCharCode !== CharCode.Space && prevCharCode !== CharCode.Tab));201if (isSingleTrailingSpace) {202continue;203}204}205206if (selections && !(currentSelection && currentSelection.start <= charIndex && charIndex < currentSelection.endExclusive)) {207// If rendering whitespace on selection, check that the charIndex falls within a selection208continue;209}210211const visibleRange = ctx.visibleRangeForPosition(new Position(lineNumber, charIndex + 1));212if (!visibleRange) {213continue;214}215216if (USE_SVG) {217maxLeft = Math.max(maxLeft, visibleRange.left);218if (chCode === CharCode.Tab) {219result += this._renderArrow(lineHeight, spaceWidth, visibleRange.left);220} else {221result += `<circle cx="${(visibleRange.left + spaceWidth / 2).toFixed(2)}" cy="${(lineHeight / 2).toFixed(2)}" r="${(spaceWidth / 7).toFixed(2)}" />`;222}223} else {224if (chCode === CharCode.Tab) {225result += `<div class="mwh" style="left:${visibleRange.left}px;height:${lineHeight}px;">${canUseHalfwidthRightwardsArrow ? String.fromCharCode(0xFFEB) : String.fromCharCode(0x2192)}</div>`;226} else {227result += `<div class="mwh" style="left:${visibleRange.left}px;height:${lineHeight}px;">${String.fromCharCode(renderSpaceCharCode)}</div>`;228}229}230}231232if (USE_SVG) {233maxLeft = Math.round(maxLeft + spaceWidth);234return (235`<svg style="bottom:0;position:absolute;width:${maxLeft}px;height:${lineHeight}px" viewBox="0 0 ${maxLeft} ${lineHeight}" xmlns="http://www.w3.org/2000/svg" fill="${color}">`236+ result237+ `</svg>`238);239}240241return result;242}243244private _renderArrow(lineHeight: number, spaceWidth: number, left: number): string {245const strokeWidth = spaceWidth / 7;246const width = spaceWidth;247const dy = lineHeight / 2;248const dx = left;249250const p1 = { x: 0, y: strokeWidth / 2 };251const p2 = { x: 100 / 125 * width, y: p1.y };252const p3 = { x: p2.x - 0.2 * p2.x, y: p2.y + 0.2 * p2.x };253const p4 = { x: p3.x + 0.1 * p2.x, y: p3.y + 0.1 * p2.x };254const p5 = { x: p4.x + 0.35 * p2.x, y: p4.y - 0.35 * p2.x };255const p6 = { x: p5.x, y: -p5.y };256const p7 = { x: p4.x, y: -p4.y };257const p8 = { x: p3.x, y: -p3.y };258const p9 = { x: p2.x, y: -p2.y };259const p10 = { x: p1.x, y: -p1.y };260261const p = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10];262const parts = p.map((p) => `${(dx + p.x).toFixed(2)} ${(dy + p.y).toFixed(2)}`).join(' L ');263return `<path d="M ${parts}" />`;264}265266public render(startLineNumber: number, lineNumber: number): string {267if (!this._renderResult) {268return '';269}270const lineIndex = lineNumber - startLineNumber;271if (lineIndex < 0 || lineIndex >= this._renderResult.length) {272return '';273}274return this._renderResult[lineIndex];275}276}277278class WhitespaceOptions {279280public readonly renderWhitespace: 'none' | 'boundary' | 'selection' | 'trailing' | 'all';281public readonly renderWithSVG: boolean;282public readonly spaceWidth: number;283public readonly middotWidth: number;284public readonly wsmiddotWidth: number;285public readonly canUseHalfwidthRightwardsArrow: boolean;286public readonly lineHeight: number;287public readonly stopRenderingLineAfter: number;288289constructor(config: IEditorConfiguration) {290const options = config.options;291const fontInfo = options.get(EditorOption.fontInfo);292const experimentalWhitespaceRendering = options.get(EditorOption.experimentalWhitespaceRendering);293if (experimentalWhitespaceRendering === 'off') {294// whitespace is rendered in the view line295this.renderWhitespace = 'none';296this.renderWithSVG = false;297} else if (experimentalWhitespaceRendering === 'svg') {298this.renderWhitespace = options.get(EditorOption.renderWhitespace);299this.renderWithSVG = true;300} else {301this.renderWhitespace = options.get(EditorOption.renderWhitespace);302this.renderWithSVG = false;303}304this.spaceWidth = fontInfo.spaceWidth;305this.middotWidth = fontInfo.middotWidth;306this.wsmiddotWidth = fontInfo.wsmiddotWidth;307this.canUseHalfwidthRightwardsArrow = fontInfo.canUseHalfwidthRightwardsArrow;308this.lineHeight = options.get(EditorOption.lineHeight);309this.stopRenderingLineAfter = options.get(EditorOption.stopRenderingLineAfter);310}311312public equals(other: WhitespaceOptions): boolean {313return (314this.renderWhitespace === other.renderWhitespace315&& this.renderWithSVG === other.renderWithSVG316&& this.spaceWidth === other.spaceWidth317&& this.middotWidth === other.middotWidth318&& this.wsmiddotWidth === other.wsmiddotWidth319&& this.canUseHalfwidthRightwardsArrow === other.canUseHalfwidthRightwardsArrow320&& this.lineHeight === other.lineHeight321&& this.stopRenderingLineAfter === other.stopRenderingLineAfter322);323}324}325326327