Skip to content

Commit

Permalink
comment support enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
HYP3R00T committed Aug 10, 2024
1 parent 6d80050 commit 11626d0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
59 changes: 57 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@
"test": "vscode-test"
},
"devDependencies": {
"@types/vscode": "^1.91.0",
"@types/mocha": "^10.0.7",
"@types/node": "20.x",
"@types/vscode": "^1.91.0",
"@typescript-eslint/eslint-plugin": "^7.14.1",
"@typescript-eslint/parser": "^7.11.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5",
"@vscode/test-cli": "^0.0.9",
"@vscode/test-electron": "^2.4.0"
"@vscode/test-electron": "^2.4.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5"
},
"dependencies": {
"comment-json": "^4.2.4"
}
}
21 changes: 7 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import * as cjson from "comment-json";

export function activate(context: vscode.ExtensionContext) {
let settings_organizer_global = vscode.commands.registerCommand(
Expand Down Expand Up @@ -41,12 +42,12 @@ export function activate(context: vscode.ExtensionContext) {

if (fs.existsSync(userSettingsPath)) {
const rawSettings = fs.readFileSync(userSettingsPath, "utf-8");
const cleanedSettings = preprocessJSON(rawSettings);
const settings = JSON.parse(cleanedSettings);
const settings = cjson.parse(rawSettings);
console.log(settings);
const categorizedSettings = categorizeSettings(settings);
fs.writeFileSync(
userSettingsPath,
JSON.stringify(categorizedSettings, null, 2)
cjson.stringify(categorizedSettings, null, 2)
);
vscode.window.showInformationMessage("Global settings.json organized!");
} else {
Expand Down Expand Up @@ -74,12 +75,12 @@ export function activate(context: vscode.ExtensionContext) {

if (fs.existsSync(userSettingsPath)) {
const rawSettings = fs.readFileSync(userSettingsPath, "utf-8");
const cleanedSettings = preprocessJSON(rawSettings);
const settings = JSON.parse(cleanedSettings);
const settings = cjson.parse(rawSettings);
console.log(settings);
const categorizedSettings = categorizeSettings(settings);
fs.writeFileSync(
userSettingsPath,
JSON.stringify(categorizedSettings, null, 2)
cjson.stringify(categorizedSettings, null, 2)
);
vscode.window.showInformationMessage("Local settings.json organized!");
} else {
Expand All @@ -94,14 +95,6 @@ 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 11626d0

Please sign in to comment.