Skip to content

Commit 4d18cec

Browse files
authored
Fix Context Debugger Startup (#76)
1 parent a2e02bc commit 4d18cec

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

client/src/services/context.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { extension } from "../state";
22
import { LJContext, Range, LJVariable } from "../types/context";
3-
import { SourcePosition } from "../types/diagnostics";
3+
import { RefinementMismatchError, SourcePosition } from "../types/diagnostics";
44
import { getOriginalVariableName } from "../utils/utils";
55

66
export function handleContext(context: LJContext) {
@@ -11,6 +11,7 @@ export function handleContext(context: LJContext) {
1111
const { allVars, visibleVars } = getSelectionContextVariables(extension.file, extension.currentSelection);
1212
extension.context.visibleVars = visibleVars;
1313
extension.context.allVars = allVars;
14+
updateErrorAtCursor();
1415
extension.webview.sendMessage({ type: "context", context: extension.context, errorAtCursor: extension.errorAtCursor });
1516
}
1617

@@ -24,6 +25,22 @@ export function getSelectionContextVariables(file: string, selection: Range): {
2425
return { visibleVars: visibleVarsByAnnotationPosition, allVars };
2526
}
2627

28+
export function updateErrorAtCursor() {
29+
if (!extension.file || !extension.currentSelection) return;
30+
const errors: RefinementMismatchError[] = extension.diagnostics?.filter(d => d.type === 'refinement-error' || d.type === 'state-refinement-error') as RefinementMismatchError[] || [];
31+
const scopes = extension.context?.fileScopes[extension.file] || [];
32+
const errorAtCursor = errors.find(error => {
33+
if (!error.position) return false;
34+
const sameFile = error.position.file === extension.file;
35+
const beforeCursor = isPositionBefore(error.position, extension.currentSelection);
36+
if (!sameFile || !beforeCursor) return false;
37+
// check if error is within a scope that contains the cursor
38+
const errorScope = scopes.find(scope => isRangeWithin(error.position, scope));
39+
return errorScope && isRangeWithin(extension.currentSelection, errorScope);
40+
});
41+
extension.errorAtCursor = errorAtCursor;
42+
}
43+
2744
function getVariablesInScope(variables: LJVariable[], file: string, selection: Range): LJVariable[] {
2845
const scopes = extension.context.fileScopes[file] || [];
2946
const enclosingScopes = scopes.filter(scope => isRangeWithin(selection, scope));

client/src/services/diagnostics.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as vscode from "vscode";
22
import { extension } from "../state";
3-
import { LJDiagnostic, RefinementMismatchError } from "../types/diagnostics";
3+
import { LJDiagnostic } from "../types/diagnostics";
44
import { StatusBarState, updateStatusBar } from "./status-bar";
5-
import { isPositionBefore, isRangeWithin } from "./context";
5+
import { updateErrorAtCursor } from "./context";
66

77
/**
88
* Handles LiquidJava diagnostics received from the language server
@@ -13,7 +13,10 @@ export function handleLJDiagnostics(diagnostics: LJDiagnostic[]) {
1313
const statusBarState: StatusBarState = containsError ? "failed" : "passed";
1414
updateStatusBar(statusBarState);
1515
extension.diagnostics = diagnostics;
16+
updateErrorAtCursor();
1617
extension.webview?.sendMessage({ type: "diagnostics", diagnostics });
18+
if (extension.context)
19+
extension.webview?.sendMessage({ type: "context", context: extension.context, errorAtCursor: extension.errorAtCursor });
1720
}
1821

1922
/**
@@ -37,19 +40,3 @@ export async function verify() {
3740

3841
extension.client.sendNotification("liquidjava/verify", { uri });
3942
}
40-
41-
export function updateErrorAtCursor() {
42-
if (!extension.file || !extension.currentSelection) return;
43-
const errors: RefinementMismatchError[] = extension.diagnostics?.filter(d => d.type === 'refinement-error' || d.type === 'state-refinement-error') as RefinementMismatchError[] || [];
44-
const scopes = extension.context?.fileScopes[extension.file] || [];
45-
const errorAtCursor = errors.find(error => {
46-
if (!error.position) return false;
47-
const sameFile = error.position.file === extension.file;
48-
const beforeCursor = isPositionBefore(error.position, extension.currentSelection);
49-
if (!sameFile || !beforeCursor) return false;
50-
// check if error is within a scope that contains the cursor
51-
const errorScope = scopes.find(scope => isRangeWithin(error.position, scope));
52-
return errorScope && isRangeWithin(extension.currentSelection, errorScope);
53-
});
54-
extension.errorAtCursor = errorAtCursor;
55-
}

client/src/services/events.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import * as vscode from 'vscode';
22
import { extension } from '../state';
33
import { updateStateMachine } from './state-machine';
44
import { SELECTION_DEBOUNCE_MS } from '../utils/constants';
5-
import { getSelectionContextVariables, normalizeRange } from './context';
5+
import { getSelectionContextVariables, normalizeRange, updateErrorAtCursor } from './context';
66
import { normalizeFilePath, toRange } from '../utils/utils';
7-
import { updateErrorAtCursor } from './diagnostics';
8-
import type { Range } from '../types/context';
97

108
let selectionTimeout: NodeJS.Timeout | null = null;
119

@@ -60,11 +58,14 @@ export async function onSelectionChange(event: vscode.TextEditorSelectionChangeE
6058
* @param selection The new selection
6159
*/
6260
function handleContextUpdate(selection: vscode.Selection) {
63-
if (!extension.file || !extension.context) return;
64-
61+
if (!extension.file) return;
62+
6563
const normalizedRange = normalizeRange(toRange(selection));
66-
const { allVars, visibleVars } = getSelectionContextVariables(extension.file, normalizedRange);
6764
extension.currentSelection = normalizedRange;
65+
66+
if (!extension.context) return;
67+
68+
const { allVars, visibleVars } = getSelectionContextVariables(extension.file, normalizedRange);
6869
extension.context.visibleVars = visibleVars;
6970
extension.context.allVars = allVars;
7071
updateErrorAtCursor();

0 commit comments

Comments
 (0)