@@ -14,7 +14,7 @@ import {
14
14
ConfigurationRequest ,
15
15
CompletionItemKind ,
16
16
} from 'vscode-languageserver/node'
17
- import { TextDocument } from 'vscode-languageserver-textdocument'
17
+ import { Position , TextDocument } from 'vscode-languageserver-textdocument'
18
18
import { Utils , URI } from 'vscode-uri'
19
19
import { getLanguageModelCache } from './languageModelCache'
20
20
import { Stylesheet } from 'vscode-css-languageservice'
@@ -117,6 +117,7 @@ function getDocumentContext(
117
117
async function withDocumentAndSettings < T > (
118
118
uri : string ,
119
119
callback : ( result : {
120
+ original : TextDocument
120
121
document : TextDocument
121
122
settings : LanguageSettings | undefined
122
123
} ) => T | Promise < T > ,
@@ -126,13 +127,57 @@ async function withDocumentAndSettings<T>(
126
127
return null
127
128
}
128
129
return await callback ( {
130
+ original : document ,
129
131
document : createVirtualCssDocument ( document ) ,
130
132
settings : await getDocumentSettings ( document ) ,
131
133
} )
132
134
}
133
135
136
+ function isInImportDirective ( doc : TextDocument , pos : Position ) {
137
+ let text = doc . getText ( {
138
+ start : { line : pos . line , character : 0 } ,
139
+ end : pos ,
140
+ } )
141
+
142
+ // Scan backwards to see if we're inside an `@import` directive
143
+ let foundImport = false
144
+ let foundDirective = false
145
+
146
+ for ( let i = text . length - 1 ; i >= 0 ; i -- ) {
147
+ let char = text [ i ]
148
+ if ( char === '\n' ) break
149
+
150
+ if ( char === '(' && ! foundDirective ) {
151
+ if ( text . startsWith ( ' source(' , i - 7 ) ) {
152
+ foundDirective = true
153
+ } else if ( text . startsWith ( ' prefix(' , i - 7 ) ) {
154
+ foundDirective = true
155
+ } else if ( text . startsWith ( ' theme(' , i - 6 ) ) {
156
+ foundDirective = true
157
+ }
158
+ }
159
+
160
+ if ( char === '@' && ! foundImport ) {
161
+ if ( text . startsWith ( '@import ' , i ) ) {
162
+ foundImport = true
163
+ }
164
+ }
165
+ }
166
+
167
+ return foundImport && foundDirective
168
+ }
169
+
134
170
connection . onCompletion ( async ( { textDocument, position } , _token ) =>
135
- withDocumentAndSettings ( textDocument . uri , async ( { document, settings } ) => {
171
+ withDocumentAndSettings ( textDocument . uri , async ( { original, document, settings } ) => {
172
+ // If we're inside source(…), prefix(…), or theme(…), don't show
173
+ // completions from the CSS language server
174
+ if ( isInImportDirective ( original , position ) ) {
175
+ return {
176
+ isIncomplete : false ,
177
+ items : [ ] ,
178
+ }
179
+ }
180
+
136
181
let result = await cssLanguageService . doComplete2 (
137
182
document ,
138
183
position ,
0 commit comments