Skip to content

Commit 150d824

Browse files
authored
Edit Settings command opens JSON file (#230)
1 parent 52e172d commit 150d824

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/extension.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,39 @@ export function activate(context: vscode.ExtensionContext) {
137137
);
138138
context.subscriptions.push(
139139
vscode.commands.registerCommand(`${extensionId}.editSettings`, (server?: ServerTreeItem) => {
140-
// Until there's a dedicated settings editor the best we can do is jump to the right section
141-
vscode.commands.executeCommand("workbench.action.openSettings", `@ext:${extensionId}`);
140+
// Attempt to open the correct JSON file
141+
server = server instanceof ServerTreeItem ? server : undefined;
142+
const servers = vscode.workspace.getConfiguration("intersystems").inspect<{ [key: string]: any }>("servers");
143+
const openJSONArg = { revealSetting: { key: "intersystems.servers" } };
144+
const revealServer = (): void => {
145+
// Find the start of the server's settings block
146+
const editor = vscode.window.activeTextEditor;
147+
const regex = new RegExp(`"${server?.name}"\\s*:`);
148+
if (editor && (editor.document.uri.path.endsWith("/settings.json") || editor.document.uri.path.endsWith(".code-workspace"))) {
149+
// The cursor is currently at "|intersystems.servers", so start our scan from there
150+
for (let i = editor.selection.start.line; i < editor.document.lineCount; i++) {
151+
const line = editor.document.lineAt(i).text;
152+
const match = regex.exec(line);
153+
const commentStart = line.indexOf("//");
154+
if (match && (commentStart == -1 || match.index < commentStart)) {
155+
const cursorPos = new vscode.Position(i, match.index + 1);
156+
editor.revealRange(new vscode.Range(cursorPos, cursorPos), vscode.TextEditorRevealType.InCenter);
157+
editor.selection = new vscode.Selection(cursorPos, cursorPos);
158+
break;
159+
}
160+
}
161+
}
162+
};
163+
if (server && servers?.workspaceValue?.hasOwnProperty(server.name)) {
164+
// Open the workspace settings file
165+
vscode.commands.executeCommand("workbench.action.openWorkspaceSettingsFile", openJSONArg).then(revealServer);
166+
} else if (server && servers?.globalValue?.hasOwnProperty(server.name)) {
167+
// Open the user settings.json
168+
vscode.commands.executeCommand("workbench.action.openSettingsJson", openJSONArg).then(revealServer);
169+
} else {
170+
// Just show the UI
171+
vscode.commands.executeCommand("workbench.action.openSettings", `@ext:${extensionId}`);
172+
}
142173
}),
143174
);
144175
context.subscriptions.push(

0 commit comments

Comments
 (0)