fix: add version check for nvim_set_option_value and nvim_get_option_value

This commit is contained in:
27Onion Nebell
2025-04-18 16:33:09 +08:00
parent 7d2387dc12
commit e93286a489
11 changed files with 110 additions and 51 deletions

View File

@@ -208,7 +208,7 @@ end
---@param bufnr integer ---@param bufnr integer
---@return boolean include ---@return boolean include
function M.should_include_symbol(kind, bufnr) function M.should_include_symbol(kind, bufnr)
local ft = vim.api.nvim_get_option_value('ft', { buf = bufnr }) local ft = utils.buf_get_option(bufnr, 'ft')
-- There can only be one kind in markdown and norg as of now -- There can only be one kind in markdown and norg as of now
if ft == 'markdown' or ft == 'norg' or kind == nil then if ft == 'markdown' or ft == 'norg' or kind == nil then
return true return true

View File

@@ -1,6 +1,8 @@
---@class outline.Float ---@class outline.Float
local Float = {} local Float = {}
local utils = require('outline.utils')
---@class outline.Float ---@class outline.Float
---@field bufnr integer ---@field bufnr integer
---@field winnr integer ---@field winnr integer
@@ -19,7 +21,7 @@ function Float:open(lines, hl, title, indent)
indent = indent or 0 indent = indent or 0
self.bufnr = vim.api.nvim_create_buf(false, true) self.bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_set_option_value('bufhidden', 'delete', { buf = self.bufnr }) utils.buf_set_option(self.bufnr, 'bufhidden', 'delete')
local maxwidth = 0 local maxwidth = 0
for _, l in ipairs(lines) do for _, l in ipairs(lines) do
@@ -60,10 +62,10 @@ function Float:open(lines, hl, title, indent)
end end
end end
vim.api.nvim_set_option_value('winfixwidth', true, { win = self.winnr }) utils.win_set_option(self.winnr, 'winfixwidth', true)
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines) vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
vim.api.nvim_set_option_value('modifiable', false, { buf = self.bufnr }) utils.buf_set_option(self.bufnr, 'modifiable', false)
vim.api.nvim_set_option_value('ft', 'OutlineHelp', { buf = self.bufnr }) utils.buf_set_option(self.bufnr, 'ft', 'OutlineHelp')
if hl then if hl then
self.ns = vim.api.nvim_create_namespace('OutlineHelp') self.ns = vim.api.nvim_create_namespace('OutlineHelp')

View File

@@ -3,7 +3,7 @@ local cfg = require('outline.config')
local highlight = require('outline.highlight') local highlight = require('outline.highlight')
local providers = require('outline.providers.init') local providers = require('outline.providers.init')
local symbols = require('outline.symbols') local symbols = require('outline.symbols')
local utils = require('outline.utils.init') local utils = require('outline.utils')
local M = { local M = {
---@type outline.Sidebar[] ---@type outline.Sidebar[]
@@ -278,7 +278,7 @@ function M.show_status()
} }
if buf and vim.api.nvim_buf_is_valid(buf) then if buf and vim.api.nvim_buf_is_valid(buf) then
ctx.ft = vim.api.nvim_get_option_value('ft', { buf = buf }) ctx.ft = utils.buf_get_option(buf, 'ft')
ctx.filter = cfg.o.symbols.user_config_filter[ctx.ft] ctx.filter = cfg.o.symbols.user_config_filter[ctx.ft]
-- 'else' is handled in help.lua -- 'else' is handled in help.lua
end end

View File

