Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 23 additions & 7 deletions crates/extension_host/src/extension_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ use language::{
use node_runtime::NodeRuntime;
use project::ContextProviderWithTasks;
use release_channel::ReleaseChannel;
use remote::RemoteClient;
use remote::{OnRemoteClientCreated, RemoteClient, RemoteClientEvent};
use semver::Version;
use serde::{Deserialize, Serialize};
use settings::{SemanticTokenRules, Settings, SettingsStore};
use std::ops::RangeInclusive;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::LazyLock;
use std::{
Expand Down Expand Up @@ -292,6 +293,12 @@ pub fn init(
});

cx.set_global(GlobalExtensionStore(store));

cx.set_global(OnRemoteClientCreated(Rc::new(|client, cx| {
Comment thread
SomeoneToIgnore marked this conversation as resolved.
Resolved
if let Some(store) = ExtensionStore::try_global(cx) {
store.update(cx, |store, cx| store.register_remote_client(client, cx));
}
})));
}

impl ExtensionStore {
Expand Down Expand Up @@ -1959,13 +1966,22 @@ impl ExtensionStore {
anyhow::Ok(())
}

pub fn register_remote_client(
&mut self,
client: Entity<RemoteClient>,
_cx: &mut Context<Self>,
) {
pub fn register_remote_client(&mut self, client: Entity<RemoteClient>, cx: &mut Context<Self>) {
self.remote_clients.push(client.downgrade());
self.ssh_registered_tx.unbounded_send(()).ok();

cx.subscribe(&client, |this, _client, event, _cx| {
if matches!(event, RemoteClientEvent::Reconnected) {
this.ssh_registered_tx.unbounded_send(()).ok();
Comment thread
SomeoneToIgnore marked this conversation as resolved.
Resolved
}
})
.detach();

cx.spawn(async move |this, cx| {
Self::sync_extensions_to_remotes(&this, client.downgrade(), cx)
.await
.log_err();
})
.detach();
Comment thread
SomeoneToIgnore marked this conversation as resolved.
Resolved
}
}

Expand Down
42 changes: 31 additions & 11 deletions crates/extension_host/src/headless_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use anyhow::{Context as _, Result};
use client::{TypedEnvelope, proto};
use collections::{HashMap, HashSet};
use extension::{
Extension, ExtensionDebugAdapterProviderProxy, ExtensionHostProxy, ExtensionLanguageProxy,
ExtensionLanguageServerProxy, ExtensionManifest,
Event, Extension, ExtensionDebugAdapterProviderProxy, ExtensionEvents, ExtensionHostProxy,
ExtensionLanguageProxy, ExtensionLanguageServerProxy, ExtensionManifest,
};
use fs::{Fs, RemoveOptions, RenameOptions};
use futures::future::{FutureExt as _, join_all};
Expand Down Expand Up @@ -85,24 +85,31 @@ impl HeadlessExtensionStore {
})
.collect();

