1
1
import * as vscode from 'vscode' ;
2
2
import { ref , computed } from '@vue/reactivity' ;
3
- import * as shared from '@volar/shared' ;
4
3
import { parse , SFCBlock } from '@vue/compiler-sfc' ;
5
4
6
5
export function activate ( context : vscode . ExtensionContext ) {
@@ -14,34 +13,70 @@ export function activate(context: vscode.ExtensionContext) {
14
13
const editor = vscode . window . activeTextEditor ;
15
14
if ( ! editor ) return ;
16
15
16
+ const layout = await vscode . workspace . getConfiguration ( 'volar' ) . get < { left : string [ ] , right : string [ ] ; } > ( 'splitEditors.layout' ) ?? { left : [ ] , right : [ ] } ;
17
+
17
18
const doc = editor . document ;
18
19
const { descriptor } = getDocDescriptor ( doc . getText ( ) ) ;
19
- const leftBlocks = [
20
- descriptor . scriptSetup ,
21
- descriptor . script ,
22
- ...descriptor . styles ,
23
- ] . filter ( shared . notEmpty ) ;
24
- const rightBlocks = [
25
- descriptor . template ,
26
- ...descriptor . customBlocks ,
27
- ] . filter ( shared . notEmpty ) ;
20
+ let leftBlocks : SFCBlock [ ] = [ ] ;
21
+ let rightBlocks : SFCBlock [ ] = [ ] ;
22
+
23
+ if ( descriptor . script ) {
24
+ if ( layout . left . includes ( 'script' ) ) {
25
+ leftBlocks . push ( descriptor . script ) ;
26
+ }
27
+ if ( layout . right . includes ( 'script' ) ) {
28
+ leftBlocks . push ( descriptor . script ) ;
29
+ }
30
+ }
31
+ if ( descriptor . scriptSetup ) {
32
+ if ( layout . left . includes ( 'scriptSetup' ) ) {
33
+ rightBlocks . push ( descriptor . scriptSetup ) ;
34
+ }
35
+ if ( layout . right . includes ( 'scriptSetup' ) ) {
36
+ rightBlocks . push ( descriptor . scriptSetup ) ;
37
+ }
38
+ }
39
+ if ( descriptor . template ) {
40
+ if ( layout . left . includes ( 'template' ) ) {
41
+ rightBlocks . push ( descriptor . template ) ;
42
+ }
43
+ if ( layout . right . includes ( 'template' ) ) {
44
+ rightBlocks . push ( descriptor . template ) ;
45
+ }
46
+ }
47
+ if ( layout . left . includes ( 'styles' ) ) {
48
+ leftBlocks = leftBlocks . concat ( descriptor . styles ) ;
49
+ }
50
+ if ( layout . right . includes ( 'styles' ) ) {
51
+ rightBlocks = rightBlocks . concat ( descriptor . styles ) ;
52
+ }
53
+ if ( layout . left . includes ( 'customBlocks' ) ) {
54
+ leftBlocks = leftBlocks . concat ( descriptor . customBlocks ) ;
55
+ }
56
+ if ( layout . right . includes ( 'customBlocks' ) ) {
57
+ rightBlocks = rightBlocks . concat ( descriptor . customBlocks ) ;
58
+ }
28
59
29
60
await foldingBlocks ( leftBlocks ) ;
30
61
await vscode . commands . executeCommand ( 'workbench.action.toggleSplitEditorInGroup' ) ;
31
62
await foldingBlocks ( rightBlocks ) ;
32
63
33
64
async function foldingBlocks ( blocks : SFCBlock [ ] ) {
34
65
35
- const firstBlock = blocks . sort ( ( a , b ) => a . loc . start . offset - b . loc . start . offset ) [ 0 ] ;
36
-
37
66
const editor = vscode . window . activeTextEditor ;
38
67
if ( ! editor ) return ;
39
68
40
- editor . selections = blocks . map ( block => new vscode . Selection ( doc . positionAt ( block . loc . start . offset ) , doc . positionAt ( block . loc . start . offset ) ) ) ;
69
+ editor . selections = blocks . length
70
+ ? blocks . map ( block => new vscode . Selection ( doc . positionAt ( block . loc . start . offset ) , doc . positionAt ( block . loc . start . offset ) ) )
71
+ : [ new vscode . Selection ( doc . positionAt ( doc . getText ( ) . length ) , doc . positionAt ( doc . getText ( ) . length ) ) ] ;
41
72
42
73
await vscode . commands . executeCommand ( 'editor.unfoldAll' ) ;
43
74
await vscode . commands . executeCommand ( 'editor.foldLevel1' ) ;
44
- editor . revealRange ( new vscode . Range ( doc . positionAt ( firstBlock . loc . start . offset ) , new vscode . Position ( editor . document . lineCount , 0 ) ) , vscode . TextEditorRevealType . AtTop ) ;
75
+
76
+ const firstBlock = blocks . sort ( ( a , b ) => a . loc . start . offset - b . loc . start . offset ) [ 0 ] ;
77
+ if ( firstBlock ) {
78
+ editor . revealRange ( new vscode . Range ( doc . positionAt ( firstBlock . loc . start . offset ) , new vscode . Position ( editor . document . lineCount , 0 ) ) , vscode . TextEditorRevealType . AtTop ) ;
79
+ }
45
80
}
46
81
}
47
82
}
0 commit comments