window bar
window bar 是显示在每个窗口的上面的,默认它是不显示的,你需要配置才可以。你可以把它看成和底部的状态栏类似的东西,只不过它显示在窗口顶部。
通过:h winbar命令可以查看它的帮助文档。我们可以通过配置选项来配置winbar的显示内容。
配置的命令是vim.opt.winbar=配置内容。
代码上下文
winbar 可以显示任何内容,但是我们更多的时候是希望它显示一些有意义的内容,比如文件名和一些代码的函数名,属性信息等,此外,我们还可以显示文件的状态,比如文件是否被修改了。
函数api介绍
- get_winbar 函数用来显示当前代码的上下文信息
- get_location 函数用来获取当前代码的上下文。
- get_modified 用来显示文件名,检查文件是否修改,如果修改显示图标。
- WinBarSeparator, WinBarContext, and WinBarFilename 是用来显示自定义的高亮。
在代码中我们使用nvim-navic插件来帮助我们获取代码的上下文更详细的信息。
高亮配置
为了更好地显示窗口样式,我们可以通过修改winbar提供的高亮组样式来修改winbar的样式。
- WinBar 用来配置当前窗口的样式
- WinBarNC 用来配置非当前窗口样式。
主要代码
local M = {} local colors = require "config.colors" local navic = require "nvim-navic" local utils = require "utils" local icons = require "config.icons" vim.api.nvim_set_hl(0, "WinBarSeparator", { fg = colors.grey }) vim.api.nvim_set_hl(0, "WinBarFilename", { fg = colors.green, bg = colors.grey }) vim.api.nvim_set_hl(0, "WinBarContext", { fg = colors.green, bg = colors.grey }) M.winbar_filetype_exclude = { "help", "startify", "dashboard", "packer", "neogitstatus", "NvimTree", "Trouble", "alpha", "lir", "Outline", "spectre_panel", "toggleterm", } local excludes = function() if vim.tbl_contains(M.winbar_filetype_exclude, vim.bo.filetype) then vim.opt_local.winbar = nil return true end return false end local function get_modified() if utils.get_buf_option "mod" then local mod = icons.git.Mod return "%#WinBarFilename#" .. mod .. " " .. "%t" .. "%*" end return "%#WinBarFilename#" .. "%t" .. "%*" end local function get_location() local location = navic.get_location() if not utils.is_empty(location) then return "%#WinBarContext#" .. " " .. icons.ui.ChevronRight .. " " .. location .. "%*" end return "" end function M.get_winbar() if excludes() then return "" end if navic.is_available() then return "%#WinBarSeparator#" .. "%=" .. "" .. "%*" .. get_modified() .. get_location() .. "%#WinBarSeparator#" .. "" .. "%*" else return "%#WinBarSeparator#" .. "%=" .. "" .. "%*" .. get_modified() .. "%#WinBarSeparator#" .. "" .. "%*" end end return M
总结
neovim的插件目前几乎都是用lua进行编写的,lua使用起来不仅效率高,而且配置起来也非常的方便,此外,neovim也在不断地完善自己的api,用户开发起来变得非常轻松。
原文地址:https://mp.weixin.qq.com/s/Tk2rqnpOGVo3a5ER0h1Ejg