@@ -1,4 +1,5 @@
local cfg = require('outline.config') local cfg = require('outline.config')
local utils = require('outline.utils')
-- A floating window to preview the location of a symbol from the outline. -- A floating window to preview the location of a symbol from the outline.
-- Classical preview reads entire lines into a new buffer for preview. Live -- Classical preview reads entire lines into a new buffer for preview. Live
@@ -151,12 +152,12 @@ end
---Set buf & win options, and setup highlight ---Set buf & win options, and setup highlight
function Preview:setup() function Preview:setup()
vim.api.nvim_set_option_value('winhl', self.conf.winhl, { win = self.win }) utils.win_set_option(self.win, 'winhl', self.conf.winhl)
vim.api.nvim_set_option_value('winblend', self.conf.winblend, { win = self.win }) utils.win_set_option(self.win, 'winblend', self.conf.winblend)
local code_buf = self.s.code.buf local code_buf = self.s.code.buf
local ft = vim.api.nvim_get_option_value('filetype', { buf = code_buf }) local ft = utils.buf_get_option(code_buf, 'filetype')
vim.api.nvim_set_option_value('syntax', ft, { buf = self.buf }) utils.buf_set_option(self.buf, 'syntax', ft)
local ts_highlight_fn = vim.treesitter.start local ts_highlight_fn = vim.treesitter.start
if not _G._outline_nvim_has[8] then if not _G._outline_nvim_has[8] then
@@ -167,9 +168,9 @@ function Preview:setup()
end end
pcall(ts_highlight_fn, self.buf, ft) pcall(ts_highlight_fn, self.buf, ft)
vim.api.nvim_set_option_value('bufhidden', 'delete', { buf = self.buf }) utils.buf_set_option(self.buf, 'bufhidden', 'delete')
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf }) utils.buf_set_option(self.buf, 'modifiable', false)
vim.api.nvim_set_option_value('cursorline', true, { win = self.win }) utils.win_set_option(self.win, 'cursorline', true)
end end
function Preview:update() function Preview:update()
@@ -180,9 +181,9 @@ function Preview:update()
local lines = vim.api.nvim_buf_get_lines(self.s.code.buf, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(self.s.code.buf, 0, -1, false)
if self.buf ~= nil then if self.buf ~= nil then
vim.api.nvim_set_option_value('modifiable', true, { buf = self.buf }) utils.buf_set_option(self.buf, 'modifiable', true)
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines) vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines)
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf }) utils.buf_set_option(self.buf, 'modifiable', false)
vim.api.nvim_win_set_cursor(self.win, { node.line + 1, node.character }) vim.api.nvim_win_set_cursor(self.win, { node.line + 1, node.character })
end end
end end
@@ -221,7 +222,7 @@ end
---Creates new preview window and sets the content. Calls setup and set_lines. ---Creates new preview window and sets the content. Calls setup and set_lines.
function LivePreview:create() function LivePreview:create()
self.codewin = self.s.code.win self.codewin = self.s.code.win
self.initial_cursorline = vim.api.nvim_get_option_value('cursorline', { win = self.s.code.win }) self.initial_cursorline = utils.win_get_option(self.s.code.win, 'cursorline')
self.outline_height = vim.api.nvim_win_get_height(self.s.view.win) self.outline_height = vim.api.nvim_win_get_height(self.s.view.win)
self.width = cfg.get_preview_width(self.conf) self.width = cfg.get_preview_width(self.conf)
self.height = cfg.get_preview_height(self.conf, self.outline_height) self.height = cfg.get_preview_height(self.conf, self.outline_height)
@@ -256,9 +257,9 @@ end
---Set buf & win options, and autocmds ---Set buf & win options, and autocmds
function LivePreview:setup() function LivePreview:setup()
vim.api.nvim_set_option_value('winhl', self.conf.winhl, { win = self.win }) utils.win_set_option(self.win, 'winhl', self.conf.winhl)
vim.api.nvim_set_option_value('winblend', self.conf.winblend, { win = self.win }) utils.win_set_option(self.win, 'winblend', self.conf.winblend)
vim.api.nvim_set_option_value('cursorline', true, { win = self.win }) utils.win_set_option(self.win, 'cursorline', true)
vim.api.nvim_create_autocmd('WinClosed', { vim.api.nvim_create_autocmd('WinClosed', {
pattern = tostring(self.win), pattern = tostring(self.win),
@@ -273,7 +274,7 @@ function LivePreview:setup()
once = true, once = true,
callback = function() callback = function()
-- This doesn't work at all? -- This doesn't work at all?
vim.api.nvim_set_option_value('cursorline', self.initial_cursorline, { win = self.win }) utils.win_set_option(self.win, 'cursorline', self.initial_cursorline)
end, end,
}) })
end end
@@ -286,7 +287,7 @@ end
function LivePreview:focus() function LivePreview:focus()
vim.api.nvim_set_current_win(self.win) vim.api.nvim_set_current_win(self.win)
-- Remove this when the autocmd for WinEnter works above -- Remove this when the autocmd for WinEnter works above
vim.api.nvim_set_option_value('cursorline', self.initial_cursorline, { win = self.win }) utils.win_set_option(self.win, 'cursorline', self.initial_cursorline)
end end
---Create, focus, or update preview ---Create, focus, or update preview

