11import { extension } from "../state" ;
22import { LJContext , Range , LJVariable } from "../types/context" ;
3- import { SourcePosition } from "../types/diagnostics" ;
3+ import { RefinementMismatchError , SourcePosition } from "../types/diagnostics" ;
44import { getOriginalVariableName } from "../utils/utils" ;
55
66export 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+
2744function getVariablesInScope ( variables : LJVariable [ ] , file : string , selection : Range ) : LJVariable [ ] {
2845 const scopes = extension . context . fileScopes [ file ] || [ ] ;
2946 const enclosingScopes = scopes . filter ( scope => isRangeWithin ( selection , scope ) ) ;
0 commit comments