From f4ed9d9e20247d00acb11c112d40a93272e816e1 Mon Sep 17 00:00:00 2001 From: Richard Bidin Date: Sat, 5 Feb 2022 16:55:46 -0300 Subject: [PATCH 1/2] Simplifies extension configuration --- README.md | 5 +-- package.json | 19 ++++++--- src/@types/types.ts | 1 + src/app/app.ts | 56 +++++++++++++++------------ src/extension.ts | 16 +++++++- src/utils/constants/configDefaults.ts | 2 +- src/utils/helpers/fileHelpers.ts | 2 +- src/utils/helpers/getConfig.ts | 2 +- 8 files changed, 65 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 98e6304..91d6565 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,11 @@ ### How to use it - Install it through VS Code extensions tab -- Manually configure it through the settings page or use the template below on your `settings.json` file +- Manually configure it through the settings page or use the template below in your `settings.json` file - Example: ```json { - "termPreview.enabled": true, - "termPreview.absolutePathToTermRoot": "/Users/richard/big-project/terms/", + "termPreview.absolutePathToTermsRoot": "/Users/richard/big-project/terms/", "termPreview.file": "terms.{{LANGUAGE}}.utf-8.inc", "termPreview.languages": ["pt-br", "en-us"], } diff --git a/package.json b/package.json index f75cf8a..dc5463c 100644 --- a/package.json +++ b/package.json @@ -19,17 +19,26 @@ "vscode": "^1.63.0" }, "categories": ["Other"], - "activationEvents": ["*"], + "activationEvents": [ + "onCommand:term-preview.loadTerms", + "onStartupFinished" + ], "main": "./out/extension.js", "contributes": { + "commands": [ + { + "command": "term-preview.loadTerms", + "title": "Load terms" + } + ], "configuration": { "title": "Term Preview", "properties": { "termPreview.enabled": { "order": 0, "type": "boolean", - "markdownDescription": "Enables the term preview on hover", - "default": false + "markdownDescription": "Enables the extension", + "default": true }, "termPreview.absolutePathToTermsRoot": { "type": "string", @@ -38,7 +47,7 @@ }, "termPreview.file": { "type": "string", - "markdownDescription": "Your file name. `{{LANGUAGE}}` is the placeholder for the language", + "markdownDescription": "Your file name. `{{LANGUAGE}}` will be replaced by each of the selected languages", "default": "" }, "termPreview.languages": { @@ -46,7 +55,7 @@ "items": { "type": "string" }, - "markdownDescription": "The languages you want your terms translated to (must match file names)", + "markdownDescription": "The languages you want your terms translated to (must match file name)", "default": ["en-us"] } } diff --git a/src/@types/types.ts b/src/@types/types.ts index 1c82b2f..2067c34 100644 --- a/src/@types/types.ts +++ b/src/@types/types.ts @@ -6,6 +6,7 @@ export interface ITermHover extends vscode.HoverProvider { export interface ExtensionConfig { enabled: boolean, + file: string, filePath: string; languages: string[]; } diff --git a/src/app/app.ts b/src/app/app.ts index 9b24050..72feef5 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -2,7 +2,7 @@ import * as fs from 'fs'; import * as vscode from 'vscode'; import { ExtensionConfig, Map } from '../@types/types'; import { termsfileParserRegex } from '../utils/constants/regexConstants'; -import { getFilePaths } from '../utils/helpers/fileHelpers'; +import { getFiles } from '../utils/helpers/fileHelpers'; import getInitialConfig from '../utils/helpers/getConfig'; import TermHover from './termHover'; @@ -19,14 +19,27 @@ class App { this.terms = {}; this.getTranslationsByTerm = this.getTranslationsByTerm.bind(this); + } - this.statusBarItem.show(); + hasMinimumConfigurationsSet(): boolean { + const { enabled, file } = this.config; + + if (!enabled) { + return false; + } + + if (!file) { + return false; + } + + return true; } getInitialStatusBarItem() { return vscode.window.createStatusBarItem(APP_STATUS_BAR_ID, vscode.StatusBarAlignment.Right); } + /* this method is used by its child as a callback */ getTranslationsByTerm(term: string): string[] { const { languages } = this.config; @@ -42,47 +55,40 @@ class App { file = fs.readFileSync(path, 'utf-8'); matchedFileTerms = file.matchAll(termsfileParserRegex); - Array.from(matchedFileTerms).forEach(match => { - const term = match[1]; - const value = match[2]; - - terms[term] = value; - }); - } catch (_e) { + Array.from(matchedFileTerms).forEach(match => terms[match[1]] = match[2]); + } catch (err) { throw new Error(`Could not find or parse ${path}`); } return terms; } - setStatusBarText(text: string, tooltip:string, icon: string ) { + setStatusBarText(text: string, tooltip:string, icon: string) { this.statusBarItem.tooltip = tooltip; this.statusBarItem.text = `$(${icon}) ${text}`; + + this.statusBarItem.show(); } - run(context: vscode.ExtensionContext) { - const { enabled, languages } = this.config; - let files: string[]; - let termHover, disposableHover; - - if (!enabled) { + run(): vscode.Disposable | undefined { + this.config = getInitialConfig(); + this.terms = {}; + + this.statusBarItem.hide(); + + if (!this.hasMinimumConfigurationsSet()) { return; } - files = getFilePaths(); - - files.forEach((file, index) => { - const language = languages[index]; + getFiles().forEach((file, index) => { + const language = this.config.languages[index]; this.terms[language] = this.getTermsFromFile(file); }); this.setStatusBarText('Terms file(s) loaded!', 'Your file(s) seem(s) to be loaded and parsed', 'check'); - - termHover = new TermHover(this.getTranslationsByTerm); - disposableHover = vscode.languages.registerHoverProvider('*', termHover); - - context.subscriptions.push(disposableHover); + + return vscode.languages.registerHoverProvider('*', new TermHover(this.getTranslationsByTerm)); } } diff --git a/src/extension.ts b/src/extension.ts index 371eab4..c6efdb9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,10 +1,22 @@ import * as vscode from 'vscode'; import App from './app/app'; -export function activate(context: vscode.ExtensionContext) { +export function activate(_context: vscode.ExtensionContext) { const app = new App(); + let disposable: vscode.Disposable | undefined; + let runApp = () => { + try { + disposable && disposable.dispose(); + disposable = app.run(); + } catch (err: any) { + vscode.window.showErrorMessage(err.message); + } + }; - app.run(context); + runApp(); + + vscode.commands.registerCommand('term-preview.loadTerms', runApp); + vscode.workspace.onDidChangeConfiguration(runApp); } export function deactivate() {} \ No newline at end of file diff --git a/src/utils/constants/configDefaults.ts b/src/utils/constants/configDefaults.ts index 81112f4..e24bd29 100644 --- a/src/utils/constants/configDefaults.ts +++ b/src/utils/constants/configDefaults.ts @@ -1,6 +1,6 @@ const configDefaults = { ABS_PATH: '', - ENABLED: false, + ENABLED: true, FILE: '', LANGUAGES: ['en-us'] }; diff --git a/src/utils/helpers/fileHelpers.ts b/src/utils/helpers/fileHelpers.ts index 05a5d15..f31c570 100644 --- a/src/utils/helpers/fileHelpers.ts +++ b/src/utils/helpers/fileHelpers.ts @@ -1,7 +1,7 @@ import { LANGUAGE } from '../constants/fileNamePlaceholders'; import getInitialConfig from './getConfig'; -export const getFilePaths = (): string[] => { +export function getFiles(): string[] { const { languages, filePath } = getInitialConfig(); return languages.map(language => filePath.replace(LANGUAGE, language)); diff --git a/src/utils/helpers/getConfig.ts b/src/utils/helpers/getConfig.ts index d18c34c..54ca7b2 100644 --- a/src/utils/helpers/getConfig.ts +++ b/src/utils/helpers/getConfig.ts @@ -11,7 +11,7 @@ function getInitialConfig(): ExtensionConfig { const enabled = appConfig.get('enabled', configDefaults.ENABLED); const filePath = nodePath.join(abs, file); - return { enabled, filePath, languages }; + return { enabled, filePath, languages, file }; } export default getInitialConfig; \ No newline at end of file From 6281530f214c807b8ee90a937ea2a7856917786e Mon Sep 17 00:00:00 2001 From: Richard Bidin Date: Sat, 5 Feb 2022 17:13:54 -0300 Subject: [PATCH 2/2] Releases v0.1.0-beta --- CHANGELOG.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1eea34..0bf4d1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Version 0.1.0-beta +* Simplifies configuration +* Update terms whenever the configuration is changed +* Added the command `Load terms` + ## Version 0.0.5-alpha * Easier configuration * Supports multiple term languages diff --git a/package.json b/package.json index dc5463c..9b071bf 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "url": "https://github.com/nidib/term-preview/issues", "email": "nidibdev@outlook.com" }, - "version": "0.0.5-alpha", + "version": "0.1.0-beta", "engines": { "vscode": "^1.63.0" },