on master help sign in
Fix closing buffer with custom keymap (#3633)
45dbcb6A-Walrus
helix-term/src/ui/editor.rs+14−11
@@ -1263,17 +1263,20 @@ impl Component for EditorView {
1263 if cx.editor.should_close() {
1263 if cx.editor.should_close() {
1264 return EventResult::Ignored(None);
1264 return EventResult::Ignored(None);
1265 }
1265 }
1266+ // if the focused view still exists and wasn't closed
1267+ if cx.editor.tree.contains(focus) {
1266− let config = cx.editor.config();
1268+ let config = cx.editor.config();
1267− let mode = cx.editor.mode();
1269+ let mode = cx.editor.mode();
1268− let view = cx.editor.tree.get_mut(focus);
1270+ let view = cx.editor.tree.get_mut(focus);
1269− let doc = cx.editor.documents.get_mut(&view.doc).unwrap();
1271+ let doc = cx.editor.documents.get_mut(&view.doc).unwrap();
1270−
1272+
1271− view.ensure_cursor_in_view(doc, config.scrolloff);
1273+ view.ensure_cursor_in_view(doc, config.scrolloff);
1272−
1274+
1273− // Store a history state if not in insert mode. This also takes care of
1275+ // Store a history state if not in insert mode. This also takes care of
1274− // committing changes when leaving insert mode.
1276+ // committing changes when leaving insert mode.
1275− if mode != Mode::Insert {
1277+ if mode != Mode::Insert {
1276− doc.append_changes_to_history(view.id);
1278+ doc.append_changes_to_history(view.id);
1279+ }
1277 }
1280 }
1278
1281
1279 EventResult::Consumed(callback)
1282 EventResult::Consumed(callback)
helix-view/src/tree.rs+17
@@ -270,10 +270,18 @@ impl Tree {
270 })
270 })
271 }
271 }
272
272
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`]
273 pub fn get(&self, index: ViewId) -> &View {
277 pub fn get(&self, index: ViewId) -> &View {
274 self.try_get(index).unwrap()
278 self.try_get(index).unwrap()
275 }
279 }
276
280
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`]
277 pub fn try_get(&self, index: ViewId) -> Option<&View> {
285 pub fn try_get(&self, index: ViewId) -> Option<&View> {
278 match &self.nodes[index] {
286 match &self.nodes[index] {
279 Node {
287 Node {
@@ -284,6 +292,10 @@ impl Tree {
284 }
292 }
285 }
293 }
286
294
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`]
287 pub fn get_mut(&mut self, index: ViewId) -> &mut View {
299 pub fn get_mut(&mut self, index: ViewId) -> &mut View {
288 match &mut self.nodes[index] {
300 match &mut self.nodes[index] {
289 Node {
301 Node {
@@ -294,6 +306,11 @@ impl Tree {
294 }
306 }
295 }
307 }
296
308
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+
297 pub fn is_empty(&self) -> bool {
314 pub fn is_empty(&self) -> bool {
298 match &self.nodes[self.root] {
315 match &self.nodes[self.root] {
299 Node {
316 Node {