1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 10:30: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
"
" 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 = {}

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
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*

View File

@ -13,38 +13,33 @@ function M.create_buf(listed, scratch)
end
function M.set_lines(bufnr, startindex, endindex, replacement)
if startindex < 0 then
startindex = #vim.buffer(bufnr) + 1 + startindex
if startindex < 0 then
startindex = #vim.buffer(bufnr) + 1 + startindex
end
if endindex < 0 then
endindex = #vim.buffer(bufnr) + 1 + endindex
end
if #replacement == endindex - startindex then
for i = startindex, endindex - 1, 1 do
vim.buffer(bufnr)[i + 1] = replacement[i - startindex]
end
if endindex < 0 then
endindex = #vim.buffer(bufnr) + 1 + endindex
else
if endindex < #vim.buffer(bufnr) then
for i = endindex + 1, #vim.buffer(bufnr), 1 do
replacement:add(vim.buffer(bufnr)[i])
end
end
if #replacement == endindex - startindex then
for i = startindex, endindex - 1, 1
do
vim.buffer(bufnr)[i + 1] = replacement[i - startindex]
end
else
if endindex < #vim.buffer(bufnr) then
for i = endindex + 1, #vim.buffer(bufnr), 1
do
replacement:add(vim.buffer(bufnr)[i])
end
end
for i = startindex, #replacement + startindex - 1, 1
do
if i + 1 > #vim.buffer(bufnr) then
vim.buffer(bufnr):insert(replacement[i - startindex])
else
vim.buffer(bufnr)[i + 1] = replacement[i - startindex]
end
end
for i = #replacement + startindex + 1, #vim.buffer(bufnr), 1
do
vim.buffer(bufnr)[#replacement + startindex + 1] = nil
end
for i = startindex, #replacement + startindex - 1, 1 do
if i + 1 > #vim.buffer(bufnr) then
vim.buffer(bufnr):insert(replacement[i - startindex])
else
vim.buffer(bufnr)[i + 1] = replacement[i - startindex]
end
end
for i = #replacement + startindex + 1, #vim.buffer(bufnr), 1 do
vim.buffer(bufnr)[#replacement + startindex + 1] = nil
end
end
end
function M.listed_buffers() -- {{{
@ -53,24 +48,37 @@ end
-- }}}
function M.resize(size, ...)
local arg = {...}
local cmd = arg[1] or 'vertical'
vim.cmd(cmd .. ' resize ' .. size)
local arg = { ... }
local cmd = arg[1] or 'vertical'
vim.cmd(cmd .. ' resize ' .. size)
end
function M.open_pos(cmd, filename, line, col)
vim.cmd('silent ' .. cmd .. ' ' .. filename)
vim.fn.cursor(line, col)
vim.cmd('silent ' .. cmd .. ' ' .. filename)
vim.fn.cursor(line, col)
end
---@param bufnr number the buffer number
---@param opt string option name
---@param value any option 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
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

View File

@ -9,6 +9,7 @@
local M = {}
local win = require('spacevim.api.vim.window')
local buffer = require('spacevim.api.vim.buffer')
local default_conf = {
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
scrollbar_bufnr = vim.api.nvim_create_buf(false, true)
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)
return scrollbar_bufnr
end