Skip to content

Commit

Permalink
allow comments in config.json (#1470)
Browse files Browse the repository at this point in the history
* ✨ comments in config.json

* ✨ file association for .jsonc
  • Loading branch information
sestinj authored Jun 11, 2024
1 parent ecd7a81 commit d9ff68c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 55 deletions.
5 changes: 3 additions & 2 deletions core/config/load.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as JSONC from "comment-json";
import * as fs from "fs";
import path from "path";
import {
Expand Down Expand Up @@ -57,7 +58,7 @@ const { execSync } = require("child_process");

function resolveSerializedConfig(filepath: string): SerializedContinueConfig {
let content = fs.readFileSync(filepath, "utf8");
const config = JSON.parse(content) as SerializedContinueConfig;
const config = JSONC.parse(content) as unknown as SerializedContinueConfig;
if (config.env && Array.isArray(config.env)) {
const env = {
...process.env,
Expand All @@ -74,7 +75,7 @@ function resolveSerializedConfig(filepath: string): SerializedContinueConfig {
});
}

return JSON.parse(content);
return JSONC.parse(content) as unknown as SerializedContinueConfig;
}

const configMergeKeys = {
Expand Down
25 changes: 10 additions & 15 deletions core/config/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { readFileSync, writeFileSync } from "fs";
import { ModelDescription } from "../index.js";
import { editConfigJson, getConfigJsonPath } from "../util/paths.js";
import { editConfigJson } from "../util/paths.js";

function stringify(obj: any, indentation?: number): string {
return JSON.stringify(
Expand All @@ -13,21 +12,17 @@ function stringify(obj: any, indentation?: number): string {
}

export function addModel(model: ModelDescription) {
const config = readFileSync(getConfigJsonPath(), "utf8");
const configJson = JSON.parse(config);
editConfigJson((config) => {
if (config.models?.some((m: any) => stringify(m) === stringify(model))) {
return config;
}
if (config.models?.some((m: any) => m?.title === model.title)) {
model.title = `${model.title} (1)`;
}

// De-duplicate
if (configJson.models?.some((m: any) => stringify(m) === stringify(model))) {
config.models.push(model);
return config;
}
if (configJson.models?.some((m: any) => m?.title === model.title)) {
model.title = `${model.title} (1)`;
}

configJson.models.push(model);
const newConfigString = stringify(configJson, 2);
writeFileSync(getConfigJsonPath(), newConfigString);
return newConfigString;
});
}

export function addOpenAIKey(key: string) {
Expand Down
33 changes: 2 additions & 31 deletions core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { DevDataSqliteDb } from "./util/devdataSqlite.js";
import { fetchwithRequestOptions } from "./util/fetchWithOptions.js";
import historyManager from "./util/history.js";
import type { IMessenger, Message } from "./util/messenger";
import { editConfigJson, getConfigJsonPath } from "./util/paths.js";
import { editConfigJson } from "./util/paths.js";
import { Telemetry } from "./util/posthog.js";
import { streamDiffLines } from "./util/verticalEdit.js";

Expand Down Expand Up @@ -179,37 +179,8 @@ export class Core {
// Edit config
on("config/addModel", (msg) => {
const model = msg.data.model;
const newConfigString = addModel(model);
addModel(model);
this.configHandler.reloadConfig();
this.ide.openFile(getConfigJsonPath());

// Find the range where it was added and highlight
const lines = newConfigString.split("\n");
let startLine: number | undefined;
let endLine: number | undefined;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];

if (!startLine) {
if (line.trim() === `"title": "${model.title}",`) {
startLine = i - 1;
}
} else {
if (line.startsWith(" }")) {
endLine = i;
break;
}
}
}

if (startLine && endLine) {
this.ide.showLines(
getConfigJsonPath(),
startLine,
endLine,
// "#fff1"
);
}
});
on("config/addOpenAiKey", (msg) => {
addOpenAIKey(msg.data);
Expand Down
36 changes: 34 additions & 2 deletions core/package-lock.json

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

1 change: 1 addition & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"axios": "^1.6.7",
"cheerio": "^1.0.0-rc.12",
"commander": "^12.0.0",
"comment-json": "^4.2.3",
"dbinfoz": "^0.1.4",
"dotenv": "^16.3.1",
"fastest-levenshtein": "^1.0.16",
Expand Down
15 changes: 10 additions & 5 deletions core/util/paths.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as JSONC from "comment-json";
import dotenv from "dotenv";
import * as fs from "fs";
import * as os from "os";
Expand Down Expand Up @@ -152,12 +153,16 @@ export function getDevDataFilePath(fileName: string): string {

export function editConfigJson(
callback: (config: SerializedContinueConfig) => SerializedContinueConfig,
) {
): void {
const config = fs.readFileSync(getConfigJsonPath(), "utf8");
let configJson = JSON.parse(config);
configJson = callback(configJson);
fs.writeFileSync(getConfigJsonPath(), JSON.stringify(configJson, null, 2));
return configJson;
let configJson = JSONC.parse(config);
// Check if it's an object
if (typeof configJson === "object" && configJson !== null) {
configJson = callback(configJson as any) as any;
fs.writeFileSync(getConfigJsonPath(), JSONC.stringify(configJson, null, 2));
} else {
console.warn("config.json is not a valid object");
}
}

function getMigrationsFolderPath(): string {
Expand Down
1 change: 1 addition & 0 deletions extensions/vscode/package-lock.json

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

9 changes: 9 additions & 0 deletions extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
"main": "./out/extension.js",
"browser": "./out/extension.js",
"contributes": {
"languages": [
{
"filenames": [
"config.json",
".continuerc.json"
],
"id": "jsonc"
}
],
"configuration": {
"title": "Continue",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions gui/package-lock.json

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

0 comments on commit d9ff68c

Please sign in to comment.