@@ -41,25 +41,25 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
4141 this . cache . clear ( ) ;
4242 }
4343
44- watchFile ( uri : Uri , listener : ( e : Uri ) => any ) {
45- const key = uri . toString ( ) ;
44+ watchFile ( path : string , listener : ( e : Uri ) => any ) {
45+ if ( this . watchers . has ( path ) ) {
46+ return ;
47+ }
4648
47- if ( ! this . watchers . has ( key ) ) {
48- const watcher = workspace . createFileSystemWatcher ( uri . fsPath ) ;
49+ const watcher = workspace . createFileSystemWatcher ( path ) ;
4950
50- watcher . onDidCreate ( listener ) ;
51- watcher . onDidChange ( listener ) ;
52- watcher . onDidDelete ( listener ) ;
51+ watcher . onDidCreate ( listener ) ;
52+ watcher . onDidChange ( listener ) ;
53+ watcher . onDidDelete ( listener ) ;
5354
54- this . watchers . set ( key , watcher ) ;
55- }
55+ this . watchers . set ( path , watcher ) ;
5656 }
5757
5858 getStyleSheets ( uri : Uri ) : string [ ] {
5959 return workspace . getConfiguration ( "css" , uri ) . get < string [ ] > ( "styleSheets" , [ ] ) ;
6060 }
6161
62- getRelativePath ( uri : Uri , spec : string , ext ?: string ) : string {
62+ getPath ( uri : Uri , spec : string , ext ?: string ) : string {
6363 const folder = workspace . getWorkspaceFolder ( uri ) ;
6464 const name = ext ? join ( dirname ( spec ) , basename ( spec , ext ) + ext ) : spec ;
6565
@@ -90,25 +90,32 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
9090 } ) ;
9191 }
9292
93- async fetchLocal ( key : string , uri : Uri ) : Promise < void > {
94- const file = Uri . file ( this . getRelativePath ( uri , key ) ) ;
93+ async fetchLocal ( path : string ) : Promise < void > {
94+ if ( this . cache . has ( path ) ) {
95+ return ;
96+ }
97+
9598 const items : CompletionItem [ ] = [ ] ;
9699
97100 try {
98- const content = await workspace . fs . readFile ( file ) ;
101+ const content = await workspace . fs . readFile ( Uri . file ( path ) ) ;
99102 this . parseTextToItems ( content . toString ( ) , items ) ;
100103 } catch ( error ) {
101104 }
102105
103- this . cache . set ( key , items ) ;
104- this . watchFile ( file , e => this . cache . delete ( key ) ) ;
106+ this . cache . set ( path , items ) ;
107+ this . watchFile ( path , ( ) => this . cache . delete ( path ) ) ;
105108 }
106109
107- async fetchRemote ( key : string ) : Promise < void > {
110+ async fetchRemote ( path : string ) : Promise < void > {
111+ if ( this . cache . has ( path ) ) {
112+ return ;
113+ }
114+
108115 const items : CompletionItem [ ] = [ ] ;
109116
110117 try {
111- const res = await fetch ( key ) ;
118+ const res = await fetch ( path ) ;
112119
113120 if ( res . ok ) {
114121 const text = await res . text ( ) ;
@@ -117,20 +124,23 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
117124 } catch ( error ) {
118125 }
119126
120- this . cache . set ( key , items ) ;
127+ this . cache . set ( path , items ) ;
121128 }
122129
123- async fetchStyleSheet ( key : string , uri : Uri ) : Promise < void > {
124- if ( ! this . cache . has ( key ) ) {
125- if ( this . isRemote . test ( key ) ) {
126- await this . fetchRemote ( key ) ;
127- } else {
128- await this . fetchLocal ( key , uri ) ;
129- }
130+ async fetch ( uri : Uri , path : string ) : Promise < string > {
131+ if ( this . isRemote . test ( path ) ) {
132+ await this . fetchRemote ( path ) ;
133+ } else {
134+ const base = basename ( uri . fsPath , extname ( uri . fsPath ) ) ;
135+
136+ path = this . getPath ( uri , path . replace ( / \$ { \s * f i l e B a s e n a m e N o E x t e n s i o n \s * } / , base ) ) ;
137+ await this . fetchLocal ( path ) ;
130138 }
139+
140+ return path ;
131141 }
132142
133- findDocumentStyles ( uri : Uri , keys : Set < string > , text : string ) {
143+ findEmbedded ( uri : Uri , keys : Set < string > , text : string ) {
134144 const key = uri . toString ( ) ;
135145 const items : CompletionItem [ ] = [ ] ;
136146 const findStyles = / < s t y l e [ ^ > ] * > ( [ ^ < ] + ) < \/ s t y l e > / gi;
@@ -145,14 +155,13 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
145155 keys . add ( key ) ;
146156 }
147157
148- async findStyleSheets ( uri : Uri , keys : Set < string > ) : Promise < void > {
149- for ( const key of this . getStyleSheets ( uri ) ) {
150- await this . fetchStyleSheet ( key , uri ) ;
151- keys . add ( key ) ;
158+ async findFixed ( uri : Uri , keys : Set < string > ) : Promise < void > {
159+ for ( const sheet of this . getStyleSheets ( uri ) ) {
160+ keys . add ( await this . fetch ( uri , sheet ) ) ;
152161 }
153162 }
154163
155- async findDocumentLinks ( uri : Uri , keys : Set < string > , text : string ) : Promise < void > {
164+ async findLinks ( uri : Uri , keys : Set < string > , text : string ) : Promise < void > {
156165 const findLinks = / < l i n k ( [ ^ > ] + ) > / gi;
157166
158167 let link ;
@@ -164,34 +173,31 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
164173 const href = this . findLinkHref . exec ( link [ 1 ] ) ;
165174
166175 if ( href ) {
167- const key = href [ 2 ] ;
168-
169- await this . fetchStyleSheet ( key , uri ) ;
170- keys . add ( key ) ;
176+ keys . add ( await this . fetch ( uri , href [ 2 ] ) ) ;
171177 }
172178 }
173179 }
174180 }
175181
176- async findExtendedStyles ( uri : Uri , keys : Set < string > , text : string , level : number = 0 ) : Promise < void > {
182+ async findInherited ( uri : Uri , keys : Set < string > , text : string , level : number = 0 ) : Promise < void > {
177183 const extended = this . findExtended . exec ( text ) ;
178184
179185 if ( extended && level < 3 ) {
180186 level ++ ;
181187
182188 const name = extended [ 2 ] ;
183189 const ext = extname ( name ) || extname ( uri . fsPath ) ;
184- const key = this . getRelativePath ( uri , name , ext ) ;
190+ const key = this . getPath ( uri , name , ext ) ;
185191 const file = Uri . file ( key ) ;
186192
187193 try {
188194 const content = await workspace . fs . readFile ( file ) ;
189195 const text = content . toString ( ) ;
190196
191- this . findDocumentStyles ( file , keys , text ) ;
197+ this . findEmbedded ( file , keys , text ) ;
192198
193- await this . findDocumentLinks ( file , keys , text ) ;
194- await this . findExtendedStyles ( file , keys , text , level ) ;
199+ await this . findLinks ( file , keys , text ) ;
200+ await this . findInherited ( file , keys , text , level ) ;
195201 } catch ( error ) {
196202 }
197203 }
@@ -202,11 +208,11 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
202208 const uri = document . uri ;
203209 const text = document . getText ( ) ;
204210
205- this . findDocumentStyles ( uri , keys , text ) ;
211+ this . findEmbedded ( uri , keys , text ) ;
206212
207- await this . findStyleSheets ( uri , keys ) ;
208- await this . findDocumentLinks ( uri , keys , text ) ;
209- await this . findExtendedStyles ( uri , keys , text ) ;
213+ await this . findFixed ( uri , keys ) ;
214+ await this . findLinks ( uri , keys , text ) ;
215+ await this . findInherited ( uri , keys , text ) ;
210216
211217 const ids = new Map < string , CompletionItem > ( ) ;
212218 const classes = new Map < string , CompletionItem > ( ) ;
0 commit comments