Skip to content

Commit

Permalink
recognize bang of :wq command to quit on unsaved changes
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Oct 20, 2024
1 parent f498478 commit e4b35a8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
44 changes: 43 additions & 1 deletion editor/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestEditorOpenWriteQuit(t *testing.T) {
}
}

func TestEditorOpenForceQuit(t *testing.T) {
func TestEditorOpenQuitBang(t *testing.T) {
ui := newTestUI()
editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline())
if err := editor.Init(); err != nil {
Expand Down Expand Up @@ -200,6 +200,48 @@ func TestEditorOpenForceQuit(t *testing.T) {
}
}

func TestEditorOpenWriteQuitBang(t *testing.T) {
ui := newTestUI()
editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline())
if err := editor.Init(); err != nil {
t.Fatalf("err should be nil but got: %v", err)
}
f, err := createTemp(t.TempDir(), "ab")
if err != nil {
t.Fatalf("err should be nil but got: %v", err)
}
if err := editor.Open(f.Name()); err != nil {
t.Fatalf("err should be nil but got: %v", err)
}
go func() {
ui.Emit(event.Event{Type: event.SwitchFocus})
ui.Emit(event.Event{Type: event.StartAppendEnd})
ui.Emit(event.Event{Type: event.Rune, Rune: 'c'})
ui.Emit(event.Event{Type: event.ExitInsert})
ui.Emit(event.Event{Type: event.WriteQuit, Arg: f.Name() + ".out", Bang: true})
}()
if err := editor.Run(); err != nil {
t.Errorf("err should be nil but got: %v", err)
}
if err := editor.Close(); err != nil {
t.Errorf("err should be nil but got: %v", err)
}
bs, err := os.ReadFile(f.Name())
if err != nil {
t.Errorf("err should be nil but got: %v", err)
}
if expected := "ab"; string(bs) != expected {
t.Errorf("file contents should be %q but got %q", expected, string(bs))
}
bs, err = os.ReadFile(f.Name() + ".out")
if err != nil {
t.Errorf("err should be nil but got: %v", err)
}
if expected := "abc"; string(bs) != expected {
t.Errorf("file contents should be %q but got %q", expected, string(bs))
}
}

func TestEditorReadWriteQuit(t *testing.T) {
ui := newTestUI()
editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline())
Expand Down
2 changes: 1 addition & 1 deletion window/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func (m *Manager) Emit(e event.Event) {
case event.WriteQuit:
if _, _, err := m.write(e); err != nil {
m.eventCh <- event.Event{Type: event.Error, Error: err}
} else if err := m.quit(event.Event{}); err != nil {
} else if err := m.quit(event.Event{Bang: e.Bang}); err != nil {
m.eventCh <- event.Event{Type: event.Error, Error: err}
}
default:
Expand Down

0 comments on commit e4b35a8

Please sign in to comment.