connectToWorkspace function

Future<LazyVSCodeBridgeAdapter> connectToWorkspace(
  1. String name, {
  2. String host = '127.0.0.1',
  3. int minPort = defaultVSCodeBridgePort,
  4. int maxPort = maxVSCodeBridgePort,
  5. BridgePortProbe probe = _defaultProbe,
  6. BridgeIdentityFetcher fetchIdentity = fetchBridgeWorkspaceName,
  7. bool initializeVSCode = false,
  8. BridgeAdapterFactory adapterFactory = _defaultAdapterFactory,
})

Resolves the bridge port for the workspace named name (via findBridgePortForWorkspace) and returns a connected adapter bound to it.

The adapter is connected before returning; if the resolved bridge cannot be connected a StateError is thrown. When initializeVSCode is true, the returned adapter is also promoted to the global VSCode singleton so VSCode.instance targets that window. The BridgeWorkspaceNotFoundException raised by the underlying scan when no window matches name propagates unchanged.

Implementation

Future<LazyVSCodeBridgeAdapter> connectToWorkspace(
  String name, {
  String host = '127.0.0.1',
  int minPort = defaultVSCodeBridgePort,
  int maxPort = maxVSCodeBridgePort,
  BridgePortProbe probe = _defaultProbe,
  BridgeIdentityFetcher fetchIdentity = fetchBridgeWorkspaceName,
  bool initializeVSCode = false,
  BridgeAdapterFactory adapterFactory = _defaultAdapterFactory,
}) async {
  final port = await findBridgePortForWorkspace(
    name,
    host: host,
    minPort: minPort,
    maxPort: maxPort,
    probe: probe,
    fetchIdentity: fetchIdentity,
  );
  final adapter = adapterFactory(host, port);
  final connected = await adapter.connect();
  if (!connected) {
    throw StateError(
      'Found workspace "$name" on port $port but could not connect to its '
      'bridge.',
    );
  }
  if (initializeVSCode) VSCode.initialize(adapter);
  return adapter;
}