From e93286a4897a32ab04672579b71461011a77deef Mon Sep 17 00:00:00 2001 From: 27Onion Nebell Date: Fri, 18 Apr 2025 16:33:09 +0800 Subject: [PATCH] fix: add version check for `nvim_set_option_value` and `nvim_get_option_value` --- lua/outline/config.lua | 2 +- lua/outline/float.lua | 10 +++--- lua/outline/init.lua | 4 +-- lua/outline/preview.lua | 31 +++++++++--------- lua/outline/providers/markdown.lua | 4 ++- lua/outline/providers/norg.lua | 3 +- lua/outline/providers/nvim-lsp.lua | 3 +- lua/outline/sidebar.lua | 10 +++--- lua/outline/utils/init.lua | 50 ++++++++++++++++++++++++++++++ lua/outline/utils/lsp.lua | 3 +- lua/outline/view.lua | 41 ++++++++++++------------ 11 files changed, 110 insertions(+), 51 deletions(-) diff --git a/lua/outline/config.lua b/lua/outline/config.lua index bb3b037..82ddc37 100644 --- a/lua/outline/config.lua +++ b/lua/outline/config.lua @@ -208,7 +208,7 @@ end ---@param bufnr integer ---@return boolean include 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 if ft == 'markdown' or ft == 'norg' or kind == nil then return true diff --git a/lua/outline/float.lua b/lua/outline/float.lua index 7273a47..ff46c54 100644 --- a/lua/outline/float.lua +++ b/lua/outline/float.lua @@ -1,6 +1,8 @@ ---@class outline.Float local Float = {} +local utils = require('outline.utils') + ---@class outline.Float ---@field bufnr integer ---@field winnr integer @@ -19,7 +21,7 @@ function Float:open(lines, hl, title, indent) indent = indent or 0 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 for _, l in ipairs(lines) do @@ -60,10 +62,10 @@ function Float:open(lines, hl, title, indent) 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_set_option_value('modifiable', false, { buf = self.bufnr }) - vim.api.nvim_set_option_value('ft', 'OutlineHelp', { buf = self.bufnr }) + utils.buf_set_option(self.bufnr, 'modifiable', false) + utils.buf_set_option(self.bufnr, 'ft', 'OutlineHelp') if hl then self.ns = vim.api.nvim_create_namespace('OutlineHelp') diff --git a/lua/outline/init.lua b/lua/outline/init.lua index 0d3ab90..cccbec0 100644 --- a/lua/outline/init.lua +++ b/lua/outline/init.lua @@ -3,7 +3,7 @@ local cfg = require('outline.config') local highlight = require('outline.highlight') local providers = require('outline.providers.init') local symbols = require('outline.symbols') -local utils = require('outline.utils.init') +local utils = require('outline.utils') local M = { ---@type outline.Sidebar[] @@ -278,7 +278,7 @@ function M.show_status() } 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] -- 'else' is handled in help.lua end diff --git a/lua/outline/preview.lua b/lua/outline/preview.lua index e712e6b..fb53400 100644 --- a/lua/outline/preview.lua +++ b/lua/outline/preview.lua @@ -1,4 +1,5 @@ local cfg = require('outline.config') +local utils = require('outline.utils') -- 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 @@ -151,12 +152,12 @@ end ---Set buf & win options, and setup highlight function Preview:setup() - vim.api.nvim_set_option_value('winhl', self.conf.winhl, { win = self.win }) - vim.api.nvim_set_option_value('winblend', self.conf.winblend, { win = self.win }) + utils.win_set_option(self.win, 'winhl', self.conf.winhl) + utils.win_set_option(self.win, 'winblend', self.conf.winblend) local code_buf = self.s.code.buf - local ft = vim.api.nvim_get_option_value('filetype', { buf = code_buf }) - vim.api.nvim_set_option_value('syntax', ft, { buf = self.buf }) + local ft = utils.buf_get_option(code_buf, 'filetype') + utils.buf_set_option(self.buf, 'syntax', ft) local ts_highlight_fn = vim.treesitter.start if not _G._outline_nvim_has[8] then @@ -167,9 +168,9 @@ function Preview:setup() end pcall(ts_highlight_fn, self.buf, ft) - vim.api.nvim_set_option_value('bufhidden', 'delete', { buf = self.buf }) - vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf }) - vim.api.nvim_set_option_value('cursorline', true, { win = self.win }) + utils.buf_set_option(self.buf, 'bufhidden', 'delete') + utils.buf_set_option(self.buf, 'modifiable', false) + utils.win_set_option(self.win, 'cursorline', true) end 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) 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_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 }) end end @@ -221,7 +222,7 @@ end ---Creates new preview window and sets the content. Calls setup and set_lines. function LivePreview:create() 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.width = cfg.get_preview_width(self.conf) self.height = cfg.get_preview_height(self.conf, self.outline_height) @@ -256,9 +257,9 @@ end ---Set buf & win options, and autocmds function LivePreview:setup() - vim.api.nvim_set_option_value('winhl', self.conf.winhl, { win = self.win }) - vim.api.nvim_set_option_value('winblend', self.conf.winblend, { win = self.win }) - vim.api.nvim_set_option_value('cursorline', true, { win = self.win }) + utils.win_set_option(self.win, 'winhl', self.conf.winhl) + utils.win_set_option(self.win, 'winblend', self.conf.winblend) + utils.win_set_option(self.win, 'cursorline', true) vim.api.nvim_create_autocmd('WinClosed', { pattern = tostring(self.win), @@ -273,7 +274,7 @@ function LivePreview:setup() once = true, callback = function() -- 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 @@ -286,7 +287,7 @@ end function LivePreview:focus() vim.api.nvim_set_current_win(self.win) -- 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 ---Create, focus, or update preview diff --git a/lua/outline/providers/markdown.lua b/lua/outline/providers/markdown.lua index ccbf7a6..7eea302 100644 --- a/lua/outline/providers/markdown.lua +++ b/lua/outline/providers/markdown.lua @@ -15,11 +15,13 @@ local M = { name = 'markdown', } +local utils = require('outline.utils') + ---@param bufnr integer ---@param config table? ---@return boolean ft_is_markdown 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 for _, ft_check in ipairs(config.filetypes) do if ft_check == ft then diff --git a/lua/outline/providers/norg.lua b/lua/outline/providers/norg.lua index ef2d434..20a58e7 100644 --- a/lua/outline/providers/norg.lua +++ b/lua/outline/providers/norg.lua @@ -17,11 +17,12 @@ local M = { ] ]], } +local utils = require('outline.utils') ---@param bufnr integer ---@param config table? 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 end diff --git a/lua/outline/providers/nvim-lsp.lua b/lua/outline/providers/nvim-lsp.lua index 40e94da..820de53 100644 --- a/lua/outline/providers/nvim-lsp.lua +++ b/lua/outline/providers/nvim-lsp.lua @@ -1,6 +1,7 @@ local cfg = require('outline.config') local jsx = require('outline.providers.jsx') local lsp_utils = require('outline.utils.lsp') +local utils = require('outline.utils') local l = vim.lsp @@ -252,7 +253,7 @@ function M.show_hover(sidebar) border = cfg.o.preview_window.border, 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 end diff --git a/lua/outline/sidebar.lua b/lua/outline/sidebar.lua index 888f7d3..74f97d1 100644 --- a/lua/outline/sidebar.lua +++ b/lua/outline/sidebar.lua @@ -5,7 +5,7 @@ local folding = require('outline.folding') local parser = require('outline.parser') local providers = require('outline.providers.init') local symbols = require('outline.symbols') -local utils = require('outline.utils.init') +local utils = require('outline.utils') local strlen = vim.fn.strlen @@ -249,7 +249,7 @@ function Sidebar:update_cursor_style() local hide_cursor = type(cl) ~= 'string' 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' end @@ -265,7 +265,7 @@ function Sidebar:reset_cursor_style() local cl = cfg.o.outline_window.show_cursorline 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 -- vim.opt doesn't seem to provide a way to remove last item, like a pop() -- 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 return end - local ft = vim.api.nvim_get_option_value('ft', { buf = buf }) - local listed = vim.api.nvim_get_option_value('ft', { buf = buf }) + local ft = utils.buf_get_option(buf, 'ft') + local listed = utils.buf_get_option(buf, 'ft') if ft == 'OutlineHelp' or not (listed or ft == 'help') then return end diff --git a/lua/outline/utils/init.lua b/lua/outline/utils/init.lua index 75346f7..c98e5db 100644 --- a/lua/outline/utils/init.lua +++ b/lua/outline/utils/init.lua @@ -123,4 +123,54 @@ function M.deepcopy_excluding(t, keys) return res 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 diff --git a/lua/outline/utils/lsp.lua b/lua/outline/utils/lsp.lua index 14709cf..a6a972e 100644 --- a/lua/outline/utils/lsp.lua +++ b/lua/outline/utils/lsp.lua @@ -1,4 +1,5 @@ local config = require('outline.config') +local utils = require('outline.utils') local M = {} @@ -14,7 +15,7 @@ function M.is_buf_attached_to_lsp(bufnr) end 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 --- Merge all client token lists in an LSP response diff --git a/lua/outline/view.lua b/lua/outline/view.lua index ac78909..6853cfa 100644 --- a/lua/outline/view.lua +++ b/lua/outline/view.lua @@ -1,5 +1,6 @@ local cfg = require('outline.config') local highlight = require('outline.highlight') +local utils = require('outline.utils') ---@class outline.View local View = {} @@ -19,10 +20,10 @@ function View:setup_view(split_command) self.buf = vim.api.nvim_create_buf(false, true) -- 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 - vim.api.nvim_set_option_value('bufhidden', 'delete', { buf = self.buf }) + utils.buf_set_option(self.buf, 'bufhidden', 'delete') -- create a split vim.cmd(split_command) @@ -38,38 +39,38 @@ function View:setup_view(split_command) end -- window stuff - vim.api.nvim_set_option_value('spell', false, { win = self.win }) - vim.api.nvim_set_option_value('signcolumn', 'no', { win = self.win }) - vim.api.nvim_set_option_value('foldcolumn', '0', { win = self.win }) - vim.api.nvim_set_option_value('number', false, { win = self.win }) - vim.api.nvim_set_option_value('relativenumber', false, { win = self.win }) - vim.api.nvim_set_option_value('winfixwidth', true, { win = self.win }) - vim.api.nvim_set_option_value('list', false, { win = self.win }) - vim.api.nvim_set_option_value('wrap', cfg.o.outline_window.wrap, { win = self.win }) - vim.api.nvim_set_option_value('winhl', cfg.o.outline_window.winhl, { win = self.win }) - vim.api.nvim_set_option_value('linebreak', true, { win = self.win }) -- 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, 'spell', false) + utils.win_set_option(self.win, 'signcolumn', 'no') + utils.win_set_option(self.win, 'foldcolumn', '0') + utils.win_set_option(self.win, 'number', false) + utils.win_set_option(self.win, 'relativenumber', false) + utils.win_set_option(self.win, 'winfixwidth', true) + utils.win_set_option(self.win, 'list', false) + utils.win_set_option(self.win, 'wrap', cfg.o.outline_window.wrap) + utils.win_set_option(self.win, 'winhl', cfg.o.outline_window.winhl) + utils.win_set_option(self.win, 'linebreak', true) -- 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 -- continuity of the tree UI, but there's currently no way to style the -- color, apart from globally overriding hl-NonText, which will potentially -- 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 local tab = vim.api.nvim_get_current_tabpage() 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 - vim.api.nvim_set_option_value('nu', true, { win = self.win }) + utils.win_set_option(self.win, 'nu', true) end 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 local cl = cfg.o.outline_window.show_cursorline 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 @@ -100,9 +101,9 @@ end ---@param lines string[] function View:rewrite_lines(lines) 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_set_option_value('modifiable', false, { buf = self.buf }) + utils.buf_set_option(self.buf, 'modifiable', false) end end