首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >通过一个插件来了解neovim的winbar属性

通过一个插件来了解neovim的winbar属性

作者头像
程序那些事儿
发布2023-03-07 11:10:44
发布2023-03-07 11:10:44
90100
代码可运行
举报
文章被收录于专栏:程序那些事儿程序那些事儿
运行总次数:0
代码可运行

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 用来配置非当前窗口样式。

主要代码

代码语言:javascript
代码运行次数:0
运行
复制
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,用户开发起来变得非常轻松。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-11-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序那些事儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • window bar
  • 代码上下文
  • 函数api介绍
  • 高亮配置
  • 主要代码
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档