Skip to content

Commit

Permalink
Merge pull request #1 from nidib/v0.0.5-alpha
Browse files Browse the repository at this point in the history
Releases v0.0.5-alpha
  • Loading branch information
nidib authored Jan 28, 2022
2 parents e74b5eb + 4f6f63e commit 4fdceea
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ out
dist
node_modules
.vscode-test/
.DS_Store
*.vsix
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog

## Version 0.0.5-alpha
* Easier configuration
* Supports multiple term languages
* On/Off extension toggle

## Version 0.0.1-alpha
* First release
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
### 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

- Example:
```json
{
"termPreview.absolutePathToProjectRoot": "",
"termPreview.innerPathToTermsRoot": "",
"termPreview.filePrefix": "",
"termPreview.language": "",
"termPreview.fileSuffix": ""
"termPreview.enabled": true,
"termPreview.absolutePathToTermRoot": "/Users/richard/big-project/terms/",
"termPreview.file": "terms.{{LANGUAGE}}.utf-8.inc",
"termPreview.languages": ["pt-br", "en-us"],
}
```
57 changes: 24 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
"displayName": "Term Preview",
"description": "Preview your project's international terms",
"publisher": "richard-bidin",
"author": {
"name": "Richard Bidin"
},
"repository": {
"type": "git",
"url": "https://github.com/nidib/term-preview"
},
"version": "0.0.1-alpha",
"bugs": {
"url": "https://github.com/nidib/term-preview/issues",
"email": "[email protected]"
},
"version": "0.0.5-alpha",
"engines": {
"vscode": "^1.63.0"
},
Expand All @@ -18,45 +25,29 @@
"configuration": {
"title": "Term Preview",
"properties": {
"termPreview.absolutePathToProjectRoot": {
"type": "string",
"markdownDescription": "The absolute path to your project root",
"default": ""
},
"termPreview.innerPathToTermsRoot": {
"type": "string",
"markdownDescription": "The path to the terms folder relative to the project root",
"default": ""
"termPreview.enabled": {
"order": 0,
"type": "boolean",
"markdownDescription": "Enables the term preview on hover",
"default": false
},
"termPreview.filePrefix": {
"termPreview.absolutePathToTermsRoot": {
"type": "string",
"markdownDescription": "The file prefix",
"markdownDescription": "The absolute path to your terms root",
"default": ""
},
"termPreview.language": {
"termPreview.file": {
"type": "string",
"markdownDescription": "Goes in between prefix, and suffix",
"default": "en-us"
},
"termPreview.fileSuffix": {
"type": "string",
"markdownDescription": "The file suffix including the extension",
"markdownDescription": "Your file name. `{{LANGUAGE}}` is the placeholder for the language",
"default": ""
},
"termPreview.fileSeparator": {
"type": "string",
"markdownDescription": "Goes in between prefix, language, and suffix",
"default": "."
},
"termPreview.showFlag": {
"type": "boolean",
"markdownDescription": "Whether or not to display the language flag",
"default": false
},
"termPreview.watchForChanges": {
"type": "boolean",
"markdownDescription": "Whether or not to keep watching for changes on your file",
"default": false
"termPreview.languages": {
"type":"array",
"items": {
"type": "string"
},
"markdownDescription": "The languages you want your terms translated to (must match file names)",
"default": ["en-us"]
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/@types/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import * as vscode from 'vscode';

export interface ITermHover extends vscode.HoverProvider {
getTerms: MapCallback<string>;
language: string;
showFlag: boolean;
getTranslationsByTerm: Callback<string, string[]>;
}

export interface ExtensionConfig {
language: string;
path: string | null;
showFlag: boolean;
watchForChanges: boolean;
enabled: boolean,
filePath: string;
languages: string[];
}

export type Map<T> = { [key: string]: T };

export type MapCallback<T> = () => Map<T>;
export type Callback<I, T> = (_arg0: I) => T;
67 changes: 35 additions & 32 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,87 @@ 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 getInitialConfig from '../utils/helpers/getConfig';
import TermHover from './termHover';

const APP_STATUS_BAR_ID = 'app-name_status-bar';

class App {
config: ExtensionConfig;
statusBarItem: vscode.StatusBarItem;
terms: Map<string>;
terms: Map<Map<string>>;

constructor() {
this.config = getInitialConfig();
this.statusBarItem = this.getInitialStatusBarItem();
this.terms = {};

this.getTerms = this.getTerms.bind(this);
this.handleFileChange = this.handleFileChange.bind(this);
this.getTranslationsByTerm = this.getTranslationsByTerm.bind(this);

this.statusBarItem.show();
}

getTerms() {
return this.terms;
getInitialStatusBarItem() {
return vscode.window.createStatusBarItem(APP_STATUS_BAR_ID, vscode.StatusBarAlignment.Right);
}

getInitialStatusBarItem() {
return vscode.window.createStatusBarItem('app-name_status-bar', vscode.StatusBarAlignment.Right);
getTranslationsByTerm(term: string): string[] {
const { languages } = this.config;

return languages.map(language => this.terms[language][term]);
}

getTermsFromFile(): Map<string> {
const { path } = this.config;
getTermsFromFile(path: string): Map<string> {
let terms: Map<string> = {};
let file: string;
let matchedFileTerms: IterableIterator<RegExpMatchArray>;

try {
file = fs.readFileSync(path as string, 'utf-8');
file = fs.readFileSync(path, 'utf-8');
matchedFileTerms = file.matchAll(termsfileParserRegex);

Array.from(matchedFileTerms).forEach(match => {
const term = match[1];
const value = match[2];

this.terms[term] = value;
terms[term] = value;
});
} catch (e) {
throw new Error('Could not find or parse the specified file');
} finally {
this.setStatusBarText('Terms file loaded!', 'Your terms file seems to be loaded and parsed. You can now hover over a term!', 'check');
} catch (_e) {
throw new Error(`Could not find or parse ${path}`);
}

return this.terms;
return terms;
}

setStatusBarText(text: string, tooltip:string, icon: string ) {
this.statusBarItem.tooltip = tooltip;
this.statusBarItem.text = `$(${icon}) ${text}`;
}

handleFileChange(): void {
this.terms = this.getTermsFromFile();
}

run(context: vscode.ExtensionContext) {
const { path, showFlag, language } = this.config;
const { enabled, languages } = this.config;
let files: string[];
let termHover, disposableHover;

if (path) {
this.terms = this.getTermsFromFile();
termHover = new TermHover(this.getTerms, showFlag, language);
if (!enabled) {
return;
}

if (this.config.watchForChanges) {
fs.watchFile(path, this.handleFileChange);
}
files = getFilePaths();

disposableHover = vscode.languages.registerHoverProvider('*', termHover);
files.forEach((file, index) => {
const language = languages[index];

context.subscriptions.push(disposableHover);
} else {
throw new Error('You must provide a the global and relative path to your terms file. Ctrl + , to set it up');
}
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);
}
}

Expand Down
56 changes: 24 additions & 32 deletions src/app/termHover.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
import * as vscode from 'vscode';
import { ITermHover, MapCallback } from '../@types/types';
import flags from '../utils/constants/flags';
import { Callback, ITermHover } from '../@types/types';
import { termRegex } from '../utils/constants/regexConstants';

class TermHover implements ITermHover {
getTerms;
showFlag;
language;

constructor(getTerms: MapCallback<string>, showFlag: boolean, language: string) {
this.getTerms = getTerms;
this.showFlag = showFlag;
this.language = language;
}

private getTranslation(translation: string): string {
if (this.showFlag) {
return `${flags[this.language]} ${translation}`;
}
getTranslationsByTerm;

return translation;
constructor(getTranslationsByTerm: Callback<string, string[]>) {
this.getTranslationsByTerm = getTranslationsByTerm;
}

provideHover(document: vscode.TextDocument, position: vscode.Position, _token: vscode.CancellationToken): vscode.ProviderResult<vscode.Hover> {
const range = document.getWordRangeAtPosition(position);
const word = document.getText(range);
let isTerm = termRegex.test(word);
let translation, markdownTranslation;
let terms;

if (isTerm) {
terms = this.getTerms();
translation = terms[word];

if (translation) {
translation = this.getTranslation(translation);
markdownTranslation = new vscode.MarkdownString(translation);
markdownTranslation.supportHtml = true;

return new vscode.Hover(markdownTranslation);
}
let translations: string[];
let markdownTranslations: vscode.MarkdownString[] = [];

if (!isTerm) {
return null;
}

translations = this.getTranslationsByTerm(word);

if (!translations.length) {
return null;
}

return null;
markdownTranslations = translations.map(translation => {
const curr = new vscode.MarkdownString(translation);

curr.supportHtml = true;

return curr;
});

return new vscode.Hover(markdownTranslations);
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/utils/constants/configDefaults.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const configDefaults = {
ABS_PATH: '',
REL_PATH: '',
FILE_PREFIX: '',
LANGUAGE: 'en-us',
FILE_SUFFIX: '',
FILE_SEPARATOR: '.',
SHOW_FLAG: false,
WATCH_FILES: true
ENABLED: false,
FILE: '',
LANGUAGES: ['en-us']
};

export default configDefaults;
1 change: 1 addition & 0 deletions src/utils/constants/fileNamePlaceholders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LANGUAGE = '{{LANGUAGE}}';
7 changes: 0 additions & 7 deletions src/utils/constants/flags.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/utils/helpers/fileHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LANGUAGE } from '../constants/fileNamePlaceholders';
import getInitialConfig from './getConfig';

export const getFilePaths = (): string[] => {
const { languages, filePath } = getInitialConfig();

return languages.map(language => filePath.replace(LANGUAGE, language));
}
Loading

0 comments on commit 4fdceea

Please sign in to comment.