Improve documentation

This commit is contained in:
Edgar Geier 2023-03-12 10:30:27 +01:00
parent 384c5c1fdc
commit 3331f41d7a
No known key found for this signature in database
GPG key ID: DE2B55319457EB56
6 changed files with 37 additions and 14 deletions

View file

@ -333,6 +333,7 @@ impl Buffer {
buffer buffer
} }
/// Mutably borrows the buffer together with an [`FontSystem`] for more convenient methods
pub fn borrow_with<'a>( pub fn borrow_with<'a>(
&'a mut self, &'a mut self,
font_system: &'a mut FontSystem, font_system: &'a mut FontSystem,
@ -343,7 +344,7 @@ impl Buffer {
} }
} }
pub(crate) fn relayout(&mut self, font_system: &mut FontSystem) { fn relayout(&mut self, font_system: &mut FontSystem) {
#[cfg(all(feature = "std", not(target_arch = "wasm32")))] #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
let instant = std::time::Instant::now(); let instant = std::time::Instant::now();
@ -360,7 +361,8 @@ impl Buffer {
log::debug!("relayout: {:?}", instant.elapsed()); log::debug!("relayout: {:?}", instant.elapsed());
} }
pub(crate) fn shape_until(&mut self, font_system: &mut FontSystem, lines: i32) -> i32 { /// Pre-shape lines in the buffer, up to `lines`, return actual number of layout lines
pub fn shape_until(&mut self, font_system: &mut FontSystem, lines: i32) -> i32 {
#[cfg(all(feature = "std", not(target_arch = "wasm32")))] #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
let instant = std::time::Instant::now(); let instant = std::time::Instant::now();
@ -387,7 +389,8 @@ impl Buffer {
total_layout total_layout
} }
pub(crate) fn shape_until_cursor(&mut self, font_system: &mut FontSystem, cursor: Cursor) { /// Shape lines until cursor, also scrolling to include cursor in view
pub fn shape_until_cursor(&mut self, font_system: &mut FontSystem, cursor: Cursor) {
#[cfg(all(feature = "std", not(target_arch = "wasm32")))] #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
let instant = std::time::Instant::now(); let instant = std::time::Instant::now();
@ -427,7 +430,8 @@ impl Buffer {
self.shape_until_scroll(font_system); self.shape_until_scroll(font_system);
} }
pub(crate) fn shape_until_scroll(&mut self, font_system: &mut FontSystem) { /// Shape lines until scroll
pub fn shape_until_scroll(&mut self, font_system: &mut FontSystem) {
let lines = self.visible_lines(); let lines = self.visible_lines();
let scroll_end = self.scroll + lines; let scroll_end = self.scroll + lines;
@ -466,7 +470,8 @@ impl Buffer {
LayoutCursor::new(cursor.line, 0, 0) LayoutCursor::new(cursor.line, 0, 0)
} }
pub(crate) fn line_shape( /// Shape the provided line index and return the result
pub fn line_shape(
&mut self, &mut self,
font_system: &mut FontSystem, font_system: &mut FontSystem,
line_i: usize, line_i: usize,
@ -475,7 +480,8 @@ impl Buffer {
Some(line.shape(font_system)) Some(line.shape(font_system))
} }
pub(crate) fn line_layout( /// Lay out the provided line index and return the result
pub fn line_layout(
&mut self, &mut self,
font_system: &mut FontSystem, font_system: &mut FontSystem,
line_i: usize, line_i: usize,
@ -489,7 +495,12 @@ impl Buffer {
self.metrics self.metrics
} }
fn set_metrics(&mut self, font_system: &mut FontSystem, metrics: Metrics) { /// Set the current [`Metrics`]
///
/// # Panics
///
/// Will panic if `metrics.font_size` is zero.
pub fn set_metrics(&mut self, font_system: &mut FontSystem, metrics: Metrics) {
if metrics != self.metrics { if metrics != self.metrics {
assert_ne!(metrics.font_size, 0.0, "font size cannot be 0"); assert_ne!(metrics.font_size, 0.0, "font size cannot be 0");
self.metrics = metrics; self.metrics = metrics;
@ -503,7 +514,8 @@ impl Buffer {
self.wrap self.wrap
} }
pub(crate) fn set_wrap(&mut self, font_system: &mut FontSystem, wrap: Wrap) { /// Set the current [`Wrap`]
pub fn set_wrap(&mut self, font_system: &mut FontSystem, wrap: Wrap) {
if wrap != self.wrap { if wrap != self.wrap {
self.wrap = wrap; self.wrap = wrap;
self.relayout(font_system); self.relayout(font_system);
@ -516,7 +528,8 @@ impl Buffer {
(self.width, self.height) (self.width, self.height)
} }
pub(crate) fn set_size(&mut self, font_system: &mut FontSystem, width: f32, height: f32) { /// Set the current buffer dimensions
pub fn set_size(&mut self, font_system: &mut FontSystem, width: f32, height: f32) {
let clamped_width = width.max(0.0); let clamped_width = width.max(0.0);
let clamped_height = height.max(0.0); let clamped_height = height.max(0.0);
@ -546,7 +559,8 @@ impl Buffer {
(self.height / self.metrics.line_height) as i32 (self.height / self.metrics.line_height) as i32
} }
pub(crate) fn set_text(&mut self, font_system: &mut FontSystem, text: &str, attrs: Attrs) { /// Set text of buffer, using provided attributes for each line by default
pub fn set_text(&mut self, font_system: &mut FontSystem, text: &str, attrs: Attrs) {
self.lines.clear(); self.lines.clear();
for line in text.lines() { for line in text.lines() {
self.lines self.lines
@ -679,8 +693,9 @@ impl Buffer {
new_cursor_opt new_cursor_opt
} }
/// Draw the buffer
#[cfg(feature = "swash")] #[cfg(feature = "swash")]
pub(crate) fn draw<F>( pub fn draw<F>(
&self, &self,
font_system: &mut FontSystem, font_system: &mut FontSystem,
cache: &mut crate::SwashCache, cache: &mut crate::SwashCache,

View file

@ -35,7 +35,7 @@ impl Editor {
} }
} }
pub(crate) fn set_layout_cursor(&mut self, font_system: &mut FontSystem, cursor: LayoutCursor) { fn set_layout_cursor(&mut self, font_system: &mut FontSystem, cursor: LayoutCursor) {
let layout = self let layout = self
.buffer .buffer
.line_layout(font_system, cursor.line) .line_layout(font_system, cursor.line)

View file

@ -79,6 +79,7 @@ pub enum Action {
/// A trait to allow easy replacements of [`Editor`], like `SyntaxEditor` /// A trait to allow easy replacements of [`Editor`], like `SyntaxEditor`
pub trait Edit { pub trait Edit {
/// Mutably borrows `self` together with an [`FontSystem`] for more convenient methods
fn borrow_with<'a>( fn borrow_with<'a>(
&'a mut self, &'a mut self,
font_system: &'a mut FontSystem, font_system: &'a mut FontSystem,

View file

@ -60,8 +60,13 @@ impl<'a> SyntaxEditor<'a> {
}) })
} }
/// Load text from a file, and also set syntax to the best option
///
/// ## Errors
///
/// Returns an [`io::Error`] if reading the file fails
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub(crate) fn load_text<P: AsRef<Path>>( pub fn load_text<P: AsRef<Path>>(
&mut self, &mut self,
font_system: &mut FontSystem, font_system: &mut FontSystem,
path: P, path: P,

View file

@ -29,8 +29,9 @@ impl<'a> ViEditor<'a> {
} }
} }
/// Load text from a file, and also set syntax to the best option
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub(crate) fn load_text<P: AsRef<std::path::Path>>( pub fn load_text<P: AsRef<std::path::Path>>(
&mut self, &mut self,
font_system: &mut FontSystem, font_system: &mut FontSystem,
path: P, path: P,

View file

@ -13,6 +13,7 @@ mod std;
// re-export fontdb // re-export fontdb
pub use fontdb; pub use fontdb;
/// A value borrowed together with an [`FontSystem`]
pub struct BorrowedWithFontSystem<'a, T> { pub struct BorrowedWithFontSystem<'a, T> {
pub(crate) inner: &'a mut T, pub(crate) inner: &'a mut T,
pub(crate) font_system: &'a mut FontSystem, pub(crate) font_system: &'a mut FontSystem,