|
| 1 | +import { CompositeKeyDefaultMap } from "@cursorless/common"; |
| 2 | +import { toVscodeRange } from "@cursorless/vscode-common"; |
| 3 | +import { |
| 4 | + DecorationRangeBehavior, |
| 5 | + DecorationRenderOptions, |
| 6 | + TextEditorDecorationType, |
| 7 | +} from "vscode"; |
| 8 | +import { vscodeApi } from "../../../../vscodeApi"; |
| 9 | +import { VscodeTextEditorImpl } from "../../VscodeTextEditorImpl"; |
| 10 | +import { RangeTypeColors } from "../RangeTypeColors"; |
| 11 | +import { |
| 12 | + BorderStyle, |
| 13 | + DecorationStyle, |
| 14 | + DifferentiatedStyle, |
| 15 | + DifferentiatedStyledRangeList, |
| 16 | +} from "./decorationStyle.types"; |
| 17 | +import { getDifferentiatedStyleMapKey } from "./getDifferentiatedStyleMapKey"; |
| 18 | + |
| 19 | +const BORDER_WIDTH = "1px"; |
| 20 | +const BORDER_RADIUS = "2px"; |
| 21 | + |
| 22 | +/** |
| 23 | + * Handles the actual rendering of decorations for |
| 24 | + * {@link VscodeFancyRangeHighlighter}. |
| 25 | + */ |
| 26 | +export class VscodeFancyRangeHighlighterRenderer { |
| 27 | + private decorationTypes: CompositeKeyDefaultMap< |
| 28 | + DifferentiatedStyle, |
| 29 | + TextEditorDecorationType |
| 30 | + >; |
| 31 | + |
| 32 | + constructor(colors: RangeTypeColors) { |
| 33 | + this.decorationTypes = new CompositeKeyDefaultMap( |
| 34 | + ({ style }) => getDecorationStyle(colors, style), |
| 35 | + getDifferentiatedStyleMapKey, |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Renders the given ranges in the given editor. |
| 41 | + * |
| 42 | + * @param editor The editor to render the decorations in. |
| 43 | + * @param decoratedRanges A list with one element per differentiated style, |
| 44 | + * each of which contains a list of ranges to render for that style. We render |
| 45 | + * the ranges in order of increasing differentiation index. |
| 46 | + * {@link VscodeFancyRangeHighlighter} uses this to ensure that nested ranges |
| 47 | + * are rendered after their parents. Otherwise they partially interleave, |
| 48 | + * which looks bad. |
| 49 | + */ |
| 50 | + setRanges( |
| 51 | + editor: VscodeTextEditorImpl, |
| 52 | + decoratedRanges: DifferentiatedStyledRangeList[], |
| 53 | + ): void { |
| 54 | + /** |
| 55 | + * Keep track of which styles have no ranges, so that we can set their |
| 56 | + * range list to `[]` |
| 57 | + */ |
| 58 | + const untouchedDecorationTypes = new Set(this.decorationTypes.values()); |
| 59 | + |
| 60 | + decoratedRanges.sort( |
| 61 | + (a, b) => |
| 62 | + a.differentiatedStyle.differentiationIndex - |
| 63 | + b.differentiatedStyle.differentiationIndex, |
| 64 | + ); |
| 65 | + |
| 66 | + decoratedRanges.forEach( |
| 67 | + ({ differentiatedStyle: styleParameters, ranges }) => { |
| 68 | + const decorationType = this.decorationTypes.get(styleParameters); |
| 69 | + |
| 70 | + vscodeApi.editor.setDecorations( |
| 71 | + editor.vscodeEditor, |
| 72 | + decorationType, |
| 73 | + ranges.map(toVscodeRange), |
| 74 | + ); |
| 75 | + |
| 76 | + untouchedDecorationTypes.delete(decorationType); |
| 77 | + }, |
| 78 | + ); |
| 79 | + |
| 80 | + untouchedDecorationTypes.forEach((decorationType) => { |
| 81 | + editor.vscodeEditor.setDecorations(decorationType, []); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + dispose() { |
| 86 | + Array.from(this.decorationTypes.values()).forEach((decorationType) => { |
| 87 | + decorationType.dispose(); |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function getDecorationStyle( |
| 93 | + colors: RangeTypeColors, |
| 94 | + borders: DecorationStyle, |
| 95 | +): TextEditorDecorationType { |
| 96 | + const options: DecorationRenderOptions = { |
| 97 | + light: { |
| 98 | + backgroundColor: colors.background.light, |
| 99 | + borderColor: getBorderColor( |
| 100 | + colors.borderSolid.light, |
| 101 | + colors.borderPorous.light, |
| 102 | + borders, |
| 103 | + ), |
| 104 | + }, |
| 105 | + dark: { |
| 106 | + backgroundColor: colors.background.dark, |
| 107 | + borderColor: getBorderColor( |
| 108 | + colors.borderSolid.dark, |
| 109 | + colors.borderPorous.dark, |
| 110 | + borders, |
| 111 | + ), |
| 112 | + }, |
| 113 | + borderStyle: getBorderStyle(borders), |
| 114 | + borderWidth: BORDER_WIDTH, |
| 115 | + borderRadius: getBorderRadius(borders), |
| 116 | + rangeBehavior: DecorationRangeBehavior.ClosedClosed, |
| 117 | + isWholeLine: borders.isWholeLine, |
| 118 | + }; |
| 119 | + |
| 120 | + return vscodeApi.window.createTextEditorDecorationType(options); |
| 121 | +} |
| 122 | + |
| 123 | +function getBorderStyle(borders: DecorationStyle): string { |
| 124 | + return [borders.top, borders.right, borders.bottom, borders.left].join(" "); |
| 125 | +} |
| 126 | + |
| 127 | +function getBorderColor( |
| 128 | + solidColor: string, |
| 129 | + porousColor: string, |
| 130 | + borders: DecorationStyle, |
| 131 | +): string { |
| 132 | + return [ |
| 133 | + borders.top === BorderStyle.solid ? solidColor : porousColor, |
| 134 | + borders.right === BorderStyle.solid ? solidColor : porousColor, |
| 135 | + borders.bottom === BorderStyle.solid ? solidColor : porousColor, |
| 136 | + borders.left === BorderStyle.solid ? solidColor : porousColor, |
| 137 | + ].join(" "); |
| 138 | +} |
| 139 | + |
| 140 | +function getBorderRadius(borders: DecorationStyle): string { |
| 141 | + return [ |
| 142 | + getSingleCornerBorderRadius(borders.top, borders.left), |
| 143 | + getSingleCornerBorderRadius(borders.top, borders.right), |
| 144 | + getSingleCornerBorderRadius(borders.bottom, borders.right), |
| 145 | + getSingleCornerBorderRadius(borders.bottom, borders.left), |
| 146 | + ].join(" "); |
| 147 | +} |
| 148 | + |
| 149 | +function getSingleCornerBorderRadius(side1: BorderStyle, side2: BorderStyle) { |
| 150 | + // We only round the corners if both sides are solid, as that makes them look |
| 151 | + // more finished, whereas we want the dotted borders to look unfinished / cut |
| 152 | + // off. |
| 153 | + return side1 === BorderStyle.solid && side2 === BorderStyle.solid |
| 154 | + ? BORDER_RADIUS |
| 155 | + : "0px"; |
| 156 | +} |
0 commit comments