@@ -13,30 +13,32 @@ function activate(context) {
13
13
config . update ( "enable-compile-vue-file-on-did-save-code" , false ) ;
14
14
statusBarUi . StatusBarUi . notWatching ( ) ;
15
15
} ) ;
16
+ let format = vscode . commands . registerCommand ( "vue3snippets.format" , ( ) => {
17
+ const editor = vscode . window . activeTextEditor ;
18
+ const filepath = editor . document . uri . fsPath ;
19
+ if ( ! editor ) throw new Error ( 'no active editor' ) ;
20
+ const doc = editor . document ;
21
+ const lineCount = doc . lineCount ;
22
+ const text = doc . getText ( ) ;
23
+ const start = new vscode . Position ( 0 , 0 ) ;
24
+ const end = new vscode . Position ( lineCount + 1 , 0 ) ;
25
+ const range = new vscode . Range ( start , end ) ;
26
+ const prettierText = prettier . format ( text , { filepath } ) ;
27
+ editor . edit ( ( editBuilder , error ) => {
28
+ error && window . showErrorMessage ( error ) ;
29
+ editBuilder . replace ( range , prettierText ) ;
30
+ } ) ;
31
+ } ) ;
16
32
context . subscriptions . push ( compileOn ) ;
17
33
context . subscriptions . push ( compileOff ) ;
18
- vscode . workspace . onWillSaveTextDocument ( ( { document } ) => {
34
+ context . subscriptions . push ( format ) ;
35
+ vscode . workspace . onWillSaveTextDocument ( ( ) => {
19
36
let config = vscode . workspace . getConfiguration ( "vue3snippets" ) ;
20
37
let isEnableOnDidSaveTextDocument = config . get ( "enable-compile-vue-file-on-did-save-code" ) ;
21
38
if ( ! isEnableOnDidSaveTextDocument ) { return } ;
22
39
let activeTextEditor = vscode . window . activeTextEditor ;
23
40
if ( activeTextEditor && activeTextEditor . document . languageId === 'vue' ) {
24
- const filepath = document . uri . fsPath ;
25
- const editor = vscode . window . activeTextEditor ;
26
- if ( ! editor ) throw new Error ( 'no active editor' ) ;
27
- const doc = editor . document ;
28
- const lineCount = doc . lineCount ;
29
- const text = doc . getText ( ) ;
30
- const start = new vscode . Position ( 0 , 0 ) ;
31
- const end = new vscode . Position ( lineCount + 1 , 0 ) ;
32
- const range = new vscode . Range ( start , end ) ;
33
- const prettierText = prettier . format ( text , { filepath } ) ;
34
- editor . edit ( ( editBuilder , error ) => {
35
- error && window . showErrorMessage ( error ) ;
36
- editBuilder . replace ( range , prettierText ) ;
37
- } ) ;
38
- } else {
39
- // vscode.window.showInformationMessage('It‘s not a .vue file');
41
+ vscode . commands . executeCommand ( "vue3snippets.format" ) ;
40
42
}
41
43
} ) ;
42
44
statusBarUi . StatusBarUi . init ( vscode . workspace . getConfiguration ( "vue3snippets" ) . get ( "enable-compile-vue-file-on-did-save-code" ) ) ;
0 commit comments