on master help sign in
Document macros (#693)
99a753aoberblastmeister
helix-view/src/editor.rs−5
@@ -345,11 +345,6 @@ impl Editor {
345 .find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
345 .find(|doc| doc.path().map(|p| p == path.as_ref()).unwrap_or(false))
346 }
346 }
347
347
348− // pub fn current_document(&self) -> Document {
349− // let id = self.view().doc;
350− // let doc = &mut editor.documents[id];
351− // }
352−
353 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
348 pub fn cursor(&self) -> (Option<Position>, CursorKind) {
354 let view = view!(self);
349 let view = view!(self);
355 let doc = &self.documents[view.doc];
350 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)`
1#[macro_export]
12#[macro_export]
2macro_rules! current {
13macro_rules! current {
3 ( $( $editor:ident ).+ ) => {{
14 ( $( $editor:ident ).+ ) => {{
@@ -7,6 +18,8 @@ macro_rules! current {
7 }};
18 }};
8}
19}
9
20
21+/// Get the current document mutably.
22+/// Returns `&mut Document`
10#[macro_export]
23#[macro_export]
11macro_rules! doc_mut {
24macro_rules! doc_mut {
12 ( $( $editor:ident ).+ ) => {{
25 ( $( $editor:ident ).+ ) => {{
@@ -14,6 +27,8 @@ macro_rules! doc_mut {
14 }};
27 }};
15}
28}
16
29
30+/// Get the current view mutably.
31+/// Returns `&mut View`
17#[macro_export]
32#[macro_export]
18macro_rules! view_mut {
33macro_rules! view_mut {
19 ( $( $editor:ident ).+ ) => {{
34 ( $( $editor:ident ).+ ) => {{
@@ -21,6 +36,8 @@ macro_rules! view_mut {
21 }};
36 }};
22}
37}
23
38
39+/// Get the current view immutably
40+/// Returns `&View`
24#[macro_export]
41#[macro_export]
25macro_rules! view {
42macro_rules! view {
26 ( $( $editor:ident ).+ ) => {{
43 ( $( $editor:ident ).+ ) => {{