-
Notifications
You must be signed in to change notification settings - Fork 47
/
layoutManager.ts
154 lines (138 loc) · 3.81 KB
/
layoutManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { commands, TextDocument, ViewColumn, window } from "vscode";
import * as config from "../config";
enum EditorLayoutOrientation {
horizontal = 0,
vertical = 1,
}
const EditorLayouts = {
splitOne: {
orientation: EditorLayoutOrientation.horizontal,
groups: [{}, {}],
},
splitTwo: {
orientation: EditorLayoutOrientation.horizontal,
groups: [
{
orientation: EditorLayoutOrientation.vertical,
groups: [{}, {}],
size: 0.5,
},
{ groups: [{}], size: 0.5 },
],
},
splitThree: {
orientation: EditorLayoutOrientation.horizontal,
groups: [
{
orientation: EditorLayoutOrientation.vertical,
groups: [{}, {}, {}],
size: 0.5,
},
{ groups: [{}], size: 0.5 },
],
},
grid: {
orientation: EditorLayoutOrientation.horizontal,
groups: [
{
orientation: EditorLayoutOrientation.vertical,
groups: [{}, {}],
size: 0.5,
},
{
orientation: EditorLayoutOrientation.vertical,
groups: [{}, {}],
size: 0.5,
},
],
},
};
export enum SwingLayout {
grid = "grid",
preview = "preview",
splitBottom = "splitBottom",
splitLeft = "splitLeft",
splitLeftTabbed = "splitLeftTabbed",
splitRight = "splitRight",
splitRightTabbed = "splitRightTabbed",
splitTop = "splitTop",
}
export async function createLayoutManager(
includedFiles: number,
layout?: string
) {
if (!layout) {
layout = await config.get("layout");
}
let currentViewColumn = ViewColumn.One;
let previewViewColumn = includedFiles + 1;
let editorLayout: any;
if (includedFiles === 3) {
editorLayout =
layout === SwingLayout.grid
? EditorLayouts.grid
: EditorLayouts.splitThree;
} else if (includedFiles === 2) {
editorLayout = EditorLayouts.splitTwo;
} else {
editorLayout = EditorLayouts.splitOne;
}
if (layout === SwingLayout.splitRight) {
editorLayout = {
...editorLayout,
groups: [...editorLayout.groups].reverse(),
};
currentViewColumn = ViewColumn.Two;
previewViewColumn = ViewColumn.One;
} else if (layout === SwingLayout.splitTop) {
editorLayout = {
...editorLayout,
orientation: EditorLayoutOrientation.vertical,
};
} else if (layout === SwingLayout.splitBottom) {
editorLayout = {
orientation: EditorLayoutOrientation.vertical,
groups: [...editorLayout.groups].reverse(),
};
currentViewColumn = ViewColumn.Two;
previewViewColumn = ViewColumn.One;
} else if (layout === SwingLayout.splitLeftTabbed) {
editorLayout = EditorLayouts.splitOne;
previewViewColumn = ViewColumn.Two;
} else if (layout === SwingLayout.splitRightTabbed) {
editorLayout = EditorLayouts.splitOne;
currentViewColumn = ViewColumn.Two;
previewViewColumn = ViewColumn.One;
}
await commands.executeCommand("workbench.action.closeAllEditors");
await commands.executeCommand("workbench.action.closePanel");
await commands.executeCommand("workbench.action.closeSidebar");
// The preview layout mode only shows a single file,
// so there's no need to set a custom editor layout for it.
if (includedFiles > 0 && layout !== SwingLayout.preview) {
await commands.executeCommand("vscode.setEditorLayout", editorLayout);
}
return {
previewViewColumn,
showDocument: async function(
document: TextDocument,
preserveFocus: boolean = true
) {
if (layout === SwingLayout.preview) {
return;
}
const editor = window.showTextDocument(document, {
preview: false,
viewColumn: currentViewColumn,
preserveFocus,
});
if (
layout !== SwingLayout.splitLeftTabbed &&
layout !== SwingLayout.splitRightTabbed
) {
currentViewColumn++;
}
return editor;
},
};
}