From 73d3cd71a1356e17b4affb600e6977eea2aa53cf Mon Sep 17 00:00:00 2001 From: liubang Date: Fri, 12 May 2023 23:03:55 +0800 Subject: [PATCH] perf(plugins): reduce configuration files to shorten startup time --- lua/lb/utils/util.lua | 68 ++++++++++++++ lua/plugins/lualine.lua | 177 ------------------------------------- lua/plugins/nvim-navic.lua | 92 ------------------- lua/plugins/ui.lua | 149 +++++++++++++++++++++++++++++++ 4 files changed, 217 insertions(+), 269 deletions(-) delete mode 100644 lua/plugins/lualine.lua delete mode 100644 lua/plugins/nvim-navic.lua diff --git a/lua/lb/utils/util.lua b/lua/lb/utils/util.lua index 04e2599e..d823ab5f 100644 --- a/lua/lb/utils/util.lua +++ b/lua/lb/utils/util.lua @@ -287,6 +287,74 @@ function M.strip_archive_subpath(path) -- {{{ return path end -- }}} +function M.file_size_format() -- {{{ + local file = vim.fn.expand "%:p" + if file == nil or #file == 0 then + return "" + end + local size = vim.fn.getfsize(file) + if size <= 0 then + return "" + end + + local suffixes = { "B", "KB", "MB", "GB" } + + local i = 1 + while size > 1024 and i < #suffixes do + size = size / 1024 + i = i + 1 + end + + local format = i == 1 and "[%d%s]" or "[%.1f%s]" + return string.format(format, size, suffixes[i]) +end -- }}} + +function M.lineinfo() -- {{{ + local line = vim.fn.line "." + local column = vim.fn.col "." + return string.format("%d:%d %s", line, column, "[%p%%]") +end -- }}} + +-- stylua: ignore +local lsp_names = { --{{{ + ["null-ls"] = "NLS", + ["diagnostics_on_open"] = "Diagnostics", + ["diagnostics_on_save"] = "Diagnostics", + clangd = "C++", + gopls = "Go", + rust_analyzer = "Rust", + lua_ls = "Lua", + intelephense = "PHP", + pyright = "Python", + bashls = "Bash", + dockerls = "Docker", + tsserver = "TS", + jsonls = "JSON", + sqls = "SQL", + texlab = "LaTeX", + taplo = "TOML", + html = "HTML", + vimls = "Vim", + yamlls = "YAML", + cssls = "CSS", + emmet_ls = "EMMET", + jdtls = "Java", +} +--}}} + +function M.lsp_clients_format() -- {{{ + local clients = {} + for _, client in pairs(vim.lsp.get_active_clients { bufnr = 0 }) do + local name = lsp_names[client.name] or client.name + clients[#clients + 1] = name + end + return " " .. table.concat(clients, " 珞 ") +end -- }}} + +function M.mode_format() -- {{{ + return "\u{e7c5} " .. require("lualine.utils.mode").get_mode() +end -- }}} + return M -- vim: fdm=marker fdl=0 diff --git a/lua/plugins/lualine.lua b/lua/plugins/lualine.lua deleted file mode 100644 index 6ef8ed6a..00000000 --- a/lua/plugins/lualine.lua +++ /dev/null @@ -1,177 +0,0 @@ ---===================================================================== --- --- lualine.lua - --- --- Created by liubang on 2022/12/30 23:34 --- Last Modified: 2023/02/19 00:39 --- ---===================================================================== - -local M = { "nvim-lualine/lualine.nvim", event = "VeryLazy" } - -function M.config() - local lualine = require "lualine" - - local lineinfo = function() - local line = vim.fn.line "." - local column = vim.fn.col "." - return string.format("%d:%d %s", line, column, "[%p%%]") - end - - local filesize = function() - local file = vim.fn.expand "%:p" - if file == nil or #file == 0 then - return "" - end - local size = vim.fn.getfsize(file) - if size <= 0 then - return "" - end - - local suffixes = { "B", "KB", "MB", "GB" } - - local i = 1 - while size > 1024 and i < #suffixes do - size = size / 1024 - i = i + 1 - end - - local format = i == 1 and "[%d%s]" or "[%.1f%s]" - return string.format(format, size, suffixes[i]) - end - - local mode = function() - return "\u{e7c5} " .. require("lualine.utils.mode").get_mode() - end - - -- stylua: ignore start - local lsp_names = { --{{{ - ["null-ls"] = "NLS", - ["diagnostics_on_open"] = "Diagnostics", - ["diagnostics_on_save"] = "Diagnostics", - clangd = "C++", - gopls = "Go", - rust_analyzer = "Rust", - lua_ls = "Lua", - intelephense = "PHP", - pyright = "Python", - bashls = "Bash", - dockerls = "Docker", - tsserver = "TS", - jsonls = "JSON", - sqls = "SQL", - texlab = "LaTeX", - taplo = "TOML", - html = "HTML", - vimls = "Vim", - yamlls = "YAML", - cssls = "CSS", - emmet_ls = "EMMET", - jdtls = "Java", - } - --}}} - -- stylua: ignore end - - local lsp_clients = function() - local clients = {} - for _, client in pairs(vim.lsp.get_active_clients { bufnr = 0 }) do - local name = lsp_names[client.name] or client.name - clients[#clients + 1] = name - end - return " " .. table.concat(clients, " 珞 ") - end - - lualine.setup { - options = { - icons_enabled = true, - theme = "gruvbox-material", - component_separators = "", - section_separators = "", - always_divide_middle = true, - globalstatus = true, - }, - sections = { - lualine_a = { mode }, - lualine_b = { - { "branch", icon = "\u{e725}" }, - { "diff" }, - { "diagnostics", colored = true }, - }, - lualine_c = { - -- '%=', -- center - { - "filename", - path = 1, - file_status = true, - shorting_target = 40, - symbols = { - modified = " \u{f040}", - readonly = " \u{f023}", - unnamed = "[No Name]", - }, - cond = function() - local f = vim.fn.expand "%:p" - if f:len() > 6 and f:sub(0, 6) == "jdt://" then - return false - end - return true - end, - }, - { - filesize, - cond = function() - local ft = vim.api.nvim_buf_get_option(0, "filetype") - if ft == "alpha" or ft == "NvimTree" or ft == "aerial" then - return false - end - return true - end, - }, - -- stylua: ignore - { - function() return require("nvim-navic").get_location() end, - cond = function() return require("nvim-navic").is_available() end, - }, - }, - lualine_x = { - lineinfo, - { - lsp_clients, - cond = function() - local ft = vim.api.nvim_buf_get_option(0, "filetype") - if ft == "alpha" or ft == "NvimTree" or ft == "aerial" then - return false - end - return next(vim.lsp.get_active_clients { bufnr = 0 }) ~= nil - end, - }, - }, - lualine_y = { - { "filetype", icon_only = true, colored = true }, - { "encoding" }, - }, - lualine_z = { - { - "fileformat", - icons_enabled = true, - symbols = { - unix = " UNIX", - dos = " WIN", - mac = " OSX", - }, - }, - }, - }, - inactive_sections = { - lualine_a = {}, - lualine_b = {}, - lualine_c = { "filename" }, - lualine_x = { "location" }, - lualine_y = {}, - lualine_z = {}, - }, - extensions = { "nvim-tree" }, - } -end - -return M diff --git a/lua/plugins/nvim-navic.lua b/lua/plugins/nvim-navic.lua deleted file mode 100644 index 3c780ae2..00000000 --- a/lua/plugins/nvim-navic.lua +++ /dev/null @@ -1,92 +0,0 @@ ---===================================================================== --- --- nvim-navic.lua - --- --- Created by liubang on 2022/12/30 22:08 --- Last Modified: 2022/12/30 22:08 --- ---===================================================================== -return { - "SmiteshP/nvim-navic", - init = function() - vim.g.navic_silence = true - vim.api.nvim_create_autocmd("LspAttach", { - callback = function(args) - local buffer = args.buf - local client = vim.lsp.get_client_by_id(args.data.client_id) - if client.server_capabilities.documentSymbolProvider then - require("nvim-navic").attach(client, buffer) - end - end, - }) - end, - opts = function() - -- stylua: ignore - local icons = { --{{{ - File = " ", - Module = " ", - Namespace = " ", - Package = " ", - Class = " ", - Method = " ", - Property = " ", - Field = " ", - Constructor = " ", - Enum = " ", - Interface = " ", - Function = " ", - Variable = " ", - Constant = " ", - String = " ", - Number = " ", - Boolean = " ", - Array = " ", - Object = " ", - Key = " ", - Null = " ", - EnumMember = " ", - Struct = " ", - Event = " ", - Operator = " ", - TypeParameter = " ", - } --}}} - - return { - -- stylua: ignore - icons = { - Array = icons.array, - Boolean = icons.boolean, - Class = icons.class, - Constant = icons.constant, - Constructor = icons.constructor, - Enum = icons.enum, - EnumMember = icons.enumMember, - Event = icons.event, - Field = icons.field, - File = icons.file, - Function = icons.func, - Interface = icons.interface, - Key = icons.key, - Method = icons.method, - Module = icons.module, - Namespace = icons.namespace, - Null = icons.null, - Number = icons.number, - Object = icons.object, - Operator = icons.operator, - Package = icons.package, - Property = icons.property, - String = icons.string, - Struct = icons.struct, - TypeParameter = icons.typeParameter, - Variable = icons.variable, - }, - separator = " > ", - -- separator = separator, - depth_limit = 3, - highlight = true, - depth_limit_indicator = "..", - safe_output = true, - } - end, -} diff --git a/lua/plugins/ui.lua b/lua/plugins/ui.lua index 89aa03bb..25e1a9dc 100644 --- a/lua/plugins/ui.lua +++ b/lua/plugins/ui.lua @@ -352,6 +352,101 @@ return { }, -- }}} }, + -- statusline + { + "nvim-lualine/lualine.nvim", -- {{{ + event = "VeryLazy", + opts = function() + return { + options = { + icons_enabled = true, + theme = "gruvbox-material", + component_separators = "", + section_separators = "", + always_divide_middle = true, + globalstatus = true, + }, + sections = { + lualine_a = { require("lb.utils.util").mode_format }, + lualine_b = { + { "branch", icon = "\u{e725}" }, + { "diff" }, + { "diagnostics", colored = true }, + }, + lualine_c = { + { + "filename", + path = 1, + file_status = true, + shorting_target = 40, + symbols = { + modified = " \u{f040}", + readonly = " \u{f023}", + unnamed = "[No Name]", + }, + cond = function() + local f = vim.fn.expand "%:p" + return f:len() <= 6 or f:sub(0, 6) ~= "jdt://" + end, + }, + { + require("lb.utils.util").file_size_format, + cond = function() + local ft = vim.api.nvim_buf_get_option(0, "filetype") + if ft == "alpha" or ft == "NvimTree" or ft == "aerial" then + return false + end + return true + end, + }, + -- stylua: ignore + { + function() return require("nvim-navic").get_location() end, + cond = function() return require("nvim-navic").is_available() end, + }, + }, + lualine_x = { + require("lb.utils.util").lineinfo, + { + require("lb.utils.util").lsp_clients_format, + cond = function() + local ft = vim.api.nvim_buf_get_option(0, "filetype") + if ft == "alpha" or ft == "NvimTree" or ft == "aerial" then + return false + end + return next(vim.lsp.get_active_clients { bufnr = 0 }) ~= nil + end, + }, + }, + lualine_y = { + { "filetype", icon_only = true, colored = true }, + { "encoding" }, + }, + lualine_z = { + { + "fileformat", + icons_enabled = true, + symbols = { + unix = " UNIX", + dos = " WIN", + mac = " OSX", + }, + }, + }, + }, + inactive_sections = { + lualine_a = {}, + lualine_b = {}, + lualine_c = { "filename" }, + lualine_x = { "location" }, + lualine_y = {}, + lualine_z = {}, + }, + extensions = { "nvim-tree" }, + } + end, + -- }}} + }, { "j-hui/fidget.nvim", -- {{{ event = { "LspAttach" }, @@ -445,6 +540,60 @@ return { end, -- }}} }, + { + "SmiteshP/nvim-navic", -- {{{ + init = function() + vim.g.navic_silence = true + vim.api.nvim_create_autocmd("LspAttach", { + callback = function(args) + local buffer = args.buf + local client = vim.lsp.get_client_by_id(args.data.client_id) + if client.server_capabilities.documentSymbolProvider then + require("nvim-navic").attach(client, buffer) + end + end, + }) + end, + opts = function() + return { + -- stylua: ignore + icons = { + File = " ", + Module = " ", + Namespace = " ", + Package = " ", + Class = " ", + Method = " ", + Property = " ", + Field = " ", + Constructor = " ", + Enum = " ", + Interface = " ", + Function = " ", + Variable = " ", + Constant = " ", + String = " ", + Number = " ", + Boolean = " ", + Array = " ", + Object = " ", + Key = " ", + Null = " ", + EnumMember = " ", + Struct = " ", + Event = " ", + Operator = " ", + TypeParameter = " ", + }, + separator = " > ", + depth_limit = 3, + highlight = true, + depth_limit_indicator = "..", + safe_output = true, + } + end, + -- }}} + }, } -- vim: fdm=marker fdl=0