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
5 changes: 4 additions & 1 deletion client/src/webview/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export function getScript(vscode: VSCodeApi, document: Document, window: Window)
const vcImplicationStepButton = target.closest?.('.vc-step-btn');
if (vcImplicationStepButton) {
e.stopPropagation();
handleVCImplicationStepClick(vcImplicationStepButton);
handleVCImplicationStepClick(vcImplicationStepButton, () => {
root.querySelector<HTMLElement>('.highlight-var-btn.selected')?.classList.remove('selected');
vscode.postMessage({ type: 'highlight', range: null });
});
return;
}

Expand Down
7 changes: 7 additions & 0 deletions client/src/webview/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,13 @@ export function getStyles(): string {
.highlight-var-btn.selected {
background-color: var(--vscode-button-background);
}
.vc-binder.highlight-var-btn {
margin: 0;
padding: 0 0.15rem;
}
.vc-binder.highlight-var-btn.selected {
color: var(--lj-token-identifier);
}
.highlight-var-btn.error,
.diagnostic-reveal-btn.error {
background-color: color-mix(in srgb, var(--vscode-errorForeground) 80%, transparent);
Expand Down
53 changes: 43 additions & 10 deletions client/src/webview/views/diagnostics/vc-changes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { VCImplication } from "../../../types/vc-implications";
import type { TranslationTable } from "../../../types/diagnostics";
import { renderHighlightedInlineExpression } from "../../highlighting";
import { escapeHtml } from "../../utils";
import { renderSourceHighlightButton } from "../sections";

type ChangeKind = "unchanged" | "removed" | "added";
type DiffOperation<T> = { kind: ChangeKind; value: T };
Expand Down Expand Up @@ -34,11 +36,29 @@ function getImplicationLines(node: VCImplication): string[] {
return lines;
}

export function renderVCLine(line: string, className = "", predicateContent?: string): string {
function renderBinder(binder: string, type: string, translationTable?: TranslationTable): string {
const placement = translationTable?.[binder.slice(1)];
if (!placement?.position?.file) {
return `<span class="vc-node vc-binder" title="${escapeHtml(type)}">${escapeHtml(binder)}</span>`;
}
return renderSourceHighlightButton(
escapeHtml(binder),
type,
placement.position,
"vc-node vc-binder",
);
}

export function renderVCLine(
line: string,
className = "",
predicateContent?: string,
translationTable?: TranslationTable,
): string {
const { binder, type, predicate } = parseImplicationLine(line);
return /*html*/`
<div class="vc-line ${className}">
${binder ? /*html*/`<div class="vc-binder-cell"><span class="vc-node vc-binder" title="${type}">${escapeHtml(binder)}</span></div>` : ""}
${binder ? /*html*/`<div class="vc-binder-cell">${renderBinder(binder, type, translationTable)}</div>` : ""}
<div class="vc-predicate-cell"><span class="vc-node">${predicateContent ?? renderHighlightedInlineExpression(predicate)}</span></div>
</div>
`;
Expand Down Expand Up @@ -168,43 +188,56 @@ function alignChangedLines(removed: string[], added: string[]): Array<[string |
return lines;
}

function renderChangedDestinationLines(removed: string[], added: string[]): string {
function renderChangedDestinationLines(
removed: string[],
added: string[],
translationTable?: TranslationTable,
): string {
if (added.length === 0) return "";

return alignChangedLines(removed, added)
.map(([before, after]) => {
if (after === undefined) return "";
if (before === undefined) return renderVCLine(after, "vc-change-line");
if (before === undefined) return renderVCLine(after, "vc-change-line", undefined, translationTable);
const change = renderDestinationTokenDiff(
parseImplicationLine(before).predicate,
parseImplicationLine(after).predicate,
);
return renderVCLine(after, change.hasAddedContent ? "" : "vc-change-line", change.content);
return renderVCLine(
after,
change.hasAddedContent ? "" : "vc-change-line",
change.content,
translationTable,
);
})
.join("");
}

export function renderImplication(node: VCImplication): string {
export function renderImplication(node: VCImplication, translationTable?: TranslationTable): string {
return getImplicationLines(node)
.map(line => renderVCLine(line))
.map(line => renderVCLine(line, "", undefined, translationTable))
.join("");
}

export function renderImplicationChange(before: VCImplication, after: VCImplication): string {
export function renderImplicationChange(
before: VCImplication,
after: VCImplication,
translationTable?: TranslationTable,
): string {
const operations = diffSequence(getImplicationLines(before), getImplicationLines(after));
let html = "";
const changed = { removed: [] as string[], added: [] as string[] };

const flushChanges = () => {
html += renderChangedDestinationLines(changed.removed, changed.added);
html += renderChangedDestinationLines(changed.removed, changed.added, translationTable);
changed.removed.length = 0;
changed.added.length = 0;
};

for (const operation of operations) {
if (operation.kind === "unchanged") {
flushChanges();
html += renderVCLine(operation.value);
html += renderVCLine(operation.value, "", undefined, translationTable);
continue;
}
changed[operation.kind].push(operation.value);
Expand Down
58 changes: 33 additions & 25 deletions client/src/webview/views/diagnostics/vc-implications.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { RefinementMismatchError } from "../../../types/diagnostics";
import type { RefinementMismatchError, TranslationTable } from "../../../types/diagnostics";
import type { VCSimplificationResult } from "../../../types/vc-implications";
import { renderHighlightedExpression } from "../../highlighting";
import { renderCodicon } from "../../icons";
import { escapeHtml } from "../../utils";
import { renderImplication, renderImplicationChange } from "./vc-changes";

const stepIndexes = new Map<string, number>(); // errorId => step index, preserved across re-renders
const simplificationSteps = new Map<string, VCSimplificationResult[]>(); // errorId => simplification steps
// state to preserve across re-renders
type VCState = {
steps: VCSimplificationResult[];
translationTable: TranslationTable;
stepIndex: number;
};

const vcStates = new Map<string, VCState>(); // errorId => VCState

function renderStepButton(errorId: string, step: "previous" | "next", disabled: boolean): string {
const label = `${step === "previous" ? "Previous" : "Next"} simplification`;
Expand Down Expand Up @@ -40,45 +46,49 @@ function renderStepHeader(
`;
}

function getTargetStepIndex(errorId: string, step: string | null): number | undefined {
const steps = simplificationSteps.get(errorId);
if (!steps) return;

const index = stepIndexes.get(errorId) ?? 0;
const targetIndex = step === "previous" ? index + 1 : step === "next" ? index - 1 : -1;
if (targetIndex < 0 || targetIndex >= steps.length) return;
function getTargetStepIndex(state: VCState, step: string | null): number | undefined {
const targetIndex = step === "previous"
? state.stepIndex + 1
: step === "next"
? state.stepIndex - 1
: -1;
if (targetIndex < 0 || targetIndex >= state.steps.length) return;
return targetIndex;
}

function renderSelectedStep(errorId: string, previousIndex?: number): string {
const steps = simplificationSteps.get(errorId);
if (!steps) return "";
const state = vcStates.get(errorId);
if (!state) return "";

const index = Math.min(stepIndexes.get(errorId) ?? 0, steps.length - 1);
const current = steps[index];
const { steps, translationTable, stepIndex } = state;
const current = steps[stepIndex];
const previous = previousIndex === undefined ? undefined : steps[previousIndex];
const implication = previous
? `<div class="vc-chain">${renderImplicationChange(previous.implication, current.implication)}</div>`
: `<div class="vc-chain">${renderImplication(current.implication)}</div>`;
? `<div class="vc-chain">${renderImplicationChange(previous.implication, current.implication, translationTable)}</div>`
: `<div class="vc-chain">${renderImplication(current.implication, translationTable)}</div>`;

return /*html*/`
${steps.length > 1 ? renderStepHeader(errorId, current, index, steps.length) : ""}
${steps.length > 1 ? renderStepHeader(errorId, current, stepIndex, steps.length) : ""}
${implication}
`;
}

export function handleVCImplicationStepClick(target: Element): boolean {
export function handleVCImplicationStepClick(target: Element, onStepChanged?: () => void): boolean {
const errorId = target.getAttribute("data-error-id");
const step = target.getAttribute("data-vc-step");
if (!errorId || (target as HTMLButtonElement).disabled) return false;

const currentIndex = stepIndexes.get(errorId) ?? 0;
const targetIndex = getTargetStepIndex(errorId, step);
const state = vcStates.get(errorId);
if (!state) return false;

const currentIndex = state.stepIndex;
const targetIndex = getTargetStepIndex(state, step);
const container = target.closest?.(".vc-container");
if (targetIndex === undefined) return false;

stepIndexes.set(errorId, targetIndex);
state.stepIndex = targetIndex;
if (container) container.innerHTML = renderSelectedStep(errorId, currentIndex);
onStepChanged?.();
return true;
}

Expand All @@ -98,10 +108,8 @@ export function renderVCImplication(
for (let current: VCSimplificationResult | null = result; current; current = current.origin) {
steps.push(current);
}
simplificationSteps.set(errorId, steps);

const index = Math.min(stepIndexes.get(errorId) ?? 0, steps.length - 1);
stepIndexes.set(errorId, index);
const stepIndex = Math.min(vcStates.get(errorId)?.stepIndex ?? 0, steps.length - 1);
vcStates.set(errorId, { steps, translationTable: error.translationTable, stepIndex });

return /*html*/ `
<div class="container vc-container" data-error-id="${errorId}">
Expand Down
21 changes: 17 additions & 4 deletions client/src/webview/views/sections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,30 @@ export function renderVariableHighlightButton(variable: LJVariable): string {
const displayName = getSimpleName(variable.name);
const position = variable.position;
if (!position || !position.file) return `<code>${displayName}</code>`;
return renderSourceHighlightButton(
`<code>${renderHighlightedInlineExpression(displayName)}</code>`,
variable.type,
position,
);
}

export function renderSourceHighlightButton(
content: string,
title: string,
position: SourcePosition & { file: string },
className = "",
): string {
return /*html*/`
<button
class="highlight-var-btn"
title="${variable.type}"
class="highlight-var-btn${className ? ` ${className}` : ""}"
title="${escapeHtml(title)}"
data-start-line="${position.lineStart}"
data-start-column="${position.colStart}"
data-end-line="${position.lineEnd}"
data-end-column="${position.colEnd}"
data-file="${position.file}"
data-file="${escapeHtml(position.file)}"
>
<code>${renderHighlightedInlineExpression(displayName)}</code>
${content}
</button>
`;
}
Expand Down