Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/editor/preview-style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export interface PreviewBoxStyleInput {
lineCount: number;
maxLineLength: number;
fontSize: number;
lineHeight: number;
marginLeftPx?: number;
}

export function resolveLineHeight(
fontSize: number,
lineHeightSetting: number,
): number {
if (lineHeightSetting > 0) {
if (lineHeightSetting < fontSize) {
return Math.ceil(fontSize * lineHeightSetting);
}
return Math.ceil(lineHeightSetting);
}
return Math.ceil(fontSize * 1.35);
}

export function computePreviewBoxStyle({
lineCount,
maxLineLength,
fontSize,
lineHeight,
marginLeftPx = 12,
}: PreviewBoxStyleInput): string {
const safeLines = Math.max(1, lineCount);
const safeLineHeight = Math.max(1, lineHeight);
const charWidth = fontSize * 0.6;
const width = Math.max(1, Math.ceil(maxLineLength * charWidth));
const height = safeLines * safeLineHeight;

return `none; display: inline-block; vertical-align: top; height: ${height}px; width: ${width}px; margin-left: ${marginLeftPx}px`;
}
49 changes: 32 additions & 17 deletions src/editor/syntax-highlight-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import type { ThemeRegistrationAny } from "@shikijs/types";
import type { IRawThemeSetting } from "@shikijs/vscode-textmate";
import * as vscode from "vscode";
import { z } from "zod";
import {
computePreviewBoxStyle,
resolveLineHeight,
} from "~/editor/preview-style.ts";

const ALL_LANGS = [
...langBash,
Expand Down Expand Up @@ -572,10 +576,10 @@ function renderSyntaxHighlightedSvgFromLines(
cachePrefix: string,
): vscode.Uri {
const settings = getEditorFontSettings();
const lineHeight =
settings.lineHeight > 0
? settings.lineHeight
: Math.ceil(settings.fontSize * 1.35);
const lineHeight = resolveLineHeight(
settings.fontSize,
settings.lineHeight,
);
const fontSize = settings.fontSize;
const textY = Math.ceil(lineHeight - settings.fontSize * 0.25);
const charWidth = settings.fontSize * 0.6;
Expand Down Expand Up @@ -672,7 +676,6 @@ function renderSyntaxHighlightedSvgFromLines(
.slice(0, 16);
const svgPath = path.join(getSvgCacheDir(), `${cachePrefix}-${hash}.svg`);
fs.writeFileSync(svgPath, svg, "utf8");

return vscode.Uri.file(svgPath);
}

Expand Down Expand Up @@ -737,13 +740,23 @@ export function createHighlightedBoxDecoration(
dark,
highlightRanges,
);

const settings = getEditorFontSettings();
const lineHeight = resolveLineHeight(
settings.fontSize,
settings.lineHeight,
);
const boxStyle = computePreviewBoxStyle({
lineCount: 1,
maxLineLength: text.length,
fontSize: settings.fontSize,
lineHeight,
});
return {
range,
renderOptions: {
after: {
contentIconPath: svgUri,
textDecoration: buildBoxTextDecoration("-40%"),
textDecoration: boxStyle,
},
},
};
Expand All @@ -763,22 +776,24 @@ export function createHighlightedBoxDecorationMultiline(
highlightRangesByLine,
);
const settings = getEditorFontSettings();
const lineHeight =
settings.lineHeight > 0
? settings.lineHeight
: Math.ceil(settings.fontSize * 1.35);

const lineHeight = resolveLineHeight(
settings.fontSize,
settings.lineHeight,
);
const maxLineLength = Math.max(1, ...lines.map((line) => line.length));
const boxStyle = computePreviewBoxStyle({
lineCount: Math.max(1, lines.length),
maxLineLength,
fontSize: settings.fontSize,
lineHeight,
});
return {
range,
renderOptions: {
after: {
contentIconPath: svgUri,
textDecoration: buildBoxTextDecoration(`-${lineHeight / 2}px`),
textDecoration: boxStyle,
},
},
};
}

function buildBoxTextDecoration(translateY: string): string {
return `none; position: absolute; top: 50%; transform: translateY(${translateY}); margin-left: 12px`;
}