@@ -5,15 +5,14 @@ import { getOriginalVariableName } from "../utils/utils";
55
66export function handleContext ( context : LJContext ) {
77 extension . context = context ;
8- extension . logger ?. client . info ( JSON . stringify ( context . methods , null , 2 ) )
98 if ( ! extension . file || ! extension . currentSelection ) return ;
109
1110 // update variables based on new context in current selection
1211 const { allVars, visibleVars } = getSelectionContextVariables ( extension . file , extension . currentSelection ) ;
1312 extension . context . visibleVars = visibleVars ;
1413 extension . context . allVars = allVars ;
1514 updateErrorAtCursor ( ) ;
16- extension . webview . sendMessage ( { type : "context" , context : extension . context , errorAtCursor : extension . errorAtCursor } ) ;
15+ extension . webview ? .sendMessage ( { type : "context" , context : extension . context , errorAtCursor : extension . errorAtCursor } ) ;
1716}
1817
1918export function getSelectionContextVariables ( file : string , selection : Range ) : { visibleVars : LJVariable [ ] ; allVars : LJVariable [ ] } {
@@ -28,32 +27,37 @@ export function getSelectionContextVariables(file: string, selection: Range): {
2827
2928export function updateErrorAtCursor ( ) {
3029 if ( ! extension . file || ! extension . currentSelection ) return ;
30+ const file = extension . file ;
31+ const selection = extension . currentSelection ;
3132 const errors : RefinementMismatchError [ ] = extension . diagnostics ?. filter ( d => d . type === 'refinement-error' || d . type === 'state-refinement-error' ) as RefinementMismatchError [ ] || [ ] ;
32- const scopes = extension . context ?. fileScopes [ extension . file ] || [ ] ;
33+ const scopes = extension . context ?. fileScopes [ file ] || [ ] ;
3334 const errorAtCursor = errors . find ( error => {
34- if ( ! error . position ) return false ;
35- const sameFile = error . position . file === extension . file ;
36- const beforeCursor = isPositionBefore ( error . position , extension . currentSelection ) ;
35+ const position = error . position ;
36+ if ( ! position ) return false ;
37+ const sameFile = position . file === file ;
38+ const beforeCursor = isPositionBefore ( position , selection ) ;
3739 if ( ! sameFile || ! beforeCursor ) return false ;
3840 // check if error is within a scope that contains the cursor
39- const errorScope = scopes . find ( scope => isRangeWithin ( error . position , scope ) ) ;
40- return errorScope && isRangeWithin ( extension . currentSelection , errorScope ) ;
41+ const errorScope = scopes . find ( scope => isRangeWithin ( position , scope ) ) ;
42+ return errorScope && isRangeWithin ( selection , errorScope ) ;
4143 } ) ;
4244 extension . errorAtCursor = errorAtCursor ;
4345}
4446
4547function getVariablesInScope ( variables : LJVariable [ ] , file : string , selection : Range ) : LJVariable [ ] {
46- const scopes = extension . context . fileScopes [ file ] || [ ] ;
48+ const scopes = extension . context ? .fileScopes [ file ] || [ ] ;
4749 const enclosingScopes = scopes . filter ( scope => isRangeWithin ( selection , scope ) ) ;
48- return variables . filter ( v =>
49- v . position ?. file === file &&
50- enclosingScopes . some ( scope => isRangeWithin ( v . position , scope ) )
51- ) ;
50+ return variables . filter ( v => {
51+ const position = v . position ;
52+ if ( ! position ) return false
53+ return position . file === file &&
54+ enclosingScopes . some ( scope => isRangeWithin ( position , scope ) )
55+ } ) ;
5256}
5357
5458function getVisibleVariables ( variables : LJVariable [ ] , file : string , selection : Range , useAnnotationPosition : boolean ) : LJVariable [ ] {
5559 const isCollapsedRange = selection . lineStart === selection . lineEnd && selection . colStart === selection . colEnd ;
56- const fileScopes = isCollapsedRange ? ( extension . context . fileScopes [ file ] || [ ] ) : [ ] ;
60+ const fileScopes = isCollapsedRange ? ( extension . context ? .fileScopes [ file ] || [ ] ) : [ ] ;
5761 return variables . filter ( ( variable ) => {
5862 // variable must be declared in the same file
5963 if ( ! variable . position || variable . position ?. file !== file ) return false ;
@@ -147,4 +151,4 @@ function normalizeVariableRefinements(variables: LJVariable[]): LJVariable[] {
147151 }
148152 return [ v ] ;
149153 } ) ;
150- }
154+ }
0 commit comments