1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! `.sql` files and SQL dumps: open, save, save as, and the import dialog.
use std::path::PathBuf;
use gpui_kit::component::WindowExt;
use gpui_kit::prelude::*;
use gpui_kit::{App, Context, Entity, WeakEntity, Window, px};
use crate::ui::import_dialog::{ImportEvent, ImportView};
use crate::ui::sql_file;
use super::{
ImportSqlDump, OpenFile, SaveFile, SaveFileAs, Session, SessionEvent, SessionPanel, Status,
};
impl Session {
pub(super) fn on_open_file(
&mut self,
_: &OpenFile,
_window: &mut Window,
cx: &mut Context<Self>,
) {
self.open_file(cx);
}
pub(super) fn on_save_file(
&mut self,
_: &SaveFile,
_window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(panel) = self.active_panel() {
self.save(&panel, false, cx);
}
}
pub(super) fn on_save_file_as(
&mut self,
_: &SaveFileAs,
_window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(panel) = self.active_panel() {
self.save(&panel, true, cx);
}
}
pub(super) fn on_import_dump(
&mut self,
_: &ImportSqlDump,
_window: &mut Window,
cx: &mut Context<Self>,
) {
self.import_dump(cx);
}
/// Ask for a dump file, then open the import dialog over the session.
pub(crate) fn import_dump(&mut self, cx: &mut Context<Self>) {
let prompt = sql_file::prompt_for_import(cx);
cx.spawn(async move |this, cx| match prompt.await {
Ok(Some(path)) => {
this.update_in(cx, |this, window, cx| {
this.open_import_dialog(path, window, cx)
})
.ok();
}
Ok(None) => {}
Err(error) => {
this.update_in(cx, |this, window, cx| this.report(error, window, cx))
.ok();
}
})
.detach();
}
/// Build the import dialog for `path`, if one is not already open.
pub(super) fn open_import_dialog(
&mut self,
path: PathBuf,
window: &mut Window,
cx: &mut Context<Self>,
) {
if self.import.is_some() {
return;
}
let view = cx.new(|cx| ImportView::new(self.connection.clone(), path, window, cx));
cx.subscribe_in(&view, window, Self::on_import_event)
.detach();
let session = cx.entity().downgrade();
let body = view.clone();
window.open_dialog(cx, move |dialog, _window, _cx| {
let session = session.clone();
dialog
.title("Import SQL dump")
.w(px(560.))
.h(px(460.))
// The dialog's own keys are off so `enter` cannot dismiss it;
// `escape` is bound to `CloseImport` on the body instead.
.keyboard(false)
// Closed from outside its body (the overlay, the close
// button): a run still going is stopped rather than left
// running with nothing on screen to stop it.
.on_close(move |_, _window, cx| {
session
.update(cx, |this, cx| {
if let Some(view) = this.import.take() {
view.update(cx, |view, cx| view.cancel(cx));
}
cx.notify();
})
.ok();
})
.child(body.clone())
});
// Put the keyboard on the body so `escape` reaches `CloseImport`.
view.update(cx, |view, cx| view.focus(window, cx));
self.import = Some(view);
}
/// The dialog's own buttons: dismiss it, or note that the run changed the
/// schema.
fn on_import_event(
&mut self,
_: &Entity<ImportView>,
event: &ImportEvent,
window: &mut Window,
cx: &mut Context<Self>,
) {
match event {
ImportEvent::Dismissed => {
self.import = None;
window.close_dialog(cx);
cx.notify();
}
ImportEvent::Finished => {
// The import wrote objects and rows; the sidebar and any open
// table are stale.
self.refresh(cx);
}
}
}
/// Stop an import in flight, the way [`Self::cancel_query`] gives up
/// on a query.
pub(crate) fn cancel_import(&mut self, cx: &mut Context<Self>) {
if let Some(view) = self.import.clone() {
view.update(cx, |view, cx| view.cancel(cx));
}
}
/// Ask for SQL files and give each one its own tab.
pub(crate) fn open_file(&mut self, cx: &mut Context<Self>) {
let prompt = sql_file::prompt_for_open(cx);
cx.spawn(async move |this, cx| {
let paths = match prompt.await {
Ok(Some(paths)) => paths,
Ok(None) => return,
Err(error) => {
this.update_in(cx, |this, window, cx| this.report(error, window, cx))
.ok();
return;
}
};
for path in paths {
match cx.background_spawn(sql_file::read(path.clone())).await {
Ok(sql) => {
this.update_in(cx, |this, window, cx| {
this.open_file_tab(path, sql, window, cx)
})
.ok();
}
Err(error) => {
this.update_in(cx, |this, window, cx| this.report(error, window, cx))
.ok();
}
}
}
})
.detach();
}
/// Show `sql` read from `path` in a new tab named after the file.
fn open_file_tab(
&mut self,
path: PathBuf,
sql: String,
window: &mut Window,
cx: &mut Context<Self>,
) {
let panel = self.open_tab(None, sql, false, window, cx);
panel.update(cx, |panel, cx| {
panel.set_status(Status::Done(format!("Opened {}", path.display())));
panel.set_file(path, cx);
});
}
/// Save `panel`'s query tab.
///
/// A tab with no file yet — or "Save As", with `ask` set — asks the
/// platform for a path first.
pub(super) fn save(&mut self, panel: &Entity<SessionPanel>, ask: bool, cx: &mut Context<Self>) {
// A table tab has no buffer to save, so saving it writes its staged
// edits instead. `TableView` binds the same key itself, which covers
// the grid having focus; this covers everywhere else in the session.
if panel.read(cx).table_view().is_some() {
panel.update(cx, |panel, cx| panel.commit(cx));
return;
}
let Some(editor) = panel.read(cx).editor() else {
return;
};
let file = panel.read(cx).file_path();
let sql = editor.read(cx).sql(cx);
if let Some(path) = file.clone().filter(|_| !ask) {
self.write(panel.downgrade(), path, sql, cx);
return;
}
let title = panel.read(cx).title();
let prompt = sql_file::prompt_for_save(file.as_deref(), &title, "sql", cx);
let weak = panel.downgrade();
cx.spawn(async move |this, cx| match prompt.await {
Ok(Some(path)) => {
this.update(cx, |this, cx| this.write(weak, path, sql, cx))
.ok();
}
Ok(None) => {}
Err(error) => {
this.update_in(cx, |this, window, cx| this.report(error, window, cx))
.ok();
}
})
.detach();
}
/// Write `sql` to `path`, then bind the tab to it.
pub(super) fn write(
&mut self,
panel: WeakEntity<SessionPanel>,
path: PathBuf,
sql: String,
cx: &mut Context<Self>,
) {
let task = cx.background_spawn(sql_file::write(path.clone(), sql.clone()));
cx.spawn(async move |this, cx| {
let written = task.await;
this.update_in(cx, |_this, window, cx| {
// The tab may have been closed while the file was written.
let Some(panel) = panel.upgrade() else {
return;
};
let error = match written {
Ok(()) => None,
Err(error) => Some(format!("{error:#}")),
};
panel.update(cx, |panel, cx| {
match &error {
None => {
panel.set_status(Status::Done(format!("Saved {}", path.display())));
panel.set_file(path, cx);
panel.set_baseline(sql);
}
Some(error) => panel.set_status(Status::Error(error.clone())),
}
cx.notify();
});
if let Some(error) = error {
crate::ui::notify_error(window, cx, format!("Error: {error}"));
}
// A save changes the file binding and the clean state, both of
// which the saved workspace records.
cx.emit(SessionEvent::Changed);
})
.ok();
})
.detach();
}
/// Put a file error where the user can see it.
///
/// Only query tabs carry a status bar, so an error raised while a table
/// tab is in front goes to the query tab nearest it.
pub(super) fn report(
&mut self,
error: anyhow::Error,
window: &mut Window,
cx: &mut Context<Self>,
) {
let message = format!("{error:#}");
// The toast is raised whether or not there is a status bar to put the
// error in too: with no query tab open there is nowhere else to say it.
if let Some(panel) = self.nearest_query_panel(cx) {
panel.update(cx, |panel, cx| {
panel.set_status(Status::Error(message.clone()));
cx.notify();
});
}
crate::ui::notify_error(window, cx, format!("Error: {message}"));
}
/// The active panel if it holds a query, else the next one that does.
fn nearest_query_panel(&self, cx: &App) -> Option<Entity<SessionPanel>> {
if self.panels.is_empty() {
return None;
}
let start = self.active_tab_index();
(0..self.panels.len())
.map(|offset| (start + offset) % self.panels.len())
.map(|index| &self.panels[index])
.find(|panel| panel.read(cx).is_query())
.cloned()
}
}