Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b47e12c

Browse files
author
Damian Sznajder
committed
fix: typescript dynamic generate
1 parent 7a7f32a commit b47e12c

File tree

10 files changed

+1311
-106
lines changed

10 files changed

+1311
-106
lines changed

‎package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
}
4040
},
4141
"activationEvents": [
42+
"onLanguage:typescript",
43+
"onLanguage:javascript",
4244
"onCommand:reactSnippets.search"
4345
],
4446
"contributes": {
@@ -89,6 +91,11 @@
8991
"markdownDescription": "Controls how many spaces snippets will have.\nOnly applies when `#reactSnippets.settings.prettierEnabled#` is disabled",
9092
"default": 2
9193
},
94+
"reactSnippets.settings.languageScopes": {
95+
"type": "string",
96+
"markdownDescription": "defines the language scopes for which the snippets will be available.\nUse comma separated values.\nFor example: `typescript,typescriptreact,javascript,javascriptreact`",
97+
"default": "typescript,typescriptreact,javascript,javascriptreact"
98+
},
9299
"reactSnippets.settings.typescriptComponentPropsStatePrefix": {
93100
"type": "string",
94101
"markdownDescription": "Controls which prefix for typescript snippets should use for props/state.",

‎src/generateSnippets.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { writeFile } from 'fs';
2-
import { workspace } from 'vscode';
32

4-
import { ExtensionSettings, replaceSnippetPlaceholders } from './helpers';
3+
import {
4+
extensionConfig,
5+
formatSnippet,
6+
replaceSnippetPlaceholders,
7+
} from './helpers';
58
import componentsSnippets, {
69
ComponentsSnippet,
710
} from './sourceSnippets/components';
@@ -47,11 +50,10 @@ export type Snippets = {
4750
};
4851

4952
const getSnippets = () => {
50-
const config = workspace.getConfiguration(
51-
'reactSnippets',
52-
) as unknown as ExtensionSettings;
53+
const { typescript, languageScopes } = extensionConfig();
5354

5455
const snippets = [
56+
...(typescript ? typescriptSnippets : []),
5557
...componentsSnippets,
5658
...consoleSnippets,
5759
...hooksSnippets,
@@ -60,13 +62,17 @@ const getSnippets = () => {
6062
...reactNativeSnippets,
6163
...reduxSnippets,
6264
...testsSnippets,
63-
...(config.typescript ? typescriptSnippets : []),
6465
].reduce((acc, snippet) => {
65-
acc[snippet.key] = snippet;
66+
const snippetBody =
67+
typeof snippet.body === 'string' ? snippet.body : snippet.body.join('\n');
68+
acc[snippet.key] = Object.assign(snippet, {
69+
scope: languageScopes,
70+
body: formatSnippet(snippetBody).split('\n'),
71+
});
6672
return acc;
6773
}, {} as Snippets);
6874

69-
return replaceSnippetPlaceholders(JSON.stringify(snippets, null, 2),config);
75+
return replaceSnippetPlaceholders(JSON.stringify(snippets, null, 2));
7076
};
7177

7278
export const generateSnippets = () =>

‎src/helpers.ts

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,40 @@ import { workspace } from 'vscode';
44
import { SnippetPlaceholders } from './types';
55

66
export type ExtensionSettings = {
7+
languageScopes: string;
78
prettierEnabled: boolean;
89
semiColons: boolean;
910
importReactOnTop: boolean;
10-
quotes: boolean;
11+
singleQuote: boolean;
1112
typescript: boolean;
1213
tabWidth: number;
1314
typescriptComponentPropsStatePrefix: 'type' | 'interface';
1415
};
1516

16-
export const replaceSnippetPlaceholders = (
17-
snippetString: string,
18-
extensionConfig: ExtensionSettings,
19-
) =>
17+
let prettierConfig: prettier.Options | null;
18+
prettier
19+
.resolveConfig('', { editorconfig: true })
20+
.then((config) => (prettierConfig = config));
21+
22+
export const extensionConfig = () =>
23+
workspace.getConfiguration(
24+
'reactSnippets.settings',
25+
) as unknown as ExtensionSettings;
26+
27+
const getPrettierConfig = (): Options => {
28+
const { semiColons, singleQuote, tabWidth, prettierEnabled } =
29+
extensionConfig();
30+
31+
return {
32+
parser: 'typescript',
33+
semi: semiColons,
34+
singleQuote,
35+
tabWidth,
36+
...(prettierEnabled && prettierConfig),
37+
};
38+
};
39+
40+
export const replaceSnippetPlaceholders = (snippetString: string) =>
2041
String(snippetString)
2142
.replace(
2243
new RegExp(SnippetPlaceholders.FileName, 'g'),
@@ -25,11 +46,7 @@ export const replaceSnippetPlaceholders = (
2546
.replace(new RegExp(SnippetPlaceholders.FirstTab, 'g'), '${1:first}')
2647
.replace(new RegExp(SnippetPlaceholders.SecondTab, 'g'), '${2:second}')
2748
.replace(new RegExp(SnippetPlaceholders.ThirdTab, 'g'), '${3:third}')
28-
.replace(new RegExp(SnippetPlaceholders.LastTab, 'g'), '0ドル')
29-
.replace(
30-
new RegExp(SnippetPlaceholders.TypeInterface, 'g'),
31-
extensionConfig.typescriptComponentPropsStatePrefix || 'type',
32-
);
49+
.replace(new RegExp(SnippetPlaceholders.LastTab, 'g'), '0ドル');
3350

3451
export const revertSnippetPlaceholders = (snippetString: string) =>
3552
String(snippetString)
@@ -42,37 +59,15 @@ export const revertSnippetPlaceholders = (snippetString: string) =>
4259
.replace(new RegExp(/\${3:third}/, 'g'), SnippetPlaceholders.ThirdTab)
4360
.replace(new RegExp(/\$0/, 'g'), SnippetPlaceholders.LastTab);
4461

45-
let prettierConfig: prettier.Options | null;
46-
47-
prettier
48-
.resolveConfig('', { editorconfig: true })
49-
.then((config) => (prettierConfig = config));
50-
51-
export const getPrettierConfig = (): Options => {
52-
const settings = workspace.getConfiguration(
53-
'reactSnippets.settings',
54-
) as unknown as ExtensionSettings;
55-
56-
return {
57-
semi: settings.semiColons,
58-
singleQuote: settings.quotes,
59-
tabWidth: settings.tabWidth,
60-
parser: 'typescript',
61-
...(settings.prettierEnabled && prettierConfig),
62-
};
62+
export const formatSnippet = (string: string) => {
63+
const prettierConfig = getPrettierConfig();
64+
return prettier.format(string, prettierConfig);
6365
};
6466

65-
export const parseSnippet = (body: string[]) => {
66-
const workspaceConfig = workspace.getConfiguration(
67-
'reactSnippets',
68-
) as unknown as ExtensionSettings;
67+
export const parseSnippet = (body: string | string[]) => {
6968
const snippetBody = typeof body === 'string' ? body : body.join('\n');
7069

71-
const prettierConfig = getPrettierConfig();
72-
const parsedBody = prettier.format(
73-
revertSnippetPlaceholders(snippetBody),
74-
prettierConfig,
70+
return replaceSnippetPlaceholders(
71+
formatSnippet(revertSnippetPlaceholders(snippetBody)),
7572
);
76-
77-
return replaceSnippetPlaceholders(parsedBody, workspaceConfig);
7873
};

‎src/index.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,19 @@ import snippetSearch from './snippetSearch';
1212
const showRestartMessage = async ({
1313
affectsConfiguration,
1414
}: ConfigurationChangeEvent) => {
15-
let timeoutId: NodeJS.Timeout | undefined;
1615
if (affectsConfiguration('reactSnippets')) {
1716
await generateSnippets();
18-
timeoutId && clearTimeout(timeoutId);
19-
timeoutId = setTimeout(() => {
20-
window
21-
.showWarningMessage(
22-
'React Snippets: Please restart VS Code to apply snippet formatting changes',
23-
'Restart VS Code',
24-
'Ignore',
25-
)
26-
.then((action?: string) => {
27-
if (action === 'Restart VS Code') {
28-
commands.executeCommand('workbench.action.reloadWindow');
29-
}
30-
});
31-
}, 3000);
17+
window
18+
.showWarningMessage(
19+
'React Snippets: Please restart VS Code to apply snippet formatting changes',
20+
'Restart VS Code',
21+
'Ignore',
22+
)
23+
.then((action?: string) => {
24+
if (action === 'Restart VS Code') {
25+
commands.executeCommand('workbench.action.reloadWindow');
26+
}
27+
});
3228
}
3329
};
3430

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /