From 57b504b9e8ae95c294c17e97e7f017f6f802ebbc Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 15 Oct 2023 22:28:56 +0200 Subject: [PATCH 1/5] fix(prettier): use prettier instead of prettierd. Too many people get truncated files. Fixes #712. See #1735 --- .../plugins/extras/formatting/prettier.lua | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lua/lazyvim/plugins/extras/formatting/prettier.lua b/lua/lazyvim/plugins/extras/formatting/prettier.lua index 6f1f3b58..a93f6ecf 100644 --- a/lua/lazyvim/plugins/extras/formatting/prettier.lua +++ b/lua/lazyvim/plugins/extras/formatting/prettier.lua @@ -2,7 +2,7 @@ return { { "williamboman/mason.nvim", opts = function(_, opts) - table.insert(opts.ensure_installed, "prettierd") + table.insert(opts.ensure_installed, "prettier") end, }, { @@ -11,7 +11,7 @@ return { opts = function(_, opts) local nls = require("null-ls") opts.sources = opts.sources or {} - table.insert(opts.sources, nls.builtins.formatting.prettierd) + table.insert(opts.sources, nls.builtins.formatting.prettier) end, }, { @@ -19,22 +19,22 @@ return { optional = true, opts = { formatters_by_ft = { - ["javascript"] = { { "prettierd", "prettier" } }, - ["javascriptreact"] = { { "prettierd", "prettier" } }, - ["typescript"] = { { "prettierd", "prettier" } }, - ["typescriptreact"] = { { "prettierd", "prettier" } }, - ["vue"] = { { "prettierd", "prettier" } }, - ["css"] = { { "prettierd", "prettier" } }, - ["scss"] = { { "prettierd", "prettier" } }, - ["less"] = { { "prettierd", "prettier" } }, - ["html"] = { { "prettierd", "prettier" } }, - ["json"] = { { "prettierd", "prettier" } }, - ["jsonc"] = { { "prettierd", "prettier" } }, - ["yaml"] = { { "prettierd", "prettier" } }, - ["markdown"] = { { "prettierd", "prettier" } }, - ["markdown.mdx"] = { { "prettierd", "prettier" } }, - ["graphql"] = { { "prettierd", "prettier" } }, - ["handlebars"] = { { "prettierd", "prettier" } }, + ["javascript"] = { "prettier" }, + ["javascriptreact"] = { "prettier" }, + ["typescript"] = { "prettier" }, + ["typescriptreact"] = { "prettier" }, + ["vue"] = { "prettier" }, + ["css"] = { "prettier" }, + ["scss"] = { "prettier" }, + ["less"] = { "prettier" }, + ["html"] = { "prettier" }, + ["json"] = { "prettier" }, + ["jsonc"] = { "prettier" }, + ["yaml"] = { "prettier" }, + ["markdown"] = { "prettier" }, + ["markdown.mdx"] = { "prettier" }, + ["graphql"] = { "prettier" }, + ["handlebars"] = { "prettier" }, }, }, }, From 782fe0bef0789c8f090284df80c00799471bc16f Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 15 Oct 2023 22:34:42 +0200 Subject: [PATCH 2/5] feat: disable kind_filter for markdown and help --- lua/lazyvim/config/init.lua | 10 ++++++++-- .../plugins/extras/editor/symbols-outline.lua | 20 ++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lua/lazyvim/config/init.lua b/lua/lazyvim/config/init.lua index 84163165..19686b57 100644 --- a/lua/lazyvim/config/init.lua +++ b/lua/lazyvim/config/init.lua @@ -92,7 +92,7 @@ local defaults = { Variable = "󰀫 ", }, }, - ---@type table? + ---@type table? kind_filter = { default = { "Class", @@ -109,6 +109,8 @@ local defaults = { "Struct", "Trait", }, + markdown = false, + help = false, -- you can specify a different filter for each filetype lua = { "Class", @@ -214,7 +216,11 @@ function M.get_kind_filter(buf) if M.kind_filter == false then return end - return M.kind_filter[ft] or M.kind_filter.default + if M.kind_filter[ft] == false then + return + end + ---@diagnostic disable-next-line: return-type-mismatch + return type(M.kind_filter) == "table" and type(M.kind_filter.default) == "table" and M.kind_filter.default or nil end ---@param name "autocmds" | "options" | "keymaps" diff --git a/lua/lazyvim/plugins/extras/editor/symbols-outline.lua b/lua/lazyvim/plugins/extras/editor/symbols-outline.lua index f0b0ba92..32cdd17f 100644 --- a/lua/lazyvim/plugins/extras/editor/symbols-outline.lua +++ b/lua/lazyvim/plugins/extras/editor/symbols-outline.lua @@ -12,14 +12,20 @@ return { symbols = {}, symbol_blacklist = {}, } + local filter = Config.kind_filter - for kind, symbol in pairs(defaults.symbols) do - opts.symbols[kind] = { - icon = Config.icons.kinds[kind] or symbol.icon, - hl = symbol.hl, - } - if not vim.tbl_contains(Config.kind_filter.default, kind) then - table.insert(opts.symbol_blacklist, kind) + if type(filter) == "table" then + filter = filter.default + if type(filter) == "table" then + for kind, symbol in pairs(defaults.symbols) do + opts.symbols[kind] = { + icon = Config.icons.kinds[kind] or symbol.icon, + hl = symbol.hl, + } + if not vim.tbl_contains(filter, kind) then + table.insert(opts.symbol_blacklist, kind) + end + end end end return opts From 8af7309c7e31f55125eaade5fe86d04d63133999 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 15 Oct 2023 22:37:49 +0200 Subject: [PATCH 3/5] feat(lualine): pretty_path now highlights file basename when modified --- lua/lazyvim/plugins/ui.lua | 8 ++---- lua/lazyvim/util/init.lua | 4 +++ lua/lazyvim/util/lualine.lua | 55 ++++++++++++++++++++++++++++++++++++ lua/lazyvim/util/root.lua | 39 +++++-------------------- 4 files changed, 69 insertions(+), 37 deletions(-) diff --git a/lua/lazyvim/plugins/ui.lua b/lua/lazyvim/plugins/ui.lua index 0707fb5b..73a52cdf 100644 --- a/lua/lazyvim/plugins/ui.lua +++ b/lua/lazyvim/plugins/ui.lua @@ -131,7 +131,9 @@ return { sections = { lualine_a = { "mode" }, lualine_b = { "branch" }, + lualine_c = { + Util.lualine.root_dir(), { "diagnostics", symbols = { @@ -142,11 +144,7 @@ return { }, }, { "filetype", icon_only = true, separator = "", padding = { left = 1, right = 0 } }, - { - function() - return Util.root.pretty_path() - end, - }, + { Util.lualine.pretty_path() }, }, lualine_x = { -- stylua: ignore diff --git a/lua/lazyvim/util/init.lua b/lua/lazyvim/util/init.lua index 4a98aee4..24fd0a1b 100644 --- a/lua/lazyvim/util/init.lua +++ b/lua/lazyvim/util/init.lua @@ -49,6 +49,10 @@ setmetatable(M, { end, }) +function M.is_win() + return vim.loop.os_uname().sysname:find("Windows") ~= nil +end + ---@param plugin string function M.has(plugin) return require("lazy.core.config").spec.plugins[plugin] ~= nil diff --git a/lua/lazyvim/util/lualine.lua b/lua/lazyvim/util/lualine.lua index 8efcf714..7eb353bf 100644 --- a/lua/lazyvim/util/lualine.lua +++ b/lua/lazyvim/util/lualine.lua @@ -43,4 +43,59 @@ function M.cmp_source(name, icon) } end +---@param component any +---@param text string +---@param hl_group? string +---@return string +function M.format(component, text, hl_group) + if not hl_group then + return text + end + ---@type table + component.hl_cache = component.hl_cache or {} + local lualine_hl_group = component.hl_cache[hl_group] + if not lualine_hl_group then + local utils = require("lualine.utils.utils") + lualine_hl_group = component:create_hl({ fg = utils.extract_highlight_colors(hl_group, "fg") }, "LV_" .. hl_group) + component.hl_cache[hl_group] = lualine_hl_group + end + return component:format_hl(lualine_hl_group) .. text .. component:get_default_hl() +end + +---@param opts? {relative: "cwd"|"root", modified_hl: string?} +function M.pretty_path(opts) + opts = vim.tbl_extend("force", { + relative = "cwd", + modified_hl = "Constant", + }, opts or {}) + + return function(self) + local path = vim.fn.expand("%:p") --[[@as string]] + + if path == "" then + return "" + end + local root = Util.root.get() + local cwd = Util.root.cwd() + + if opts.relative == "cwd" and path:find(cwd, 1, true) == 1 then + path = path:sub(#cwd + 2) + else + path = path:sub(#root + 2) + end + + local sep = package.config:sub(1, 1) + local parts = vim.split(path, "[\\/]") + if #parts > 3 then + parts = { parts[1], "…", parts[#parts - 1], parts[#parts] } + end + + if opts.modified_hl and vim.bo.modified then + parts[#parts] = M.format(self, parts[#parts], opts.modified_hl) + end + + return table.concat(parts, sep) + end +end + return M diff --git a/lua/lazyvim/util/root.lua b/lua/lazyvim/util/root.lua index d799fe44..80aece22 100644 --- a/lua/lazyvim/util/root.lua +++ b/lua/lazyvim/util/root.lua @@ -58,6 +58,10 @@ function M.bufpath(buf) return M.realpath(vim.api.nvim_buf_get_name(assert(buf))) end +function M.cwd() + return M.realpath(vim.loop.cwd()) or "" +end + function M.realpath(path) if path == "" or path == nil then return nil @@ -144,38 +148,9 @@ function M.get() return roots[1] and roots[1].paths[1] or vim.loop.cwd() end -M.pretty_cache = {} ---@type table -function M.pretty_path() - local path = vim.fn.expand("%:p") --[[@as string]] - if path == "" then - return "" - end - - path = Util.norm(path) - if M.pretty_cache[path] then - return M.pretty_cache[path] - end - local cache_key = path - local cwd = M.realpath(vim.loop.cwd()) or "" - - if path:find(cwd, 1, true) == 1 then - path = path:sub(#cwd + 2) - else - local roots = M.detect({ spec = { ".git" } }) - local root = roots[1] and roots[1].paths[1] or nil - if root then - path = path:sub(#vim.fs.dirname(root) + 2) - end - end - - local sep = package.config:sub(1, 1) - local parts = vim.split(path, "[\\/]") - if #parts > 3 then - parts = { parts[1], "…", parts[#parts - 1], parts[#parts] } - end - local ret = table.concat(parts, sep) - M.pretty_cache[cache_key] = ret - return ret +---@param opts? {hl_last?: string} +function M.pretty_path(opts) + return "" end return M From 6f88b8b36f8d4fd0e958e59b4e37e1a9ed2acb78 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 15 Oct 2023 22:38:15 +0200 Subject: [PATCH 4/5] fix(root): dont use single-file lsps for root detection. use workspaces only --- lua/lazyvim/util/root.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lua/lazyvim/util/root.lua b/lua/lazyvim/util/root.lua index 80aece22..519546e1 100644 --- a/lua/lazyvim/util/root.lua +++ b/lua/lazyvim/util/root.lua @@ -32,13 +32,12 @@ function M.detectors.lsp(buf) end local roots = {} ---@type string[] for _, client in pairs(Util.lsp.get_clients({ bufnr = buf })) do + -- only check workspace folders, since we're not interested in clients + -- running in single file mode local workspace = client.config.workspace_folders for _, ws in pairs(workspace or {}) do roots[#roots + 1] = vim.uri_to_fname(ws.uri) end - if client.config.root_dir then - roots[#roots + 1] = client.config.root_dir - end end return vim.tbl_filter(function(path) path = Util.norm(path) From dfdfcad1aab0ee39ac3876e47cbeb727eb4f1e95 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 15 Oct 2023 22:38:44 +0200 Subject: [PATCH 5/5] feat(lualine): new root dir component that only shows when cwd != root_dir --- lua/lazyvim/util/lualine.lua | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lua/lazyvim/util/lualine.lua b/lua/lazyvim/util/lualine.lua index 7eb353bf..1ec2cd19 100644 --- a/lua/lazyvim/util/lualine.lua +++ b/lua/lazyvim/util/lualine.lua @@ -98,4 +98,46 @@ function M.pretty_path(opts) end end +---@param opts? {cwd:false, subdirectory: true, parent: true, other: true, icon?:string} +function M.root_dir(opts) + opts = vim.tbl_extend("force", { + cwd = false, + subdirectory = true, + parent = true, + other = true, + icon = "󱉭 ", + color = Util.ui.fg("Special"), + }, opts or {}) + + local function get() + local cwd = Util.root.cwd() + local root = Util.root.get() + local name = vim.fs.basename(root) + + if root == cwd then + -- root is cwd + return opts.cwd and name + elseif root:find(cwd, 1, true) == 1 then + -- root is subdirectory of cwd + return opts.subdirectory and name + elseif cwd:find(root, 1, true) == 1 then + -- root is parent directory of cwd + return opts.parent and name + else + -- root and cwd are not related + return opts.other and name + end + end + + return { + function() + return (opts.icon and opts.icon .. " ") .. get() + end, + cond = function() + return type(get()) == "string" + end, + color = opts.color, + } +end + return M