77 commands ,
88 ExtensionContext ,
99 languages ,
10+ TextDocument ,
1011 window ,
1112 workspace ,
1213} from "vscode" ;
@@ -17,56 +18,59 @@ import {
1718} from "./settings" ;
1819import { Provider , clear , invalidate } from "./provider" ;
1920
20- export function activate ( context : ExtensionContext ) {
21- const enabledLanguages = getEnabledLanguages ( ) ;
22- const validations = languages . createDiagnosticCollection ( ) ;
23- const provider = new Provider ( ) ;
21+ const enabledLanguages = getEnabledLanguages ( ) ;
22+ const validations = languages . createDiagnosticCollection ( ) ;
23+ const provider = new Provider ( ) ;
24+
25+ async function validate (
26+ document : TextDocument ,
27+ type : AutoValidation | undefined
28+ ) {
29+ if ( enabledLanguages . includes ( document . languageId ) ) {
30+ const validation = getAutoValidation ( document ) ;
31+ if ( ! type || type === validation ) {
32+ validations . set ( document . uri , await provider . validate ( document ) ) ;
33+ } else if ( validation !== AutoValidation . ALWAYS ) {
34+ validations . delete ( document . uri ) ;
35+ }
36+ }
37+ }
2438
39+ export function activate ( context : ExtensionContext ) {
2540 context . subscriptions . push (
2641 languages . registerCompletionItemProvider ( enabledLanguages , provider ) ,
2742 languages . registerDefinitionProvider ( enabledLanguages , provider ) ,
2843 workspace . onDidSaveTextDocument ( async ( document ) => {
2944 invalidate ( document . uri . toString ( ) ) ;
30- if ( enabledLanguages . includes ( document . languageId ) ) {
31- const validation = getAutoValidation ( document ) ;
32- if ( validation === AutoValidation . SAVE ) {
33- validations . set ( document . uri , await provider . validate ( document ) ) ;
34- }
35- }
45+ await validate ( document , AutoValidation . SAVE ) ;
3646 } ) ,
3747 workspace . onDidOpenTextDocument ( async ( document ) => {
38- if ( enabledLanguages . includes ( document . languageId ) ) {
39- const validation = getAutoValidation ( document ) ;
40- if ( validation === AutoValidation . ALWAYS ) {
41- validations . set ( document . uri , await provider . validate ( document ) ) ;
42- }
43- }
48+ await validate ( document , AutoValidation . ALWAYS ) ;
4449 } ) ,
4550 workspace . onDidChangeTextDocument ( async ( event ) => {
46- const document = event . document ;
47- if ( enabledLanguages . includes ( document . languageId ) ) {
48- const validation = getAutoValidation ( document ) ;
49- if ( validation === AutoValidation . ALWAYS ) {
50- validations . set ( document . uri , await provider . validate ( document ) ) ;
51- } else {
52- validations . delete ( document . uri ) ;
53- }
51+ if ( event . contentChanges . length > 0 ) {
52+ await validate ( event . document , AutoValidation . ALWAYS ) ;
5453 }
5554 } ) ,
5655 workspace . onDidCloseTextDocument ( ( document ) => {
5756 validations . delete ( document . uri ) ;
5857 } ) ,
59- commands . registerCommand ( "vscode-html-css.validate" , async ( ) => {
60- const editor = window . activeTextEditor ;
61- if ( editor ) {
62- const document = editor . document ;
63- if ( enabledLanguages . includes ( document . languageId ) ) {
64- validations . set ( document . uri , await provider . validate ( document ) ) ;
58+ commands . registerCommand (
59+ "vscode-html-css.validate" ,
60+ async ( type : AutoValidation | undefined ) => {
61+ const editor = window . activeTextEditor ;
62+ if ( editor ) {
63+ await validate ( editor . document , type ) ;
6564 }
6665 }
67- } ) ,
66+ ) ,
6867 commands . registerCommand ( "vscode-html-css.clear" , ( ) => clear ( ) )
6968 ) ;
69+
70+ return commands . executeCommand (
71+ "vscode-html-css.validate" ,
72+ AutoValidation . ALWAYS
73+ ) ;
7074}
7175
7276export function deactivate ( ) { }
0 commit comments