@@ -11,24 +11,30 @@ import {
1111} from '@codemirror/view'
1212import { type Model } from '~/api/client'
1313import { type ModelFile } from '~/models'
14- import { isNil } from '~/utils'
1514
1615import { useSqlMeshExtension } from './SqlMeshDialect'
1716
1817export { useSqlMeshExtension }
1918
20- export function SqlMeshModel ( models : Map < string , Model > ) : Extension {
19+ export function SqlMeshModel (
20+ models : Map < string , Model > ,
21+ model : Model ,
22+ columns : string [ ] ,
23+ ) : Extension {
2124 return ViewPlugin . fromClass (
2225 class SqlMeshModelView {
2326 decorations : DecorationSet = Decoration . set ( [ ] )
2427 constructor ( readonly view : EditorView ) {
25- this . decorations = getDecorations ( models , view )
28+ this . decorations = getDecorations ( models , view , model , columns )
2629 }
2730
2831 update ( viewUpdate : ViewUpdate ) : void {
29- if ( viewUpdate . docChanged ) {
30- this . decorations = getDecorations ( models , viewUpdate . view )
31- }
32+ this . decorations = getDecorations (
33+ models ,
34+ viewUpdate . view ,
35+ model ,
36+ columns ,
37+ )
3238 }
3339 } ,
3440 {
@@ -75,11 +81,11 @@ export function HoverTooltip(models: Map<string, Model>): Extension {
7581 create ( ) {
7682 const dom = document . createElement ( 'div' )
7783 const template = `
78- <div class="flex items-center">
79- <span>Model Name:</span>
80- <span class="px-2 py-1 inline-block ml-1 bg-alternative-100 text-alternative-500 rounded">${ model . name } </span>
81- </div>
82- `
84+ <div class="flex items-center">
85+ <span>Model Name:</span>
86+ <span class="px-2 py-1 inline-block ml-1 bg-alternative-100 text-alternative-500 rounded">${ model . name } </span>
87+ </div>
88+ `
8389
8490 dom . className =
8591 'text-xs font-bold px-3 py-3 bg-white border-2 border-secondary-100 rounded outline-none shadow-lg mb-2'
@@ -97,8 +103,11 @@ export function HoverTooltip(models: Map<string, Model>): Extension {
97103function getDecorations (
98104 models : Map < string , Model > ,
99105 view : EditorView ,
106+ model : Model ,
107+ columns : string [ ] ,
100108) : DecorationSet {
101109 const decorations : any = [ ]
110+ const modelColumns = model . columns . map ( c => c . name )
102111
103112 for ( const range of view . visibleRanges ) {
104113 syntaxTree ( view . state ) . iterate ( {
@@ -107,21 +116,68 @@ function getDecorations(
107116 enter ( { from, to } ) {
108117 // In case model name represented in qoutes
109118 // like in python files, we need to remove qoutes
110- const model = view . state . doc
111- . sliceString ( from , to )
119+ let maybeModelOrColumn = view . state . doc
120+ . sliceString ( from - 1 , to + 1 )
112121 . replaceAll ( '"' , '' )
113122 . replaceAll ( "'" , '' )
123+ let isOriginal = false
124+
125+ if (
126+ maybeModelOrColumn . startsWith ( '.' ) ||
127+ maybeModelOrColumn . endsWith ( ':' )
128+ ) {
129+ isOriginal = true
130+ }
131+
132+ maybeModelOrColumn = maybeModelOrColumn . slice (
133+ 1 ,
134+ maybeModelOrColumn . length - 1 ,
135+ )
114136
115- if ( isNil ( models . get ( model ) ) ) return true
137+ if (
138+ columns . includes ( maybeModelOrColumn ) &&
139+ ! modelColumns . includes ( maybeModelOrColumn )
140+ ) {
141+ isOriginal = true
142+ }
116143
117- const decoration = Decoration . mark ( {
118- attributes : {
119- class : 'sqlmesh-model' ,
120- model,
121- } ,
122- } ) . range ( from , to )
144+ let decoration
145+
146+ if ( maybeModelOrColumn === model . name ) {
147+ decoration = Decoration . mark ( {
148+ attributes : {
149+ class : 'sqlmesh-model --is-active-model' ,
150+ model : maybeModelOrColumn ,
151+ } ,
152+ } ) . range ( from , to )
153+ } else if ( models . get ( maybeModelOrColumn ) != null ) {
154+ decoration = Decoration . mark ( {
155+ attributes : {
156+ class : 'sqlmesh-model' ,
157+ model : maybeModelOrColumn ,
158+ } ,
159+ } ) . range ( from , to )
160+ } else if ( modelColumns . includes ( maybeModelOrColumn ) ) {
161+ decoration = Decoration . mark ( {
162+ attributes : {
163+ class : `sqlmesh-model__column --is-active-model ${
164+ isOriginal ? '--is-original' : ' --is-derived'
165+ } `,
166+ column : maybeModelOrColumn ,
167+ } ,
168+ } ) . range ( from , to )
169+ } else if ( columns . includes ( maybeModelOrColumn ) ) {
170+ decoration = Decoration . mark ( {
171+ attributes : {
172+ class : `sqlmesh-model__column ${
173+ isOriginal ? '--is-original' : ' --is-derived'
174+ } `,
175+ column : maybeModelOrColumn ,
176+ } ,
177+ } ) . range ( from , to )
178+ }
123179
124- decorations . push ( decoration )
180+ decoration != null && decorations . push ( decoration )
125181 } ,
126182 } )
127183 }
0 commit comments