Sync config with ssh remotes (#17349)

Release Notes:

- N/A

---------

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Conrad Irwin 2024-09-04 12:28:51 -06:00 committed by GitHub
parent 4b094798e0
commit 7fb94c4c4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 609 additions and 193 deletions

View file

@ -27,7 +27,7 @@ use client::{
use clock::ReplicaId;
use collections::{BTreeSet, HashMap, HashSet};
use debounced_delay::DebouncedDelay;
use environment::ProjectEnvironment;
pub use environment::ProjectEnvironment;
use futures::{
channel::mpsc::{self, UnboundedReceiver},
future::try_join_all,
@ -58,12 +58,9 @@ use lsp::{CompletionContext, DocumentHighlightKind, LanguageServer, LanguageServ
use lsp_command::*;
use node_runtime::NodeRuntime;
use parking_lot::{Mutex, RwLock};
use paths::{
local_settings_file_relative_path, local_tasks_file_relative_path,
local_vscode_tasks_file_relative_path,
};
use paths::{local_tasks_file_relative_path, local_vscode_tasks_file_relative_path};
use prettier_support::{DefaultPrettier, PrettierInstance};
use project_settings::{LspSettings, ProjectSettings};
use project_settings::{LspSettings, ProjectSettings, SettingsObserver};
use remote::SshSession;
use rpc::{
proto::{AnyProtoClient, SSH_PROJECT_ID},
@ -174,6 +171,7 @@ pub struct Project {
last_formatting_failure: Option<String>,
buffers_being_formatted: HashSet<BufferId>,
environment: Model<ProjectEnvironment>,
settings_observer: Model<SettingsObserver>,
}
#[derive(Default)]
@ -505,6 +503,14 @@ impl FormatTrigger {
}
}
enum EntitySubscription {
Project(PendingEntitySubscription<Project>),
BufferStore(PendingEntitySubscription<BufferStore>),
WorktreeStore(PendingEntitySubscription<WorktreeStore>),
LspStore(PendingEntitySubscription<LspStore>),
SettingsObserver(PendingEntitySubscription<SettingsObserver>),
}
#[derive(Clone)]
pub enum DirectoryLister {
Project(Model<Project>),
@ -584,7 +590,6 @@ impl Project {
client.add_model_message_handler(Self::handle_unshare_project);
client.add_model_request_handler(Self::handle_update_buffer);
client.add_model_message_handler(Self::handle_update_worktree);
client.add_model_message_handler(Self::handle_update_worktree_settings);
client.add_model_request_handler(Self::handle_reload_buffers);
client.add_model_request_handler(Self::handle_synchronize_buffers);
client.add_model_request_handler(Self::handle_format_buffers);
@ -600,6 +605,7 @@ impl Project {
WorktreeStore::init(&client);
BufferStore::init(&client);
LspStore::init(&client);
SettingsObserver::init(&client);
}
pub fn local(
@ -629,14 +635,18 @@ impl Project {
cx.subscribe(&buffer_store, Self::on_buffer_store_event)
.detach();
let environment = ProjectEnvironment::new(env, cx);
let settings_observer = cx.new_model(|cx| {
SettingsObserver::new_local(fs.clone(), worktree_store.clone(), cx)
});
let environment = ProjectEnvironment::new(&worktree_store, env, cx);
let lsp_store = cx.new_model(|cx| {
LspStore::new(
buffer_store.clone(),
worktree_store.clone(),
Some(environment.clone()),
languages.clone(),
client.http_client(),
Some(client.http_client()),
fs.clone(),
None,
None,
@ -665,6 +675,7 @@ impl Project {
languages,
client,
user_store,
settings_observer,
fs,
ssh_session: None,
buffers_needing_diff: Default::default(),
@ -704,14 +715,21 @@ impl Project {
this.worktree_store.update(cx, |store, _cx| {
store.set_upstream_client(client.clone());
});
this.settings_observer = cx.new_model(|cx| {
SettingsObserver::new_ssh(ssh.clone().into(), this.worktree_store.clone(), cx)
});
ssh.subscribe_to_entity(SSH_PROJECT_ID, &cx.handle());
ssh.subscribe_to_entity(SSH_PROJECT_ID, &this.buffer_store);
ssh.subscribe_to_entity(SSH_PROJECT_ID, &this.worktree_store);
ssh.subscribe_to_entity(SSH_PROJECT_ID, &this.lsp_store);
ssh.subscribe_to_entity(SSH_PROJECT_ID, &this.settings_observer);
client.add_model_message_handler(Self::handle_update_worktree);
client.add_model_message_handler(Self::handle_create_buffer_for_peer);
client.add_model_message_handler(BufferStore::handle_update_buffer_file);
client.add_model_message_handler(BufferStore::handle_update_diff_base);
LspStore::init(&client);
SettingsObserver::init(&client);
this.ssh_session = Some(ssh);
});
@ -746,12 +764,17 @@ impl Project {
) -> Result<Model<Self>> {
client.authenticate_and_connect(true, &cx).await?;
let subscriptions = (
client.subscribe_to_entity::<Self>(remote_id)?,
client.subscribe_to_entity::<BufferStore>(remote_id)?,
client.subscribe_to_entity::<WorktreeStore>(remote_id)?,
client.subscribe_to_entity::<LspStore>(remote_id)?,
);
let subscriptions = [
EntitySubscription::Project(client.subscribe_to_entity::<Self>(remote_id)?),
EntitySubscription::BufferStore(client.subscribe_to_entity::<BufferStore>(remote_id)?),
EntitySubscription::WorktreeStore(
client.subscribe_to_entity::<WorktreeStore>(remote_id)?,
),
EntitySubscription::LspStore(client.subscribe_to_entity::<LspStore>(remote_id)?),
EntitySubscription::SettingsObserver(
client.subscribe_to_entity::<SettingsObserver>(remote_id)?,
),
];
let response = client
.request_envelope(proto::JoinProject {
project_id: remote_id,
@ -771,12 +794,7 @@ impl Project {
async fn from_join_project_response(
response: TypedEnvelope<proto::JoinProjectResponse>,
subscription: (
PendingEntitySubscription<Project>,
PendingEntitySubscription<BufferStore>,
PendingEntitySubscription<WorktreeStore>,
PendingEntitySubscription<LspStore>,
),
subscriptions: [EntitySubscription; 5],
client: Arc<Client>,
user_store: Model<UserStore>,
languages: Arc<LanguageRegistry>,
@ -803,7 +821,7 @@ impl Project {
worktree_store.clone(),
None,
languages.clone(),
client.http_client(),
Some(client.http_client()),
fs.clone(),
None,
Some(client.clone().into()),
@ -814,6 +832,9 @@ impl Project {
lsp_store
})?;
let settings_observer =
cx.new_model(|cx| SettingsObserver::new_remote(worktree_store.clone(), cx))?;
let this = cx.new_model(|cx| {
let replica_id = response.payload.replica_id as ReplicaId;
let tasks = Inventory::new(cx);
@ -850,6 +871,7 @@ impl Project {
snippets,
fs,
ssh_session: None,
settings_observer: settings_observer.clone(),
client_subscriptions: Default::default(),
_subscriptions: vec![cx.on_release(Self::release)],
client: client.clone(),
@ -876,7 +898,7 @@ impl Project {
.dev_server_project_id
.map(|dev_server_project_id| DevServerProjectId(dev_server_project_id)),
search_history: Self::new_search_history(),
environment: ProjectEnvironment::new(None, cx),
environment: ProjectEnvironment::new(&worktree_store, None, cx),
remotely_created_buffers: Arc::new(Mutex::new(RemotelyCreatedBuffers::default())),
last_formatting_failure: None,
buffers_being_formatted: Default::default(),
@ -888,12 +910,24 @@ impl Project {
this
})?;
let subscriptions = [
subscription.0.set_model(&this, &mut cx),
subscription.1.set_model(&buffer_store, &mut cx),
subscription.2.set_model(&worktree_store, &mut cx),
subscription.3.set_model(&lsp_store, &mut cx),
];
let subscriptions = subscriptions
.into_iter()
.map(|s| match s {
EntitySubscription::BufferStore(subscription) => {
subscription.set_model(&buffer_store, &mut cx)
}
EntitySubscription::WorktreeStore(subscription) => {
subscription.set_model(&worktree_store, &mut cx)
}
EntitySubscription::SettingsObserver(subscription) => {
subscription.set_model(&settings_observer, &mut cx)
}
EntitySubscription::Project(subscription) => subscription.set_model(&this, &mut cx),
EntitySubscription::LspStore(subscription) => {
subscription.set_model(&lsp_store, &mut cx)
}
})
.collect::<Vec<_>>();
let user_ids = response
.payload
@ -924,12 +958,19 @@ impl Project {
) -> Result<Model<Self>> {
client.authenticate_and_connect(true, &cx).await?;
let subscriptions = (
client.subscribe_to_entity::<Self>(remote_id.0)?,
client.subscribe_to_entity::<BufferStore>(remote_id.0)?,
client.subscribe_to_entity::<WorktreeStore>(remote_id.0)?,
client.subscribe_to_entity::<LspStore>(remote_id.0)?,
);
let subscriptions = [
EntitySubscription::Project(client.subscribe_to_entity::<Self>(remote_id.0)?),
EntitySubscription::BufferStore(
client.subscribe_to_entity::<BufferStore>(remote_id.0)?,
),
EntitySubscription::WorktreeStore(
client.subscribe_to_entity::<WorktreeStore>(remote_id.0)?,
),
EntitySubscription::LspStore(client.subscribe_to_entity::<LspStore>(remote_id.0)?),
EntitySubscription::SettingsObserver(
client.subscribe_to_entity::<SettingsObserver>(remote_id.0)?,
),
];
let response = client
.request_envelope(proto::JoinHostedProject {
project_id: remote_id.0,
@ -1047,13 +1088,10 @@ impl Project {
.unwrap();
project.update(cx, |project, cx| {
// In tests we always populate the environment to be empty so we don't run the shell
let tree_id = tree.read(cx).id();
let environment = ProjectEnvironment::test(&[(tree_id, HashMap::default())], cx);
project.environment = environment.clone();
project
.lsp_store
.update(cx, |lsp_store, _| lsp_store.set_environment(environment));
project.environment.update(cx, |environment, _| {
environment.set_cached(&[(tree_id, HashMap::default())])
});
});
tree.update(cx, |tree, _| tree.as_local().unwrap().scan_complete())
@ -1066,6 +1104,10 @@ impl Project {
self.lsp_store.clone()
}
pub fn worktree_store(&self) -> Model<WorktreeStore> {
self.worktree_store.clone()
}
fn on_settings_changed(&mut self, cx: &mut ModelContext<Self>) {
let mut language_servers_to_start = Vec::new();
let mut language_formatters_to_check = Vec::new();
@ -1499,6 +1541,9 @@ impl Project {
self.client
.subscribe_to_entity(project_id)?
.set_model(&self.lsp_store, &mut cx.to_async()),
self.client
.subscribe_to_entity(project_id)?
.set_model(&self.settings_observer, &mut cx.to_async()),
]);
self.buffer_store.update(cx, |buffer_store, cx| {
@ -1510,21 +1555,9 @@ impl Project {
self.lsp_store.update(cx, |lsp_store, cx| {
lsp_store.shared(project_id, self.client.clone().into(), cx)
});
let store = cx.global::<SettingsStore>();
for worktree in self.worktrees(cx) {
let worktree_id = worktree.read(cx).id().to_proto();
for (path, content) in store.local_settings(worktree.entity_id().as_u64() as usize) {
self.client
.send(proto::UpdateWorktreeSettings {
project_id,
worktree_id,
path: path.to_string_lossy().into(),
content: Some(content),
})
.log_err();
}
}
self.settings_observer.update(cx, |settings_observer, cx| {
settings_observer.shared(project_id, self.client.clone().into(), cx)
});
self.client_state = ProjectClientState::Shared {
remote_id: project_id,
@ -1608,6 +1641,9 @@ impl Project {
buffer_store.forget_shared_buffers();
buffer_store.unshared(cx)
});
self.settings_observer.update(cx, |settings_observer, cx| {
settings_observer.unshared(cx);
});
self.client
.send(proto::UnshareProject {
project_id: remote_id,
@ -2147,10 +2183,6 @@ impl Project {
match event {
worktree::Event::UpdatedEntries(changes) => {
if is_local {
this.lsp_store.update(cx, |lsp_store, cx| {
lsp_store
.update_local_worktree_language_servers(&worktree, changes, cx);
});
this.update_local_worktree_settings(&worktree, changes, cx);
this.update_prettier_settings(&worktree, changes, cx);
}
@ -2198,12 +2230,6 @@ impl Project {
}
return;
}
self.environment.update(cx, |environment, _| {
environment.remove_worktree_environment(id_to_remove);
});
self.lsp_store.update(cx, |lsp_store, cx| {
lsp_store.remove_worktree(id_to_remove, cx);
});
let mut prettier_instances_to_clean = FuturesUnordered::new();
if let Some(prettier_paths) = self.prettiers_per_worktree.remove(&id_to_remove) {
@ -3818,11 +3844,8 @@ impl Project {
if worktree.read(cx).is_remote() {
return;
}
let project_id = self.remote_id();
let worktree_id = worktree.entity_id();
let remote_worktree_id = worktree.read(cx).id();
let mut settings_contents = Vec::new();
for (path, _, change) in changes.iter() {
let removed = change == &PathChange::Removed;
let abs_path = match worktree.read(cx).absolutize(path) {
@ -3833,24 +3856,7 @@ impl Project {
}
};
if path.ends_with(local_settings_file_relative_path()) {
let settings_dir = Arc::from(
path.ancestors()
.nth(local_settings_file_relative_path().components().count())
.unwrap(),
);
let fs = self.fs.clone();
settings_contents.push(async move {
(
settings_dir,
if removed {
None
} else {
Some(async move { fs.load(&abs_path).await }.await)
},
)
});
} else if path.ends_with(local_tasks_file_relative_path()) {
if path.ends_with(local_tasks_file_relative_path()) {
self.task_inventory().update(cx, |task_inventory, cx| {
if removed {
task_inventory.remove_local_static_source(&abs_path);
@ -3898,43 +3904,6 @@ impl Project {
})
}
}
if settings_contents.is_empty() {
return;
}
let client = self.client.clone();
cx.spawn(move |_, cx| async move {
let settings_contents: Vec<(Arc<Path>, _)> =
futures::future::join_all(settings_contents).await;
cx.update(|cx| {
cx.update_global::<SettingsStore, _>(|store, cx| {
for (directory, file_content) in settings_contents {
let file_content = file_content.and_then(|content| content.log_err());
store
.set_local_settings(
worktree_id.as_u64() as usize,
directory.clone(),
file_content.as_deref(),
cx,
)
.log_err();
if let Some(remote_id) = project_id {
client
.send(proto::UpdateWorktreeSettings {
project_id: remote_id,
worktree_id: remote_worktree_id.to_proto(),
path: directory.to_string_lossy().into_owned(),
content: file_content,
})
.log_err();
}
}
});
})
.ok();
})
.detach();
}
pub fn set_active_path(&mut self, entry: Option<ProjectPath>, cx: &mut ModelContext<Self>) {
@ -4236,29 +4205,6 @@ impl Project {
})?
}
async fn handle_update_worktree_settings(
this: Model<Self>,
envelope: TypedEnvelope<proto::UpdateWorktreeSettings>,
mut cx: AsyncAppContext,
) -> Result<()> {
this.update(&mut cx, |this, cx| {
let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
if let Some(worktree) = this.worktree_for_id(worktree_id, cx) {
cx.update_global::<SettingsStore, _>(|store, cx| {
store
.set_local_settings(
worktree.entity_id().as_u64() as usize,
PathBuf::from(&envelope.payload.path).into(),
envelope.payload.content.as_deref(),
cx,
)
.log_err();
});
}
Ok(())
})?
}
async fn handle_update_buffer(
this: Model<Self>,
envelope: TypedEnvelope<proto::UpdateBuffer>,