-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathstatusbar.ts
203 lines (173 loc) · 5.01 KB
/
statusbar.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import * as vscode from 'vscode';
import * as path from 'path';
import { ServeD, config } from "./extension";
var dubLoaded = false;
export function setupDub(served: ServeD): vscode.Disposable {
let subscriptions: vscode.Disposable[] = [new vscode.Disposable(() => { dubLoaded = false; })];
if (dubLoaded)
return new vscode.Disposable(() => { });
dubLoaded = true;
subscriptions.push(new ConfigSelector(served));
subscriptions.push(new ArchSelector(served));
subscriptions.push(new BuildSelector(served));
subscriptions.push(new CompilerSelector(served));
return vscode.Disposable.from(...subscriptions);
}
export function isStatusbarRelevantDocument(document: vscode.TextDocument): boolean {
var language = document.languageId;
if (language == "d" || language == "dml" || language == "diet")
return true;
var filename = path.basename(document.fileName.toLowerCase());
if (filename == "dub.json" || filename == "dub.sdl")
return true;
return false;
}
export function checkStatusbarVisibility(overrideConfig: string, editor?: vscode.TextEditor | null): boolean {
if (editor === null) {
if (config(null).get(overrideConfig, false))
return true
else
return false
} else {
if (!editor)
editor = vscode.window.activeTextEditor;
if (editor) {
if (config(editor.document.uri).get(overrideConfig, false) || isStatusbarRelevantDocument(editor.document))
return true
else
return false
} else {
return false;
}
}
}
class GenericSelector implements vscode.Disposable {
subscriptions: vscode.Disposable[] = [];
item?: vscode.StatusBarItem;
constructor(
public served: ServeD,
public x: number,
public command: string,
public tooltip: string,
public event: string,
public method: string,
public fallback: string
) {
this.create();
}
protected create() {
this.item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, this.x);
this.item.command = this.command;
this.item.tooltip = this.tooltip;
this.updateDocumentVisibility();
this.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(editor => {
this.updateDocumentVisibility(editor || null);
}));
this.served.on(this.event, config => {
if (this.item)
this.item.text = config || this.fallback;
});
this.served.on("workspace-change", () => {
this.update();
});
this.update();
}
updateDocumentVisibility(editor?: vscode.TextEditor | null) {
if (this.item) {
if (checkStatusbarVisibility("alwaysShowDubStatusButtons", editor))
this.item.show();
else
this.item.hide();
}
}
update() {
this.served.client.sendRequest<string>(this.method).then(config => {
if (this.item)
this.item.text = config || this.fallback;
});
}
dispose() {
vscode.Disposable.from(...this.subscriptions).dispose();
}
}
class ConfigSelector extends GenericSelector {
constructor(served: ServeD) {
super(served, 0.92145,
"code-d.switchConfiguration", "Switch Configuration", "config-change",
"served/getConfig", "(config)");
}
}
class ArchSelector extends GenericSelector {
constructor(served: ServeD) {
super(served, 0.92144,
"code-d.switchArchType", "Switch Arch Type", "arch-type-change",
"served/getArchType", "(default arch)");
}
}
class BuildSelector extends GenericSelector {
constructor(served: ServeD) {
super(served, 0.92143,
"code-d.switchBuildType", "Switch Build Type", "build-type-change",
"served/getBuildType", "(build type)");
}
}
class CompilerSelector extends GenericSelector {
constructor(served: ServeD) {
super(served, 0.92142,
"code-d.switchCompiler", "Switch Compiler", "compiler-change",
"served/getCompiler", "(compiler)");
}
}
export class StartupProgress {
startedGlobal: boolean = false;
workspace: string | undefined;
progress: vscode.Progress<{ message?: string, increment?: number }> | undefined;
resolve: Function | undefined;
reject: Function | undefined;
constructor() {
}
async startGlobal() {
if (this.startedGlobal)
return;
this.startedGlobal = true;
vscode.window.withProgress({
cancellable: false,
location: vscode.ProgressLocation.Window,
title: "D"
}, (progress, token) => {
this.progress = progress;
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
});
}
finishGlobal() {
if (!this.startedGlobal)
return;
this.startedGlobal = false;
if (this.resolve)
this.resolve();
this.progress = undefined;
this.resolve = undefined;
this.reject = undefined;
}
globalStep(step: number, max: number, title: string, msg: string) {
if (!this.startedGlobal || !this.progress)
return;
let percent = step / (max || 1);
this.progress.report({
message: title + " (" + formatPercent(percent) + "): " + msg
});
}
setWorkspace(name: string) {
this.workspace = name;
this.globalStep(0, 1, "workspace " + name, "starting up...");
}
workspaceStep(step: number, max: number, msg: string) {
this.globalStep(step, max, "workspace " + this.workspace, msg);
}
}
function formatPercent(p: number) {
return (p * 100).toFixed(1) + " %";
}