View File

@@ -15,11 +15,13 @@ local M = {
name = 'markdown', name = 'markdown',
} }
local utils = require('outline.utils')
---@param bufnr integer ---@param bufnr integer
---@param config table? ---@param config table?
---@return boolean ft_is_markdown ---@return boolean ft_is_markdown
function M.supports_buffer(bufnr, config) function M.supports_buffer(bufnr, config)
local ft = vim.api.nvim_get_option_value('ft', { buf = bufnr }) local ft = utils.buf_get_option(bufnr, 'ft')
if config and config.filetypes then if config and config.filetypes then
for _, ft_check in ipairs(config.filetypes) do for _, ft_check in ipairs(config.filetypes) do
if ft_check == ft then if ft_check == ft then

View File

@@ -17,11 +17,12 @@ local M = {
] ]
]], ]],
} }
local utils = require('outline.utils')
---@param bufnr integer ---@param bufnr integer
---@param config table? ---@param config table?
function M.supports_buffer(bufnr, config) function M.supports_buffer(bufnr, config)
if vim.api.nvim_get_option_value('ft', { buf = bufnr }) ~= 'norg' then if utils.buf_get_option(bufnr, 'ft') ~= 'norg' then
return false return false
end end

View File

@@ -1,6 +1,7 @@
local cfg = require('outline.config') local cfg = require('outline.config')
local jsx = require('outline.providers.jsx') local jsx = require('outline.providers.jsx')
local lsp_utils = require('outline.utils.lsp') local lsp_utils = require('outline.utils.lsp')
local utils = require('outline.utils')
local l = vim.lsp local l = vim.lsp
@@ -252,7 +253,7 @@ function M.show_hover(sidebar)
border = cfg.o.preview_window.border, border = cfg.o.preview_window.border,
width = code_width, width = code_width,
}) })
vim.api.nvim_set_option_value('winhighlight', cfg.o.preview_window.winhl, { win = winnr }) utils.win_set_option(winnr, 'winhighlight', cfg.o.preview_window.winhl)
return true return true
end end

View File

@@ -5,7 +5,7 @@ local folding = require('outline.folding')
local parser = require('outline.parser') local parser = require('outline.parser')
local providers = require('outline.providers.init') local providers = require('outline.providers.init')
local symbols = require('outline.symbols') local symbols = require('outline.symbols')
local utils = require('outline.utils.init') local utils = require('outline.utils')
local strlen = vim.fn.strlen local strlen = vim.fn.strlen
@@ -249,7 +249,7 @@ function Sidebar:update_cursor_style()
local hide_cursor = type(cl) ~= 'string' local hide_cursor = type(cl) ~= 'string'
if cl == 'focus_in_outline' or cl == 'focus_in_code' then if cl == 'focus_in_outline' or cl == 'focus_in_code' then
vim.api.nvim_set_option_value('cursorline', cl == 'focus_in_outline', { win = 0 }) utils.win_set_option(0, 'cursorline', cl == 'focus_in_outline')
hide_cursor = cl == 'focus_in_outline' hide_cursor = cl == 'focus_in_outline'
end end
@@ -265,7 +265,7 @@ function Sidebar:reset_cursor_style()
local cl = cfg.o.outline_window.show_cursorline local cl = cfg.o.outline_window.show_cursorline
if cl == 'focus_in_outline' or cl == 'focus_in_code' then if cl == 'focus_in_outline' or cl == 'focus_in_code' then
vim.api.nvim_set_option_value('cursorline', cl ~= 'focus_in_outline', { win = 0 }) utils.win_set_option(0, 'cursorline', cl ~= 'focus_in_outline')
end end
-- vim.opt doesn't seem to provide a way to remove last item, like a pop() -- vim.opt doesn't seem to provide a way to remove last item, like a pop()
-- vim.o.guicursor = vim.o.guicursor:gsub(",n.-:.-$", "") -- vim.o.guicursor = vim.o.guicursor:gsub(",n.-:.-$", "")
@@ -344,8 +344,8 @@ function Sidebar:__refresh()
if focused_outline or not self.view:is_open() then if focused_outline or not self.view:is_open() then
return return
end end
local ft = vim.api.nvim_get_option_value('ft', { buf = buf }) local ft = utils.buf_get_option(buf, 'ft')
local listed = vim.api.nvim_get_option_value('ft', { buf = buf }) local listed = utils.buf_get_option(buf, 'ft')
if ft == 'OutlineHelp' or not (listed or ft == 'help') then if ft == 'OutlineHelp' or not (listed or ft == 'help') then
return return
end end