cx.spawn(async move |this, cx| {
let extensions_changed = !to_remove.is_empty() || !to_load.is_empty();

cx.spawn(async move |store, cx| {
let mut missing = Vec::new();

for extension_id in to_remove {
log::info!("removing extension: {}", extension_id);
this.update(cx, |this, cx| this.uninstall_extension(&extension_id, cx))?
store
.update(cx, |store, cx| store.uninstall_extension(&extension_id, cx))?
.await?;
}

for extension in to_load {
if let Err(e) = Self::load_extension(this.clone(), extension.clone(), cx).await {
if let Err(e) = Self::load_extension(store.clone(), extension.clone(), cx).await {
log::info!("failed to load extension: {}, {:#}", extension.id, e);
missing.push(extension)
} else if extension.dev {
missing.push(extension)
}
}

if extensions_changed {
Comment thread
SomeoneToIgnore marked this conversation as resolved.
Resolved
store.update(cx, |_, cx| notify_extensions_changed(cx)).ok();
}

Ok(missing)
})
}
Expand Down Expand Up @@ -275,19 +282,24 @@ impl HeadlessExtensionStore {
let path = self.extension_dir.join(&extension.id);
let fs = self.fs.clone();

cx.spawn(async move |this, cx| {
cx.spawn(async move |store, cx| {
if fs.is_dir(&path).await {
this.update(cx, |this, cx| {
this.uninstall_extension(&extension.id.clone().into(), cx)
})?
.await?;
store
.update(cx, |store, cx| {
store.uninstall_extension(&extension.id.clone().into(), cx)
})?
.await?;
}

fs.rename(&tmp_path, &path, RenameOptions::default())
.await
.with_context(|| format!("Failed to rename {tmp_path:?} to {path:?}"))?;

Self::load_extension(this, extension, cx).await
Self::load_extension(store.clone(), extension, cx).await?;

store.update(cx, |_, cx| notify_extensions_changed(cx)).ok();

Ok(())
})
}

Expand Down Expand Up @@ -354,3 +366,11 @@ impl HeadlessExtensionStore {
Ok(proto::Ack {})
}
}

fn notify_extensions_changed(cx: &mut App) {
if let Some(events) = ExtensionEvents::try_global(cx) {
events.update(cx, |this, cx| {
this.emit(Event::ExtensionsInstalledChanged, cx)
});
}
}
25 changes: 9 additions & 16 deletions crates/json_schema_store/src/json_schema_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,8 @@ async fn resolve_dynamic_schema(
.chain(languages.available_lsp_adapter_names())
.map(|name| name.to_string())
.collect();

let mut i = 0;
while i < lsp_adapter_names.len() {
let mut j = i + 1;
while j < lsp_adapter_names.len() {
if lsp_adapter_names[i] == lsp_adapter_names[j] {
lsp_adapter_names.swap_remove(j);
} else {
j += 1;
}
}
i += 1;
}
lsp_adapter_names.sort_unstable();
lsp_adapter_names.dedup();

cx.update(|cx| {
let font_names = &cx.text_system().all_font_names();
Expand Down Expand Up @@ -374,11 +363,15 @@ async fn resolve_dynamic_schema(
})
}
"project_settings" => {
let lsp_adapter_names = languages
let mut lsp_adapter_names: Vec<String> = languages
.all_lsp_adapters()
.into_iter()
.map(|adapter| adapter.name().to_string())
.collect::<Vec<_>>();
.map(|adapter| adapter.name())
.chain(languages.available_lsp_adapter_names())
.map(|name| name.to_string())
.collect();
lsp_adapter_names.sort_unstable();
lsp_adapter_names.dedup();

let language_names = &languages
.language_names()
Expand Down
12 changes: 11 additions & 1 deletion crates/project/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ pub use prettier_store::PrettierStore;
use project_settings::{ProjectSettings, SettingsObserver, SettingsObserverEvent};
#[cfg(target_os = "windows")]
use remote::wsl_path_to_windows_path;
use remote::{RemoteClient, RemoteConnectionOptions, same_remote_connection_identity};
use remote::{
OnRemoteClientCreated, RemoteClient, RemoteConnectionOptions, same_remote_connection_identity,
};
use rpc::{
AnyProtoClient, ErrorCode,
proto::{LanguageServerPromptResponse, REMOTE_SERVER_PROJECT_ID},
Expand Down Expand Up @@ -1685,6 +1687,13 @@ impl Project {
GitStore::init(&remote_proto);
AgentServerStore::init_remote(&remote_proto);

if let Some(on_remote_client_created) = cx
.try_global::<OnRemoteClientCreated>()
.map(|on_remote_client_created| on_remote_client_created.0.clone())
{
(on_remote_client_created)(remote.clone(), cx);
Comment thread
SomeoneToIgnore marked this conversation as resolved.
Resolved
}

this
})
}
Expand Down Expand Up @@ -3861,6 +3870,7 @@ impl Project {
});
cx.emit(Event::DisconnectedFromRemote { server_not_running });
}
&remote::RemoteClientEvent::Reconnected => {}
}
}

Expand Down
14 changes: 1 addition & 13 deletions crates/recent_projects/src/remote_connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
use anyhow::{Context as _, Result};
use askpass::EncryptedPassword;
use editor::Editor;
use extension_host::ExtensionStore;
use futures::{FutureExt as _, channel::oneshot, select};
use gpui::{AppContext, AsyncApp, PromptLevel, WindowHandle};

Expand Down Expand Up @@ -246,7 +245,6 @@ pub async fn open_remote_project(
(window, workspace)
};

let mut remote_workspace = None;
loop {
let (cancel_tx, mut cancel_rx) = oneshot::channel();
let delegate = window.update(cx, {
Expand Down Expand Up @@ -413,24 +411,14 @@ pub async fn open_remote_project(
});
}

Ok((workspace, items)) => {
remote_workspace = workspace;
Ok((_workspace, items)) => {
navigate_to_positions(&window, items, &paths_with_positions, cx);
}
}

break;
}

