11import * as fs from 'fs/promises' ;
22import * 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
56const 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
129export 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
7442async function saveSnippets ( context : vscode . ExtensionContext , content : string ) {
0 commit comments