Skip to content

Commit

Permalink
fix(highlight): forward search / for long line
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyaowong committed May 20, 2024
1 parent 824f064 commit 52efd42
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ HighlightManager listens to the `ext_linegrid` API and renders highlights using
### ViewportManager

ViewportManager is responsible for syncing the viewport (editor window, scroll position) between vscode and nvim. It
listens to the `win_viewport` event, and supplements it with the custom `window-scroll` event triggered by the
`WinScrolled` autocommand. It also keeps track of the cursor position more reliably than `grid_cursor_goto`.
listens to the `win_viewport` event, and supplements it with the custom `viewport-changed` event. It also keeps track of
the cursor position more reliably than `grid_cursor_goto`.

### CursorManager

Expand Down
2 changes: 2 additions & 0 deletions runtime/lua/vscode-neovim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ local default_optons = require("vscode-neovim.default-options")
local cursor = require("vscode-neovim.cursor")
local highlight = require("vscode-neovim.highlight")
local sync_options = require("vscode-neovim.sync-options")
local viewport = require("vscode-neovim.viewport")

default_optons.setup()
cursor.setup()
highlight.setup()
sync_options.setup()
viewport.setup()

local vscode = {
-- actions
Expand Down
59 changes: 59 additions & 0 deletions runtime/lua/vscode-neovim/viewport.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local M = {}

local api, fn = vim.api, vim.fn

M.event_group = api.nvim_create_augroup("vscode-neovim.viewport", { clear = true })
M.viewport_changed_ns = api.nvim_create_namespace("vscode-neovim.viewport.changed")

---@class WinContext
---@field textoff integer
---@field topline integer
---@field botline integer
---@field width integer
---@field height integer
---@field leftcol integer
----@field winnr integer Don't use this
---@field winid integer

---@return WinContext
local function get_win_context()
local info = fn.getwininfo(api.nvim_get_current_win())[1]
local view = fn.winsaveview()
return vim.tbl_extend("force", info, view)
end

local function setup_viewport_changed()
---@type table<integer, WinContext>
local ctx_cache = {}

api.nvim_set_decoration_provider(M.viewport_changed_ns, {
on_win = function()
local ctx = get_win_context()
local cache = ctx_cache[ctx.winid]
if cache and vim.deep_equal(ctx, cache) then
return
end
ctx_cache[ctx.winid] = ctx
fn.VSCodeExtensionNotify("viewport-changed", ctx)
end,
})

-- cleanup cache
api.nvim_create_autocmd({ "WinClosed" }, {
group = M.event_group,
callback = function()
local wins = api.nvim_list_wins()
for win in pairs(ctx_cache) do
if not vim.tbl_contains(win, wins) then
ctx_cache[win] = nil
end
end
end,
})
end

function M.setup()
setup_viewport_changed()
end

return M
4 changes: 2 additions & 2 deletions src/eventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ type EventsMapping = {
["move-cursor"]: ["top" | "middle" | "bottom"];
scroll: ["page" | "halfPage", "up" | "down"];
["scroll-line"]: ["up" | "down"];
["window-scroll"]: [
number,
["viewport-changed"]: [
{
winid: number;
lnum: number;
col: number;
coladd: number;
Expand Down
10 changes: 5 additions & 5 deletions src/viewport_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export class ViewportManager implements Disposable {
this.cursorChanged,
window.onDidChangeTextEditorVisibleRanges(this.onDidChangeVisibleRange),
eventBus.on("redraw", this.handleRedraw, this),
eventBus.on("window-scroll", ([winId, saveView]) => {
const gridId = this.main.bufferManager.getGridIdForWinId(winId);
eventBus.on("viewport-changed", ([{ winid, leftcol, skipcol }]) => {
const gridId = this.main.bufferManager.getGridIdForWinId(winid);
if (!gridId) {
logger.warn(`Unable to update scrolled view. No grid for winId: ${winId}`);
logger.warn(`Unable to update scrolled view. No grid for winId: ${winid}`);
return;
}
const view = this.getViewport(gridId);
view.leftcol = saveView.leftcol;
view.skipcol = saveView.skipcol;
view.leftcol = leftcol;
view.skipcol = skipcol;
}),
);
}
Expand Down
1 change: 0 additions & 1 deletion vim/vscode-neovim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ augroup VscodeGeneral
autocmd InsertEnter * call <SID>onInsertEnter()
" Trigger filetype detection
autocmd BufAdd * do BufRead
autocmd WinScrolled * call VSCodeExtensionNotify('window-scroll', win_getid(), winsaveview())
autocmd VimEnter,ModeChanged * call VSCodeExtensionNotify('mode-changed', mode())
autocmd WinEnter * call VSCodeExtensionNotify('window-changed', win_getid())
" LazyVim will clear runtimepath by default. To avoid user intervention, we need to set it again.
Expand Down

0 comments on commit 52efd42

Please sign in to comment.