Skip to content

Open missing files without -c and create them on save - #6

Open
alberti42 wants to merge 1 commit into
spamwax:masterfrom
alberti42:create-on-save
Open

alberti42 wants to merge 1 commit into
spamwax:masterfrom
alberti42:create-on-save

Conversation

@alberti42

Copy link
Copy Markdown

Summary

This PR lets rmate open a file that does not exist yet without -c/--create, as the Ruby implementation does. The editor shows an empty buffer, and the file is created when the editor first saves it.

Note: If the buffer is closed without saving, no file is left behind. This is the same behavior of Ruby implementation and what a user would intuitively expect. Note that this is different than the current -c/--create option, which creates the empty file first, even if later the user decides to save nothing.

Motivation

Without -c, rmate exits on a missing file with Error: "No such file or directory (os error 2)". Issue #1 asked for a way to open a new file without running touch first, and -c was added for that (cb8ccd6). With -c, the file is created on disk at open, even if you never save it.

The Ruby rmate implementation creates nothing at open time. For a missing file it sends data: 0:

cmd.read_file(path)               if path != '-' and File.exist? path
cmd['data']         = "0"     unless path == '-' or  File.exist? path

The README says rmate-rs supports all options and rmate.rc settings of the Ruby implementation, so this PR follows the same pattern. It also resolves the TODO in main.rs: "Flip how the setting for creating non-existing files work so by default it doesn't create new empty files."

Changes

These changes are automatically generated by CLAUDE:

  • src/file_handler.rs, get_requested_buffers(): without -c, a missing file is no longer an error. A new helper, canonicalize_missing(), resolves the path through its parent directory, which must exist. The buffer gets size 0, and the read-only check is skipped until the file is saved.
  • src/file_handler.rs, write_to_disk(): the target is opened with create(true), and the backup to file~ is skipped when there is no file to back up.
  • src/remote_editor.rs, open_file_in_remote(): for a missing file, sends empty contents instead of failing on File::open.
  • src/settings.rs: the -c help now reads "Create a missing file when opening it. Without this flag, a missing file is created when it is first saved".
  • src/main.rs: removes the TODO.
  • README.md: one entry under Features.

Testing

Tested on macOS (aarch64) against a test editor that receives the file, then sends save and close, and against an Emacs rmate server:

Case Result
missing file, no -c opened empty, created on save
missing file, buffer closed without saving no file created
missing file, -c created at open, saved
parent directory missing No such file or directory, exit 1
missing file in an existing subdirectory, relative path created on save
existing file backed up, saved, backup removed, as before
missing file in a read-only directory Couldn't save: … Permission denied; nothing created
Emacs rmate server no file before saving; saved contents on disk

Also tested end to end on Linux (RHEL 9, kernel 5.14, x86_64): rmate on the remote host opened a missing file in the editor on macOS, with and without -c. Without -c, the file was created on save, and no file was created when the buffer was closed without saving. With -c, the file was created at open.

Additional remarks

  • This PR is independent of Add UNIX socket support (-s/--unixsocket) #5 (UNIX socket support). Both branches start from master, and they merge without conflicts in either order.
  • is_some_and and is_none_or need Rust 1.82. CI builds with stable.

Like the Ruby rmate, open a file that does not exist yet as an empty
buffer (data: 0) and create it when the editor first saves it. If the
buffer is closed without saving, no file is left behind. The parent
directory must exist.

-c/--create keeps its behaviour: it creates the file when opening it.

On save, the file is opened with create(true), and the backup step is
skipped when there is no file to back up.

Fixes the TODO in main.rs about the default for creating missing files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant