@@ -41,35 +41,33 @@ 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 ( ) ;
46-
47- if ( ! this . watchers . has ( key ) ) {
48- const watcher = workspace . createFileSystemWatcher ( uri . fsPath ) ;
44+ watchFile ( path : string , listener : ( e : Uri ) => any ) {
45+ if ( ! this . watchers . has ( path ) ) {
46+ const watcher = workspace . createFileSystemWatcher ( path ) ;
4947
5048 watcher . onDidCreate ( listener ) ;
5149 watcher . onDidChange ( listener ) ;
5250 watcher . onDidDelete ( listener ) ;
5351
54- this . watchers . set ( key , watcher ) ;
52+ this . watchers . set ( path , watcher ) ;
5553 }
5654 }
5755
58- getStyleSheets ( uri : Uri ) : string [ ] {
59- return workspace . getConfiguration ( "css" , uri ) . get < string [ ] > ( "styleSheets" , [ ] ) ;
60- }
61-
62- getRelativePath ( uri : Uri , spec : string , ext ?: string ) : string {
56+ getRelativePath ( uri : Uri , path : string , ext ?: string ) : string {
6357 const folder = workspace . getWorkspaceFolder ( uri ) ;
64- const name = ext ? join ( dirname ( spec ) , basename ( spec , ext ) + ext ) : spec ;
58+ const name = ext ? join ( dirname ( path ) , basename ( path , ext ) + ext ) : path ;
6559
6660 return folder
67- ? join ( isAbsolute ( spec )
61+ ? join ( isAbsolute ( path )
6862 ? folder . uri . fsPath
6963 : dirname ( uri . fsPath ) , name )
7064 : join ( dirname ( uri . fsPath ) , name ) ;
7165 }
7266
67+ getStyleSheets ( uri : Uri ) : string [ ] {
68+ return workspace . getConfiguration ( "css" , uri ) . get < string [ ] > ( "styleSheets" , [ ] ) ;
69+ }
70+
7371 parseTextToItems ( text : string , items : CompletionItem [ ] ) {
7472 walk ( parse ( text ) , node => {
7573
@@ -90,47 +88,38 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
9088 } ) ;
9189 }
9290
93- async fetchLocal ( key : string , uri : Uri ) : Promise < void > {
94- const file = Uri . file ( this . getRelativePath ( uri , key ) ) ;
95- const items : CompletionItem [ ] = [ ] ;
96-
97- try {
98- const content = await workspace . fs . readFile ( file ) ;
99- this . parseTextToItems ( content . toString ( ) , items ) ;
100- } catch ( error ) {
91+ async fetchStyleSheet ( key : string , uri : Uri ) : Promise < void > {
92+ if ( this . cache . has ( key ) ) {
93+ return ;
10194 }
10295
103- this . cache . set ( key , items ) ;
104- this . watchFile ( file , e => this . cache . delete ( key ) ) ;
105- }
106-
107- async fetchRemote ( key : string ) : Promise < void > {
10896 const items : CompletionItem [ ] = [ ] ;
10997
110- try {
111- const res = await fetch ( key ) ;
98+ if ( this . isRemote . test ( key ) ) {
99+ try {
100+ const res = await fetch ( key ) ;
112101
113- if ( res . ok ) {
114- const text = await res . text ( ) ;
115- this . parseTextToItems ( text , items ) ;
102+ if ( res . ok ) {
103+ this . parseTextToItems ( await res . text ( ) , items ) ;
104+ }
105+ } catch ( error ) {
116106 }
117- } catch ( error ) {
118- }
119-
120- this . cache . set ( key , items ) ;
121- }
107+ } else {
108+ const path = this . getRelativePath ( uri , key ) ;
122109
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 ) ;
110+ try {
111+ const content = await workspace . fs . readFile ( Uri . file ( path ) ) ;
112+ this . parseTextToItems ( content . toString ( ) , items ) ;
113+ } catch ( error ) {
129114 }
115+
116+ this . watchFile ( path , e => this . cache . delete ( key ) ) ;
130117 }
118+
119+ this . cache . set ( key , items ) ;
131120 }
132121
133- findDocumentStyles ( uri : Uri , keys : Set < string > , text : string ) {
122+ findEmbedded ( uri : Uri , keys : Set < string > , text : string ) {
134123 const key = uri . toString ( ) ;
135124 const items : CompletionItem [ ] = [ ] ;
136125 const findStyles = / < s t y l e [ ^ > ] * > ( [ ^ < ] + ) < \/ s t y l e > / gi;
@@ -145,14 +134,14 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
145134 keys . add ( key ) ;
146135 }
147136
148- async findStyleSheets ( uri : Uri , keys : Set < string > ) : Promise < void > {
137+ async findFixed ( uri : Uri , keys : Set < string > ) : Promise < void > {
149138 for ( const key of this . getStyleSheets ( uri ) ) {
150139 await this . fetchStyleSheet ( key , uri ) ;
151140 keys . add ( key ) ;
152141 }
153142 }
154143
155- async findDocumentLinks ( uri : Uri , keys : Set < string > , text : string ) : Promise < void > {
144+ async findLinked ( uri : Uri , keys : Set < string > , text : string ) : Promise < void > {
156145 const findLinks = / < l i n k ( [ ^ > ] + ) > / gi;
157146
158147 let link ;
@@ -173,7 +162,7 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
173162 }
174163 }
175164
176- async findExtendedStyles ( uri : Uri , keys : Set < string > , text : string , level : number = 0 ) : Promise < void > {
165+ async findInherited ( uri : Uri , keys : Set < string > , text : string , level : number = 0 ) : Promise < void > {
177166 const extended = this . findExtended . exec ( text ) ;
178167
179168 if ( extended && level < 3 ) {
@@ -188,10 +177,10 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
188177 const content = await workspace . fs . readFile ( file ) ;
189178 const text = content . toString ( ) ;
190179
191- this . findDocumentStyles ( file , keys , text ) ;
180+ this . findEmbedded ( file , keys , text ) ;
192181
193- await this . findDocumentLinks ( file , keys , text ) ;
194- await this . findExtendedStyles ( file , keys , text , level ) ;
182+ await this . findLinked ( file , keys , text ) ;
183+ await this . findInherited ( file , keys , text , level ) ;
195184 } catch ( error ) {
196185 }
197186 }
@@ -202,11 +191,11 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
202191 const uri = document . uri ;
203192 const text = document . getText ( ) ;
204193
205- this . findDocumentStyles ( uri , keys , text ) ;
194+ this . findEmbedded ( uri , keys , text ) ;
206195
207- await this . findStyleSheets ( uri , keys ) ;
208- await this . findDocumentLinks ( uri , keys , text ) ;
209- await this . findExtendedStyles ( uri , keys , text ) ;
196+ await this . findFixed ( uri , keys ) ;
197+ await this . findLinked ( uri , keys , text ) ;
198+ await this . findInherited ( uri , keys , text ) ;
210199
211200 const ids = new Map < string , CompletionItem > ( ) ;
212201 const classes = new Map < string , CompletionItem > ( ) ;
0 commit comments