Skip to content

Commit 167d0aa

Browse files
authored
Workspace (#37)
* Worksapce struct * performance: Introduce the Workspace (effectively to cache document retreival)
1 parent 147e738 commit 167d0aa

File tree

4 files changed

+75
-25
lines changed

4 files changed

+75
-25
lines changed

src/app.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::view::session::SessionView;
1717
use crate::view::session::SessionViewMode;
1818
use crate::view::session::SessionViewState;
1919
use crate::view::View;
20+
use crate::workspace::Workspace;
2021
use anyhow::Result;
2122
use crossterm::event::KeyCode;
2223
use log::info;
@@ -186,6 +187,7 @@ pub struct App {
186187
pub command_input: Input,
187188
pub command_response: Option<String>,
188189
pub client: Arc<Mutex<DbgpClient>>,
190+
pub workspace: Workspace,
189191

190192
pub history: History,
191193

@@ -206,7 +208,7 @@ pub struct App {
206208

207209
impl App {
208210
pub fn new(config: Config, receiver: Receiver<AppEvent>, sender: Sender<AppEvent>) -> App {
209-
let client = DbgpClient::new(None);
211+
let client = Arc::new(Mutex::new(DbgpClient::new(None)));
210212
App {
211213
is_connected: false,
212214
config,
@@ -216,7 +218,9 @@ impl App {
216218
sender: sender.clone(),
217219
quit: false,
218220
history: History::default(),
219-
client: Arc::new(Mutex::new(client)),
221+
client: Arc::clone(&client),
222+
workspace: Workspace::new(Arc::clone(&client)),
223+
220224
counter: 0,
221225
context_depth: 4,
222226
stack_max_context_fetch: 1,
@@ -347,24 +351,27 @@ impl App {
347351
}
348352
}
349353
AppEvent::ClientConnected(s) => {
350-
let mut client = self.client.lock().await;
351-
let response = client.deref_mut().connect(s).await?;
352-
for (feature, value) in [
353-
("max_depth", self.context_depth.to_string().as_str()),
354-
("extended_properties", "1"),
355-
] {
356-
info!("setting feature {} to {:?}", feature, value);
357-
client.feature_set(feature, value).await?;
358-
}
354+
let filepath = {
355+
let mut client = self.client.lock().await;
356+
let response = client.deref_mut().connect(s).await?;
357+
for (feature, value) in [
358+
("max_depth", self.context_depth.to_string().as_str()),
359+
("extended_properties", "1"),
360+
] {
361+
info!("setting feature {} to {:?}", feature, value);
362+
client.feature_set(feature, value).await?;
363+
}
364+
response.fileuri.clone()
365+
};
359366
self.is_connected = true;
360367
self.server_status = None;
361368
self.view_current = CurrentView::Session;
362369
self.session_view.mode = SessionViewMode::Current;
363-
let source = client.source(response.fileuri.clone()).await.unwrap();
364370

371+
let source = self.workspace.open(filepath.clone()).await;
365372
self.history = History::default();
366373
self.history
367-
.push(HistoryEntry::initial(response.fileuri.clone(), source));
374+
.push(HistoryEntry::initial(filepath.clone(), source.text.clone()));
368375
}
369376
AppEvent::Snapshot() => {
370377
self.snapshot().await?;
@@ -537,25 +544,24 @@ impl App {
537544

538545
/// capture the current status and push it onto the history stack
539546
pub async fn snapshot(&mut self) -> Result<()> {
540-
let mut client = self.client.lock().await;
541-
let stack = client.deref_mut().get_stack().await?;
547+
let stack = {
548+
self.client.lock().await.deref_mut().get_stack().await?
549+
};
542550
let mut entry = HistoryEntry::new();
543551
for (level, frame) in stack.entries.iter().enumerate() {
544552
let filename = &frame.filename;
545553
let line_no = frame.line;
546-
let context = match (level as u16) < self.stack_max_context_fetch {
547-
true => Some(client.deref_mut().context_get(level as u16).await.unwrap()),
548-
false => None,
554+
let context = {
555+
match (level as u16) < self.stack_max_context_fetch {
556+
true => Some(self.client.lock().await.deref_mut().context_get(level as u16).await.unwrap()),
557+
false => None,
558+
}
549559
};
550-
let source_code = client
551-
.deref_mut()
552-
.source(filename.to_string())
553-
.await
554-
.unwrap();
555560

561+
let document = self.workspace.open(filename.to_string()).await;
556562
let source = SourceContext {
557-
source: source_code,
558-
filename: filename.to_string(),
563+
source: document.text.to_string(),
564+
filename: document.filename.to_string(),
559565
line_no,
560566
};
561567

src/dbgp/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pub struct DbgpClient {
170170
tid: u32,
171171
stream: Option<TcpStream>,
172172
}
173+
173174
impl DbgpClient {
174175
pub(crate) fn new(s: Option<TcpStream>) -> Self {
175176
Self { stream: s, tid: 0 }

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod notification;
66
pub mod view;
77
pub mod analyzer;
88
pub mod theme;
9+
pub mod workspace;
910

1011
use app::App;
1112
use better_panic::Settings;

src/workspace.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::dbgp::client::DbgpClient;
2+
use std::collections::hash_map::Entry;
3+
use std::collections::HashMap;
4+
use std::sync::Arc;
5+
use tokio::sync::Mutex;
6+
7+
pub struct Document {
8+
pub filename: String,
9+
pub text: String,
10+
}
11+
12+
pub struct Workspace {
13+
client: Arc<Mutex<DbgpClient>>,
14+
documents: HashMap<String, Document>,
15+
}
16+
17+
impl Workspace {
18+
pub fn new(client: Arc<Mutex<DbgpClient>>) -> Self {
19+
Workspace {
20+
client,
21+
documents: HashMap::new(),
22+
}
23+
}
24+
25+
pub async fn open(&mut self, filename: String) -> &Document {
26+
let entry = self.documents.entry(filename.clone());
27+
if let Entry::Vacant(entry) = entry {
28+
let source = self.client
29+
.lock()
30+
.await
31+
.source(filename.to_string())
32+
.await
33+
.expect("Could not retrieve source");
34+
entry.insert(Document {
35+
filename: filename.clone(),
36+
text: source.clone(),
37+
});
38+
};
39+
40+
self.documents.get(filename.as_str()).unwrap()
41+
}
42+
}

0 commit comments

Comments
 (0)