From c4da6670a4335a3f937aca92214bcc829024859d Mon Sep 17 00:00:00 2001 From: hedy Date: Sun, 26 Nov 2023 15:27:57 +0800 Subject: [PATCH] refactor: Consistent naming of buf/win fields --- lua/outline/hover.lua | 2 +- lua/outline/preview.lua | 10 ++--- lua/outline/sidebar.lua | 24 ++++++------ lua/outline/view.lua | 82 ++++++++++++++++++++--------------------- 4 files changed, 59 insertions(+), 59 deletions(-) diff --git a/lua/outline/hover.lua b/lua/outline/hover.lua index 9295452..39c5051 100644 --- a/lua/outline/hover.lua +++ b/lua/outline/hover.lua @@ -17,7 +17,7 @@ end -- handler yoinked from the default implementation function M.show_hover() - local current_line = vim.api.nvim_win_get_cursor(outline.current.view.winnr)[1] + local current_line = vim.api.nvim_win_get_cursor(outline.current.view.win)[1] local node = outline.current.flats[current_line] local hover_params = get_hover_params(node, outline.current.code.win) diff --git a/lua/outline/preview.lua b/lua/outline/preview.lua index bad59f0..14bb911 100644 --- a/lua/outline/preview.lua +++ b/lua/outline/preview.lua @@ -11,7 +11,7 @@ local state = { local function is_current_win_outline() local curwin = vim.api.nvim_get_current_win() - return curwin == outline.current.view.winnr + return curwin == outline.current.view.win end local function has_code_win(winnr) @@ -29,7 +29,7 @@ M.has_code_win = has_code_win ---@param preview_width integer local function get_col(preview_width) ---@type integer - local outline_winnr = outline.current.view.winnr + local outline_winnr = outline.current.view.win local outline_col = vim.api.nvim_win_get_position(outline_winnr)[2] local outline_width = vim.api.nvim_win_get_width(outline_winnr) local code_col = vim.api.nvim_win_get_position(outline.current.code.win)[2] @@ -50,15 +50,15 @@ end ---@param outline_height integer local function get_row(preview_height, outline_height) local offset = math.floor((outline_height - preview_height) / 2) - 1 - return vim.api.nvim_win_get_position(outline.current.view.winnr)[1] + offset + return vim.api.nvim_win_get_position(outline.current.view.win)[1] + offset end local function get_height() - return vim.api.nvim_win_get_height(outline.current.view.winnr) + return vim.api.nvim_win_get_height(outline.current.view.win) end local function get_hovered_node() - local hovered_line = vim.api.nvim_win_get_cursor(outline.current.view.winnr)[1] + local hovered_line = vim.api.nvim_win_get_cursor(outline.current.view.win)[1] local node = outline.current.flats[hovered_line] return node end diff --git a/lua/outline/sidebar.lua b/lua/outline/sidebar.lua index 25f5c09..8715947 100644 --- a/lua/outline/sidebar.lua +++ b/lua/outline/sidebar.lua @@ -86,7 +86,7 @@ function Sidebar:initial_handler(response, opts) self.view:setup_view(sc) -- clear state when buffer is closed - vim.api.nvim_buf_attach(self.view.bufnr, false, { + vim.api.nvim_buf_attach(self.view.buf, false, { on_detach = function(_, _) self:reset_state() end, @@ -123,7 +123,7 @@ function Sidebar:nmap(cfg_name, method, args) for _, key in ipairs(keys) do vim.keymap.set( 'n', key, fn, - { silent = true, noremap = true, buffer = self.view.bufnr } + { silent = true, noremap = true, buffer = self.view.buf } ) end end @@ -264,7 +264,7 @@ function Sidebar:update_cursor_pos(current) col = #tostring(vim.api.nvim_buf_line_count(buf) - 1) end if current then -- Don't attempt to set cursor if the matching node is not found - vim.api.nvim_win_set_cursor(self.view.winnr, { current.line_in_outline, col }) + vim.api.nvim_win_set_cursor(self.view.win, { current.line_in_outline, col }) end end @@ -315,7 +315,7 @@ end ---Re-request symbols from provider function Sidebar:__refresh() - local focused_outline = self.view.bufnr == vim.api.nvim_get_current_buf() + local focused_outline = self.view.buf == vim.api.nvim_get_current_buf() if focused_outline or not self.view:is_open() then return end @@ -337,7 +337,7 @@ end ---Currently hovered node in outline ---@return outline.FlatSymbolNode function Sidebar:_current_node() - local current_line = vim.api.nvim_win_get_cursor(self.view.winnr)[1] + local current_line = vim.api.nvim_win_get_cursor(self.view.win)[1] return self.flats[current_line] end @@ -384,8 +384,8 @@ function Sidebar:wrap_goto_location(fn) local pos = vim.api.nvim_win_get_cursor(0) self:__goto_location(true) fn() - vim.fn.win_gotoid(self.view.winnr) - vim.api.nvim_win_set_cursor(self.view.winnr, pos) + vim.fn.win_gotoid(self.view.win) + vim.api.nvim_win_set_cursor(self.view.win, pos) end ---@param direction "up"|"down" @@ -419,7 +419,7 @@ function Sidebar:_set_folded(folded, move_cursor, node_index) node.folded = folded if move_cursor then - vim.api.nvim_win_set_cursor(self.view.winnr, { node_index, 0 }) + vim.api.nvim_win_set_cursor(self.view.win, { node_index, 0 }) end self:_update_lines(false) @@ -539,7 +539,7 @@ end ---@return boolean is_open function Sidebar:focus() if self.view:is_open() then - vim.fn.win_gotoid(self.view.winnr) + vim.fn.win_gotoid(self.view.win) return true end return false @@ -561,7 +561,7 @@ function Sidebar:focus_toggle() if self.view:is_open() and require('outline.preview').has_code_win(self.code.win) then local winid = vim.fn.win_getid() if winid == self.code.win then - vim.fn.win_gotoid(self.view.winnr) + vim.fn.win_gotoid(self.view.win) else vim.fn.win_gotoid(self.code.win) end @@ -578,7 +578,7 @@ end function Sidebar:has_focus() local winid = vim.fn.win_getid() - return self.view:is_open() and winid == self.view.winnr + return self.view:is_open() and winid == self.view.win end ---Whether there is currently an available provider. @@ -593,7 +593,7 @@ end function Sidebar:_highlight_current_item(winnr, update_cursor) local has_provider = self:has_provider() local has_outline_open = self.view:is_open() - local current_buffer_is_outline = self.view.bufnr == vim.api.nvim_get_current_buf() + local current_buffer_is_outline = self.view.buf == vim.api.nvim_get_current_buf() if not has_provider then return diff --git a/lua/outline/view.lua b/lua/outline/view.lua index 6ab9aaf..183bf31 100644 --- a/lua/outline/view.lua +++ b/lua/outline/view.lua @@ -5,21 +5,21 @@ local highlight = require('outline.highlight') local View = {} ---@class outline.View ----@field bufnr integer ----@field winnr integer +---@field buf integer +---@field win integer function View:new() - return setmetatable({ bufnr = nil, winnr = nil }, { __index = View }) + return setmetatable({ buf = nil, win = nil }, { __index = View }) end ---Creates the outline window and sets it up ---@param split_command string A valid split command that is to be executed in order to create the view. function View:setup_view(split_command) -- create a scratch unlisted buffer - self.bufnr = vim.api.nvim_create_buf(false, true) + self.buf = vim.api.nvim_create_buf(false, true) -- delete buffer when window is closed / buffer is hidden - vim.api.nvim_buf_set_option(self.bufnr, 'bufhidden', 'delete') + vim.api.nvim_buf_set_option(self.buf, 'bufhidden', 'delete') -- create a split vim.cmd(split_command) @@ -27,73 +27,73 @@ function View:setup_view(split_command) vim.cmd('vertical resize ' .. cfg.get_window_width()) -- get current (outline) window and attach our buffer to it - self.winnr = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_buf(self.winnr, self.bufnr) + self.win = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_buf(self.win, self.buf) -- window stuff - vim.api.nvim_win_set_option(self.winnr, 'spell', false) - vim.api.nvim_win_set_option(self.winnr, 'signcolumn', 'no') - vim.api.nvim_win_set_option(self.winnr, 'foldcolumn', '0') - vim.api.nvim_win_set_option(self.winnr, 'number', false) - vim.api.nvim_win_set_option(self.winnr, 'relativenumber', false) - vim.api.nvim_win_set_option(self.winnr, 'winfixwidth', true) - vim.api.nvim_win_set_option(self.winnr, 'list', false) - vim.api.nvim_win_set_option(self.winnr, 'wrap', cfg.o.outline_window.wrap) - vim.api.nvim_win_set_option(self.winnr, 'winhl', cfg.o.outline_window.winhl) - vim.api.nvim_win_set_option(self.winnr, 'linebreak', true) -- only has effect when wrap=true - vim.api.nvim_win_set_option(self.winnr, 'breakindent', true) -- only has effect when wrap=true + vim.api.nvim_win_set_option(self.win, 'spell', false) + vim.api.nvim_win_set_option(self.win, 'signcolumn', 'no') + vim.api.nvim_win_set_option(self.win, 'foldcolumn', '0') + vim.api.nvim_win_set_option(self.win, 'number', false) + vim.api.nvim_win_set_option(self.win, 'relativenumber', false) + vim.api.nvim_win_set_option(self.win, 'winfixwidth', true) + vim.api.nvim_win_set_option(self.win, 'list', false) + vim.api.nvim_win_set_option(self.win, 'wrap', cfg.o.outline_window.wrap) + vim.api.nvim_win_set_option(self.win, 'winhl', cfg.o.outline_window.winhl) + vim.api.nvim_win_set_option(self.win, 'linebreak', true) -- only has effect when wrap=true + vim.api.nvim_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_win_set_option(self.winnr, 'showbreak', ' ') -- only has effect when wrap=true. + vim.api.nvim_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.bufnr, 'OUTLINE_' .. tostring(tab)) - vim.api.nvim_buf_set_option(self.bufnr, 'filetype', 'Outline') - vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', false) + vim.api.nvim_buf_set_name(self.buf, 'OUTLINE_' .. tostring(tab)) + vim.api.nvim_buf_set_option(self.buf, 'filetype', 'Outline') + vim.api.nvim_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_win_set_option(self.winnr, 'nu', true) + vim.api.nvim_win_set_option(self.win, 'nu', true) end if cfg.o.outline_window.show_relative_numbers then - vim.api.nvim_win_set_option(self.winnr, 'rnu', true) + vim.api.nvim_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_win_set_option(self.winnr, 'cursorline', true) + vim.api.nvim_win_set_option(self.win, 'cursorline', true) end end ---Close view window and remove winnr/bufnr fields function View:close() - if self.winnr then - vim.api.nvim_win_close(self.winnr, true) - self.winnr = nil - self.bufnr = nil + if self.win then + vim.api.nvim_win_close(self.win, true) + self.win = nil + self.buf = nil end end ---Return whether view has valid buf and win numbers function View:is_open() - return self.winnr - and self.bufnr - and vim.api.nvim_buf_is_valid(self.bufnr) - and vim.api.nvim_win_is_valid(self.winnr) + return self.win + and self.buf + and vim.api.nvim_buf_is_valid(self.buf) + and vim.api.nvim_win_is_valid(self.win) end ---Replace all lines in buffer with given new `lines` ---@param lines string[] function View:rewrite_lines(lines) - vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', true) - vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines) - vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', false) + vim.api.nvim_buf_set_option(self.buf, 'modifiable', true) + vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines) + vim.api.nvim_buf_set_option(self.buf, 'modifiable', false) end function View:clear_all_ns() - highlight.clear_all_ns(self.bufnr) + highlight.clear_all_ns(self.buf) end ---Ensure all existing highlights are already cleared before calling! @@ -102,12 +102,12 @@ end ---@param details string[] ---@param linenos string[] function View:add_hl_and_ns(hl, nodes, details, linenos) - highlight.items(self.bufnr, hl) + highlight.items(self.buf, hl) if cfg.o.outline_items.highlight_hovered_item then - highlight.hovers(self.bufnr, nodes) + highlight.hovers(self.buf, nodes) end if cfg.o.outline_items.show_symbol_details then - highlight.details(self.bufnr, details) + highlight.details(self.buf, details) end -- Note on hl_mode: @@ -118,7 +118,7 @@ function View:add_hl_and_ns(hl, nodes, details, linenos) if cfg.o.outline_items.show_symbol_lineno then -- stylua: ignore start highlight.linenos( - self.bufnr, linenos, + self.buf, linenos, (cfg.o.outline_window.hide_cursor and 'combine') or 'replace' ) -- stylua: ignore end