Skip to content

Commit

Permalink
src/Neovim.ts: stop removing nvim_ from API names due to clash
Browse files Browse the repository at this point in the history
A vim.ui_attach function whose interface differs from nvim_ui_attach was
introduced in 0.9, and would overwrite nvim_ui_attach for Firenvim. The
only reason this didn't cause any issues yet is that vim.ui_attach is
marked deprecated, but we can't ignore deprecated functions because
otherwise Firenvim breaks immediately when functions get deprecated
(e.g. which is what happened in #1603).
  • Loading branch information
glacambre committed Apr 28, 2024
1 parent 79ca923 commit 222efad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
9 changes: 2 additions & 7 deletions src/Neovim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function neovim(
CanvasRenderer.setSettings(settings);
CanvasRenderer.setCanvas(canvas);
CanvasRenderer.events.on("resize", ({grid, width, height}: any) => {
(functions as any).ui_try_resize_grid(grid, width, height);
(functions as any).nvim_ui_try_resize_grid(grid, width, height);
});
CanvasRenderer.events.on("frameResize", ({width, height}: any) => {
page.resizeEditor(width, height);
Expand Down Expand Up @@ -126,13 +126,8 @@ export async function neovim(
stdout.setTypes(apiInfo.types);

Object.assign(functions, apiInfo.functions
.filter(f => f.deprecated_since === undefined)
.reduce((acc, cur) => {
let name = cur.name;
if (name.startsWith("nvim_")) {
name = name.slice(5);
}
acc[name] = (...args: any[]) => request(cur.name, args);
acc[cur.name] = (...args: any[]) => request(cur.name, args);
return acc;
}, {} as {[k: string]: (...args: any[]) => any}));
functions.get_current_channel = () => channel;
Expand Down
30 changes: 15 additions & 15 deletions src/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ export const isReady = browser

const nvim = await nvimPromise;

keyHandler.on("input", (s: string) => nvim.input(s));
keyHandler.on("input", (s: string) => nvim.nvim_input(s));
rendererEvents.on("modeChange", (s: NvimMode) => keyHandler.setMode(s));

// We need to set client info before running ui_attach because we want this
// info to be available when UIEnter is triggered
const extInfo = browser.runtime.getManifest();
const [major, minor, patch] = extInfo.version.split(".");
nvim.set_client_info(extInfo.name,
nvim.nvim_set_client_info(extInfo.name,
{ major, minor, patch },
"ui",
{},
{},
);

nvim.ui_attach(
nvim.nvim_ui_attach(
cols < 1 ? 1 : cols,
rows < 1 ? 1 : rows,
{
Expand All @@ -75,40 +75,40 @@ export const isReady = browser
width * window.devicePixelRatio,
height * window.devicePixelRatio
);
nvim.ui_try_resize_grid(getGridId(), nCols, nRows);
nvim.nvim_ui_try_resize_grid(getGridId(), nCols, nRows);
page.resizeEditor(Math.floor(width / nCols) * nCols, Math.floor(height / nRows) * nRows);
}
});
page.on("frame_sendKey", (args) => nvim.input(args.join("")));
page.on("get_buf_content", (r: any) => r(nvim.buf_get_lines(0, 0, -1, 0)));
page.on("frame_sendKey", (args) => nvim.nvim_input(args.join("")));
page.on("get_buf_content", (r: any) => r(nvim.nvim_buf_get_lines(0, 0, -1, 0)));

// Create file, set its content to the textarea's, write it
const filename = toFileName(urlSettings.filename, url, selector, language);
const content = await contentPromise;
const [line, col] = cursor;
const writeFilePromise = nvim.call_function("writefile", [content.split("\n"), filename])
.then(() => nvim.command(`edit ${filename} `
const writeFilePromise = nvim.nvim_call_function("writefile", [content.split("\n"), filename])
.then(() => nvim.nvim_command(`edit ${filename} `
+ `| call nvim_win_set_cursor(0, [${line}, ${col}])`));

// Can't get coverage for this as browsers don't let us reliably
// push data to the server on beforeunload.
/* istanbul ignore next */
window.addEventListener("beforeunload", () => {
nvim.ui_detach();
nvim.command("qall!");
nvim.nvim_ui_detach();
nvim.nvim_command("qall!");
});

// Keep track of last active instance (necessary for firenvim#focus_input() & others)
const chan = nvim.get_current_channel();
function setCurrentChan() {
nvim.set_var("last_focused_firenvim_channel", chan);
nvim.nvim_set_var("last_focused_firenvim_channel", chan);
}
setCurrentChan();
window.addEventListener("focus", setCurrentChan);
window.addEventListener("click", setCurrentChan);

// Ask for notifications when user writes/leaves firenvim
nvim.exec_lua(`
nvim.nvim_exec_lua(`
local args = {...}
local augroupName = args[1]
local filename = args[2]
Expand Down Expand Up @@ -181,7 +181,7 @@ export const isReady = browser
(evt.metaKey ? "D" : "") +
(evt.shiftKey ? "S" : "");
const [x, y] = getGridCoordinates(evt.pageX, evt.pageY);
nvim.input_mouse(button,
nvim.nvim_input_mouse(button,
action,
modifiers,
getGridId(),
Expand All @@ -208,11 +208,11 @@ export const isReady = browser
window.addEventListener("focus", () => {
document.documentElement.style.opacity = "1";
keyHandler.focus();
nvim.command("doautocmd FocusGained");
nvim.nvim_command("doautocmd FocusGained");
});
window.addEventListener("blur", () => {
document.documentElement.style.opacity = "0.5";
nvim.command("doautocmd FocusLost");
nvim.nvim_command("doautocmd FocusLost");
});
keyHandler.focus();
return new Promise ((resolve, reject) => setTimeout(() => {
Expand Down

0 comments on commit 222efad

Please sign in to comment.