Skip to content

Commit

Permalink
fix: #1
Browse files Browse the repository at this point in the history
  • Loading branch information
HYP3R00T committed Jul 18, 2024
1 parent b9f141a commit e73fa17
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ export function activate(context: vscode.ExtensionContext) {
console.log(`Looking for settings.json at: ${userSettingsPath}`);

if (fs.existsSync(userSettingsPath)) {
const settings = JSON.parse(fs.readFileSync(userSettingsPath, 'utf-8'));
const rawSettings = fs.readFileSync(userSettingsPath, 'utf-8');
const cleanedSettings = preprocessJSON(rawSettings);
const settings = JSON.parse(cleanedSettings);
const categorizedSettings = categorizeSettings(settings);
fs.writeFileSync(userSettingsPath, JSON.stringify(categorizedSettings, null, 2));
vscode.window.showInformationMessage('Global settings.json organized!');
} else {
vscode.window.showErrorMessage('Global settings.json not found!');
}
});

let settings_organizer_local = vscode.commands.registerCommand('extension.settings-organizer-local', () => {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
Expand All @@ -31,7 +34,9 @@ export function activate(context: vscode.ExtensionContext) {
console.log(`Looking for settings.json at: ${userSettingsPath}`);

if (fs.existsSync(userSettingsPath)) {
const settings = JSON.parse(fs.readFileSync(userSettingsPath, 'utf-8'));
const rawSettings = fs.readFileSync(userSettingsPath, 'utf-8');
const cleanedSettings = preprocessJSON(rawSettings);
const settings = JSON.parse(cleanedSettings);
const categorizedSettings = categorizeSettings(settings);
fs.writeFileSync(userSettingsPath, JSON.stringify(categorizedSettings, null, 2));
vscode.window.showInformationMessage('Local settings.json organized!');
Expand All @@ -46,6 +51,14 @@ export function activate(context: vscode.ExtensionContext) {

export function deactivate() { }

function preprocessJSON(jsonString: string): string {
// Remove comments
jsonString = jsonString.replace(/\/\/.*$/gm, '');
// Remove trailing commas
jsonString = jsonString.replace(/,(\s*[}\]])/g, '$1');
return jsonString;
}

function categorizeSettings(settings: any) {
const sortedSettings: { [key: string]: any } = {};
const categoriesOrder = [
Expand Down

0 comments on commit e73fa17

Please sign in to comment.