From 111b5282727c8b3a4abb8d6a433fe8904a000300 Mon Sep 17 00:00:00 2001 From: dualfroz Date: Sat, 3 Oct 2026 18:54:01 +0200 Subject: [PATCH] refactor(buffer): move the current window cursor lookup to utils --- src/buffer_manager.ts | 20 ++------------------ src/utils/index.ts | 1 + src/utils/neovim.ts | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 src/utils/neovim.ts diff --git a/src/buffer_manager.ts b/src/buffer_manager.ts index 07b0f84..d243a70 100644 --- a/src/buffer_manager.ts +++ b/src/buffer_manager.ts @@ -10,7 +10,6 @@ import { EventEmitter, LogLevel, NotebookDocument, - Position, ProgressLocation, Selection, TextDocument, @@ -38,6 +37,7 @@ import { convertVimPositionToEditorPosition, disposeAll, fileExists, + getCurrentWinCursorInBuf, wait, } from "./utils"; @@ -359,7 +359,7 @@ export class BufferManager implements Disposable { if (window.activeTextEditor?.document !== doc) { // Neovim moved to this buffer itself (e.g. a jump to a global mark), so its cursor // must win over the selection vscode restores when showing the document again - const neovimCursor = await this.getNeovimCursorInBuffer(bufnr); + const neovimCursor = await getCurrentWinCursorInBuf(this.client, bufnr); const editor = await window.showTextDocument(doc, { // viewColumn: vscode.ViewColumn.Active, // !need to force editor to appear in the same column even if vscode 'revealIfOpen' setting is true @@ -380,22 +380,6 @@ export class BufferManager implements Disposable { } } - /** - * Get the (0, 0)-indexed byte cursor of the current neovim window, if it shows the given buffer - */ - private async getNeovimCursorInBuffer(bufId: number): Promise { - const cursor = (await this.client.lua( - ` - local buf = ... - if vim.api.nvim_get_current_buf() == buf then - return vim.api.nvim_win_get_cursor(0) - end - `, - [bufId], - )) as [number, number] | null; - return cursor ? new Position(cursor[0] - 1, cursor[1]) : undefined; - } - private handleWindowChanged = async (winId: number, token: CancellationToken): Promise => { logger.debug(`window changed, target window id: ${winId}`); if (winId === 1000) { diff --git a/src/utils/index.ts b/src/utils/index.ts index 70a91d4..de65c29 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,6 @@ export * from "./async"; export * from "./fs"; +export * from "./neovim"; export * from "./pending_updates"; export * from "./text"; export * from "./vscode"; diff --git a/src/utils/neovim.ts b/src/utils/neovim.ts new file mode 100644 index 0000000..fda9537 --- /dev/null +++ b/src/utils/neovim.ts @@ -0,0 +1,18 @@ +import { NeovimClient } from "neovim"; +import { Position } from "vscode"; + +/** + * Get the (0, 0)-indexed byte cursor of the current neovim window, if it shows the given buffer + */ +export async function getCurrentWinCursorInBuf(client: NeovimClient, bufId: number): Promise { + const cursor = (await client.lua( + ` + local buf = ... + if vim.api.nvim_get_current_buf() == buf then + return vim.api.nvim_win_get_cursor(0) + end + `, + [bufId], + )) as [number, number] | null; + return cursor ? new Position(cursor[0] - 1, cursor[1]) : undefined; +}