Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit Settings command opens JSON file #230

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,39 @@ export function activate(context: vscode.ExtensionContext) {
);
context.subscriptions.push(
vscode.commands.registerCommand(`${extensionId}.editSettings`, (server?: ServerTreeItem) => {
// Until there's a dedicated settings editor the best we can do is jump to the right section
vscode.commands.executeCommand("workbench.action.openSettings", `@ext:${extensionId}`);
// Attempt to open the correct JSON file
server = server instanceof ServerTreeItem ? server : undefined;
const servers = vscode.workspace.getConfiguration("intersystems").inspect<{ [key: string]: any }>("servers");
const openJSONArg = { revealSetting: { key: "intersystems.servers" } };
const revealServer = (): void => {
// Find the start of the server's settings block
const editor = vscode.window.activeTextEditor;
const regex = new RegExp(`"${server?.name}"\\s*:`);
if (editor && (editor.document.uri.path.endsWith("/settings.json") || editor.document.uri.path.endsWith(".code-workspace"))) {
// The cursor is currently at "|intersystems.servers", so start our scan from there
for (let i = editor.selection.start.line; i < editor.document.lineCount; i++) {
const line = editor.document.lineAt(i).text;
const match = regex.exec(line);
const commentStart = line.indexOf("//");
if (match && (commentStart == -1 || match.index < commentStart)) {
const cursorPos = new vscode.Position(i, match.index + 1);
editor.revealRange(new vscode.Range(cursorPos, cursorPos));
isc-bsaviano marked this conversation as resolved.
Show resolved Hide resolved
editor.selection = new vscode.Selection(cursorPos, cursorPos);
break;
}
}
}
};
if (server && servers?.workspaceValue?.hasOwnProperty(server.name)) {
// Open the workspace settings file
vscode.commands.executeCommand("workbench.action.openWorkspaceSettingsFile", openJSONArg).then(revealServer);
} else if (server && servers?.globalValue?.hasOwnProperty(server.name)) {
// Open the user settings.json
vscode.commands.executeCommand("workbench.action.openSettingsJson", openJSONArg).then(revealServer);
} else {
// Just show the UI
vscode.commands.executeCommand("workbench.action.openSettings", `@ext:${extensionId}`);
}
}),
);
context.subscriptions.push(
Expand Down
Loading