|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Atom++ — Hackability (M3, feature 1: user init script) |
| 3 | + * |
| 4 | + * Atom's signature "run my own code when the editor starts" hook. On startup we load |
| 5 | + * ~/.atom-plus-plus/init.js and run it with a small `atompp` convenience API plus the full |
| 6 | + * `vscode` API in scope. It is live-reloadable ("Atom++: Reload Init Script") — no restart. |
| 7 | + * |
| 8 | + * This runs the user's OWN file from their home directory (exactly like Atom). It is not |
| 9 | + * fetched or remote; errors are caught and surfaced so a broken init never breaks startup. |
| 10 | + *--------------------------------------------------------------------------------------------*/ |
| 11 | +// @ts-check |
| 12 | +'use strict'; |
| 13 | + |
| 14 | +const vscode = require('vscode'); |
| 15 | +const os = require('os'); |
| 16 | +const fs = require('fs'); |
| 17 | +const path = require('path'); |
| 18 | +const { createRequire } = require('module'); |
| 19 | + |
| 20 | +const INIT_DIR = path.join(os.homedir(), '.atom-plus-plus'); |
| 21 | +const INIT_PATH = path.join(INIT_DIR, 'init.js'); |
| 22 | + |
| 23 | +/** @type {vscode.OutputChannel} */ |
| 24 | +let output; |
| 25 | +/** Disposables created by the *current* run of the init script (cleared on reload). */ |
| 26 | +let userDisposables = []; |
| 27 | + |
| 28 | +const TEMPLATE = `// Atom++ — User Init Script |
| 29 | +// This file runs every time Atom++ starts. Edit it, then run |
| 30 | +// "Atom++: Reload Init Script" from the Command Palette (no restart needed). |
| 31 | +// |
| 32 | +// Two things are in scope: |
| 33 | +// • atompp — a small convenience API (everything you register is auto-cleaned on reload) |
| 34 | +// • vscode — the full VS Code extension API (the escape hatch for anything else) |
| 35 | +// |
| 36 | +// Uncomment an example to try it, then reload: |
| 37 | +
|
| 38 | +// 1. A custom item in the status bar: |
| 39 | +// atompp.addStatusBarItem('🚀 init.js', 'Added from my init script'); |
| 40 | +
|
| 41 | +// 2. A custom command (run it from the Command Palette): |
| 42 | +// atompp.registerCommand('myinit.hello', () => { |
| 43 | +// atompp.showMessage('Hello from your init script!'); |
| 44 | +// }); |
| 45 | +
|
| 46 | +// 3. React to saving a file: |
| 47 | +// atompp.onDidSave(doc => console.log('Saved:', doc.fileName)); |
| 48 | +
|
| 49 | +// 4. Anything in the raw VS Code API: |
| 50 | +// vscode.window.showInformationMessage('Atom++ init script loaded.'); |
| 51 | +`; |
| 52 | + |
| 53 | +/** Build the friendly Atom++ API handed to the init script. Returns { api, subs }. */ |
| 54 | +function makeApi() { |
| 55 | + /** @type {vscode.Disposable[]} */ |
| 56 | + const subs = []; |
| 57 | + const track = (d) => { if (d && typeof d.dispose === 'function') { subs.push(d); } return d; }; |
| 58 | + const api = { |
| 59 | + version: '0.1.0', |
| 60 | + /** The full VS Code API. */ |
| 61 | + vscode, |
| 62 | + /** Disposables registered here are cleaned up automatically on the next reload. */ |
| 63 | + subscriptions: subs, |
| 64 | + registerCommand: (id, fn) => track(vscode.commands.registerCommand(id, fn)), |
| 65 | + onDidSave: (fn) => track(vscode.workspace.onDidSaveTextDocument(fn)), |
| 66 | + onDidOpen: (fn) => track(vscode.workspace.onDidOpenTextDocument(fn)), |
| 67 | + onDidChangeEditor: (fn) => track(vscode.window.onDidChangeActiveTextEditor(fn)), |
| 68 | + addStatusBarItem: (text, tooltip, command) => { |
| 69 | + const item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0); |
| 70 | + item.text = String(text); |
| 71 | + if (tooltip) { item.tooltip = String(tooltip); } |
| 72 | + if (command) { item.command = command; } |
| 73 | + item.show(); |
| 74 | + subs.push(item); |
| 75 | + return item; |
| 76 | + }, |
| 77 | + showMessage: (m) => vscode.window.showInformationMessage(String(m)), |
| 78 | + showError: (m) => vscode.window.showErrorMessage(String(m)), |
| 79 | + getConfig: (section) => vscode.workspace.getConfiguration(section), |
| 80 | + log: (...args) => { output.appendLine(args.map((a) => (typeof a === 'string' ? a : JSON.stringify(a))).join(' ')); } |
| 81 | + }; |
| 82 | + return { api, subs }; |
| 83 | +} |
| 84 | + |
| 85 | +/** Dispose everything the previous run created. */ |
| 86 | +function disposeUser() { |
| 87 | + for (const d of userDisposables) { try { d.dispose(); } catch { /* ignore */ } } |
| 88 | + userDisposables = []; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Load and execute the init script. Returns true if it ran, false if skipped/missing. |
| 93 | + * @param {boolean} interactive Whether to surface success/errors to the user (vs silent on startup). |
| 94 | + */ |
| 95 | +function runInit(interactive) { |
| 96 | + if (!vscode.workspace.getConfiguration('atompp').get('init.enabled', true)) { return false; } |
| 97 | + if (!fs.existsSync(INIT_PATH)) { |
| 98 | + if (interactive) { vscode.window.showInformationMessage('Atom++: no init script yet. Use “Atom++: Open Init Script” to create one.'); } |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + disposeUser(); |
| 103 | + let code; |
| 104 | + try { code = fs.readFileSync(INIT_PATH, 'utf8'); } |
| 105 | + catch (e) { output.appendLine('[init] could not read ' + INIT_PATH + ': ' + e); return false; } |
| 106 | + |
| 107 | + const { api, subs } = makeApi(); |
| 108 | + const initRequire = createRequire(INIT_PATH); // so the script can require() its own local/global modules |
| 109 | + try { |
| 110 | + // Run the user's code with atompp/vscode/require/console in scope. |
| 111 | + const fn = new Function('atompp', 'vscode', 'require', 'console', '__dirname', '__filename', code); |
| 112 | + fn(api, vscode, initRequire, console, INIT_DIR, INIT_PATH); |
| 113 | + userDisposables = subs; |
| 114 | + output.appendLine('[init] ran ' + INIT_PATH + ' (' + subs.length + ' disposable(s) registered)'); |
| 115 | + if (interactive) { vscode.window.setStatusBarMessage('Atom++ init script reloaded', 2000); } |
| 116 | + return true; |
| 117 | + } catch (e) { |
| 118 | + const msg = (e && e.stack) ? e.stack : String(e); |
| 119 | + output.appendLine('[init] ERROR running init.js:\n' + msg); |
| 120 | + const pick = 'Show Log'; |
| 121 | + vscode.window.showErrorMessage('Atom++ init script failed: ' + ((e && e.message) || e), pick) |
| 122 | + .then((c) => { if (c === pick) { output.show(true); } }); |
| 123 | + return false; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +async function openInit() { |
| 128 | + try { |
| 129 | + if (!fs.existsSync(INIT_DIR)) { fs.mkdirSync(INIT_DIR, { recursive: true }); } |
| 130 | + if (!fs.existsSync(INIT_PATH)) { fs.writeFileSync(INIT_PATH, TEMPLATE, 'utf8'); } |
| 131 | + } catch (e) { |
| 132 | + vscode.window.showErrorMessage('Atom++: could not create init script: ' + ((e && e.message) || e)); |
| 133 | + return; |
| 134 | + } |
| 135 | + const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(INIT_PATH)); |
| 136 | + await vscode.window.showTextDocument(doc); |
| 137 | +} |
| 138 | + |
| 139 | +function activate(context) { |
| 140 | + output = vscode.window.createOutputChannel('Atom++ Init'); |
| 141 | + context.subscriptions.push( |
| 142 | + output, |
| 143 | + vscode.commands.registerCommand('atompp.init.edit', openInit), |
| 144 | + vscode.commands.registerCommand('atompp.init.reload', () => runInit(true)), |
| 145 | + { dispose: disposeUser } |
| 146 | + ); |
| 147 | + runInit(false); |
| 148 | +} |
| 149 | + |
| 150 | +function deactivate() { disposeUser(); } |
| 151 | + |
| 152 | +module.exports = { activate, deactivate }; |
0 commit comments