Repository navigation
feat(cli): open command-line files in a chosen view, and create missing ones on save - #1070
Merged
Merged
Conversation
…ng ones on save `--edit`, `--split` and `--preview` open the files on that command line in that view for that launch only, overriding "Open existing files in". A file already open switches to it. A file named on the command line that does not exist yet opens as an empty tab; the first save creates it, and closing it untouched creates nothing. Closes #1051
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this is
Two command-line additions asked for in #1051 by @mrsilver76, who reads in Markpad but wants a third-party app to open a file straight into the editor.
--edit,--splitand--previewopen the files on that command line in that view, for that launch only. "Open existing files in" stays as it is, so double-clicking a file still opens it the usual way. A file that is already open switches to the requested view. If several flags are given, the last one wins.code new.mdorvim new.md.Closes #1051
Mechanism
startup_pathsalready kept a path that doesn't exist, on purpose, and dropped every argument starting with-. The flag never got anywhere, and the missing file failed later, at the read inloadMarkdown.A launch file can reach a window by several routes:
send_markdown_path,file-path,open-files-hereafter the "Ask me" picker, and the routing to a window that is restoring it. Instead of threading a mode through each of them,command_line_pathsrecords each command-line path with the view it asked for inAppState. The window that ends up opening the file takes that record once (take_command_line_open) indocumentSession.openFiles, which every one of those routes already ends in. A file opened from Finder or Explorer has no record, so it opens exactly as before. A later command line naming the same file replaces the earlier record, and a macOS open-document event for that file drops it. That second rule covers a request nobody took, for example when the "Ask me" picker is cancelled.openFilesmatches an already-open tab by canonical path, so./a.mdorA.mdfinds thea.mdtab.For a missing file, the tab records the path it is to create (
createsFile). This only counts while the tab is still on that path, so Save As or following a link moves it back under the usual rules. The first save or a successful read clears it. While it holds, the save's "changed on disk" check doesn't read the file. It askspath_is_missinginstead: nothing there is expected, and anything there is somebody else's. That covers an empty file someonetouched and a file that can't be read, whether from a permission error or a Windows sharing violation. An untouched tab of this kind is left out of the window snapshot, so the next launch doesn't try to read a file that was never written.One launch can deliver the same path twice (
setupemitsfile-path, andsend_markdown_pathreturns it too). The second delivery has no record left, soopenFilesactivates a tab that is still waiting to create its file instead of reading it, which would have raised "Error loading file".Scope
--tagis not here. It changes how launch files are delivered, so it should be reviewed on its own.openFiles. Recent files, links and session restore never see it.code. A Finder open on macOS still reports the missing file.createsFileis not persisted or carried across windows. A tab that has text but no file yet, once restored or moved to another window, raises the conflict bar on its first save. "Keep mine" writes the file.open -a Markpad --args --edit x.mdpasses the flag on a cold start only. With the app already running,openactivates it without forwarding arguments. Running the binary inside the bundle works in both cases.Tests
window_runtime.rs: which flag wins, that a second launch records the view for each file, that a later plain launch or an open-document event clears it, and that only a path with nothing there counts as missing.documents.save.spec.ts:tabs.spec.ts, throughopenFiles:Each guard has a test that goes red without it. That holds for the
pathIsMissingcheck in the save, the path match increatesItsFile, the clear on read, theopenFileModefallback, the guard and canonical match inopenFiles, the view switch for an open missing file, and the snapshot filter.Verification
macOS 26 (Apple silicon):
With a release build under its own bundle id:
--edit exists.mdopened the file in the editor while the setting said Preview.--edit new.md(no such file) added an emptynew.mdtab in the editor, with no error and no file on disk.exists.mdopen in the editor,--preview ./exists.mdswitched that tab to preview. It did not open a second tab.--edit exists.md new2.md(new2.mdmissing) openednew2.mdas an empty editor tab, with no error. If AppKit also delivered it as an open-document event, which drops a pending request, that did not happen before the request was taken.Not tried: Windows and Linux, which is where #1051 came from. The "Ask me" picker route was not run either. It is covered by reading the code and by the
openFilestests.