Skip to content
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"onChatParticipant:mongodb.participant",
"onLanguage:json",
"onLanguage:javascript",
"onLanguage:plaintext"
"onLanguage:plaintext",
"onUri"
],
"contributes": {
"chatParticipants": [
Expand Down
41 changes: 28 additions & 13 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,17 @@ export default class ConnectionController {
})); */
}

async connectWithURI(): Promise<boolean> {
let connectionString: string | undefined;

async connectWithURI(
connectionString?: string,
reuseExisting = false
): Promise<boolean> {
log.info('connectWithURI command called');

const cancellationToken = new vscode.CancellationTokenSource();
this._connectionStringInputCancellationToken = cancellationToken;

try {
connectionString = await vscode.window.showInputBox(
connectionString ??= await vscode.window.showInputBox(
{
value: '',
ignoreFocusOut: true,
Expand Down Expand Up @@ -292,27 +293,41 @@ export default class ConnectionController {
return false;
}

return this.addNewConnectionStringAndConnect(connectionString);
return this.addNewConnectionStringAndConnect(
connectionString,
reuseExisting
);
}

// Resolves the new connection id when the connection is successfully added.
// Resolves false when it is added and not connected.
// The connection can fail to connect but be successfully added.
async addNewConnectionStringAndConnect(
connectionString: string
connectionString: string,
reuseExisting = false
): Promise<boolean> {
log.info('Trying to connect to a new connection configuration...');

const connectionStringData = new ConnectionString(connectionString);

try {
const connectResult = await this.saveNewConnectionAndConnect({
connectionId: uuidv4(),
connectionOptions: {
connectionString: connectionStringData.toString(),
},
connectionType: ConnectionTypes.CONNECTION_STRING,
});
let existingId: string | undefined;
if (reuseExisting) {
existingId = this.getSavedConnections().find(
(connection) =>
connection.connectionOptions?.connectionString === connectionString
)?.id;
}

const connectResult = await (existingId
? this.connectWithConnectionId(existingId)
: this.saveNewConnectionAndConnect({
connectionId: uuidv4(),
connectionOptions: {
connectionString: connectionStringData.toString(),
},
connectionType: ConnectionTypes.CONNECTION_STRING,
}));

return connectResult.successfullyConnected;
} catch (error) {
Expand Down
36 changes: 33 additions & 3 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export default class MDBExtensionController implements vscode.Disposable {
this.registerCommands();
this.showOverviewPageIfRecentlyInstalled();
this.subscribeToConfigurationChanges();
this.registerUriHandler();

const copilot = vscode.extensions.getExtension(COPILOT_EXTENSION_ID);
void vscode.commands.executeCommand(
Expand All @@ -210,6 +211,32 @@ export default class MDBExtensionController implements vscode.Disposable {
}
}

registerUriHandler = (): void => {
vscode.window.registerUriHandler({
handleUri: async (uri: vscode.Uri): Promise<void> => {
const command = uri.path.replace(/^\//, '');
const parameters = Object.fromEntries(new URLSearchParams(uri.query));

try {
if (
!Object.values(EXTENSION_COMMANDS).includes(
command as EXTENSION_COMMANDS
)
) {
throw new Error(
`Unable to execute command '${command}' since it is not registered by the MongoDB extension.`
);
}
await vscode.commands.executeCommand(command, parameters);
} catch (error) {
await vscode.window.showErrorMessage(
`Failed to handle '${uri}': ${error}`
);
}
},
});
};

registerCommands = (): void => {
// Register our extension's commands. These are the event handlers and
// control the functionality of our extension.
Expand All @@ -222,9 +249,12 @@ export default class MDBExtensionController implements vscode.Disposable {
this._webviewController.openWebview(this._context);
return Promise.resolve(true);
});
this.registerCommand(EXTENSION_COMMANDS.MDB_CONNECT_WITH_URI, () =>
this._connectionController.connectWithURI()
);
this.registerCommand(EXTENSION_COMMANDS.MDB_CONNECT_WITH_URI, (params) => {
return this._connectionController.connectWithURI(
params?.connectionString,
params?.reuseExisting || false
);
});
this.registerCommand(EXTENSION_COMMANDS.MDB_DISCONNECT, () =>
this._connectionController.disconnect()
);
Expand Down