Expand doc/view macros to allow fetching specific id

This simplifies the code and hides away unwraps
pull/3663/head
Blaž Hrastnik 2 years ago
parent 1caba2d3e8
commit 6ec4017a8d
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640

@ -1061,7 +1061,7 @@ impl EditorView {
let editor = &mut cxt.editor; let editor = &mut cxt.editor;
if let Some((pos, view_id)) = pos_and_view(editor, row, column) { if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap(); let doc = doc_mut!(editor, &view!(editor, view_id).doc);
if modifiers == KeyModifiers::ALT { if modifiers == KeyModifiers::ALT {
let selection = doc.selection(view_id).clone(); let selection = doc.selection(view_id).clone();
@ -1190,7 +1190,7 @@ impl EditorView {
} }
if let Some((pos, view_id)) = pos_and_view(editor, row, column) { if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap(); let doc = doc_mut!(editor, &view!(editor, view_id).doc);
doc.set_selection(view_id, Selection::point(pos)); doc.set_selection(view_id, Selection::point(pos));
cxt.editor.focus(view_id); cxt.editor.focus(view_id);
commands::MappableCommand::paste_primary_clipboard_before.execute(cxt); commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
@ -1320,8 +1320,8 @@ impl Component for EditorView {
if cx.editor.tree.contains(focus) { if cx.editor.tree.contains(focus) {
let config = cx.editor.config(); let config = cx.editor.config();
let mode = cx.editor.mode(); let mode = cx.editor.mode();
let view = cx.editor.tree.get_mut(focus); let view = view_mut!(cx.editor, focus);
let doc = cx.editor.documents.get_mut(&view.doc).unwrap(); let doc = doc_mut!(cx.editor, &view.doc);
view.ensure_cursor_in_view(doc, config.scrolloff); view.ensure_cursor_in_view(doc, config.scrolloff);

@ -890,7 +890,7 @@ impl Editor {
view.doc = doc_id; view.doc = doc_id;
view.offset = Position::default(); view.offset = Position::default();
let doc = self.documents.get_mut(&doc_id).unwrap(); let doc = doc_mut!(self, &doc_id);
doc.ensure_view_init(view.id); doc.ensure_view_init(view.id);
// TODO: reuse align_view // TODO: reuse align_view
@ -961,7 +961,7 @@ impl Editor {
} }
Action::Load => { Action::Load => {
let view_id = view!(self).id; let view_id = view!(self).id;
let doc = self.documents.get_mut(&id).unwrap(); let doc = doc_mut!(self, &id);
doc.ensure_view_init(view_id); doc.ensure_view_init(view_id);
return; return;
} }
@ -982,7 +982,7 @@ impl Editor {
}, },
); );
// initialize selection for view // initialize selection for view
let doc = self.documents.get_mut(&id).unwrap(); let doc = doc_mut!(self, &id);
doc.ensure_view_init(view_id); doc.ensure_view_init(view_id);
} }
} }
@ -1036,9 +1036,9 @@ impl Editor {
} }
pub fn close(&mut self, id: ViewId) { pub fn close(&mut self, id: ViewId) {
let view = self.tree.get(self.tree.focus); let (_view, doc) = current!(self);
// remove selection // remove selection
self.documents.get_mut(&view.doc).unwrap().remove_view(id); doc.remove_view(id);
self.tree.remove(id); self.tree.remove(id);
self._refresh(); self._refresh();
} }
@ -1112,7 +1112,7 @@ impl Editor {
.unwrap_or_else(|| self.new_document(Document::default())); .unwrap_or_else(|| self.new_document(Document::default()));
let view = View::new(doc_id, self.config().gutters.clone()); let view = View::new(doc_id, self.config().gutters.clone());
let view_id = self.tree.insert(view); let view_id = self.tree.insert(view);
let doc = self.documents.get_mut(&doc_id).unwrap(); let doc = doc_mut!(self, &doc_id);
doc.ensure_view_init(view_id); doc.ensure_view_init(view_id);
} }

@ -14,7 +14,7 @@ macro_rules! current {
($editor:expr) => {{ ($editor:expr) => {{
let view = $crate::view_mut!($editor); let view = $crate::view_mut!($editor);
let id = view.doc; let id = view.doc;
let doc = $editor.documents.get_mut(&id).unwrap(); let doc = $crate::doc_mut!($editor, &id);
(view, doc) (view, doc)
}}; }};
} }
@ -32,6 +32,9 @@ macro_rules! current_ref {
/// Returns `&mut Document` /// Returns `&mut Document`
#[macro_export] #[macro_export]
macro_rules! doc_mut { macro_rules! doc_mut {
($editor:expr, $id:expr) => {{
$editor.documents.get_mut($id).unwrap()
}};
($editor:expr) => {{ ($editor:expr) => {{
$crate::current!($editor).1 $crate::current!($editor).1
}}; }};
@ -41,6 +44,9 @@ macro_rules! doc_mut {
/// Returns `&mut View` /// Returns `&mut View`
#[macro_export] #[macro_export]
macro_rules! view_mut { macro_rules! view_mut {
($editor:expr, $id:expr) => {{
$editor.tree.get_mut($id)
}};
($editor:expr) => {{ ($editor:expr) => {{
$editor.tree.get_mut($editor.tree.focus) $editor.tree.get_mut($editor.tree.focus)
}}; }};
@ -50,6 +56,9 @@ macro_rules! view_mut {
/// Returns `&View` /// Returns `&View`
#[macro_export] #[macro_export]
macro_rules! view { macro_rules! view {
($editor:expr, $id:expr) => {{
$editor.tree.get($id)
}};
($editor:expr) => {{ ($editor:expr) => {{
$editor.tree.get($editor.tree.focus) $editor.tree.get($editor.tree.focus)
}}; }};
@ -57,6 +66,9 @@ macro_rules! view {
#[macro_export] #[macro_export]
macro_rules! doc { macro_rules! doc {
($editor:expr, $id:expr) => {{
$editor.documents[$id]
}};
($editor:expr) => {{ ($editor:expr) => {{
$crate::current_ref!($editor).1 $crate::current_ref!($editor).1
}}; }};

Loading…
Cancel
Save