@@ -41,33 +41,35 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
4141 this . cache . clear ( ) ;
4242 }
4343
44- watchFile ( path : string , listener : ( e : Uri ) => any ) {
45- if ( ! this . watchers . has ( path ) ) {
46- const watcher = workspace . createFileSystemWatcher ( path ) ;
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 ) ;
4749
4850 watcher . onDidCreate ( listener ) ;
4951 watcher . onDidChange ( listener ) ;
5052 watcher . onDidDelete ( listener ) ;
5153
52- this . watchers . set ( path , watcher ) ;
54+ this . watchers . set ( key , watcher ) ;
5355 }
5456 }
5557
56- getRelativePath ( uri : Uri , path : string , ext ?: string ) : string {
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 {
5763 const folder = workspace . getWorkspaceFolder ( uri ) ;
58- const name = ext ? join ( dirname ( path ) , basename ( path , ext ) + ext ) : path ;
64+ const name = ext ? join ( dirname ( spec ) , basename ( spec , ext ) + ext ) : spec ;
5965
6066 return folder
61- ? join ( isAbsolute ( path )
67+ ? join ( isAbsolute ( spec )
6268 ? folder . uri . fsPath
6369 : dirname ( uri . fsPath ) , name )
6470 : join ( dirname ( uri . fsPath ) , name ) ;
6571 }
6672
67- getStyleSheets ( uri : Uri ) : string [ ] {
68- return workspace . getConfiguration ( "css" , uri ) . get < string [ ] > ( "styleSheets" , [ ] ) ;
69- }
70-
7173 parseTextToItems ( text : string , items : CompletionItem [ ] ) {
7274 walk ( parse ( text ) , node => {
7375
@@ -88,38 +90,47 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
8890 } ) ;
8991 }
9092
91- async fetchStyleSheet ( key : string , uri : Uri ) : Promise < void > {
92- if ( this . cache . has ( key ) ) {
93- return ;
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 ) {
94101 }
95102
96- const items : CompletionItem [ ] = [ ] ;
103+ this . cache . set ( key , items ) ;
104+ this . watchFile ( file , e => this . cache . delete ( key ) ) ;
105+ }
97106
98- if ( this . isRemote . test ( key ) ) {
99- try {
100- const res = await fetch ( key ) ;
107+ async fetchRemote ( key : string ) : Promise < void > {
108+ const items : CompletionItem [ ] = [ ] ;
101109
102- if ( res . ok ) {
103- this . parseTextToItems ( await res . text ( ) , items ) ;
104- }
105- } catch ( error ) {
106- }
107- } else {
108- const path = this . getRelativePath ( uri , key ) ;
110+ try {
111+ const res = await fetch ( key ) ;
109112
110- try {
111- const content = await workspace . fs . readFile ( Uri . file ( path ) ) ;
112- this . parseTextToItems ( content . toString ( ) , items ) ;
113- } catch ( error ) {
113+ if ( res . ok ) {
114+ const text = await res . text ( ) ;
115+ this . parseTextToItems ( text , items ) ;
114116 }
115-
116- this . watchFile ( path , e => this . cache . delete ( key ) ) ;
117+ } catch ( error ) {
117118 }
118119
119120 this . cache . set ( key , items ) ;
120121 }
121122
122- findEmbedded ( uri : Uri , keys : Set < string > , text : string ) {
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+ }
131+ }
132+
133+ findDocumentStyles ( uri : Uri , keys : Set < string > , text : string ) {
123134 const key = uri . toString ( ) ;
124135 const items : CompletionItem [ ] = [ ] ;
125136 const findStyles = / < s t y l e [ ^ > ] * > ( [ ^ < ] + ) < \/ s t y l e > / gi;
@@ -134,14 +145,14 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
134145 keys . add ( key ) ;
135146 }
136147
137- async findFixed ( uri : Uri , keys : Set < string > ) : Promise < void > {
148+ async findStyleSheets ( uri : Uri , keys : Set < string > ) : Promise < void > {
138149 for ( const key of this . getStyleSheets ( uri ) ) {
139150 await this . fetchStyleSheet ( key , uri ) ;
140151 keys . add ( key ) ;
141152 }
142153 }
143154
144- async findLinked ( uri : Uri , keys : Set < string > , text : string ) : Promise < void > {
155+ async findDocumentLinks ( uri : Uri , keys : Set < string > , text : string ) : Promise < void > {
145156 const findLinks = / < l i n k ( [ ^ > ] + ) > / gi;
146157
147158 let link ;
@@ -162,7 +173,7 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
162173 }
163174 }
164175
165- async findInherited ( uri : Uri , keys : Set < string > , text : string , level : number = 0 ) : Promise < void > {
176+ async findExtendedStyles ( uri : Uri , keys : Set < string > , text : string , level : number = 0 ) : Promise < void > {
166177 const extended = this . findExtended . exec ( text ) ;
167178
168179 if ( extended && level < 3 ) {
@@ -177,10 +188,10 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
177188 const content = await workspace . fs . readFile ( file ) ;
178189 const text = content . toString ( ) ;
179190
180- this . findEmbedded ( file , keys , text ) ;
191+ this . findDocumentStyles ( file , keys , text ) ;
181192
182- await this . findLinked ( file , keys , text ) ;
183- await this . findInherited ( file , keys , text , level ) ;
193+ await this . findDocumentLinks ( file , keys , text ) ;
194+ await this . findExtendedStyles ( file , keys , text , level ) ;
184195 } catch ( error ) {
185196 }
186197 }
@@ -191,11 +202,11 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
191202 const uri = document . uri ;
192203 const text = document . getText ( ) ;
193204
194- this . findEmbedded ( uri , keys , text ) ;
205+ this . findDocumentStyles ( uri , keys , text ) ;
195206
196- await this . findFixed ( uri , keys ) ;
197- await this . findLinked ( uri , keys , text ) ;
198- await this . findInherited ( uri , keys , text ) ;
207+ await this . findStyleSheets ( uri , keys ) ;
208+ await this . findDocumentLinks ( uri , keys , text ) ;
209+ await this . findExtendedStyles ( uri , keys , text ) ;
199210
200211 const ids = new Map < string , CompletionItem > ( ) ;
201212 const classes = new Map < string , CompletionItem > ( ) ;
0 commit comments