Skip to content

Commit 783fbc2

Browse files
author
Damian Sznajder
committed
chore: add importReactOnTop setting handle
1 parent 6bc8ec9 commit 783fbc2

File tree

4 files changed

+91
-41
lines changed

4 files changed

+91
-41
lines changed

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "es7-react-js-snippets",
33
"displayName": "ES7+ React/Redux/React-Native snippets",
44
"description": "Extensions for React, React-Native and Redux in JS/TS with ES7+ syntax. Customizable. Built-in integration with prettier.",
5-
"version": "4.0.3",
5+
"version": "4.0.4",
66
"publisher": "dsznajder",
77
"icon": "images/logo.png",
88
"browser": "./lib/index.js",

‎src/generateSnippets.ts

+5-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { writeFile } from 'fs';
22

33
import {
44
extensionConfig,
5-
formatSnippet,
5+
parseSnippetToBody,
66
replaceSnippetPlaceholders,
77
} from './helpers';
88
import componentsSnippets, {
@@ -52,19 +52,6 @@ export type Snippets = {
5252
[key in SnippetKeys]: Snippet;
5353
};
5454

55-
// This is array of prefixes which are currently skipped because of syntax format issues
56-
const skippedSnippets = [
57-
'pge',
58-
'pse',
59-
'gdsfp',
60-
'gsbu',
61-
'scu',
62-
'cwun',
63-
'cdm',
64-
'cdup',
65-
'rconst',
66-
];
67-
6855
const getSnippets = () => {
6956
const { typescript, languageScopes } = extensionConfig();
7057

@@ -80,16 +67,10 @@ const getSnippets = () => {
8067
...testsSnippets,
8168
...othersSnippets,
8269
].reduce((acc, snippet) => {
83-
const snippetBody =
84-
typeof snippet.body === 'string' ? snippet.body : snippet.body.join('\n');
85-
const formattedSnippet = skippedSnippets.includes(snippet.prefix)
86-
? snippetBody
87-
: formatSnippet(snippetBody).split('\n');
88-
89-
const body =
90-
snippet.body.length === 1 ? formattedSnippet[0] : formattedSnippet;
91-
92-
acc[snippet.key] = Object.assign(snippet, { body, scope: languageScopes });
70+
acc[snippet.key] = Object.assign(snippet, {
71+
body: parseSnippetToBody(snippet),
72+
scope: languageScopes,
73+
});
9374
return acc;
9475
}, {} as Snippets);
9576

‎src/helpers.ts

+71-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import prettier, { Options } from 'prettier';
22
import { workspace } from 'vscode';
33

4+
import { Snippet } from './generateSnippets';
45
import { SnippetPlaceholders } from './types';
56

67
export type ExtensionSettings = {
@@ -37,6 +38,11 @@ const getPrettierConfig = (): Options => {
3738
};
3839
};
3940

41+
const formatSnippet = (string: string) => {
42+
const prettierConfig = getPrettierConfig();
43+
return prettier.format(string, prettierConfig);
44+
};
45+
4046
export const replaceSnippetPlaceholders = (snippetString: string) =>
4147
String(snippetString)
4248
.replace(
@@ -59,15 +65,75 @@ export const revertSnippetPlaceholders = (snippetString: string) =>
5965
.replace(new RegExp(/\${3:third}/, 'g'), SnippetPlaceholders.ThirdTab)
6066
.replace(new RegExp(/\$0/, 'g'), SnippetPlaceholders.LastTab);
6167

62-
export const formatSnippet = (string: string) => {
63-
const prettierConfig = getPrettierConfig();
64-
return prettier.format(string, prettierConfig);
65-
};
66-
6768
export const parseSnippet = (body: string | string[]) => {
6869
const snippetBody = typeof body === 'string' ? body : body.join('\n');
6970

7071
return replaceSnippetPlaceholders(
7172
formatSnippet(revertSnippetPlaceholders(snippetBody)),
7273
);
7374
};
75+
76+
// This is array of prefixes which are currently skipped because of syntax format issues
77+
const skippedSnippets = [
78+
'pge',
79+
'pse',
80+
'gdsfp',
81+
'gsbu',
82+
'scu',
83+
'cwun',
84+
'cdm',
85+
'cdup',
86+
'rconst',
87+
];
88+
89+
const withReactImport = [
90+
'rfce',
91+
'rfc',
92+
'rfcp',
93+
'rafce',
94+
'rafc',
95+
'rafcp',
96+
'rnfe',
97+
'rnfes',
98+
'rnf',
99+
'rnfs',
100+
'stest',
101+
'sntest',
102+
'srtest',
103+
'snrtest',
104+
'hocredux',
105+
'hoc',
106+
];
107+
108+
const replaceOrRemoveReactImport = (snippetBody: string[]) => {
109+
const reactImportIndex = snippetBody.findIndex((line) =>
110+
line.match(new RegExp(/import React/, 'g')),
111+
);
112+
113+
if (reactImportIndex !== -1) {
114+
const line = snippetBody[reactImportIndex];
115+
snippetBody[reactImportIndex] = line
116+
.replace(new RegExp(/^import React .*$/, 'g'), '')
117+
.replace(new RegExp(/^import React, /, 'g'), 'import ');
118+
}
119+
120+
return snippetBody.join('\n');
121+
};
122+
123+
export const parseSnippetToBody = (snippet: Snippet) => {
124+
const { importReactOnTop } = extensionConfig();
125+
const body =
126+
typeof snippet.body === 'string' ? snippet.body : snippet.body.join('\n');
127+
128+
const snippetBody = importReactOnTop
129+
? body
130+
: withReactImport.includes(snippet.prefix)
131+
? replaceOrRemoveReactImport(snippet.body)
132+
: body;
133+
134+
const formattedSnippet = skippedSnippets.includes(snippet.prefix)
135+
? snippetBody
136+
: formatSnippet(snippetBody).split('\n');
137+
138+
return snippet.body.length === 1 ? formattedSnippet[0] : formattedSnippet;
139+
};

‎src/index.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
commands,
33
ConfigurationChangeEvent,
44
ExtensionContext,
5+
window,
56
workspace,
67
} from 'vscode';
78

@@ -13,17 +14,19 @@ const showRestartMessage = async ({
1314
}: ConfigurationChangeEvent) => {
1415
if (affectsConfiguration('reactSnippets')) {
1516
await generateSnippets();
16-
// window
17-
// .showWarningMessage(
18-
// 'React Snippets: Please restart VS Code to apply snippet formatting changes',
19-
// 'Restart VS Code',
20-
// 'Ignore',
21-
// )
22-
// .then((action?: string) => {
23-
// if (action === 'Restart VS Code') {
24-
// commands.executeCommand('workbench.action.reloadWindow');
25-
// }
26-
// });
17+
setTimeout(() => {
18+
window
19+
.showWarningMessage(
20+
'React Snippets: Please restart VS Code to apply snippet formatting changes',
21+
'Restart VS Code',
22+
'Ignore',
23+
)
24+
.then((action?: string) => {
25+
if (action === 'Restart VS Code') {
26+
commands.executeCommand('workbench.action.reloadWindow');
27+
}
28+
});
29+
}, 1000);
2730
}
2831
};
2932

0 commit comments

Comments
 (0)