@@ -4,98 +4,116 @@ import { writeFileSync } from 'fs';
44import { dirname , join } from 'path' ;
55import { StyleSheet } from 'windicss/utils/style' ;
66import { workspace } from 'vscode' ;
7+ import { getConfig , sortClassNames , toggleConfig } from '../utils' ;
78import type { ExtensionContext } from 'vscode' ;
89import type { Core } from '../interfaces' ;
9- import { getConfig , sortClassNames , toggleConfig } from '../utils' ;
10+ import type { Disposable } from 'vscode' ;
11+
12+ let DISPOSABLES : Disposable [ ] = [ ] ;
13+
14+ export function registerCommands ( ctx : ExtensionContext , core : Core ) : Disposable [ ] {
15+ function createDisposables ( ) {
16+ const disposables : Disposable [ ] = [ ] ;
17+ disposables . push (
18+ commands . registerTextEditorCommand ( 'windicss.interpret' , ( textEditor , textEdit ) => {
19+ if ( ! core . processor ) return ;
20+ const text = textEditor . document . getText ( ) ;
21+ const parser = new HTMLParser ( text ) ;
22+ const preflights = core . processor . preflight ( text ) ;
23+ const utilities = core . processor . interpret ( parser . parseClasses ( ) . map ( i => i . result ) . join ( ' ' ) ) . styleSheet ;
24+ writeFileSync ( join ( dirname ( textEditor . document . uri . fsPath ) , 'windi.css' ) , [ preflights . build ( ) , utilities . build ( ) ] . join ( '\n' ) ) ;
25+ } )
26+ ) ;
27+
28+ disposables . push (
29+ commands . registerTextEditorCommand ( 'windicss.compile' , ( textEditor , textEdit ) => {
30+ if ( ! core . processor ) return ;
31+ const text = textEditor . document . getText ( ) ;
32+ const parser = new HTMLParser ( text ) ;
33+ const preflights = core . processor . preflight ( text ) ;
34+ const outputHTML : string [ ] = [ ] ;
35+ const outputCSS : StyleSheet [ ] = [ ] ;
36+
37+ let indexStart = 0 ;
38+
39+ for ( const p of parser . parseClasses ( ) ) {
40+ outputHTML . push ( text . substring ( indexStart , p . start ) ) ;
41+ const result = core . processor . compile ( p . result , 'windi-' , true ) ;
42+ outputCSS . push ( result . styleSheet ) ;
43+ outputHTML . push ( [ result . className , ...result . ignored ] . join ( ' ' ) ) ;
44+ indexStart = p . end ;
45+ }
46+ outputHTML . push ( text . substring ( indexStart ) ) ;
47+
48+ const utilities = outputCSS . reduce ( ( previousValue , currentValue ) => previousValue . extend ( currentValue ) , new StyleSheet ( ) ) . combine ( ) ;
49+ textEdit . replace ( new Range ( new Position ( 0 , 0 ) , textEditor . document . lineAt ( textEditor . document . lineCount - 1 ) . range . end ) , outputHTML . join ( '' ) ) ;
50+ writeFileSync ( join ( dirname ( textEditor . document . uri . fsPath ) , 'windi.css' ) , [ preflights . build ( ) , utilities . build ( ) ] . join ( '\n' ) ) ;
51+ } )
52+ ) ;
53+
54+ disposables . push (
55+ commands . registerTextEditorCommand ( 'windicss.sort' , ( textEditor , textEdit ) => {
56+ const text = textEditor . document . getText ( ) ;
57+ const parser = new HTMLParser ( text ) ;
58+
59+ const classes = parser . parseClasses ( ) ;
60+ const variants = Object . keys ( core . processor ?. resolveVariants ( ) ?? { } ) ;
61+ const variantsMap = Object . assign ( { } , ...variants . map ( ( value , index ) => ( { [ value ] : index + 1 } ) ) ) ;
62+
63+ for ( const p of classes ) {
64+ const sortedP = sortClassNames ( p . result , variantsMap ) ;
65+ textEdit . replace ( new Range ( textEditor . document . positionAt ( p . start ) , textEditor . document . positionAt ( p . end ) ) , sortedP ) ;
66+ }
67+ } )
68+ ) ;
69+
70+ // if runOnSave is enabled in settings, trigger command on file save
71+ if ( getConfig ( 'windicss.sortOnSave' ) ) {
72+ disposables . push (
73+ workspace . onWillSaveTextDocument ( ( _e ) => {
74+ commands . executeCommand ( 'windicss.sort' ) ;
75+ } )
76+ ) ;
77+ }
78+
79+ disposables . push (
80+ commands . registerCommand ( 'windicss.toggle-folding' , ( ) => toggleConfig ( 'windicss.enableCodeFolding' ) )
81+ ) ;
82+
83+ disposables . push (
84+ commands . registerCommand ( 'windicss.toggle-decorators' , ( ) => {
85+ toggleConfig ( 'windicss.enableColorDecorators' ) ;
86+ } )
87+ ) ;
88+
89+ disposables . push (
90+ commands . registerCommand ( 'windicss.toggle-preview' , ( ) => {
91+ toggleConfig ( 'windicss.enableHoverPreview' ) ;
92+ } )
93+ ) ;
94+
95+ disposables . push (
96+ commands . registerCommand ( 'windicss.toggle-completion' , ( ) => {
97+ toggleConfig ( 'windicss.enableCodeCompletion' ) ;
98+ } )
99+ ) ;
10100
11- export function registerCommands ( ctx : ExtensionContext , core : Core ) : void {
12- ctx . subscriptions . push (
13- commands . registerTextEditorCommand ( 'windicss.interpret' , ( textEditor , textEdit ) => {
14- if ( ! core . processor ) return ;
15- const text = textEditor . document . getText ( ) ;
16- const parser = new HTMLParser ( text ) ;
17- const preflights = core . processor . preflight ( text ) ;
18- const utilities = core . processor . interpret ( parser . parseClasses ( ) . map ( i => i . result ) . join ( ' ' ) ) . styleSheet ;
19- writeFileSync ( join ( dirname ( textEditor . document . uri . fsPath ) , 'windi.css' ) , [ preflights . build ( ) , utilities . build ( ) ] . join ( '\n' ) ) ;
20- } )
21- ) ;
22-
23- ctx . subscriptions . push (
24- commands . registerTextEditorCommand ( 'windicss.compile' , ( textEditor , textEdit ) => {
25- if ( ! core . processor ) return ;
26- const text = textEditor . document . getText ( ) ;
27- const parser = new HTMLParser ( text ) ;
28- const preflights = core . processor . preflight ( text ) ;
29- const outputHTML : string [ ] = [ ] ;
30- const outputCSS : StyleSheet [ ] = [ ] ;
31-
32- let indexStart = 0 ;
33-
34- for ( const p of parser . parseClasses ( ) ) {
35- outputHTML . push ( text . substring ( indexStart , p . start ) ) ;
36- const result = core . processor . compile ( p . result , 'windi-' , true ) ;
37- outputCSS . push ( result . styleSheet ) ;
38- outputHTML . push ( [ result . className , ...result . ignored ] . join ( ' ' ) ) ;
39- indexStart = p . end ;
40- }
41- outputHTML . push ( text . substring ( indexStart ) ) ;
42-
43- const utilities = outputCSS . reduce ( ( previousValue , currentValue ) => previousValue . extend ( currentValue ) , new StyleSheet ( ) ) . combine ( ) ;
44- textEdit . replace ( new Range ( new Position ( 0 , 0 ) , textEditor . document . lineAt ( textEditor . document . lineCount - 1 ) . range . end ) , outputHTML . join ( '' ) ) ;
45- writeFileSync ( join ( dirname ( textEditor . document . uri . fsPath ) , 'windi.css' ) , [ preflights . build ( ) , utilities . build ( ) ] . join ( '\n' ) ) ;
46- } )
47- ) ;
48-
49- ctx . subscriptions . push (
50- commands . registerTextEditorCommand ( 'windicss.sort' , ( textEditor , textEdit ) => {
51- const text = textEditor . document . getText ( ) ;
52- const parser = new HTMLParser ( text ) ;
53-
54- const classes = parser . parseClasses ( ) ;
55- const variants = Object . keys ( core . processor ?. resolveVariants ( ) ?? { } ) ;
56- const variantsMap = Object . assign ( { } , ...variants . map ( ( value , index ) => ( { [ value ] : index + 1 } ) ) ) ;
57-
58- for ( const p of classes ) {
59- const sortedP = sortClassNames ( p . result , variantsMap ) ;
60- textEdit . replace ( new Range ( textEditor . document . positionAt ( p . start ) , textEditor . document . positionAt ( p . end ) ) , sortedP ) ;
61- }
62- } )
63- ) ;
64-
65- // if runOnSave is enabled in settings, trigger command on file save
66- if ( getConfig ( 'windicss.sortOnSave' ) ) {
67- ctx . subscriptions . push (
68- workspace . onWillSaveTextDocument ( ( _e ) => {
69- commands . executeCommand ( 'windicss.sort' ) ;
101+ disposables . push (
102+ commands . registerCommand ( 'windicss.toggle-dynamic-completion' , ( ) => {
103+ toggleConfig ( 'enableDynamicCompletion' ) ;
70104 } )
71105 ) ;
106+
107+ ctx . subscriptions . push ( ...DISPOSABLES ) ;
108+
109+ return disposables ;
72110 }
73111
74- ctx . subscriptions . push (
75- commands . registerCommand ( 'windicss.toggle-folding' , ( ) => toggleConfig ( 'windicss.enableCodeFolding' ) )
76- ) ;
77-
78- ctx . subscriptions . push (
79- commands . registerCommand ( 'windicss.toggle-decorators' , ( ) => {
80- toggleConfig ( 'windicss.enableColorDecorators' ) ;
81- } )
82- ) ;
83-
84- ctx . subscriptions . push (
85- commands . registerCommand ( 'windicss.toggle-preview' , ( ) => {
86- toggleConfig ( 'windicss.enableHoverPreview' ) ;
87- } )
88- ) ;
89-
90- ctx . subscriptions . push (
91- commands . registerCommand ( 'windicss.toggle-completion' , ( ) => {
92- toggleConfig ( 'windicss.enableCodeCompletion' ) ;
93- } )
94- ) ;
95-
96- ctx . subscriptions . push (
97- commands . registerCommand ( 'windicss.toggle-dynamic-completion' , ( ) => {
98- toggleConfig ( 'enableDynamicCompletion' ) ;
99- } )
100- ) ;
112+ workspace . onDidChangeConfiguration ( ( ) => {
113+ DISPOSABLES . forEach ( i => i . dispose ( ) ) ;
114+ DISPOSABLES = createDisposables ( ) ?? [ ] ;
115+ } , null , ctx . subscriptions ) ;
116+
117+ DISPOSABLES = createDisposables ( ) ?? [ ] ;
118+ return DISPOSABLES ;
101119}
0 commit comments