Skip to content

Commit

Permalink
Integrate debug_panel_item table into collab msg handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Eid committed Jan 8, 2025
1 parent 21c1053 commit 034004e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 103 deletions.
99 changes: 67 additions & 32 deletions crates/collab/src/db/queries/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ impl Database {
) -> Result<TransactionGuard<HashSet<ConnectionId>>> {
let project_id = ProjectId::from_proto(update.project_id);
self.project_transaction(project_id, |tx| async move {
let mut debug_client = debug_clients::Entity::find()
let debug_client = debug_clients::Entity::find()
.filter(
Condition::all()
.add(debug_clients::Column::ProjectId.eq(project_id))
Expand All @@ -499,23 +499,50 @@ impl Database {
project_id: ActiveValue::Set(project_id),
session_id: ActiveValue::Set(update.session_id as i64),
capabilities: ActiveValue::Set(0),
panel_item: ActiveValue::Set(vec![]),
};
debug_client = Some(new_debug_client.insert(&*tx).await?);
new_debug_client.insert(&*tx).await?;
}

let mut debug_client = debug_client.unwrap();
let mut debug_panel_item = debug_panel_items::Entity::find()
.filter(
Condition::all()
.add(debug_panel_items::Column::ProjectId.eq(project_id))
.add(debug_panel_items::Column::SessionId.eq(update.session_id as i64))
.add(debug_panel_items::Column::ThreadId.eq(update.thread_id as i64))
.add(debug_panel_items::Column::Id.eq(update.client_id as i64)),
)
.one(&*tx)
.await?;

debug_client
.set_panel_item(update)
.with_context(|| "attempting to set panel item")?;
if debug_panel_item.is_none() {
let new_debug_panel_item = debug_panel_items::ActiveModel {
id: ActiveValue::Set(update.client_id as i64),
project_id: ActiveValue::Set(project_id),
session_id: ActiveValue::Set(update.session_id as i64),
thread_id: ActiveValue::Set(update.thread_id as i64),
seassion_name: ActiveValue::Set(update.session_name.clone()),
..Default::default()
};

debug_clients::Entity::update(debug_clients::ActiveModel {
id: ActiveValue::Unchanged(debug_client.id),
project_id: ActiveValue::Unchanged(debug_client.project_id),
session_id: ActiveValue::Unchanged(debug_client.session_id),
capabilities: ActiveValue::Unchanged(debug_client.capabilities),
panel_item: ActiveValue::Set(debug_client.panel_item),
debug_panel_item = Some(new_debug_panel_item.insert(&*tx).await?);
};

let mut debug_panel_item = debug_panel_item.unwrap();

debug_panel_item.set_panel_item(&update);
debug_panel_items::Entity::update(debug_panel_items::ActiveModel {
id: ActiveValue::Unchanged(debug_panel_item.id),
project_id: ActiveValue::Unchanged(debug_panel_item.project_id),
session_id: ActiveValue::Unchanged(debug_panel_item.session_id),
thread_id: ActiveValue::Unchanged(debug_panel_item.thread_id),
seassion_name: ActiveValue::Unchanged(debug_panel_item.seassion_name),
active_thread_item: ActiveValue::Set(debug_panel_item.active_thread_item),
console: ActiveValue::Set(debug_panel_item.console),
module_list: ActiveValue::Set(debug_panel_item.module_list),
thread_state: ActiveValue::Set(debug_panel_item.thread_state),
variable_list: ActiveValue::Set(debug_panel_item.variable_list),
stack_frame_list: ActiveValue::Set(debug_panel_item.stack_frame_list),
loaded_source_list: ActiveValue::Set(debug_panel_item.loaded_source_list),
})
.exec(&*tx)
.await?;
Expand Down Expand Up @@ -548,7 +575,6 @@ impl Database {
project_id: ActiveValue::Set(project_id),
session_id: ActiveValue::Set(update.session_id as i64),
capabilities: ActiveValue::Set(0),
panel_item: ActiveValue::Set(vec![]),
};
debug_client = Some(new_debug_client.insert(&*tx).await?);
}
Expand All @@ -562,7 +588,6 @@ impl Database {
project_id: ActiveValue::Unchanged(debug_client.project_id),
session_id: ActiveValue::Unchanged(debug_client.session_id),
capabilities: ActiveValue::Set(debug_client.capabilities),
panel_item: ActiveValue::Unchanged(debug_client.panel_item),
})
.exec(&*tx)
.await?;
Expand Down Expand Up @@ -881,26 +906,36 @@ impl Database {
.push(debug_client);
}

let debug_sessions = debug_sessions
.into_iter()
.map(|(session_id, clients)| {
let debug_clients = clients
let mut sessions: Vec<_> = Vec::default();

for (session_id, clients) in debug_sessions.into_iter() {
let mut debug_clients = Vec::default();

for debug_client in clients.into_iter() {
let debug_panel_items = debug_client
.find_related(debug_panel_items::Entity)
.all(tx)
.await?
.into_iter()
.map(|debug_client| proto::DebugClient {
client_id: debug_client.id as u64,
capabilities: Some(debug_client.capabilities()),
debug_panel_item: debug_client.panel_item().log_err(),
..Default::default()
})
.map(|item| item.panel_item())
.collect();

proto::DebuggerSession {
project_id,
session_id: session_id as u64,
clients: debug_clients,
}
})
.collect();
debug_clients.push(proto::DebugClient {
client_id: debug_client.id as u64,
capabilities: Some(debug_client.capabilities()),
debug_panel_items,
active_debug_line: None,
});
}

sessions.push(proto::DebuggerSession {
project_id,
session_id: session_id as u64,
clients: debug_clients,
});
}

let debug_sessions = sessions;

let mut breakpoints: HashMap<proto::ProjectPath, HashSet<proto::Breakpoint>> =
HashMap::default();
Expand Down
68 changes: 6 additions & 62 deletions crates/collab/src/db/tables/debug_clients.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::db::ProjectId;
use anyhow::Result;
use prost::Message;
use rpc::proto::{SetDebugClientCapabilities, SetDebuggerPanelItem};
use rpc::proto::SetDebugClientCapabilities;
use sea_orm::entity::prelude::*;

const SUPPORTS_LOADED_SOURCES_REQUEST_BIT: u32 = 0;
Expand All @@ -23,7 +22,6 @@ pub struct Model {
pub session_id: i64,
#[sea_orm(column_type = "Integer")]
pub capabilities: i32,
pub panel_item: Vec<u8>,
}

impl Model {
Expand Down Expand Up @@ -73,18 +71,6 @@ impl Model {

self.capabilities = capabilities_bit_mask;
}

pub fn set_panel_item(&mut self, item: &SetDebuggerPanelItem) -> Result<()> {
let mut buf = Vec::new();
item.encode(&mut buf)?;
self.panel_item = buf;
Ok(())
}

pub fn panel_item(&self) -> Result<SetDebuggerPanelItem> {
SetDebuggerPanelItem::decode(&self.panel_item[..])
.map_err(|e| anyhow::anyhow!("Failed to decode panel item: {}", e))
}
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand All @@ -105,52 +91,10 @@ impl Related<super::project::Entity> for Entity {
}
}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DebugClientCapabilities {
pub supports_loaded_sources_request: bool,
pub supports_modules_request: bool,
pub supports_restart_request: bool,
pub supports_set_expression: bool,
pub supports_single_thread_execution_requests: bool,
pub supports_step_back: bool,
pub supports_stepping_granularity: bool,
pub supports_terminate_threads_request: bool,
}

impl DebugClientCapabilities {
pub fn to_u32(&self) -> u32 {
let mut result = 0;
result |=
(self.supports_loaded_sources_request as u32) << SUPPORTS_LOADED_SOURCES_REQUEST_BIT;
result |= (self.supports_modules_request as u32) << SUPPORTS_MODULES_REQUEST_BIT;
result |= (self.supports_restart_request as u32) << SUPPORTS_RESTART_REQUEST_BIT;
result |= (self.supports_set_expression as u32) << SUPPORTS_SET_EXPRESSION_BIT;
result |= (self.supports_single_thread_execution_requests as u32)
<< SUPPORTS_SINGLE_THREAD_EXECUTION_REQUESTS_BIT;
result |= (self.supports_step_back as u32) << SUPPORTS_STEP_BACK_BIT;
result |= (self.supports_stepping_granularity as u32) << SUPPORTS_STEPPING_GRANULARITY_BIT;
result |= (self.supports_terminate_threads_request as u32)
<< SUPPORTS_TERMINATE_THREADS_REQUEST_BIT;
result
}

pub fn from_u32(value: u32) -> Self {
Self {
supports_loaded_sources_request: (value & (1 << SUPPORTS_LOADED_SOURCES_REQUEST_BIT))
!= 0,
supports_modules_request: (value & (1 << SUPPORTS_MODULES_REQUEST_BIT)) != 0,
supports_restart_request: (value & (1 << SUPPORTS_RESTART_REQUEST_BIT)) != 0,
supports_set_expression: (value & (1 << SUPPORTS_SET_EXPRESSION_BIT)) != 0,
supports_single_thread_execution_requests: (value
& (1 << SUPPORTS_SINGLE_THREAD_EXECUTION_REQUESTS_BIT))
!= 0,
supports_step_back: (value & (1 << SUPPORTS_STEP_BACK_BIT)) != 0,
supports_stepping_granularity: (value & (1 << SUPPORTS_STEPPING_GRANULARITY_BIT)) != 0,
supports_terminate_threads_request: (value
& (1 << SUPPORTS_TERMINATE_THREADS_REQUEST_BIT))
!= 0,
}
impl Related<super::debug_panel_items::Entity> for Entity {
fn to() -> RelationDef {
Relation::DebugPanelItems.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
4 changes: 1 addition & 3 deletions crates/collab/src/db/tables/debug_panel_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Model {
}

impl Model {
pub fn set_panel_item(&mut self, item: &SetDebuggerPanelItem) -> Result<()> {
pub fn set_panel_item(&mut self, item: &SetDebuggerPanelItem) {
let mut buf = Vec::new();

self.active_thread_item = item.active_thread_item;
Expand Down Expand Up @@ -72,8 +72,6 @@ impl Model {
self.loaded_source_list.clone_from(&buf);
}
}

Ok(())
}

pub fn panel_item(&self) -> SetDebuggerPanelItem {
Expand Down
10 changes: 5 additions & 5 deletions crates/project/src/dap_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,11 +1509,11 @@ impl DapStore {
.map(|session| (session.session_id, session.clients))
{
for debug_client in debug_clients {
if let Some(panel_item) = debug_client.debug_panel_item {
if let DapStoreMode::Remote(remote) = &mut self.mode {
if let Some(queue) = &mut remote.event_queue {
queue.push_back(DapStoreEvent::SetDebugPanelItem(panel_item));
}
if let DapStoreMode::Remote(remote) = &mut self.mode {
if let Some(queue) = &mut remote.event_queue {
debug_client.debug_panel_items.into_iter().for_each(|item| {
queue.push_back(DapStoreEvent::SetDebugPanelItem(item));
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/proto/proto/zed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,7 @@ message DebugClient {
uint64 client_id = 1;
SetDebugClientCapabilities capabilities = 2;
SetActiveDebugLine active_debug_line = 3;
optional SetDebuggerPanelItem debug_panel_item = 4;
repeated SetDebuggerPanelItem debug_panel_items = 4;
}

message ShutdownDebugClient {
Expand Down

0 comments on commit 034004e

Please sign in to comment.