refactor: Consistent naming of buf/win fields

This commit is contained in:
hedy
2023-11-26 15:27:57 +08:00
parent c7d38eea24
commit c4da6670a4
4 changed files with 59 additions and 59 deletions

View File

@@ -17,7 +17,7 @@ end
-- handler yoinked from the default implementation -- handler yoinked from the default implementation
function M.show_hover() 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 node = outline.current.flats[current_line]
local hover_params = get_hover_params(node, outline.current.code.win) local hover_params = get_hover_params(node, outline.current.code.win)

View File

@@ -11,7 +11,7 @@ local state = {
local function is_current_win_outline() local function is_current_win_outline()
local curwin = vim.api.nvim_get_current_win() local curwin = vim.api.nvim_get_current_win()
return curwin == outline.current.view.winnr return curwin == outline.current.view.win
end end
local function has_code_win(winnr) local function has_code_win(winnr)
@@ -29,7 +29,7 @@ M.has_code_win = has_code_win
---@param preview_width integer ---@param preview_width integer
local function get_col(preview_width) local function get_col(preview_width)
---@type integer ---@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_col = vim.api.nvim_win_get_position(outline_winnr)[2]
local outline_width = vim.api.nvim_win_get_width(outline_winnr) 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] local code_col = vim.api.nvim_win_get_position(outline.current.code.win)[2]
@@ -50,15 +50,15 @@ end
---@param outline_height integer ---@param outline_height integer
local function get_row(preview_height, outline_height) local function get_row(preview_height, outline_height)
local offset = math.floor((outline_height - preview_height) / 2) - 1 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 end
local function get_height() 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 end
local function get_hovered_node() 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] local node = outline.current.flats[hovered_line]
return node return node
end end

View File

@@ -86,7 +86,7 @@ function Sidebar:initial_handler(response, opts)
self.view:setup_view(sc) self.view:setup_view(sc)
-- clear state when buffer is closed -- 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(_, _) on_detach = function(_, _)
self:reset_state() self:reset_state()
end, end,
@@ -123,7 +123,7 @@ function Sidebar:nmap(cfg_name, method, args)
for _, key in ipairs(keys) do for _, key in ipairs(keys) do
vim.keymap.set( 'n', key, fn, vim.keymap.set( 'n', key, fn,
{ silent = true, noremap = true, buffer = self.view.bufnr } { silent = true, noremap = true, buffer = self.view.buf }
) )
end end
end end
@@ -264,7 +264,7 @@ function Sidebar:update_cursor_pos(current)
col = #tostring(vim.api.nvim_buf_line_count(buf) - 1) col = #tostring(vim.api.nvim_buf_line_count(buf) - 1)
end end
if current then -- Don't attempt to set cursor if the matching node is not found 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
end end
@@ -315,7 +315,7 @@ end
---Re-request symbols from provider ---Re-request symbols from provider
function Sidebar:__refresh() 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 if focused_outline or not self.view:is_open() then
return return
end end
@@ -337,7 +337,7 @@ end
---Currently hovered node in outline ---Currently hovered node in outline
---@return outline.FlatSymbolNode ---@return outline.FlatSymbolNode
function Sidebar:_current_node() 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] return self.flats[current_line]
end end
@@ -384,8 +384,8 @@ function Sidebar:wrap_goto_location(fn)
local pos = vim.api.nvim_win_get_cursor(0) local pos = vim.api.nvim_win_get_cursor(0)
self:__goto_location(true) self:__goto_location(true)
fn() fn()
vim.fn.win_gotoid(self.view.winnr) vim.fn.win_gotoid(self.view.win)
vim.api.nvim_win_set_cursor(self.view.winnr, pos) vim.api.nvim_win_set_cursor(self.view.win, pos)
end end
---@param direction "up"|"down" ---@param direction "up"|"down"
@@ -419,7 +419,7 @@ function Sidebar:_set_folded(folded, move_cursor, node_index)
node.folded = folded node.folded = folded
if move_cursor then 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 end
self:_update_lines(false) self:_update_lines(false)
@@ -539,7 +539,7 @@ end
---@return boolean is_open ---@return boolean is_open
function Sidebar:focus() function Sidebar:focus()
if self.view:is_open() then if self.view:is_open() then
vim.fn.win_gotoid(self.view.winnr) vim.fn.win_gotoid(self.view.win)
return true return true
end end
return false 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 if self.view:is_open() and require('outline.preview').has_code_win(self.code.win) then
local winid = vim.fn.win_getid() local winid = vim.fn.win_getid()
if winid == self.code.win then if winid == self.code.win then
vim.fn.win_gotoid(self.view.winnr) vim.fn.win_gotoid(self.view.win)
else else
vim.fn.win_gotoid(self.code.win) vim.fn.win_gotoid(self.code.win)
end end
@@ -578,7 +578,7 @@ end
function Sidebar:has_focus() function Sidebar:has_focus()
local winid = vim.fn.win_getid() 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 end
---Whether there is currently an available provider. ---Whether there is currently an available provider.
@@ -593,7 +593,7 @@ end
function Sidebar:_highlight_current_item(winnr, update_cursor) function Sidebar:_highlight_current_item(winnr, update_cursor)
local has_provider = self:has_provider() local has_provider = self:has_provider()
local has_outline_open = self.view:is_open() 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 if not has_provider then
return return

View File

