@@ -5,107 +5,122 @@ import { binaries, install } from './installer';
55import os from 'os' ;
66
77export function activate ( context : vscode . ExtensionContext ) {
8- const extension = vscode . extensions . getExtension ( 'AndcoolSystems.next-css-lint' ) ;
9- const version = extension ! . packageJSON . version ;
10-
11- const diagnosticCollection = vscode . languages . createDiagnosticCollection ( 'css-linter-diags' ) ;
12- context . subscriptions . push ( diagnosticCollection ) ;
13-
14- const platform = os . platform ( ) ;
15- const binary = binaries [ platform ] ;
16-
17- const homedir = os . homedir ( ) ;
18- const exec_path = join ( homedir , '.css-linter' , binary ) ;
19-
20- function run_diag ( ) {
21- const workspacePath = vscode . workspace . workspaceFolders ?. [ 0 ] ?. uri . fsPath ;
22- if ( ! workspacePath ) {
23- return ;
24- }
25-
26- exec ( `${ exec_path } ${ workspacePath } --minify` , ( error , stdout , stderr ) => {
27- if ( error || stderr ) {
28- console . error ( `CSS-lint error: ${ error || stderr } ` ) ;
29- return ;
30- }
31-
32- console . log ( stdout ) ;
33-
34- diagnosticCollection . clear ( ) ;
35- const error_lines = stdout . split ( '\n' ) ;
36-
37- if ( error_lines . length === 0 ) {
38- return ;
39- }
40-
41- const diagnosticsMap : Map < string , vscode . Diagnostic [ ] > = new Map ( ) ;
42- for ( let e_line of error_lines ) {
43- let frags = e_line . split ( ':' ) ;
44- if ( frags . length < 4 ) {
45- continue ;
46- }
47-
48- const filePath = frags [ 0 ] ;
49- const line = parseInt ( frags [ 1 ] ) - 1 ;
50- const col = parseInt ( frags [ 2 ] ) ;
51- const len = parseInt ( frags [ 3 ] ) ;
52- const message = frags [ 4 ] ;
53-
54- const range = new vscode . Range ( line , col , line , col + len ) ;
55- const diagnostic = new vscode . Diagnostic ( range , message , vscode . DiagnosticSeverity . Warning ) ;
56-
57- const diagnostics = diagnosticsMap . get ( filePath ) || [ ] ;
58- diagnostics . push ( diagnostic ) ;
59- diagnosticsMap . set ( filePath , diagnostics ) ;
60- }
61-
62- diagnosticsMap . forEach ( ( diags , file ) => {
63- const fileUri = vscode . Uri . file ( join ( workspacePath , file . replace ( new RegExp ( `^./` ) , '' ) ) ) ;
64- diagnosticCollection . set ( fileUri , diags ) ;
65- } ) ;
66- } ) ;
67- }
68-
69- const save_evt = vscode . workspace . onDidSaveTextDocument ( ( ) => {
70- run_diag ( ) ;
71- } ) ;
72-
73- const code_action = vscode . languages . registerCodeActionsProvider ( "css" , new CssFixProvider ( diagnosticCollection ) , {
74- providedCodeActionKinds : [ vscode . CodeActionKind . QuickFix ] ,
75- } ) ;
76-
77- install ( version )
78- . then ( ( ) => {
79- context . subscriptions . push ( save_evt ) ;
80- context . subscriptions . push ( code_action ) ;
81- run_diag ( ) ;
82- } )
83- . catch ( ( e ) => console . error ( `[CSS-linter][ERROR]: ${ e } ` ) ) ;
8+ const diagnosticCollection = vscode . languages . createDiagnosticCollection ( 'css-linter-diags' ) ;
9+ context . subscriptions . push ( diagnosticCollection ) ;
10+
11+ const platform = os . platform ( ) ;
12+ const binary = binaries [ platform ] ;
13+
14+ const homedir = os . homedir ( ) ;
15+ const exec_path = join ( homedir , '.css-linter' , binary ) ;
16+
17+ function run_diag ( ) {
18+ const workspacePath = vscode . workspace . workspaceFolders ?. [ 0 ] ?. uri . fsPath ;
19+ if ( ! workspacePath ) {
20+ return ;
21+ }
22+
23+ exec ( `${ exec_path } ${ workspacePath } --minify` , ( error , stdout , stderr ) => {
24+ if ( error || stderr ) {
25+ console . error ( `[CSS-linter][ERROR]: ${ error || stderr } ` ) ;
26+ return ;
27+ }
28+
29+ console . log ( stdout ) ;
30+
31+ diagnosticCollection . clear ( ) ;
32+ const error_lines = stdout . split ( '\n' ) ;
33+
34+ if ( error_lines . length === 0 ) {
35+ return ;
36+ }
37+
38+ const diagnosticsMap : Map < string , vscode . Diagnostic [ ] > = new Map ( ) ;
39+ for ( let e_line of error_lines ) {
40+ let frags = e_line . split ( ':' ) ;
41+ if ( frags . length < 4 ) {
42+ continue ;
43+ }
44+
45+ const filePath = frags [ 0 ] ;
46+ const line = parseInt ( frags [ 1 ] ) - 1 ;
47+ const col = parseInt ( frags [ 2 ] ) ;
48+ const len = parseInt ( frags [ 3 ] ) ;
49+ const message = frags [ 4 ] ;
50+
51+ const range = new vscode . Range ( line , col , line , col + len ) ;
52+ const diagnostic = new vscode . Diagnostic (
53+ range ,
54+ message ,
55+ vscode . DiagnosticSeverity . Warning
56+ ) ;
57+
58+ const diagnostics = diagnosticsMap . get ( filePath ) || [ ] ;
59+ diagnostics . push ( diagnostic ) ;
60+ diagnosticsMap . set ( filePath , diagnostics ) ;
61+ }
62+
63+ diagnosticsMap . forEach ( ( diags , file ) => {
64+ const fileUri = vscode . Uri . file (
65+ join ( workspacePath , file . replace ( new RegExp ( `^./` ) , '' ) )
66+ ) ;
67+ diagnosticCollection . set ( fileUri , diags ) ;
68+ } ) ;
69+ } ) ;
70+ }
71+
72+ const save_evt = vscode . workspace . onDidSaveTextDocument ( ( ) => {
73+ run_diag ( ) ;
74+ } ) ;
75+
76+ const code_action = vscode . languages . registerCodeActionsProvider (
77+ 'css' ,
78+ new CssFixProvider ( diagnosticCollection ) ,
79+ {
80+ providedCodeActionKinds : [ vscode . CodeActionKind . QuickFix ]
81+ }
82+ ) ;
83+
84+ install ( )
85+ . then ( ( ) => {
86+ context . subscriptions . push ( save_evt ) ;
87+ context . subscriptions . push ( code_action ) ;
88+ run_diag ( ) ;
89+ } )
90+ . catch ( e => console . error ( `[CSS-linter][ERROR]: ${ e } ` ) ) ;
8491}
8592
8693class CssFixProvider implements vscode . CodeActionProvider {
87- constructor ( private diagnostics : vscode . DiagnosticCollection ) { }
88-
89- provideCodeActions ( document : vscode . TextDocument , range : vscode . Range ) : vscode . CodeAction [ ] {
90- const actions : vscode . CodeAction [ ] = [ ] ;
91- const disable_rule = '/* css-lint-disable-rule unused-class*/' ;
92-
93- const diagnostics = this . diagnostics . get ( document . uri ) || [ ] ;
94- for ( const diagnostic of diagnostics ) {
95- if ( diagnostic . range . intersection ( range ) ) {
96- const fix = new vscode . CodeAction ( `Add ${ disable_rule } ` , vscode . CodeActionKind . QuickFix ) ;
97- fix . edit = new vscode . WorkspaceEdit ( ) ;
98-
99- fix . edit . insert ( document . uri , new vscode . Position ( diagnostic . range . start . line , 0 ) , `${ disable_rule } \n` ) ;
100- fix . diagnostics = [ diagnostic ] ;
101- fix . isPreferred = true ;
102-
103- actions . push ( fix ) ;
104- }
105- }
106-
107- return actions ;
108- }
94+ constructor ( private diagnostics : vscode . DiagnosticCollection ) { }
95+
96+ provideCodeActions ( document : vscode . TextDocument , range : vscode . Range ) : vscode . CodeAction [ ] {
97+ const actions : vscode . CodeAction [ ] = [ ] ;
98+ const disable_rule = '/* css-lint-disable-rule unused-class*/' ;
99+
100+ const diagnostics = this . diagnostics . get ( document . uri ) || [ ] ;
101+ for ( const diagnostic of diagnostics ) {
102+ if ( diagnostic . range . intersection ( range ) ) {
103+ const fix = new vscode . CodeAction (
104+ `Add ${ disable_rule } ` ,
105+ vscode . CodeActionKind . QuickFix
106+ ) ;
107+ fix . edit = new vscode . WorkspaceEdit ( ) ;
108+
109+ fix . edit . insert (
110+ document . uri ,
111+ new vscode . Position ( diagnostic . range . start . line , 0 ) ,
112+ `${ disable_rule } \n`
113+ ) ;
114+ fix . diagnostics = [ diagnostic ] ;
115+ fix . isPreferred = true ;
116+
117+ actions . push ( fix ) ;
118+ }
119+ }
120+
121+ return actions ;
122+ }
109123}
110124
111- export function deactivate ( ) { }
125+ export function deactivate ( ) { }
126+
0 commit comments