@@ -21,36 +21,14 @@ export function SqlMeshModel(models: Map<string, Model>): Extension {
2121 return ViewPlugin . fromClass (
2222 class SqlMeshModelView {
2323 decorations : DecorationSet = Decoration . set ( [ ] )
24+ constructor ( readonly view : EditorView ) {
25+ this . decorations = getDecorations ( models , view )
26+ }
27+
2428 update ( viewUpdate : ViewUpdate ) : void {
25- const decorations : any [ ] = [ ]
26-
27- for ( const range of viewUpdate . view . visibleRanges ) {
28- syntaxTree ( viewUpdate . view . state ) . iterate ( {
29- from : range . from ,
30- to : range . to ,
31- enter ( { from, to } ) {
32- // In case model name represented in qoutes
33- // like in python files , we need to remove qoutes
34- const model = viewUpdate . view . state . doc
35- . sliceString ( from , to )
36- . replaceAll ( '"' , '' )
37- . replaceAll ( "'" , '' )
38-
39- if ( isNil ( models . get ( model ) ) ) return true
40-
41- const decoration = Decoration . mark ( {
42- attributes : {
43- class : 'sqlmesh-model' ,
44- model,
45- } ,
46- } ) . range ( from , to )
47-
48- decorations . push ( decoration )
49- } ,
50- } )
29+ if ( viewUpdate . docChanged ) {
30+ this . decorations = getDecorations ( models , viewUpdate . view )
5131 }
52-
53- this . decorations = Decoration . set ( decorations )
5432 }
5533 } ,
5634 {
@@ -116,6 +94,41 @@ export function HoverTooltip(models: Map<string, Model>): Extension {
11694 )
11795}
11896
97+ function getDecorations (
98+ models : Map < string , Model > ,
99+ view : EditorView ,
100+ ) : DecorationSet {
101+ const decorations : any = [ ]
102+
103+ for ( const range of view . visibleRanges ) {
104+ syntaxTree ( view . state ) . iterate ( {
105+ from : range . from ,
106+ to : range . to ,
107+ enter ( { from, to } ) {
108+ // In case model name represented in qoutes
109+ // like in python files, we need to remove qoutes
110+ const model = view . state . doc
111+ . sliceString ( from , to )
112+ . replaceAll ( '"' , '' )
113+ . replaceAll ( "'" , '' )
114+
115+ if ( isNil ( models . get ( model ) ) ) return true
116+
117+ const decoration = Decoration . mark ( {
118+ attributes : {
119+ class : 'sqlmesh-model' ,
120+ model,
121+ } ,
122+ } ) . range ( from , to )
123+
124+ decorations . push ( decoration )
125+ } ,
126+ } )
127+ }
128+
129+ return Decoration . set ( decorations )
130+ }
131+
119132function handleClickOnSqlMeshModel (
120133 event : MouseEvent ,
121134 models : Map < string , Model > ,
0 commit comments