if let Some(remote_workspace) = remote_workspace {
remote_workspace.update(cx, |workspace, cx| {
if let Some(client) = workspace.project().read(cx).remote_client()
&& let Some(extension_store) = ExtensionStore::try_global(cx)
{
extension_store.update(cx, |store, cx| store.register_remote_client(client, cx));
}
});
}
Ok(window)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/remote/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ mod transport;
#[cfg(target_os = "windows")]
pub use remote_client::OpenWslPath;
pub use remote_client::{
CommandTemplate, ConnectionIdentifier, ConnectionState, Interactive, RemoteArch, RemoteClient,
RemoteClientDelegate, RemoteClientEvent, RemoteConnection, RemoteConnectionOptions, RemoteOs,
RemotePlatform, connect, has_active_connection,
CommandTemplate, ConnectionIdentifier, ConnectionState, Interactive, OnRemoteClientCreated,
RemoteArch, RemoteClient, RemoteClientDelegate, RemoteClientEvent, RemoteConnection,
RemoteConnectionOptions, RemoteOs, RemotePlatform, connect, has_active_connection,
};
pub use remote_identity::{
RemoteConnectionIdentity, remote_connection_identity, same_remote_connection_identity,
Expand Down
17 changes: 17 additions & 0 deletions crates/remote/src/remote_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use std::{
fmt,
ops::ControlFlow,
path::PathBuf,
rc::Rc,
sync::{
Arc, Weak,
atomic::{AtomicU32, AtomicU64, Ordering::SeqCst},
Expand Down Expand Up @@ -339,10 +340,17 @@ pub struct RemoteClient {
#[derive(Debug)]
pub enum RemoteClientEvent {
Disconnected { server_not_running: bool },
Reconnected,
}

impl EventEmitter<RemoteClientEvent> for RemoteClient {}

/// A callback invoked whenever a remote client is created, allowing other
/// subsystems to react to new remote connections.
pub struct OnRemoteClientCreated(pub Rc<dyn Fn(Entity<RemoteClient>, &mut App)>);

impl Global for OnRemoteClientCreated {}

/// Identifies the socket on the remote server so that reconnects
/// can re-join the same project.
pub enum ConnectionIdentifier {
Expand Down Expand Up @@ -725,6 +733,11 @@ impl RemoteClient {
cx.spawn(async move |this, cx| {
let new_state = reconnect_task.await;
this.update(cx, |this, cx| {
let reconnected = this
.state
.as_ref()
.is_some_and(State::is_reconnecting)
&& matches!(&new_state, State::Connected { .. });
this.try_set_state(cx, |old_state| {
if old_state.is_reconnecting() {
match &new_state {
Expand Down Expand Up @@ -754,6 +767,10 @@ impl RemoteClient {
}
});

if reconnected {
cx.emit(RemoteClientEvent::Reconnected);
}

if this.state_is(State::is_reconnect_failed) {
this.reconnect(cx)
} else if this.state_is(State::is_reconnect_exhausted) {
Expand Down
42 changes: 41 additions & 1 deletion crates/remote_server/src/remote_editing_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use project::{
image_store,
search::{SearchQuery, SearchResult},
};
use remote::RemoteClient;
use remote::{ConnectionState, OnRemoteClientCreated, RemoteClient};
use rpc::proto;
use serde_json::json;
use settings::{Settings, SettingsLocation, SettingsStore, initial_server_settings_content};
Expand Down Expand Up @@ -4315,6 +4315,46 @@ async fn test_remote_trash_restore(cx: &mut TestAppContext, server_cx: &mut Test
});
}

#[gpui::test]
async fn test_remote_project_creation_triggers_on_remote_client_created_callback(
Comment thread
SomeoneToIgnore marked this conversation as resolved.
Resolved
cx: &mut TestAppContext,
server_cx: &mut TestAppContext,
) {
let server_fs = Arc::new(FakeFs::new(server_cx.executor()));
server_fs
.insert_tree(
path!("/project"),
json!({
"src": {
"main.rs": "fn main() {}",
},
"README.md": "# Test Project",
}),
)
.await;

let callback_invocations = Arc::new(AtomicUsize::new(0));
cx.update(|cx| {
let callback_invocations = callback_invocations.clone();
cx.set_global(OnRemoteClientCreated(Rc::new(move |client, cx| {
assert_eq!(
client.read(cx).connection_state(),
ConnectionState::Connected
);
callback_invocations.fetch_add(1, Ordering::SeqCst);
})));
});

let (project, _headless) = init_test(&server_fs, cx, server_cx).await;

assert_eq!(
callback_invocations.load(Ordering::SeqCst),
1,
"creating a remote project should invoke the OnRemoteClientCreated callback exactly once"
);
assert!(project.read_with(cx, |project, _| project.is_remote()));
}

pub async fn init_test(
server_fs: &Arc<FakeFs>,
cx: &mut TestAppContext,
Expand Down
Loading