@@ -18,6 +18,7 @@ import { Utils, URI } from 'vscode-uri'
18
18
import { getLanguageModelCache } from './languageModelCache'
19
19
import { Stylesheet } from 'vscode-css-languageservice'
20
20
import dlv from 'dlv'
21
+ import { rewriteCss } from './rewriting'
21
22
22
23
export class CssServer {
23
24
private documents : TextDocuments < TextDocument >
@@ -332,61 +333,8 @@ export class CssServer {
332
333
} , validationDelayMs )
333
334
}
334
335
335
- function replace ( delta = 0 ) {
336
- return ( _match : string , p1 : string ) => {
337
- let lines = p1 . split ( '\n' )
338
- if ( lines . length > 1 ) {
339
- return `@media(${ MEDIA_MARKER } )${ '\n' . repeat ( lines . length - 1 ) } ${ ' ' . repeat (
340
- lines [ lines . length - 1 ] . length ,
341
- ) } {`
342
- }
343
- return `@media(${ MEDIA_MARKER } )${ ' ' . repeat ( p1 . length + delta ) } {`
344
- }
345
- }
346
- function replaceWithStyleRule ( delta = 0 ) {
347
- return ( _match : string , p1 : string ) => {
348
- let spaces = ' ' . repeat ( p1 . length + delta )
349
- return `.placeholder${ spaces } {`
350
- }
351
- }
352
-
353
336
function createVirtualCssDocument ( textDocument : TextDocument ) : TextDocument {
354
- let content = textDocument
355
- . getText ( )
356
-
357
- // Remove inline `@layer` directives
358
- // TODO: This should be unnecessary once we have updated the bundled CSS
359
- // language service
360
- . replace ( / @ l a y e r \s + [ ^ ; { ] + ( ; | $ ) / g, '' )
361
-
362
- . replace ( / @ s c r e e n ( \s + [ ^ { ] + ) { / g, replace ( - 2 ) )
363
- . replace ( / @ v a r i a n t s ( \s + [ ^ { ] + ) { / g, replace ( ) )
364
- . replace ( / @ r e s p o n s i v e ( \s * ) { / g, replace ( ) )
365
- . replace ( / @ u t i l i t y ( \s + [ ^ { ] + ) { / g, replaceWithStyleRule ( ) )
366
- . replace ( / @ c u s t o m - v a r i a n t ( \s + [ ^ ; { ] + ) ; / g, ( match : string ) => {
367
- let spaces = ' ' . repeat ( match . length - 11 )
368
- return `@media (${ MEDIA_MARKER } )${ spaces } {}`
369
- } )
370
- . replace ( / @ c u s t o m - v a r i a n t ( \s + [ ^ { ] + ) { / g, replaceWithStyleRule ( ) )
371
- . replace ( / @ v a r i a n t ( \s + [ ^ { ] + ) { / g, replaceWithStyleRule ( ) )
372
- . replace ( / @ l a y e r ( \s + [ ^ { ] { 2 , } ) { / g, replace ( - 3 ) )
373
- . replace ( / @ r e f e r e n c e \s * ( [ ^ ; ] { 2 , } ) / g, '@import $1' )
374
- . replace (
375
- / @ m e d i a ( \s + s c r e e n \s * \( [ ^ ) ] + \) ) / g,
376
- ( _match , screen ) => `@media (${ MEDIA_MARKER } )${ ' ' . repeat ( screen . length - 4 ) } ` ,
377
- )
378
- . replace (
379
- / @ i m p o r t ( \s * ) ( " (?: [ ^ " ] + ) " | ' (?: [ ^ ' ] + ) ' ) \s * ( .* ?) (? = ; | $ ) / g,
380
- ( _match , spaces , url , other ) => {
381
- // Remove`source(…)`, `theme(…)`, and `prefix(…)` from `@import`s
382
- // otherwise we'll show syntax-error diagnostics which we don't want
383
- other = other . replace ( / ( ( s o u r c e | t h e m e | p r e f i x ) \( [ ^ ) ] + \) \s * ) + ?/ g, '' )
384
-
385
- // We have to add the spaces here so the character positions line up
386
- return `@import${ spaces } "${ url . slice ( 1 , - 1 ) } " ${ other } `
387
- } ,
388
- )
389
- . replace ( / (?< = \b (?: t h e m e | c o n f i g ) \( [ ^ ) ] * ) [ . [ \] ] / g, '_' )
337
+ let content = rewriteCss ( textDocument . getText ( ) )
390
338
391
339
return TextDocument . create (
392
340
textDocument . uri ,
0 commit comments