From def26966d22dfc4b9a74e51542db246ba52f3dc9 Mon Sep 17 00:00:00 2001
From: nuid32 <91177333+nuid32@users.noreply.github.com>
Date: Sun, 5 Mar 2023 16:43:24 +0500
Subject: [PATCH] Fix lacking space panic (#6109)

* Fix lack of space for popup crash

* Fix saturating -> wrapping

* Fix wrapping -> saturating (I am an idiot)

* Remove useless "mut" in helix-tui/src/buffer.rs

Co-authored-by: Michael Davis <mcarsondavis@gmail.com>

* Remove redundant bound-check

* Return bound-check back

* Add bound-check for set_style

* Remove set_style bound-check

* Revert bound-check

---------

Co-authored-by: Michael Davis <mcarsondavis@gmail.com>
---
 helix-term/src/ui/markdown.rs | 5 +----
 helix-view/src/graphics.rs    | 4 ++--
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs
index 923dd73a..87136992 100644
--- a/helix-term/src/ui/markdown.rs
+++ b/helix-term/src/ui/markdown.rs
@@ -342,13 +342,10 @@ impl Component for Markdown {
 
     fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
         let padding = 2;
-        if padding >= viewport.1 || padding >= viewport.0 {
-            return None;
-        }
         let contents = self.parse(None);
 
         // TODO: account for tab width
-        let max_text_width = (viewport.0 - padding).min(120);
+        let max_text_width = (viewport.0.saturating_sub(padding)).min(120);
         let (width, height) = crate::ui::text::required_size(&contents, max_text_width);
 
         Some((width + padding, height + padding))
diff --git a/helix-view/src/graphics.rs b/helix-view/src/graphics.rs
index a0b645fa..e813fb56 100644
--- a/helix-view/src/graphics.rs
+++ b/helix-view/src/graphics.rs
@@ -237,8 +237,8 @@ impl Rect {
         Rect {
             x: x1,
             y: y1,
-            width: x2 - x1,
-            height: y2 - y1,
+            width: x2.saturating_sub(x1),
+            height: y2.saturating_sub(y1),
         }
     }