1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 00:50:05 +08:00

perf(scrollbar): use vim#buffer api

This commit is contained in:
Eric Wong 2024-03-22 16:49:58 +08:00
parent 5b88660a3f
commit 4563880fee
4 changed files with 63 additions and 38 deletions

View File

@ -32,6 +32,14 @@
" initfunc: the function which will be call after creating buffer " initfunc: the function which will be call after creating buffer
" "
" cmd: the ex command which will be run after the new buffer is created " cmd: the ex command which will be run after the new buffer is created
"
" get_option(bufnr, name)
"
" Gets a buffer option value.
"
" set_option(buf, opt, value)
"
" Set a buffer option value.
let s:self = {} let s:self = {}

View File

@ -6872,6 +6872,14 @@ is a dict with following keys:
cmd: the ex command which will be run after the new buffer is created cmd: the ex command which will be run after the new buffer is created
get_option(bufnr, name)
Gets a buffer option value.
set_option(buf, opt, value)
Set a buffer option value.
============================================================================== ==============================================================================
VIM#COMMAND *SpaceVim-api-vim-command* VIM#COMMAND *SpaceVim-api-vim-command*

View File

@ -20,30 +20,25 @@ function M.set_lines(bufnr, startindex, endindex, replacement)
endindex = #vim.buffer(bufnr) + 1 + endindex endindex = #vim.buffer(bufnr) + 1 + endindex
end end
if #replacement == endindex - startindex then if #replacement == endindex - startindex then
for i = startindex, endindex - 1, 1 for i = startindex, endindex - 1, 1 do
do
vim.buffer(bufnr)[i + 1] = replacement[i - startindex] vim.buffer(bufnr)[i + 1] = replacement[i - startindex]
end end
else else
if endindex < #vim.buffer(bufnr) then if endindex < #vim.buffer(bufnr) then
for i = endindex + 1, #vim.buffer(bufnr), 1 for i = endindex + 1, #vim.buffer(bufnr), 1 do
do
replacement:add(vim.buffer(bufnr)[i]) replacement:add(vim.buffer(bufnr)[i])
end end
end end
for i = startindex, #replacement + startindex - 1, 1 for i = startindex, #replacement + startindex - 1, 1 do
do
if i + 1 > #vim.buffer(bufnr) then if i + 1 > #vim.buffer(bufnr) then
vim.buffer(bufnr):insert(replacement[i - startindex]) vim.buffer(bufnr):insert(replacement[i - startindex])
else else
vim.buffer(bufnr)[i + 1] = replacement[i - startindex] vim.buffer(bufnr)[i + 1] = replacement[i - startindex]
end end
end end
for i = #replacement + startindex + 1, #vim.buffer(bufnr), 1 for i = #replacement + startindex + 1, #vim.buffer(bufnr), 1 do
do
vim.buffer(bufnr)[#replacement + startindex + 1] = nil vim.buffer(bufnr)[#replacement + startindex + 1] = nil
end end
end end
end end
@ -67,10 +62,23 @@ end
---@param opt string option name ---@param opt string option name
---@param value any option value ---@param value any option value
function M.set_option(bufnr, opt, value) function M.set_option(bufnr, opt, value)
if vim.api.nvim_set_option_value then
return vim.api.nvim_set_option_value(opt, value, {
buf = bufnr
})
end
if vim.api.nvim_buf_set_option then
return vim.api.nvim_buf_set_option(bufnr, opt, value)
end
end end
function M.get_option(bufnr, name)
if vim.api.nvim_get_option_value then
return vim.api.nvim_get_option_value(name, { buf = bufnr })
end
if vim.api.nvim_buf_get_option then
return vim.api.nvim_buf_get_option(bufnr, name)
end
end
return M return M

View File

@ -9,6 +9,7 @@
local M = {} local M = {}
local win = require('spacevim.api.vim.window') local win = require('spacevim.api.vim.window')
local buffer = require('spacevim.api.vim.buffer')
local default_conf = { local default_conf = {
max_size = 10, max_size = 10,
@ -75,7 +76,7 @@ local function create_scrollbar_buffer(size, lines)
if not vim.api.nvim_buf_is_valid(scrollbar_bufnr) then if not vim.api.nvim_buf_is_valid(scrollbar_bufnr) then
scrollbar_bufnr = vim.api.nvim_create_buf(false, true) scrollbar_bufnr = vim.api.nvim_create_buf(false, true)
end end
vim.api.nvim_buf_set_option(scrollbar_bufnr, 'buftype', 'nofile') buffer.set_option(scrollbar_bufnr, 'buftype', 'nofile')
vim.api.nvim_buf_set_lines(scrollbar_bufnr, 0, -1, false, lines) vim.api.nvim_buf_set_lines(scrollbar_bufnr, 0, -1, false, lines)
return scrollbar_bufnr return scrollbar_bufnr
end end