View File

@@ -123,4 +123,54 @@ function M.deepcopy_excluding(t, keys)
return res return res
end end
--- Get option value of given buffer.
--- @param bufnr integer
--- @param name string
--- @return any
function M.buf_get_option(bufnr, name)
if _G._outline_nvim_has[10] then
return vim.api.nvim_get_option_value(name, { buf = bufnr })
else
---@diagnostic disable-next-line:deprecated
return vim.api.nvim_buf_get_option(bufnr, name)
end
end
--- Set option value of given buffer.
--- @param bufnr integer
--- @param name string
function M.buf_set_option(bufnr, name, value)
if _G._outline_nvim_has[10] then
return vim.api.nvim_set_option_value(name, value, { buf = bufnr })
else
---@diagnostic disable-next-line:deprecated
return vim.api.nvim_buf_set_option(bufnr, name, value)
end
end
--- Get option value of given window.
--- @param winnr integer
--- @param name string
--- @return any
function M.win_get_option(winnr, name)
if _G._outline_nvim_has[10] then
return vim.api.nvim_get_option_value(name, { win = winnr })
else
---@diagnostic disable-next-line:deprecated
return vim.api.nvim_buf_get_option(winnr, name)
end
end
--- Set option value of given window.
--- @param winnr integer
--- @param name string
function M.win_set_option(winnr, name, value)
if _G._outline_nvim_has[10] then
return vim.api.nvim_set_option_value(name, value, { win = winnr })
else
---@diagnostic disable-next-line:deprecated
return vim.api.nvim_win_set_option(winnr, name, value)
end
end
return M return M

View File

@@ -1,4 +1,5 @@
local config = require('outline.config') local config = require('outline.config')
local utils = require('outline.utils')
local M = {} local M = {}
@@ -14,7 +15,7 @@ function M.is_buf_attached_to_lsp(bufnr)
end end
function M.is_buf_markdown(bufnr) function M.is_buf_markdown(bufnr)
return vim.api.nvim_get_option_value('ft', { buf = bufnr }) == 'markdown' return utils.buf_get_option(bufnr, 'ft') == 'markdown'
end end
--- Merge all client token lists in an LSP response --- Merge all client token lists in an LSP response

View File

