| 1 |
#[macro_use] |
| 2 |
pub mod macros; |
| 3 |
|
| 4 |
pub mod action; |
| 5 |
pub mod annotations; |
| 6 |
pub mod clipboard; |
| 7 |
pub mod document; |
| 8 |
pub mod editor; |
| 9 |
pub mod events; |
| 10 |
pub mod expansion; |
| 11 |
pub mod graphics; |
| 12 |
pub mod gutter; |
| 13 |
pub mod handlers; |
| 14 |
pub mod info; |
| 15 |
pub mod input; |
| 16 |
pub mod keyboard; |
| 17 |
pub mod register; |
| 18 |
pub mod theme; |
| 19 |
pub mod tree; |
| 20 |
pub mod view; |
| 21 |
|
| 22 |
use std::num::NonZeroUsize; |
| 23 |
|
| 24 |
|
| 25 |
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] |
| 26 |
pub struct DocumentId(NonZeroUsize); |
| 27 |
|
| 28 |
impl Default for DocumentId { |
| 29 |
fn default() -> DocumentId { |
| 30 |
DocumentId(NonZeroUsize::new(1).unwrap()) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
#[cfg(test)] |
| 35 |
impl DocumentId { |
| 36 |
|
| 37 |
|
| 38 |
pub(crate) fn new(id: usize) -> DocumentId { |
| 39 |
DocumentId(NonZeroUsize::new(id).expect("document id must be non-zero")) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
impl std::fmt::Display for DocumentId { |
| 44 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 45 |
f.write_fmt(format_args!("{}", self.0)) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
slotmap::new_key_type! { |
| 50 |
pub struct ViewId; |
| 51 |
} |
| 52 |
|
| 53 |
pub enum Align { |
| 54 |
Top, |
| 55 |
Center, |
| 56 |
Bottom, |
| 57 |
} |
| 58 |
|
| 59 |
pub fn align_view(doc: &mut Document, view: &View, align: Align) { |
| 60 |
let doc_text = doc.text().slice(..); |
| 61 |
let cursor = doc.selection(view.id).primary().cursor(doc_text); |
| 62 |
let viewport = view.inner_area(doc); |
| 63 |
let last_line_height = viewport.height.saturating_sub(1); |
| 64 |
let mut view_offset = doc.view_offset(view.id); |
| 65 |
|
| 66 |
let relative = match align { |
| 67 |
Align::Center => last_line_height / 2, |
| 68 |
Align::Top => 0, |
| 69 |
Align::Bottom => last_line_height, |
| 70 |
}; |
| 71 |
|
| 72 |
let text_fmt = doc.text_format(viewport.width, None); |
| 73 |
(view_offset.anchor, view_offset.vertical_offset) = char_idx_at_visual_offset( |
| 74 |
doc_text, |
| 75 |
cursor, |
| 76 |
-(relative as isize), |
| 77 |
0, |
| 78 |
&text_fmt, |
| 79 |
&view.text_annotations(doc, None), |
| 80 |
); |
| 81 |
doc.set_view_offset(view.id, view_offset); |
| 82 |
} |
| 83 |
|
| 84 |
pub use document::Document; |
| 85 |
pub use editor::Editor; |
| 86 |
use helix_core::char_idx_at_visual_offset; |
| 87 |
pub use theme::Theme; |
| 88 |
pub use view::View; |