服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - 编程技术 - 通过一个插件来了解Neovim的Winbar属性

通过一个插件来了解Neovim的Winbar属性

2022-11-09 17:33程序那些事儿梦回丶故里 编程技术

neovim的插件目前几乎都是用lua进行编写的,lua使用起来不仅效率高,而且配置起来也非常的方便,此外,neovim也在不断地完善自己的api,用户开发起来变得非常轻松。

window bar

window bar 是显示在每个窗口的上面的,默认它是不显示的,你需要配置才可以。你可以把它看成和底部的状态栏类似的东西,只不过它显示在窗口顶部。

通过一个插件来了解Neovim的Winbar属性

通过:h winbar命令可以查看它的帮助文档。我们可以通过配置选项来配置winbar的显示内容。

配置的命令是vim.opt.winbar=配置内容。

代码上下文

winbar 可以显示任何内容,但是我们更多的时候是希望它显示一些有意义的内容,比如文件名和一些代码的函数名,属性信息等,此外,我们还可以显示文件的状态,比如文件是否被修改了。

通过一个插件来了解Neovim的Winbar属性

函数api介绍

  • get_winbar 函数用来显示当前代码的上下文信息
  • get_location 函数用来获取当前代码的上下文。
  • get_modified 用来显示文件名,检查文件是否修改,如果修改显示图标。
  • WinBarSeparator, WinBarContext, and WinBarFilename 是用来显示自定义的高亮。

在代码中我们使用nvim-navic插件来帮助我们获取代码的上下文更详细的信息。

高亮配置

为了更好地显示窗口样式,我们可以通过修改winbar提供的高亮组样式来修改winbar的样式。

通过一个插件来了解Neovim的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

延伸 · 阅读

精彩推荐