@@ -1,5 +1,6 @@
local cfg = require('outline.config') local cfg = require('outline.config')
local highlight = require('outline.highlight') local highlight = require('outline.highlight')
local utils = require('outline.utils')
---@class outline.View ---@class outline.View
local View = {} local View = {}
@@ -19,10 +20,10 @@ function View:setup_view(split_command)
self.buf = vim.api.nvim_create_buf(false, true) self.buf = vim.api.nvim_create_buf(false, true)
-- set filetype -- set filetype
vim.api.nvim_set_option_value('filetype', 'Outline', { buf = self.buf }) utils.buf_set_option(self.buf, 'filetype', 'Outline')
-- delete buffer when window is closed / buffer is hidden -- delete buffer when window is closed / buffer is hidden
vim.api.nvim_set_option_value('bufhidden', 'delete', { buf = self.buf }) utils.buf_set_option(self.buf, 'bufhidden', 'delete')
-- create a split -- create a split
vim.cmd(split_command) vim.cmd(split_command)
@@ -38,38 +39,38 @@ function View:setup_view(split_command)
end end
-- window stuff -- window stuff
vim.api.nvim_set_option_value('spell', false, { win = self.win }) utils.win_set_option(self.win, 'spell', false)
vim.api.nvim_set_option_value('signcolumn', 'no', { win = self.win }) utils.win_set_option(self.win, 'signcolumn', 'no')
vim.api.nvim_set_option_value('foldcolumn', '0', { win = self.win }) utils.win_set_option(self.win, 'foldcolumn', '0')
vim.api.nvim_set_option_value('number', false, { win = self.win }) utils.win_set_option(self.win, 'number', false)
vim.api.nvim_set_option_value('relativenumber', false, { win = self.win }) utils.win_set_option(self.win, 'relativenumber', false)
vim.api.nvim_set_option_value('winfixwidth', true, { win = self.win }) utils.win_set_option(self.win, 'winfixwidth', true)
vim.api.nvim_set_option_value('list', false, { win = self.win }) utils.win_set_option(self.win, 'list', false)
vim.api.nvim_set_option_value('wrap', cfg.o.outline_window.wrap, { win = self.win }) utils.win_set_option(self.win, 'wrap', cfg.o.outline_window.wrap)
vim.api.nvim_set_option_value('winhl', cfg.o.outline_window.winhl, { win = self.win }) utils.win_set_option(self.win, 'winhl', cfg.o.outline_window.winhl)
vim.api.nvim_set_option_value('linebreak', true, { win = self.win }) -- only has effect when wrap=true utils.win_set_option(self.win, 'linebreak', true) -- only has effect when wrap=true
vim.api.nvim_set_option_value('breakindent', true, { win = self.win }) -- only has effect when wrap=true utils.win_set_option(self.win, 'breakindent', true) -- only has effect when wrap=true
-- Would be nice to use guides.markers.vertical as part of showbreak to keep -- Would be nice to use guides.markers.vertical as part of showbreak to keep
-- continuity of the tree UI, but there's currently no way to style the -- continuity of the tree UI, but there's currently no way to style the
-- color, apart from globally overriding hl-NonText, which will potentially -- color, apart from globally overriding hl-NonText, which will potentially
-- mess with other theme/user settings. So just use empty spaces for now. -- mess with other theme/user settings. So just use empty spaces for now.
vim.api.nvim_set_option_value('showbreak', ' ', { win = self.win }) -- only has effect when wrap=true. utils.win_set_option(self.win, 'showbreak', ' ') -- only has effect when wrap=true.
-- buffer stuff -- buffer stuff
local tab = vim.api.nvim_get_current_tabpage() local tab = vim.api.nvim_get_current_tabpage()
vim.api.nvim_buf_set_name(self.buf, 'OUTLINE_' .. tostring(tab)) vim.api.nvim_buf_set_name(self.buf, 'OUTLINE_' .. tostring(tab))
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf }) utils.buf_set_option(self.buf, 'modifiable', false)
if cfg.o.outline_window.show_numbers or cfg.o.outline_window.show_relative_numbers then if cfg.o.outline_window.show_numbers or cfg.o.outline_window.show_relative_numbers then
vim.api.nvim_set_option_value('nu', true, { win = self.win }) utils.win_set_option(self.win, 'nu', true)
end end
if cfg.o.outline_window.show_relative_numbers then if cfg.o.outline_window.show_relative_numbers then
vim.api.nvim_set_option_value('rnu', true, { win = self.win }) utils.win_set_option(self.win, 'rnu', true)
end end
local cl = cfg.o.outline_window.show_cursorline local cl = cfg.o.outline_window.show_cursorline
if cl == true or cl == 'focus_in_outline' then if cl == true or cl == 'focus_in_outline' then
vim.api.nvim_set_option_value('cursorline', true, { win = self.win }) utils.win_set_option(self.win, 'cursorline', true)
end end
end end
@@ -100,9 +101,9 @@ end
---@param lines string[] ---@param lines string[]
function View:rewrite_lines(lines) function View:rewrite_lines(lines)
if self.buf and vim.api.nvim_buf_is_valid(self.buf) then if self.buf and vim.api.nvim_buf_is_valid(self.buf) then
vim.api.nvim_set_option_value('modifiable', true, { buf = self.buf }) utils.buf_set_option(self.buf, 'modifiable', true)
vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines) vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines)
vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf }) utils.buf_set_option(self.buf, 'modifiable', false)
end end
end end