Skip to content

Commit 521fbd8

Browse files
committed
refactor: space replace method
1 parent 6b68de3 commit 521fbd8

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/blade/bladeSpacer.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,26 @@ const charsForChange = (doc: TextDocument, change: TextDocumentContentChangeEven
4343

4444
const spaceReplace = (editor: TextEditor, tagType: number, ranges: Array<Range>) => {
4545
const twigEnabled = config('bladeSpacer.enableTwig', true);
46-
47-
if (tagType === TAG_DOUBLE) {
48-
return editor.insertSnippet(new SnippetString('{{ ${1:${TM_SELECTED_TEXT/[{}]//g}} }}$0'), ranges);
49-
} else if (tagType === TAG_UNESCAPED) {
50-
return editor.insertSnippet(new SnippetString('{!! ${1:${TM_SELECTED_TEXT/[{} !]//g}} !!}$0'), ranges);
51-
} else if (tagType === TAG_COMMENT) {
52-
return editor.insertSnippet(new SnippetString('{{-- ${1:${TM_SELECTED_TEXT/(--)|[{} ]//g}} --}}$0'), ranges);
53-
} else if (twigEnabled && tagType === TAG_TWIG_PER) {
54-
return editor.insertSnippet(new SnippetString('{% $1 %}$0'), ranges);
55-
} else if (twigEnabled && tagType === TAG_TWIG_HASH) {
56-
return editor.insertSnippet(new SnippetString('{# $1 #}$0'), ranges);
46+
47+
const snippets: Record<number, string> = {
48+
[TAG_DOUBLE]: '{{ ${1:${TM_SELECTED_TEXT/[{}]//g}} }}$0',
49+
[TAG_UNESCAPED]: '{!! ${1:${TM_SELECTED_TEXT/[{} !]//g}} !!}$0',
50+
[TAG_COMMENT]: '{{-- ${1:${TM_SELECTED_TEXT/(--)|[{} ]//g}} --}}$0',
51+
[TAG_TWIG_PER]: '{% $1 %}$0',
52+
[TAG_TWIG_HASH]: '{# $1 #}$0'
53+
};
54+
55+
if (!twigEnabled && (tagType === TAG_TWIG_PER || tagType === TAG_TWIG_HASH)) {
56+
return;
57+
}
58+
59+
const snippet = snippets[tagType];
60+
if (snippet) {
61+
return editor.insertSnippet(new SnippetString(snippet), ranges);
5762
}
5863
};
5964

65+
6066
export const bladeSpacer = async (e: TextDocumentChangeEvent, editor?: TextEditor) => {
6167
if (!editor) {
6268
return;

0 commit comments

Comments
 (0)