on master help sign in
Fix closing buffer with custom keymap (#3633)

* Fix closing buffer with custom keymap * Add comment explaining if

commit45dbcb67832e31ca5eea936628f34f5dc26c4381 authorA-Walrus <58790821+A-Walrus@users.noreply.github.com> dateThu 1 Sep 2022, 19:59 (4 years ago) parentec28b2b
2 files changed+31−11
helix-term/src/ui/editor.rs+14−11 helix-view/src/tree.rs+17
helix-term/src/ui/editor.rs+14−11
@@ -1263,17 +1263,20 @@ impl Component for EditorView {
12631263 if cx.editor.should_close() {
12641264 return EventResult::Ignored(None);
12651265 }
1266− let config = cx.editor.config();
1267− let mode = cx.editor.mode();
1268− let view = cx.editor.tree.get_mut(focus);
1269− let doc = cx.editor.documents.get_mut(&view.doc).unwrap();
1270−
1271− view.ensure_cursor_in_view(doc, config.scrolloff);
1272−
1273− // Store a history state if not in insert mode. This also takes care of
1274− // committing changes when leaving insert mode.
1275− if mode != Mode::Insert {
1276− doc.append_changes_to_history(view.id);
1266+ // if the focused view still exists and wasn't closed
1267+ if cx.editor.tree.contains(focus) {
1268+ let config = cx.editor.config();
1269+ let mode = cx.editor.mode();
1270+ let view = cx.editor.tree.get_mut(focus);
1271+ let doc = cx.editor.documents.get_mut(&view.doc).unwrap();
1272+
1273+ view.ensure_cursor_in_view(doc, config.scrolloff);
1274+
1275+ // Store a history state if not in insert mode. This also takes care of
1276+ // committing changes when leaving insert mode.
1277+ if mode != Mode::Insert {
1278+ doc.append_changes_to_history(view.id);
1279+ }
12771280 }
12781281
12791282 EventResult::Consumed(callback)
helix-view/src/tree.rs+17
@@ -270,10 +270,18 @@ impl Tree {
270270 })
271271 }
272272
273+ /// Get reference to a [`view`] by index.
274+ /// # Panics
275+ ///
276+ /// Panics if `index` is not in self.nodes, or if the node's content is not [`Content::View`] . This can be checked with [`contains`]
273277 pub fn get(&self, index: ViewId) -> &View {
274278 self.try_get(index).unwrap()
275279 }
276280
281+ /// Try to get reference to a [`view`] by index. Returns `None` if node content is not a [`Content::View`]
282+ /// # Panics
283+ ///
284+ /// Panics if `index` is not in self.nodes. This can be checked with [`Self::contains`]
277285 pub fn try_get(&self, index: ViewId) -> Option<&View> {
278286 match &self.nodes[index] {
279287 Node {
@@ -284,6 +292,10 @@ impl Tree {
284292 }
285293 }
286294
295+ /// Get a mutable reference to a [`view`] by index.
296+ /// # Panics
297+ ///
298+ /// 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`]
287299 pub fn get_mut(&mut self, index: ViewId) -> &mut View {
288300 match &mut self.nodes[index] {
289301 Node {
@@ -294,6 +306,11 @@ impl Tree {
294306 }
295307 }
296308
309+ /// Check if tree contains a [`Node`] with a given index.
310+ pub fn contains(&self, index: ViewId) -> bool {
311+ self.nodes.contains_key(index)
312+ }
313+
297314 pub fn is_empty(&self) -> bool {
298315 match &self.nodes[self.root] {
299316 Node {