aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA-Walrus2022-09-01 16:59:39 +0000
committerGitHub2022-09-01 16:59:39 +0000
commit45dbcb67832e31ca5eea936628f34f5dc26c4381 (patch)
treef79b049f772f2645537b4c4388cfa3cf2049c158
parentec28b2b5ccf162e9df550e59a28e043d51796621 (diff)
Fix closing buffer with custom keymap (#3633)
* Fix closing buffer with custom keymap * Add comment explaining if
-rw-r--r--helix-term/src/ui/editor.rs25
-rw-r--r--helix-view/src/tree.rs17
2 files changed, 31 insertions, 11 deletions
diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs
index 539e164c..fa437a7e 100644
--- a/helix-term/src/ui/editor.rs
+++ b/helix-term/src/ui/editor.rs
@@ -1263,17 +1263,20 @@ impl Component for EditorView {
if cx.editor.should_close() {
return EventResult::Ignored(None);
}
- let config = cx.editor.config();
- let mode = cx.editor.mode();
- let view = cx.editor.tree.get_mut(focus);
- let doc = cx.editor.documents.get_mut(&view.doc).unwrap();
-
- view.ensure_cursor_in_view(doc, config.scrolloff);
-
- // Store a history state if not in insert mode. This also takes care of
- // committing changes when leaving insert mode.
- if mode != Mode::Insert {
- doc.append_changes_to_history(view.id);
+ // if the focused view still exists and wasn't closed
+ if cx.editor.tree.contains(focus) {
+ let config = cx.editor.config();
+ let mode = cx.editor.mode();
+ let view = cx.editor.tree.get_mut(focus);
+ let doc = cx.editor.documents.get_mut(&view.doc).unwrap();
+
+ view.ensure_cursor_in_view(doc, config.scrolloff);
+
+ // Store a history state if not in insert mode. This also takes care of
+ // committing changes when leaving insert mode.
+ if mode != Mode::Insert {
+ doc.append_changes_to_history(view.id);
+ }
}
EventResult::Consumed(callback)
diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs
index eaf1dbd2..72f6af57 100644
--- a/helix-view/src/tree.rs
+++ b/helix-view/src/tree.rs
@@ -270,10 +270,18 @@ impl Tree {
})
}
+ /// Get reference to a [`view`] by index.
+ /// # Panics
+ ///
+ /// Panics if `index` is not in self.nodes, or if the node's content is not [`Content::View`] . This can be checked with [`contains`]
pub fn get(&self, index: ViewId) -> &View {
self.try_get(index).unwrap()
}
+ /// Try to get reference to a [`view`] by index. Returns `None` if node content is not a [`Content::View`]
+ /// # Panics
+ ///
+ /// Panics if `index` is not in self.nodes. This can be checked with [`Self::contains`]
pub fn try_get(&self, index: ViewId) -> Option<&View> {
match &self.nodes[index] {
Node {
@@ -284,6 +292,10 @@ impl Tree {
}
}
+ /// Get a mutable reference to a [`view`] by index.
+ /// # Panics
+ ///
+ /// Panics if `index` is not in self.nodes, or if the node's content is not [`Content::View`] . This can be checked with [`Self::contains`]
pub fn get_mut(&mut self, index: ViewId) -> &mut View {
match &mut self.nodes[index] {
Node {
@@ -294,6 +306,11 @@ impl Tree {
}
}
+ /// Check if tree contains a [`Node`] with a given index.
+ pub fn contains(&self, index: ViewId) -> bool {
+ self.nodes.contains_key(index)
+ }
+
pub fn is_empty(&self) -> bool {
match &self.nodes[self.root] {
Node {