-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
112 lines (89 loc) · 3.33 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const vscode = require("vscode");
let scapSections = [];
function replaceCommonSigns(content) {
return content
.replace(/->/g, '&dash_gt;')
.replace(/=>/g, '&eqgt;')
.replace(/<=/g, '<eq;')
.replace(/{{(.*?)}}/g, (match, p1) => {
return `{{${p1.replace(/</g, '<').replace(/>/g, '>')}}}`;
});
}
function undoReplaceCommonSigns(content) {
return content
.replace(/&dash_gt;/g, '->')
.replace(/&eqgt;/g, '=>')
.replace(/<eq;/g, '<=')
.replace(/{{(.*?)}}/g, (match, p1) => {
return `{{${p1.replace(/</g, '<').replace(/>/g, '>')}}}`;
});
}
function extractScapSections(bladeSource) {
scapSections = [];
let placeholderIndex = 0;
const scriptStyleRegex = /<(script|style)[\s\S]*?<\/\1>/gi;
const modifiedBladeSource = bladeSource.replace(scriptStyleRegex, (match) => {
const placeholder = `<!-- SCRIPT_PLACEHOLDER_${placeholderIndex++} -->`;
scapSections.push(match);
return placeholder;
});
return { modifiedBladeSource, scapSections };
}
function restoreScapSections(modifiedBladeSource) {
return scapSections.reduce((source, section, index) => {
const placeholder = `<!-- SCRIPT_PLACEHOLDER_${index} -->`;
return source.replace(placeholder, section);
}, modifiedBladeSource);
}
function wrapText(content) {
return content.replace(/(\s*)([^{}]+?)(\s*)(?={{|$)/g, (match, leadingSpace, staticText, trailingSpace) => {
staticText = staticText.trim().replace(/[',.]/g, '');
return staticText ? `${leadingSpace}{{ __('${staticText}') }}${trailingSpace}` : match;
});
}
function wrapTextNodesWithBladeDirective(bladeSource) {
if (!bladeSource) return '';
const bladeDirectiveRegex = /@\w+(?:\([^)]*\))?/g;
const parts = bladeSource.split(bladeDirectiveRegex);
const directives = [...bladeSource.matchAll(bladeDirectiveRegex)].map(match => match[0]);
return parts.reduce((result, part, index) => {
const wrappedPart = part.replace(/(>)([^<]+)(<)/g, (match, p1, p2, p3) => {
return p2.trim() ? p1 + wrapText(p2) + p3 : match;
});
return result + wrappedPart + (directives[index] || '');
}, '');
}
function activate(context) {
console.log('Congratulations, your extension "localizer" is now active!');
const disposable = vscode.commands.registerCommand("laravel.blade.localizer", () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
let content = editor.document.getText();
content = replaceCommonSigns(content);
const { modifiedBladeSource } = extractScapSections(content);
let replacedContents = wrapTextNodesWithBladeDirective(modifiedBladeSource);
replacedContents = undoReplaceCommonSigns(replacedContents);
replacedContents = restoreScapSections(replacedContents);
const edit = new vscode.WorkspaceEdit();
edit.replace(
editor.document.uri,
new vscode.Range(
editor.document.positionAt(0),
editor.document.positionAt(editor.document.getText().length)
),
replacedContents
);
vscode.workspace.applyEdit(edit).then(() => {
vscode.window.showInformationMessage("Content Replaced Successfully!");
});
} else {
vscode.window.showErrorMessage("No active text editor found.");
}
});
context.subscriptions.push(disposable);
}
function deactivate() { }
module.exports = {
activate,
deactivate,
};