Skip to content

Commit b268e50

Browse files
committed
Added Preserve Formating "Initial"
1 parent 86e31b6 commit b268e50

File tree

6 files changed

+742
-143
lines changed

6 files changed

+742
-143
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
- [ ] Key validation warn users of duplicate keys when added or renamed.
7070
- [ ] Diagnostics check translation files for issues and suggest quick fixes.
7171
- [ ] Option Auto-detect translation files within a project.
72-
- [ ] Maintain original JSON formatting (spaces, indentation) to avoid unnecessary git diffs.
72+
- [x] Maintain original JSON formatting (spaces, indentation) to avoid unnecessary git diffs.
7373
- [ ] Sync missing translations ensure consistency across languages.
7474
- [ ] AI-based contextual translation suggestions. It analyzes code context to give more accurate translations.
7575
- [ ] Display inline hover editor for translation keys in code.

src/commands/removeKey.ts

+27
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ async function removeKeyCommand(): Promise<void> {
4343

4444
new JsonParser(translationFile.filePath, settings.preserveFormating).removeKey(keyPath);
4545
}
46+
47+
// Update key in editor
48+
await editor.edit((editBuilder) => {
49+
const keys = keyPath.split('.');
50+
const text = editor.document.getText(editor.selection.isEmpty ? editor.document.lineAt(editor.selection.active).range : editor.selection);
51+
52+
// Detect the start and end quotes and replace them with the new key inside the same quotes
53+
const regex = new RegExp(`(['"])(${keyPath.replace(/^['"]|['"]$/g, '')})(['"])`, 'g');
54+
55+
// Preserve the quotes around the key
56+
const updatedText = text.replace(regex, (match, startQuote, oldKey, endQuote) => {
57+
if (keys.length === 1) {
58+
return `${startQuote}${endQuote}`;
59+
} else {
60+
const path = keys.slice(0, -1).join('.');
61+
const newFullKey = `${path}`;
62+
return `${startQuote}${newFullKey}${endQuote}`;
63+
}
64+
});
65+
66+
if (editor.selection.isEmpty) {
67+
const lineRange = editor.document.lineAt(editor.selection.active).range;
68+
editBuilder.replace(lineRange, updatedText);
69+
} else {
70+
editBuilder.replace(editor.selection, updatedText);
71+
}
72+
});
4673
}
4774

4875
export { removeKeyCommand };

0 commit comments

Comments
 (0)