@@ -30,6 +30,37 @@ const defaultConfig: Config = {
30
30
] ,
31
31
} ;
32
32
33
+ function extractVarsFromDocument ( document : vscode . TextDocument ) : Var [ ] {
34
+ const file = document . getText ( ) ;
35
+ const vars = new Map ( ) ;
36
+ return file
37
+ . split ( / \r ? \n / )
38
+ . map ( ( line , i ) => {
39
+ const lineTrim = line . trim ( ) ;
40
+ const isVariable = line . trim ( ) . startsWith ( '--' ) ;
41
+ if ( ! isVariable ) {
42
+ return ;
43
+ }
44
+ const [ name , rawValue ] = lineTrim . split ( ':' ) ;
45
+ const value = rawValue . trim ( ) . replace ( ';' , '' ) ;
46
+ // Prevent duplicate or empty variables
47
+ if ( ! value || vars . has ( name ) ) {
48
+ return ;
49
+ }
50
+ vars . set ( name , value ) ;
51
+ return {
52
+ name,
53
+ value,
54
+ uri : document . uri ,
55
+ range : new vscode . Range (
56
+ new vscode . Position ( i , 4 ) ,
57
+ new vscode . Position ( i , name . length + 4 )
58
+ ) ,
59
+ } ;
60
+ } )
61
+ . filter ( Boolean ) as Var [ ] ;
62
+ }
63
+
33
64
export async function activate ( context : vscode . ExtensionContext ) {
34
65
console . log (
35
66
'Congratulations, your extension "styled-global-variable-autocomplete" is now active!'
@@ -61,40 +92,7 @@ export async function activate(context: vscode.ExtensionContext) {
61
92
const documents = await Promise . all ( filesPromises ) ;
62
93
63
94
// Get all variables from lines
64
- const finalItems = documents . flatMap ( ( document ) => {
65
- const file = document . getText ( ) ;
66
- const vars = new Map ( ) ;
67
-
68
- return file
69
- . split ( / \r ? \n / )
70
- . map ( ( line , i ) => {
71
- const lineTrim = line . trim ( ) ;
72
- const isVariable = line . trim ( ) . startsWith ( '--' ) ;
73
- if ( ! isVariable ) {
74
- return ;
75
- }
76
-
77
- const [ name , rawValue ] = lineTrim . split ( ':' ) ;
78
- const value = rawValue . trim ( ) . replace ( ';' , '' ) ;
79
-
80
- // Prevent duplicate or empty variables
81
- if ( ! value || vars . has ( name ) ) {
82
- return ;
83
- }
84
-
85
- vars . set ( name , value ) ;
86
- return {
87
- name,
88
- value,
89
- uri : document . uri ,
90
- range : new vscode . Range (
91
- new vscode . Position ( i , 4 ) ,
92
- new vscode . Position ( i , name . length + 4 )
93
- ) ,
94
- } as Var ;
95
- } )
96
- . filter ( Boolean ) as Var [ ] ;
97
- } ) ;
95
+ const finalItems = documents . flatMap ( extractVarsFromDocument ) ;
98
96
99
97
// Support autocomplete
100
98
const completionProvider = vscode . languages . registerCompletionItemProvider (
@@ -110,16 +108,19 @@ export async function activate(context: vscode.ExtensionContext) {
110
108
const currentLine = document . lineAt ( position . line ) ;
111
109
const isCssPropLine = currentLine . text . includes ( 'css={{' ) ;
112
110
const isVarPresent = currentLine . text . includes ( 'var(' ) ;
113
-
114
- const variables = finalItems . map ( ( { name, value } ) =>
115
- getCompletionItem ( {
116
- name,
117
- value,
118
- range,
119
- isCssPropLine,
120
- isVarPresent,
121
- } )
122
- ) ;
111
+ const itemsInThisDocument = extractVarsFromDocument ( document ) ;
112
+
113
+ const variables = itemsInThisDocument
114
+ . concat ( finalItems )
115
+ . map ( ( { name, value } ) =>
116
+ getCompletionItem ( {
117
+ name,
118
+ value,
119
+ range,
120
+ isCssPropLine,
121
+ isVarPresent,
122
+ } )
123
+ ) ;
123
124
124
125
return variables ;
125
126
} ,
@@ -140,7 +141,9 @@ export async function activate(context: vscode.ExtensionContext) {
140
141
return [ ] ;
141
142
}
142
143
143
- return finalItems
144
+ const itemsInThisDocument = extractVarsFromDocument ( document ) ;
145
+ return itemsInThisDocument
146
+ . concat ( finalItems )
144
147
. filter ( ( { name } ) => variable === name )
145
148
. map ( ( { name, uri, range } ) => new vscode . Location ( uri , range ) ) ;
146
149
} ,
@@ -158,7 +161,9 @@ export async function activate(context: vscode.ExtensionContext) {
158
161
return null ;
159
162
}
160
163
161
- const variableItem = finalItems . find ( ( { name } ) => name === variable ) ;
164
+ const itemsInThisDocument = extractVarsFromDocument ( document ) ;
165
+ const variableItem = itemsInThisDocument
166
+ . concat ( finalItems ) . find ( ( { name } ) => name === variable ) ;
162
167
163
168
if ( ! variableItem ) {
164
169
return null ;
0 commit comments