Skip to content

Commit

Permalink
Merge pull request #2 from nidib/v0.1.0-beta
Browse files Browse the repository at this point in the history
Releases v0.1.0-beta
  • Loading branch information
nidib authored Feb 5, 2022
2 parents 4fdceea + 6281530 commit 9a50e8a
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 39 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
Expand Down
21 changes: 15 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,31 @@
"url": "https://github.com/nidib/term-preview/issues",
"email": "[email protected]"
},
"version": "0.0.5-alpha",
"version": "0.1.0-beta",
"engines": {
"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",
Expand All @@ -38,15 +47,15 @@
},
"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": {
"type":"array",
"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"]
}
}
Expand Down
1 change: 1 addition & 0 deletions src/@types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface ITermHover extends vscode.HoverProvider {

export interface ExtensionConfig {
enabled: boolean,
file: string,
filePath: string;
languages: string[];
}
Expand Down
56 changes: 31 additions & 25 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;

Expand All @@ -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));
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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() {}
2 changes: 1 addition & 1 deletion src/utils/constants/configDefaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const configDefaults = {
ABS_PATH: '',
ENABLED: false,
ENABLED: true,
FILE: '',
LANGUAGES: ['en-us']
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/helpers/fileHelpers.ts
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/utils/helpers/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 9a50e8a

Please sign in to comment.