|
| 1 | +// Copyright (C) 2018 Patrick Maué |
| 2 | +// |
| 3 | +// This file is part of vscode-journal. |
| 4 | +// |
| 5 | +// vscode-journal is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// vscode-journal is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with vscode-journal. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +import * as vscode from 'vscode'; |
| 22 | +import * as Path from 'path'; |
| 23 | +import { isNullOrUndefined, ConsoleLogger } from '../util'; |
| 24 | +import { Configuration } from '../vscode/conf'; |
| 25 | +import { Container } from './container'; |
| 26 | +import { registerCacheInvalidation, registerCodeActions, registerCommands } from './register'; |
| 27 | + |
| 28 | +interface TextMateRule { scope: string; settings: any; } |
| 29 | + |
| 30 | +/** |
| 31 | + * Extension lifecycle entry point. Builds the {@link Container} (composition |
| 32 | + * root) in a single pass, then registers commands, providers and optional |
| 33 | + * syntax highlighting. |
| 34 | + */ |
| 35 | +export class Startup { |
| 36 | + |
| 37 | + private ctrl!: Container; |
| 38 | + |
| 39 | + constructor(public config: vscode.WorkspaceConfiguration) { } |
| 40 | + |
| 41 | + public async run(context: vscode.ExtensionContext): Promise<void> { |
| 42 | + try { |
| 43 | + const channel = vscode.window.createOutputChannel("Journal"); |
| 44 | + context.subscriptions.push(channel); |
| 45 | + |
| 46 | + this.ctrl = new Container(this.config, (cfg) => new ConsoleLogger(cfg, channel)); |
| 47 | + this.ctrl.logger.debug("VSCode Journal is starting"); |
| 48 | + |
| 49 | + if (this.ctrl.config.isDevelopmentModeEnabled()) { |
| 50 | + console.log("Development Mode for Journal extension is enabled, Tracing in Console and Output is activated."); |
| 51 | + } |
| 52 | + |
| 53 | + registerCommands(this.ctrl, context); |
| 54 | + registerCodeActions(this.ctrl, context); |
| 55 | + await this.registerSyntaxHighlighting(this.ctrl); |
| 56 | + registerCacheInvalidation(this.ctrl, context); |
| 57 | + |
| 58 | + console.timeEnd("startup"); |
| 59 | + console.log("VSCode-Journal extension was successfully initialized."); |
| 60 | + } catch (error) { |
| 61 | + console.error(error); |
| 62 | + throw error; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public getConfiguration(): Configuration { |
| 67 | + return this.ctrl.config; |
| 68 | + } |
| 69 | + |
| 70 | + public getJournalController(): Container { |
| 71 | + return this.ctrl; |
| 72 | + } |
| 73 | + |
| 74 | + public async registerSyntaxHighlighting(ctrl: Container): Promise<Container> { |
| 75 | + if (this.ctrl.config.isSyntaxHighlightingEnabled()) { |
| 76 | + return this.enableSyntaxHighlighting(ctrl); |
| 77 | + } else { |
| 78 | + return this.disableSyntaxHighlighting(ctrl); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public async disableSyntaxHighlighting(ctrl: Container): Promise<Container> { |
| 83 | + const tokenColorCustomizations = vscode.workspace.getConfiguration('editor.tokenColorCustomizations'); |
| 84 | + if (!tokenColorCustomizations.has("textMateRules")) { return ctrl; } |
| 85 | + |
| 86 | + const rules: TextMateRule[] = tokenColorCustomizations.get<TextMateRule[]>("textMateRules")!; |
| 87 | + const result: TextMateRule[] = rules.filter(rule => !rule.scope.includes("journal")); |
| 88 | + await vscode.workspace.getConfiguration().update("editor.tokenColorCustomizations", { "textMateRules": result }, vscode.ConfigurationTarget.Global); |
| 89 | + return ctrl; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Sets default syntax highlighting settings on startup, we try to differentiate between dark and light themes |
| 94 | + */ |
| 95 | + public async enableSyntaxHighlighting(ctrl: Container): Promise<Container> { |
| 96 | + const theme: string | undefined = vscode.workspace.getConfiguration().get<string>("workbench.colorTheme"); |
| 97 | + let style: string; |
| 98 | + if (isNullOrUndefined(theme) || theme!.search('Light') > -1) { style = "light"; } |
| 99 | + else if (theme!.search('High Contrast') > -1) { style = "high-contrast"; } |
| 100 | + else { style = "dark"; } |
| 101 | + |
| 102 | + const tokenColorCustomizations: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('editor.tokenColorCustomizations'); |
| 103 | + const rules: TextMateRule[] | undefined = tokenColorCustomizations.get<TextMateRule[]>("textMateRules"); |
| 104 | + |
| 105 | + if (isNullOrUndefined(rules) || rules!.length > 0) { |
| 106 | + return ctrl; |
| 107 | + } |
| 108 | + |
| 109 | + if (style.startsWith("high-contrast")) { return ctrl; } |
| 110 | + |
| 111 | + const ext: vscode.Extension<any> | undefined = vscode.extensions.getExtension("pajoma.vscode-journal"); |
| 112 | + if (isNullOrUndefined(ext)) { throw Error("Failed to load this extension"); } |
| 113 | + |
| 114 | + const colorConfigDir: string = Path.join(ext!.extensionPath, "res", "colors"); |
| 115 | + const rawData = await vscode.workspace.fs.readFile(vscode.Uri.file(Path.join(colorConfigDir, style + ".json"))); |
| 116 | + const data = Buffer.from(rawData).toString('utf-8'); |
| 117 | + |
| 118 | + // FIXME: this is a workaround, since we can't simply inject the textMateRules here (not registered configuration) |
| 119 | + const existingConfig = vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations'); |
| 120 | + const mutableExistingConfig = JSON.parse(JSON.stringify(existingConfig)); |
| 121 | + mutableExistingConfig.textMateRules = JSON.parse(data); |
| 122 | + await vscode.workspace.getConfiguration("editor").update("tokenColorCustomizations", mutableExistingConfig, vscode.ConfigurationTarget.Global); |
| 123 | + |
| 124 | + return ctrl; |
| 125 | + } |
| 126 | +} |
0 commit comments