-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API for extending the connections pane (#5785)
Addresses: #5689 Add a positron public API that can be used to register new connection drivers. Registered drivers are used by the connections pane 'New Connection' button.
- Loading branch information
Showing
24 changed files
with
466 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (C) 2023-2024 Posit Software, PBC. All rights reserved. | ||
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as positron from 'positron'; | ||
import * as vscode from 'vscode'; | ||
|
||
export function registerConnectionDrivers(context: vscode.ExtensionContext) { | ||
context.subscriptions.push( | ||
positron.connections.registerConnectionDriver(new RPostgreSQLDriver()) | ||
); | ||
} | ||
|
||
class RPostgreSQLDriver implements positron.ConnectionsDriver { | ||
driverId: string = 'postgres'; | ||
metadata: positron.ConnectionsDriverMetadata = { | ||
languageId: 'r', | ||
name: 'PostgresSQL', | ||
inputs: [ | ||
{ | ||
'id': 'dbname', | ||
'label': 'Database Name', | ||
'type': 'string', | ||
'value': 'localhost' | ||
}, | ||
{ | ||
'id': 'host', | ||
'label': 'Host', | ||
'type': 'string', | ||
'value': 'localhost' | ||
}, | ||
{ | ||
'id': 'port', | ||
'label': 'Port', | ||
'type': 'number', | ||
'value': '5432' | ||
}, | ||
{ | ||
'id': 'user', | ||
'label': 'User', | ||
'type': 'string', | ||
'value': 'postgres' | ||
}, | ||
{ | ||
'id': 'password', | ||
'label': 'Password', | ||
'type': 'string', | ||
'value': 'password' | ||
}, | ||
{ | ||
'id': 'bigint', | ||
'label': 'Integer representation', | ||
'type': 'option', | ||
'options': [ | ||
{ 'identifier': 'integer64', 'title': 'integer64' }, | ||
{ 'identifier': 'integer', 'title': 'integer' }, | ||
{ 'identifier': 'numeric', 'title': 'numeric' }, | ||
{ 'identifier': 'character', 'title': 'character' } | ||
], | ||
'value': 'integer64' | ||
} | ||
] | ||
}; | ||
|
||
generateCode(inputs: positron.ConnectionsInput[]) { | ||
const dbname = inputs.find(input => input.id === 'dbname')?.value; | ||
const host = inputs.find(input => input.id === 'host')?.value; | ||
const port = inputs.find(input => input.id === 'port')?.value; | ||
const user = inputs.find(input => input.id === 'user')?.value; | ||
const password = inputs.find(input => input.id === 'password')?.value; | ||
const bigint = inputs.find(input => input.id === 'bigint')?.value; | ||
|
||
return `library(DBI) | ||
con <- dbConnect( | ||
RPostgres::Postgres(), | ||
dbname = '${dbname ?? ''}', | ||
host = '${host ?? ''}', | ||
port = ${port ?? ''}, | ||
user = '${user ?? ''}', | ||
password = '${password ?? ''}', | ||
bigint = '${bigint ?? ''}' | ||
) | ||
`; | ||
} | ||
|
||
async connect(code: string) { | ||
const exec = await positron.runtime.executeCode( | ||
'r', | ||
code, | ||
true, | ||
false, | ||
positron.RuntimeCodeExecutionMode.Interactive, | ||
positron.RuntimeErrorBehavior.Continue | ||
); | ||
if (!exec) { | ||
throw new Error('Failed to execute code'); | ||
} | ||
return; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/vs/workbench/api/browser/positron/mainThreadConnections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (C) 2023-2024 Posit Software, PBC. All rights reserved. | ||
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { ExtHostConnectionsShape, ExtHostPositronContext, MainPositronContext, MainThreadConnectionsShape } from '../../common/positron/extHost.positron.protocol.js'; | ||
import { extHostNamedCustomer, IExtHostContext } from '../../../services/extensions/common/extHostCustomers.js'; | ||
import { IDriver, IDriverMetadata, Input } from '../../../services/positronConnections/common/interfaces/positronConnectionsDriver.js'; | ||
import { IPositronConnectionsService } from '../../../services/positronConnections/common/interfaces/positronConnectionsService.js'; | ||
import { DisposableStore } from '../../../../base/common/lifecycle.js'; | ||
|
||
@extHostNamedCustomer(MainPositronContext.MainThreadConnections) | ||
export class MainThreadConnections implements MainThreadConnectionsShape { | ||
private readonly _proxy: ExtHostConnectionsShape; | ||
private readonly _disposables = new DisposableStore(); | ||
constructor( | ||
extHostContext: IExtHostContext, | ||
@IPositronConnectionsService private readonly _connectionsService: IPositronConnectionsService | ||
) { | ||
this._proxy = extHostContext.getProxy(ExtHostPositronContext.ExtHostConnections); | ||
} | ||
|
||
$registerConnectionDriver(driverId: string, metadata: IDriverMetadata, availableMethods: IAvailableDriverMethods): void { | ||
this._connectionsService.driverManager.registerDriver(new MainThreadDriverAdapter( | ||
driverId, metadata, availableMethods, this._proxy | ||
)); | ||
} | ||
|
||
$removeConnectionDriver(driverId: string): void { | ||
this._connectionsService.driverManager.removeDriver(driverId); | ||
} | ||
|
||
dispose(): void { | ||
this._disposables.dispose(); | ||
} | ||
} | ||
|
||
export interface IAvailableDriverMethods { | ||
generateCode: boolean, | ||
connect: boolean, | ||
checkDependencies: boolean, | ||
installDependencies: boolean | ||
} | ||
|
||
class MainThreadDriverAdapter implements IDriver { | ||
constructor( | ||
readonly driverId: string, | ||
readonly metadata: IDriverMetadata, | ||
private readonly availableMethods: IAvailableDriverMethods, | ||
private readonly _proxy: ExtHostConnectionsShape | ||
) { } | ||
get generateCode() { | ||
if (!this.availableMethods.generateCode) { | ||
return undefined; | ||
} | ||
return (inputs: Input[]) => this._proxy.$driverGenerateCode(this.driverId, inputs); | ||
} | ||
get connect() { | ||
if (!this.availableMethods.connect) { | ||
return undefined; | ||
} | ||
return (code: string) => this._proxy.$driverConnect(this.driverId, code); | ||
} | ||
get checkDependencies() { | ||
if (!this.availableMethods.checkDependencies) { | ||
return undefined; | ||
} | ||
return () => this._proxy.$driverCheckDependencies(this.driverId); | ||
} | ||
get installDependencies() { | ||
if (!this.availableMethods.installDependencies) { | ||
return undefined; | ||
} | ||
return () => this._proxy.$driverInstallDependencies(this.driverId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.