Start Neovim on a socket, and open, quit and check files through it
--remote-expr and --remote-send fail when the socket is gone; --remote would fall back to editing in a Neovim of its own.
This commit is contained in:
1 parent
39e96681f6
commit
56ee5364fa
1 file changed
+45
-5
+45
-5
@@ -2,14 +2,54 @@ import type { CliRenderer } from "@opentui/core";
|
|||||||
import { spawnSync } from "node:child_process";
|
import { spawnSync } from "node:child_process";
|
||||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import { join } from "node:path";
|
import { basename, join } from "node:path";
|
||||||
|
|
||||||
/** $VISUAL or $EDITOR (falling back to vi) on `file`, through the shell so EDITOR="code --wait" works. */
|
const editorSetting = () => process.env.VISUAL || process.env.EDITOR || "vi";
|
||||||
export function editorCommand(file: string): string[] {
|
const shell = () => process.env.SHELL || "sh";
|
||||||
const editor = process.env.VISUAL || process.env.EDITOR || "vi";
|
/** The editor's program, without its arguments. */
|
||||||
return [process.env.SHELL || "sh", "-c", `${editor} "$1"`, "sh", file];
|
const editorProgram = () => editorSetting().trim().split(/\s+/)[0] ?? "vi";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* $VISUAL or $EDITOR (falling back to vi) on `file`, through the shell so EDITOR="code --wait" works.
|
||||||
|
* With `listen`, Neovim serves that socket, so more files can go to it (see nvimRemote).
|
||||||
|
*/
|
||||||
|
export function editorCommand(file: string, listen?: string): string[] {
|
||||||
|
const editor = editorSetting();
|
||||||
|
return listen ? [shell(), "-c", `${editor} --listen "$2" "$1"`, "sh", file, listen] : [shell(), "-c", `${editor} "$1"`, "sh", file];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Whether the editor is Neovim, which can take more files and be asked to quit through a socket. */
|
||||||
|
export const editorIsNvim = () => basename(editorProgram()) === "nvim";
|
||||||
|
|
||||||
|
/** A socket for a Neovim started here, in a temp folder of its own (`remove` cleans up). Kept short: sockets have a length limit. */
|
||||||
|
export function nvimSocket(): { readonly path: string; readonly remove: () => void } {
|
||||||
|
const dir = mkdtempSync(join(tmpdir(), "t3tui-nvim-"));
|
||||||
|
return { path: join(dir, "nvim.sock"), remove: () => rmSync(dir, { recursive: true, force: true }) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asks the Neovim on `socket` for something: `["--remote-expr", expr]` or `["--remote-send", keys]`.
|
||||||
|
* Resolves to what it printed, or null if it couldn't be reached. Unlike `--remote`, these
|
||||||
|
* never fall back to editing in a Neovim of their own.
|
||||||
|
*/
|
||||||
|
export async function nvimRemote(socket: string, args: readonly string[]): Promise<string | null> {
|
||||||
|
const child = Bun.spawn([shell(), "-c", `${editorProgram()} --server "$@"`, "sh", socket, ...args], { stdin: "ignore", stdout: "pipe", stderr: "ignore" });
|
||||||
|
const [code, output] = await Promise.all([child.exited, new Response(child.stdout).text()]);
|
||||||
|
return code === 0 ? output : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Opens `file` in the Neovim on `socket`, in its current window. */
|
||||||
|
export const nvimOpen = (socket: string, file: string) => nvimRemote(socket, ["--remote-expr", `execute('drop ' .. fnameescape(${vimString(file)}))`]);
|
||||||
|
/** Asks the Neovim on `socket` to quit; it asks about unsaved changes itself. */
|
||||||
|
export const nvimQuit = (socket: string) => nvimRemote(socket, ["--remote-send", "<C-\\><C-N>:confirm qa<CR>"]);
|
||||||
|
/** Whether the Neovim on `socket` has unsaved changes; null if it couldn't be reached. */
|
||||||
|
export const nvimModified = async (socket: string) => {
|
||||||
|
const count = await nvimRemote(socket, ["--remote-expr", "len(getbufinfo({'bufmodified': 1}))"]);
|
||||||
|
return count === null ? null : Number.parseInt(count, 10) > 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
const vimString = (text: string) => `'${text.replaceAll("'", "''")}'`;
|
||||||
|
|
||||||
/** A scratch copy of `text` to edit: `read` returns what was saved, `remove` cleans up. */
|
/** A scratch copy of `text` to edit: `read` returns what was saved, `remove` cleans up. */
|
||||||
export function scratchFile(text: string): { readonly path: string; readonly read: () => string; readonly remove: () => void } {
|
export function scratchFile(text: string): { readonly path: string; readonly read: () => string; readonly remove: () => void } {
|
||||||
const dir = mkdtempSync(join(tmpdir(), "t3tui-"));
|
const dir = mkdtempSync(join(tmpdir(), "t3tui-"));
|
||||||
|
|||||||
Reference in new issue
Block a user