on master help sign in
Document macros (#693)

* add docs * clean up * remove * more * Update helix-view/src/macros.rs Co-authored-by: Ivan Tham <pickfire@riseup.net> Co-authored-by: Ivan Tham <pickfire@riseup.net>

commit99a753a5797d38885560e9026b86952615032556 authoroberblastmeister <61095988+oberblastmeister@users.noreply.github.com> dateSat 4 Sep 2021, 23:42 (5 years ago) parente4e93e1
2 files changed+17−5
helix-view/src/editor.rs−5 helix-view/src/macros.rs+17
helix-view/src/editor.rs−5
@@ -345,11 +345,6 @@ impl Editor {
345345 .find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
346346 }
347347
348− // pub fn current_document(&self) -> Document {
349− // let id = self.view().doc;
350− // let doc = &mut editor.documents[id];
351− // }
352−
353348 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
354349 let view = view!(self);
355350 let doc = &self.documents[view.doc];
helix-view/src/macros.rs+17
@@ -1,3 +1,14 @@
1+//! These are macros to make getting very nested fields in the `Editor` struct easier
2+//! These are macros instead of functions because functions will have to take `&mut self`
3+//! However, rust doesn't know that you only want a partial borrow instead of borrowing the
4+//! entire struct which `&mut self` says. This makes it impossible to do other mutable
5+//! stuff to the struct because it is already borrowed. Because macros are expanded,
6+//! this circumvents the problem because it is just like indexing fields by hand and then
7+//! putting a `&mut` in front of it. This way rust can see that we are only borrowing a
8+//! part of the struct and not the entire thing.
9+
10+/// Get the current view and document mutably as a tuple.
11+/// Returns `(&mut View, &mut Document)`
112#[macro_export]
213macro_rules! current {
314 ( $( $editor:ident ).+ ) => {{
@@ -7,6 +18,8 @@ macro_rules! current {
718 }};
819}
920
21+/// Get the current document mutably.
22+/// Returns `&mut Document`
1023#[macro_export]
1124macro_rules! doc_mut {
1225 ( $( $editor:ident ).+ ) => {{
@@ -14,6 +27,8 @@ macro_rules! doc_mut {
1427 }};
1528}
1629
30+/// Get the current view mutably.
31+/// Returns `&mut View`
1732#[macro_export]
1833macro_rules! view_mut {
1934 ( $( $editor:ident ).+ ) => {{
@@ -21,6 +36,8 @@ macro_rules! view_mut {
2136 }};
2237}
2338
39+/// Get the current view immutably
40+/// Returns `&View`
2441#[macro_export]
2542macro_rules! view {
2643 ( $( $editor:ident ).+ ) => {{