@@ -5,21 +5,21 @@ local highlight = require('outline.highlight')
local View = {} local View = {}
---@class outline.View ---@class outline.View
---@field bufnr integer ---@field buf integer
---@field winnr integer ---@field win integer
function View:new() function View:new()
return setmetatable({ bufnr = nil, winnr = nil }, { __index = View }) return setmetatable({ buf = nil, win = nil }, { __index = View })
end end
---Creates the outline window and sets it up ---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. ---@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) function View:setup_view(split_command)
-- create a scratch unlisted buffer -- 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 -- 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 -- create a split
vim.cmd(split_command) vim.cmd(split_command)
@@ -27,73 +27,73 @@ function View:setup_view(split_command)
vim.cmd('vertical resize ' .. cfg.get_window_width()) vim.cmd('vertical resize ' .. cfg.get_window_width())
-- get current (outline) window and attach our buffer to it -- get current (outline) window and attach our buffer to it
self.winnr = vim.api.nvim_get_current_win() self.win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(self.winnr, self.bufnr) vim.api.nvim_win_set_buf(self.win, self.buf)
-- window stuff -- window stuff
vim.api.nvim_win_set_option(self.winnr, 'spell', false) vim.api.nvim_win_set_option(self.win, 'spell', false)
vim.api.nvim_win_set_option(self.winnr, 'signcolumn', 'no') vim.api.nvim_win_set_option(self.win, 'signcolumn', 'no')
vim.api.nvim_win_set_option(self.winnr, 'foldcolumn', '0') vim.api.nvim_win_set_option(self.win, 'foldcolumn', '0')
vim.api.nvim_win_set_option(self.winnr, 'number', false) vim.api.nvim_win_set_option(self.win, 'number', false)
vim.api.nvim_win_set_option(self.winnr, 'relativenumber', false) vim.api.nvim_win_set_option(self.win, 'relativenumber', false)
vim.api.nvim_win_set_option(self.winnr, 'winfixwidth', true) vim.api.nvim_win_set_option(self.win, 'winfixwidth', true)
vim.api.nvim_win_set_option(self.winnr, 'list', false) vim.api.nvim_win_set_option(self.win, 'list', false)
vim.api.nvim_win_set_option(self.winnr, 'wrap', cfg.o.outline_window.wrap) vim.api.nvim_win_set_option(self.win, '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.win, '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.win, '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, '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_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 -- 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.bufnr, 'OUTLINE_' .. tostring(tab)) vim.api.nvim_buf_set_name(self.buf, 'OUTLINE_' .. tostring(tab))
vim.api.nvim_buf_set_option(self.bufnr, 'filetype', 'Outline') vim.api.nvim_buf_set_option(self.buf, 'filetype', 'Outline')
vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', false) 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 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 end
if cfg.o.outline_window.show_relative_numbers then 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 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_win_set_option(self.winnr, 'cursorline', true) vim.api.nvim_win_set_option(self.win, 'cursorline', true)
end end
end end
---Close view window and remove winnr/bufnr fields ---Close view window and remove winnr/bufnr fields
function View:close() function View:close()
if self.winnr then if self.win then
vim.api.nvim_win_close(self.winnr, true) vim.api.nvim_win_close(self.win, true)
self.winnr = nil self.win = nil
self.bufnr = nil self.buf = nil
end end
end end
---Return whether view has valid buf and win numbers ---Return whether view has valid buf and win numbers
function View:is_open() function View:is_open()
return self.winnr return self.win
and self.bufnr and self.buf
and vim.api.nvim_buf_is_valid(self.bufnr) and vim.api.nvim_buf_is_valid(self.buf)
and vim.api.nvim_win_is_valid(self.winnr) and vim.api.nvim_win_is_valid(self.win)
end end
---Replace all lines in buffer with given new `lines` ---Replace all lines in buffer with given new `lines`
---@param lines string[] ---@param lines string[]
function View:rewrite_lines(lines) function View:rewrite_lines(lines)
vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', true) vim.api.nvim_buf_set_option(self.buf, 'modifiable', true)
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines) vim.api.nvim_buf_set_lines(self.buf, 0, -1, false, lines)
vim.api.nvim_buf_set_option(self.bufnr, 'modifiable', false) vim.api.nvim_buf_set_option(self.buf, 'modifiable', false)
end end
function View:clear_all_ns() function View:clear_all_ns()
highlight.clear_all_ns(self.bufnr) highlight.clear_all_ns(self.buf)
end end
---Ensure all existing highlights are already cleared before calling! ---Ensure all existing highlights are already cleared before calling!
@@ -102,12 +102,12 @@ end
---@param details string[] ---@param details string[]
---@param linenos string[] ---@param linenos string[]
function View:add_hl_and_ns(hl, nodes, details, linenos) 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 if cfg.o.outline_items.highlight_hovered_item then
highlight.hovers(self.bufnr, nodes) highlight.hovers(self.buf, nodes)
end end
if cfg.o.outline_items.show_symbol_details then if cfg.o.outline_items.show_symbol_details then
highlight.details(self.bufnr, details) highlight.details(self.buf, details)
end end
-- Note on hl_mode: -- 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 if cfg.o.outline_items.show_symbol_lineno then
-- stylua: ignore start -- stylua: ignore start
highlight.linenos( highlight.linenos(
self.bufnr, linenos, self.buf, linenos,
(cfg.o.outline_window.hide_cursor and 'combine') or 'replace' (cfg.o.outline_window.hide_cursor and 'combine') or 'replace'
) )
-- stylua: ignore end -- stylua: ignore end