Skip to content

Commit c424bef

Browse files
committed
Options parsing moved to a dedicated file
1 parent 688b7a7 commit c424bef

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

src/extension.ts

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import * as fs from 'fs/promises';
22
import * as vscode from 'vscode';
3-
import { DEST_PATH, EXT_ID, ISSUES_URL, IndentationStyle, Options, TEMPLATES_BASEPATH } from './model';
3+
import { DEST_PATH, EXT_ID, ISSUES_URL, Replaces, TEMPLATES_BASEPATH } from './model';
4+
import { getReplaces } from './options';
45

56
const TAG_REGEX = /\%(\w+)\%/gm;
6-
const PRIVATE_REGEX_KEY = 'PRIVATE';
7-
const LINE_BREAK_REGEX_KEY = 'LINE_BREAK';
8-
const TAB_REGEX_KEY = 'TAB';
9-
10-
const files = ['classes', 'methods', 'calls'];
7+
const TEMPLATES = ['classes', 'methods', 'calls'];
118

129
export function activate(context: vscode.ExtensionContext) {
1310
const disposable = vscode.workspace.onDidChangeConfiguration(e => onConfigurationChanged(context, e));
@@ -18,7 +15,7 @@ function onConfigurationChanged(context: vscode.ExtensionContext, e: vscode.Conf
1815
if (!e.affectsConfiguration(EXT_ID)) { return; }
1916
const conf = vscode.workspace.getConfiguration(EXT_ID);
2017

21-
createSnippets(context, getOptions(conf))
18+
createSnippets(context, getReplaces(conf))
2219
.then(content => saveSnippets(context, content))
2320
.then(() => {
2421
vscode.window.showInformationMessage('Restart VSCode to apply the snippets', 'Restart')
@@ -27,48 +24,19 @@ function onConfigurationChanged(context: vscode.ExtensionContext, e: vscode.Conf
2724
.catch(showError);
2825
}
2926

30-
async function createSnippets(context: vscode.ExtensionContext, options: Options) {
31-
const replace = parseOptions(options);
32-
33-
const contents: string[] = await Promise.all(files.map(file =>
34-
replaceOnTemplate(context.asAbsolutePath(`${TEMPLATES_BASEPATH}/${file}.json`), replace)
27+
async function createSnippets(context: vscode.ExtensionContext, replaces: Replaces) {
28+
const contents: string[] = await Promise.all(TEMPLATES.map(file =>
29+
replaceOnTemplate(context.asAbsolutePath(`${TEMPLATES_BASEPATH}/${file}.json`), replaces)
3530
));
3631

3732
let result = {};
3833
contents.forEach(content => result = Object.assign(result, JSON.parse(content)));
3934
return JSON.stringify(result, null, '\t');
4035
}
4136

42-
function getOptions(conf: vscode.WorkspaceConfiguration) {
43-
return {
44-
style: conf.get('style') as IndentationStyle,
45-
usePrivateKeyword: conf.get('usePrivateKeyword') as boolean
46-
};
47-
}
48-
49-
function parseOptions(options: Options) {
50-
const result: Record<string, string> = {};
51-
52-
// private keyword
53-
result[PRIVATE_REGEX_KEY] = options.usePrivateKeyword ? 'private ' : '';
54-
55-
// indentation style
56-
if (options.style === 'allman') {
57-
result[LINE_BREAK_REGEX_KEY] = '",\n\t\t\t"';
58-
result[TAB_REGEX_KEY] = '\\t';
59-
} else if (options.style === 'kr') {
60-
result[LINE_BREAK_REGEX_KEY] = ' ';
61-
result[TAB_REGEX_KEY] = '';
62-
} else {
63-
throw new Error(`Invalid style: ${options.style}`);
64-
}
65-
66-
return result;
67-
}
68-
69-
async function replaceOnTemplate(filePath: string, replace: Record<string, string>) {
37+
async function replaceOnTemplate(filePath: string, replaces: Replaces) {
7038
const content = await fs.readFile(filePath, { encoding: 'utf-8' });
71-
return content.replace(TAG_REGEX, (_match: string, p1: string) => replace[p1] ?? p1);
39+
return content.replace(TAG_REGEX, (_match: string, p1: string) => replaces[p1 as keyof Replaces] ?? p1);
7240
}
7341

7442
async function saveSnippets(context: vscode.ExtensionContext, content: string) {

src/model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ export const TEMPLATES_BASEPATH = 'templates/';
33
export const DEST_PATH = 'snippets/snippets.json';
44
export const ISSUES_URL = 'https://github.com/kleber-swf/vscode-unity-code-snippets/issues';
55

6-
76
export type IndentationStyle = 'kr' | 'allman';
87

8+
export type ReplaceType = 'PRIVATE' | 'LINE_BREAK' | 'TAB';
9+
export type Replaces = Record<ReplaceType, string>;
10+
911
export interface Options {
1012
style: IndentationStyle;
1113
usePrivateKeyword: boolean;

src/options.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as vscode from 'vscode';
2+
import { IndentationStyle, Options, Replaces } from './model';
3+
4+
export function getReplaces(conf: vscode.WorkspaceConfiguration): Replaces {
5+
const options = getOptions(conf);
6+
return parseOptions(options);
7+
}
8+
9+
function getOptions(conf: vscode.WorkspaceConfiguration) {
10+
return {
11+
style: conf.get('style') as IndentationStyle,
12+
usePrivateKeyword: conf.get('usePrivateKeyword') as boolean
13+
};
14+
}
15+
16+
function parseOptions(options: Options): Replaces {
17+
const result: Replaces = {} as any;
18+
19+
// private keyword
20+
result.PRIVATE = options.usePrivateKeyword ? 'private ' : '';
21+
22+
// indentation style
23+
if (options.style === 'allman') {
24+
result.LINE_BREAK = '",\n\t\t\t"';
25+
result.TAB = '\\t';
26+
} else if (options.style === 'kr') {
27+
result.LINE_BREAK = ' ';
28+
result.TAB = '';
29+
} else {
30+
throw new Error(`Invalid style: ${options.style}`);
31+
}
32+
33+
return result;
34+
}

0 commit comments

Comments
 (0)