refactor(buffer): move the current window cursor lookup to utils

This commit is contained in:
dualfroz committed 2026-10-03 18:54:01 +02:00
1 parent 990e567f55
commit 111b528272
3 files changed
+21 -18

No files matched your search

+2 -18
View File
@@ -10,7 +10,6 @@ import {
EventEmitter, EventEmitter,
LogLevel, LogLevel,
NotebookDocument, NotebookDocument,
Position,
ProgressLocation, ProgressLocation,
Selection, Selection,
TextDocument, TextDocument,
@@ -38,6 +37,7 @@ import {
convertVimPositionToEditorPosition, convertVimPositionToEditorPosition,
disposeAll, disposeAll,
fileExists, fileExists,
getCurrentWinCursorInBuf,
wait, wait,
} from "./utils"; } from "./utils";
@@ -359,7 +359,7 @@ export class BufferManager implements Disposable {
if (window.activeTextEditor?.document !== doc) { if (window.activeTextEditor?.document !== doc) {
// Neovim moved to this buffer itself (e.g. a jump to a global mark), so its cursor // 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 // 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, { const editor = await window.showTextDocument(doc, {
// viewColumn: vscode.ViewColumn.Active, // viewColumn: vscode.ViewColumn.Active,
// !need to force editor to appear in the same column even if vscode 'revealIfOpen' setting is true // !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<Position | undefined> {
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<void> => { private handleWindowChanged = async (winId: number, token: CancellationToken): Promise<void> => {
logger.debug(`window changed, target window id: ${winId}`); logger.debug(`window changed, target window id: ${winId}`);
if (winId === 1000) { if (winId === 1000) {
+1
View File
@@ -1,5 +1,6 @@
export * from "./async"; export * from "./async";
export * from "./fs"; export * from "./fs";
export * from "./neovim";
export * from "./pending_updates"; export * from "./pending_updates";
export * from "./text"; export * from "./text";
export * from "./vscode"; export * from "./vscode";
+18
View File
@@ -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<Position | undefined> {
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;
}