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 1 commit
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
32 changes: 30 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,36 @@ 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 revealServers = (): void => {
// Find the start of the server settings block
const editor = vscode.window.activeTextEditor;
if (editor && (editor.document.uri.path.endsWith("/settings.json") || editor.document.uri.path.endsWith(".code-workspace"))) {
for (let i = 0; i < editor.document.lineCount; i++) {
const line = editor.document.lineAt(i).text;
const hasServersDef = line.indexOf("\"intersystems.servers\"");
const hasComment = line.indexOf("//");
if (hasServersDef > -1 && (hasComment == -1 || hasServersDef < hasComment)) {
const range = new vscode.Range(i, hasServersDef, i, hasServersDef + 22);
editor.revealRange(range);
editor.selection = new vscode.Selection(range.start, range.end);
break;
}
}
}
};
if (server && servers?.workspaceValue?.hasOwnProperty(server.name)) {
// Open the workspace settings file
vscode.commands.executeCommand("workbench.action.openWorkspaceSettingsFile").then(revealServers);
} else if (server && servers?.globalValue?.hasOwnProperty(server.name)) {
// Open the user settings.json
vscode.commands.executeCommand("workbench.action.openSettingsJson").then(revealServers);
} else {
// Just show the UI
vscode.commands.executeCommand("workbench.action.openSettings", `@ext:${extensionId}`);
}
}),
);
context.subscriptions.push(
Expand Down
Loading