Repository navigation
Expand file tree
/
Copy pathsidebarProvider.js
More file actions
390 lines (334 loc) · 15.4 KB
/
Copy pathsidebarProvider.js
File metadata and controls
390 lines (334 loc) · 15.4 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const { getStatus, onStateChange, getPaused, setPaused } = require('./state');
const VIEW_TYPE = 'typify.sidebar';
class SidebarProvider {
/** @param {vscode.ExtensionContext} context @param {string} projectPath @param {string} mirrorPath */
constructor(context, projectPath, mirrorPath) {
this._context = context;
this._projectPath = projectPath;
this._mirrorPath = mirrorPath;
this._view = null;
this._onReanalyze = null; // set by extension.js
onStateChange(() => this._push());
}
/** Called by extension.js so the sidebar can trigger re-analysis on config change. */
setReanalyzeCallback(fn) { this._onReanalyze = fn; }
resolveWebviewView(webviewView) {
this._view = webviewView;
webviewView.webview.options = { enableScripts: true };
webviewView.webview.html = this._html();
webviewView.webview.onDidReceiveMessage(async msg => {
switch (msg.type) {
case 'ready':
// Webview just loaded — send current state
this._push();
break;
case 'saveConfig': {
const ok = this._writeConfig(msg.config);
if (ok && this._onReanalyze) {
// Re-run analysis with new config
this._onReanalyze();
}
break;
}
case 'togglePause': {
setPaused(msg.paused);
// If resuming, trigger a fresh analysis run
if (!msg.paused && this._onReanalyze) {
this._onReanalyze();
}
this._push();
break;
}
}
});
this._push();
}
// ── Config helpers ──────────────────────────────────────────────────────
/**
* The config.json is created and owned by typify — we never create it.
* We only read it (to populate the sidebar) and patch it (on save).
*/
_configPath() {
return path.join(this._mirrorPath, '.typify', 'config.json');
}
_readConfig() {
const p = this._configPath();
if (!fs.existsSync(p)) return null; // not yet created by typify
try { return JSON.parse(fs.readFileSync(p, 'utf8')); }
catch (_) { return null; }
}
_writeConfig(patch) {
try {
const p = this._configPath();
if (!fs.existsSync(p)) return false; // typify hasn't run yet
const current = JSON.parse(fs.readFileSync(p, 'utf8'));
fs.writeFileSync(p, JSON.stringify({ ...current, ...patch }, null, '\t'), 'utf8');
return true;
} catch (err) {
vscode.window.showErrorMessage(`Typify: Could not save config — ${err.message}`);
return false;
}
}
// ── State push ──────────────────────────────────────────────────────────
_push() {
if (!this._view) return;
const { status, statusMessage } = getStatus();
const config = this._readConfig();
const paused = getPaused();
this._view.webview.postMessage({ type: 'stateUpdate', status, statusMessage, config, paused });
}
// ── Webview HTML ────────────────────────────────────────────────────────
_html() {
return /* html */`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Typify</title>
<style>
:root {
--radius: 6px;
--gap: 14px;
--font: var(--vscode-font-family, 'Segoe UI', sans-serif);
--font-mono: var(--vscode-editor-font-family, monospace);
--bg: var(--vscode-sideBar-background, #1e1e1e);
--surface: var(--vscode-editor-background, #252526);
--border: var(--vscode-panel-border, #333);
--fg: var(--vscode-foreground, #ccc);
--fg-dim: var(--vscode-descriptionForeground, #888);
--accent: var(--vscode-button-background, #0e639c);
--accent-fg: var(--vscode-button-foreground, #fff);
--input-bg: var(--vscode-input-background, #3c3c3c);
--input-fg: var(--vscode-input-foreground, #ccc);
--input-border: var(--vscode-input-border, #555);
--toggle-on: var(--vscode-statusBarItem-prominentBackground, #388bfd);
--err: var(--vscode-statusBarItem-errorBackground, #c72e0f);
--warn: var(--vscode-editorWarning-foreground, #cca700);
--ok: var(--vscode-testing-iconPassed, #4db46c);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: var(--font); font-size: 13px; color: var(--fg);
background: var(--bg); padding: var(--gap);
display: flex; flex-direction: column; gap: var(--gap); min-height: 100vh;
}
/* Header */
.header { display:flex; align-items:center; gap:8px; padding-bottom:var(--gap); border-bottom:1px solid var(--border); }
.header-title { font-weight:600; font-size:13px; letter-spacing:.4px; text-transform:uppercase; }
.header-sub { font-size:10px; color:var(--fg-dim); font-weight:400; }
/* Card */
.card { background:var(--surface); border:1px solid var(--border); border-radius:var(--radius); padding:12px; display:flex; flex-direction:column; gap:10px; }
.card-label { font-size:10px; text-transform:uppercase; letter-spacing:.8px; color:var(--fg-dim); font-weight:600; }
/* Status */
.status-row { display:flex; align-items:center; gap:8px; }
.status-dot { width:8px; height:8px; border-radius:50%; flex-shrink:0; background:var(--fg-dim); transition:background .3s; }
.status-dot.running { background:var(--warn); animation:pulse 1s ease-in-out infinite; }
.status-dot.ready { background:var(--ok); }
.status-dot.error { background:var(--err); }
.status-dot.idle { background:var(--fg-dim); }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.3} }
.status-text { font-size:12px; color:var(--fg); flex:1; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.status-detail { font-size:11px; color:var(--fg-dim); font-family:var(--font-mono); word-break:break-all; line-height:1.5; }
/* Toggle row */
.toggle-row { display:flex; align-items:flex-start; justify-content:space-between; gap:8px; }
.toggle-info { display:flex; flex-direction:column; gap:3px; flex:1; }
.toggle-title { font-size:12px; font-weight:500; color:var(--fg); }
.toggle-desc { font-size:11px; color:var(--fg-dim); line-height:1.4; }
.switch { position:relative; width:36px; height:20px; flex-shrink:0; margin-top:1px; }
.switch input { display:none; }
.track { position:absolute; inset:0; border-radius:20px; background:var(--border); cursor:pointer; transition:background .2s; }
.track::after { content:''; position:absolute; left:3px; top:3px; width:14px; height:14px; border-radius:50%; background:var(--fg-dim); transition:transform .2s, background .2s; }
.switch input:checked + .track { background:var(--toggle-on); }
.switch input:checked + .track::after { transform:translateX(16px); background:#fff; }
/* Text / number input row */
.field-row { display:flex; flex-direction:column; gap:4px; }
.field-label { font-size:11px; color:var(--fg-dim); }
.field-input {
background:var(--input-bg); color:var(--input-fg);
border:1px solid var(--input-border); border-radius:3px;
padding:4px 7px; font-size:12px; font-family:var(--font-mono);
width:100%; outline:none;
}
.field-input:focus { border-color:var(--accent); }
input[type=number].field-input { width:72px; }
/* Save button */
.save-btn {
display:flex; align-items:center; justify-content:center; gap:6px;
padding:5px 12px; border-radius:3px; font-size:12px; font-weight:600;
border:none; cursor:pointer;
background:var(--accent); color:var(--accent-fg);
transition:opacity .15s;
}
.save-btn:hover { opacity:.85; }
.save-btn:disabled { opacity:.45; cursor:default; }
/* Inline pause button (inside status card) */
.pause-btn {
display:flex; align-items:center; gap:4px;
padding:2px 8px; border-radius:3px; font-size:10px; font-weight:600;
border:1px solid var(--border); cursor:pointer;
background:transparent; color:var(--fg-dim);
transition:background .15s, color .15s, border-color .15s;
white-space:nowrap; flex-shrink:0; line-height:1.6;
}
.pause-btn:hover { background:var(--input-bg); color:var(--fg); }
.pause-btn.paused { color:var(--warn); border-color:color-mix(in srgb,var(--warn) 40%,transparent); }
/* Badge */
.badge { display:inline-flex; align-items:center; padding:1px 6px; border-radius:20px; font-size:10px; font-weight:600; text-transform:uppercase; margin-left:5px; }
.badge.exp { background:color-mix(in srgb,var(--warn) 18%,transparent); color:var(--warn); border:1px solid color-mix(in srgb,var(--warn) 35%,transparent); }
/* Footer */
.footer { margin-top:auto; font-size:10px; color:var(--fg-dim); text-align:center; opacity:.6; }
/* Divider */
.divider { height:1px; background:var(--border); margin:2px 0; }
</style>
</head>
<body>
<div class="header">
<div>
<div class="header-title">Typify</div>
<div class="header-sub">Usage-driven Static Analyzer</div>
</div>
</div>
<!-- Status -->
<div class="card">
<div style="display:flex;align-items:center;justify-content:space-between;gap:8px;">
<div class="card-label" style="margin:0">Analyzer status</div>
<button class="pause-btn" id="pauseBtn" title="Pause or resume live analysis">⏸ Pause</button>
</div>
<div class="status-row">
<div class="status-dot idle" id="dot"></div>
<div class="status-text" id="statusText">Idle</div>
</div>
<div class="status-detail" id="statusDetail"></div>
</div>
<!-- Config -->
<div class="card" id="configCard" style="display:none">
<div class="card-label">Configuration</div>
<!-- Context Retrieval toggle -->
<div class="toggle-row">
<div class="toggle-info">
<div class="toggle-title">Context Retrieval</div>
<div class="toggle-desc">Use retrieval-augmented inference for better type coverage.</div>
</div>
<label class="switch"><input type="checkbox" id="cfgRetrieval"><div class="track"></div></label>
</div>
<!-- Augment context toggle -->
<div class="toggle-row">
<div class="toggle-info">
<div class="toggle-title">Augment Context <span class="badge exp">experimental</span></div>
<div class="toggle-desc">Enrich retrieval context with additional metadata.</div>
</div>
<label class="switch"><input type="checkbox" id="cfgAugment"><div class="track"></div></label>
</div>
<!-- Type4Py toggle -->
<div class="toggle-row">
<div class="toggle-info">
<div class="toggle-title">Type4Py Neural Model</div>
<div class="toggle-desc">Enhance inference with the Type4Py deep-learning model.</div>
</div>
<label class="switch"><input type="checkbox" id="cfgType4py"><div class="track"></div></label>
</div>
<!-- Type4Py API URL (shown only when type4py is on) -->
<div class="field-row" id="apiUrlRow">
<div class="field-label">Type4Py API URL</div>
<input id="cfgApiUrl" type="text" class="field-input" placeholder="https://…">
</div>
<div class="divider"></div>
<!-- Save -->
<button class="save-btn" id="saveBtn" disabled>Save & Re-analyze</button>
</div>
<div class="footer">Typify · usage-driven static analysis</div>
<script>
const vscode = acquireVsCodeApi();
// Elements
const dot = document.getElementById('dot');
const statusText = document.getElementById('statusText');
const statusDetail = document.getElementById('statusDetail');
const cfgRetrieval = document.getElementById('cfgRetrieval');
const cfgType4py = document.getElementById('cfgType4py');
const cfgApiUrl = document.getElementById('cfgApiUrl');
const cfgAugment = document.getElementById('cfgAugment');
const apiUrlRow = document.getElementById('apiUrlRow');
const saveBtn = document.getElementById('saveBtn');
const pauseBtn = document.getElementById('pauseBtn');
const STATUS_LABELS = { idle:'Idle', running:'Analyzing…', ready:'Ready', error:'Error' };
// Track whether the user has made changes
let _dirty = false;
let _paused = false;
// ── Pause toggle ────────────────────────────────────────────────────────────
function applyPaused(paused) {
_paused = paused;
if (paused) {
pauseBtn.textContent = '▶ Resume';
pauseBtn.classList.add('paused');
} else {
pauseBtn.textContent = '⏸ Pause';
pauseBtn.classList.remove('paused');
}
}
pauseBtn.addEventListener('click', () => {
const next = !_paused;
applyPaused(next);
vscode.postMessage({ type: 'togglePause', paused: next });
});
// ── Config ──────────────────────────────────────────────────────────────────
function markDirty() {
_dirty = true;
saveBtn.disabled = false;
updateVisibility();
}
function updateVisibility() {
apiUrlRow.style.display = cfgType4py.checked ? '' : 'none';
}
function applyConfig(config) {
cfgRetrieval.checked = !!config['context-retrieval'];
cfgType4py.checked = !!config['type4py'];
cfgApiUrl.value = config['type4py-api-url'] ?? '';
cfgAugment.checked = !!config['augment-context'];
updateVisibility();
}
function readConfig() {
return {
'context-retrieval': cfgRetrieval.checked,
'type4py': cfgType4py.checked,
'type4py-api-url': cfgApiUrl.value.trim(),
'augment-context': cfgAugment.checked,
};
}
// Wire up change listeners
[cfgRetrieval, cfgType4py, cfgApiUrl, cfgAugment].forEach(el => {
el.addEventListener('change', markDirty);
el.addEventListener('input', markDirty);
});
saveBtn.addEventListener('click', () => {
saveBtn.disabled = true;
_dirty = false;
vscode.postMessage({ type: 'saveConfig', config: readConfig() });
});
window.addEventListener('message', event => {
const msg = event.data;
if (msg.type !== 'stateUpdate') return;
// Paused state
if (typeof msg.paused === 'boolean') applyPaused(msg.paused);
// Status
dot.className = 'status-dot ' + (msg.paused ? 'idle' : msg.status);
statusText.textContent = msg.paused ? 'Paused' : (STATUS_LABELS[msg.status] ?? msg.status);
statusDetail.textContent = msg.statusMessage ?? '';
// Config card — visible whenever config data is available
const hasConfig = !!msg.config;
document.getElementById('configCard').style.display = hasConfig ? '' : 'none';
// Config — only update if user hasn't made unsaved changes
if (hasConfig && !_dirty) {
applyConfig(msg.config);
}
});
// Tell the extension we're ready
vscode.postMessage({ type: 'ready' });
</script>
</body>
</html>`;
}
}
module.exports = { SidebarProvider, VIEW_TYPE };