Skip to content

Commit 96ea01d

Browse files
committed
feat: add split editors layout configs
close #810
1 parent 9f8043c commit 96ea01d

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

extensions/vscode-vue-language-features/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,14 @@
389389
"type": "boolean",
390390
"default": true,
391391
"description": "Component preview background style."
392+
},
393+
"volar.splitEditors.layout.left": {
394+
"type": "array",
395+
"default": ["script", "scriptSetup", "styles"]
396+
},
397+
"volar.splitEditors.layout.right": {
398+
"type": "array",
399+
"default": ["template", "customBlocks"]
392400
}
393401
}
394402
},

extensions/vscode-vue-language-features/src/features/splitEditors.ts

+49-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as vscode from 'vscode';
22
import { ref, computed } from '@vue/reactivity';
3-
import * as shared from '@volar/shared';
43
import { parse, SFCBlock } from '@vue/compiler-sfc';
54

65
export function activate(context: vscode.ExtensionContext) {
@@ -14,34 +13,70 @@ export function activate(context: vscode.ExtensionContext) {
1413
const editor = vscode.window.activeTextEditor;
1514
if (!editor) return;
1615

16+
const layout = await vscode.workspace.getConfiguration('volar').get<{ left: string[], right: string[]; }>('splitEditors.layout') ?? { left: [], right: [] };
17+
1718
const doc = editor.document;
1819
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+
}
2859

2960
await foldingBlocks(leftBlocks);
3061
await vscode.commands.executeCommand('workbench.action.toggleSplitEditorInGroup');
3162
await foldingBlocks(rightBlocks);
3263

3364
async function foldingBlocks(blocks: SFCBlock[]) {
3465

35-
const firstBlock = blocks.sort((a, b) => a.loc.start.offset - b.loc.start.offset)[0];
36-
3766
const editor = vscode.window.activeTextEditor;
3867
if (!editor) return;
3968

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))];
4172

4273
await vscode.commands.executeCommand('editor.unfoldAll');
4374
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+
}
4580
}
4681
}
4782
}

0 commit comments

Comments
 (0)