diff --git a/after/ftplugin/zsh.vim b/after/ftplugin/zsh.vim index dd37846..dec94bc 100644 --- a/after/ftplugin/zsh.vim +++ b/after/ftplugin/zsh.vim @@ -1 +1,2 @@ set ts=4 sw=4 +set iskeyword+=- diff --git a/init.lua b/init.lua index 96442cd..3cc32c6 100644 --- a/init.lua +++ b/init.lua @@ -1,9 +1,29 @@ -if vim.env["VIRTUAL_ENV"] ~= nil then - vim.g.python3_host_prog = vim.fn.system("which -a python3 | sed -n 2p | tr -d '\n'") -else - vim.g.python3_host_prog = vim.fn.system("which python3 | tr -d '\n'") +local should_profile = os.getenv("NVIM_PROFILE") +if should_profile then + require("profile").instrument_autocmds() + if should_profile:lower():match("^start") then + require("profile").start("*") + else + require("profile").instrument("*") + end end +local function toggle_profile() + local prof = require("profile") + if prof.is_recording() then + prof.stop() + vim.ui.input({ prompt = "Save profile to:", completion = "file", default = "profile.json" }, function(filename) + if filename then + prof.export(filename) + vim.notify(string.format("Wrote %s", filename)) + end + end) + else + prof.start("*") + end +end +vim.keymap.set("", "", toggle_profile) + -- lazy.nvim bootstrap local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" if not vim.loop.fs_stat(lazypath) then @@ -88,6 +108,8 @@ vim.opt.pumblend = 10 vim.opt.updatetime = 300 +vim.opt.fixendofline = false + local command = vim.api.nvim_create_user_command local initlua = vim.fn.stdpath("config") .. "/init.lua" command("RC", function() @@ -130,3 +152,6 @@ key("n", "Y", "y$") key("i", "", "", { desc = "completion" }) key("n", "", "nohredraw!", { desc = "clear search highlight" }) key("v", "gs", "'<,'>sort", { desc = "sort selection" }) + +key("n", "", "bnext", { desc = "next buffer" }) +key("n", "", "bprevious", { desc = "previous buffer" }) diff --git a/lazy-lock.json b/lazy-lock.json index 72e5227..2b17856 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,47 +1,47 @@ { "Vim-Jinja2-Syntax": { "branch": "master", "commit": "2c17843b074b06a835f88587e1023ceff7e2c7d1" }, - "blink.cmp": { "branch": "main", "commit": "4e9edba1b1cef1585cc65e54287229e5d34e4df8" }, - "conform.nvim": { "branch": "master", "commit": "a0ab60ed666c56b37fd7ed1847d2ac52f2482ce0" }, + "blink.cmp": { "branch": "main", "commit": "327fff91fe6af358e990be7be1ec8b78037d2138" }, + "conform.nvim": { "branch": "master", "commit": "b4aab989db276993ea5dcb78872be494ce546521" }, "deepl.vim": { "branch": "main", "commit": "59df8cc17bb28989ce562bf4712c724d23baadcd" }, "dressing.nvim": { "branch": "master", "commit": "2d7c2db2507fa3c4956142ee607431ddb2828639" }, "gen.nvim": { "branch": "main", "commit": "c8e1f574d4a3a839dde73a87bdc319a62ee1e559" }, "gina.vim": { "branch": "master", "commit": "ff6c2ddeca98f886b57fb42283c12e167d6ab575" }, - "gitsigns.nvim": { "branch": "main", "commit": "6e3c66548035e50db7bd8e360a29aec6620c3641" }, - "hover.nvim": { "branch": "main", "commit": "24a43e0eda924f1f32361c76ee9a1f0e8cc25650" }, - "indent-blankline.nvim": { "branch": "master", "commit": "3d08501caef2329aba5121b753e903904088f7e6" }, + "gitsigns.nvim": { "branch": "main", "commit": "f780609807eca1f783a36a8a31c30a48fbe150c5" }, + "hover.nvim": { "branch": "main", "commit": "15533855dcf3c6a35a09c118c4169e531847b4cc" }, + "indent-blankline.nvim": { "branch": "master", "commit": "005b56001b2cb30bfa61b7986bc50657816ba4ba" }, "lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" }, - "lazydev.nvim": { "branch": "main", "commit": "2367a6c0a01eb9edb0464731cc0fb61ed9ab9d2c" }, + "lazydev.nvim": { "branch": "main", "commit": "954ecf72dab547f2a14db473cf6253eeb67dfd4a" }, "lsp-colors.nvim": { "branch": "main", "commit": "2bbe7541747fd339bdd8923fc45631a09bb4f1e5" }, "lsp-progress.nvim": { "branch": "main", "commit": "f61cb7a788e4695ed9ae5e1b6b01bfff8f136f8b" }, - "lsp_signature.nvim": { "branch": "master", "commit": "62cadce83aaceed677ffe7a2d6a57141af7131ea" }, "lualine.nvim": { "branch": "master", "commit": "b8c23159c0161f4b89196f74ee3a6d02cdc3a955" }, - "lush.nvim": { "branch": "main", "commit": "1be16d9002f8b2e8973a19ceac199ad394dea76a" }, - "mason-lspconfig.nvim": { "branch": "main", "commit": "1ec4da522fa49dcecee8d190efda273464dd2192" }, + "lush.nvim": { "branch": "main", "commit": "9c60ec2279d62487d942ce095e49006af28eed6e" }, + "mason-lspconfig.nvim": { "branch": "main", "commit": "7f9a39fcd2ac6e979001f857727d606888f5909c" }, "mason.nvim": { "branch": "main", "commit": "7dc4facca9702f95353d5a1f87daf23d78e31c2a" }, - "multicursor.nvim": { "branch": "1.0", "commit": "9eedebdd395bbbc4711081e33b0606c079e054c3" }, - "neoconf.nvim": { "branch": "main", "commit": "987be783f7e4cb9273e0e9b21c220f3b722b1ec6" }, - "nvim-dap": { "branch": "master", "commit": "968f89f8aac11b6bdbfc942c71d3436658c1435f" }, - "nvim-genghis": { "branch": "main", "commit": "0fccd6f547d954607083b66d08043b8ed54dee7a" }, - "nvim-jdtls": { "branch": "master", "commit": "7d1545614235cf6cf68b0839556d87c8f11c5eb5" }, - "nvim-lint": { "branch": "master", "commit": "ee04d481d4e6089892c2fb2ad8924b1a053591e1" }, - "nvim-lspconfig": { "branch": "master", "commit": "3d97ec4174bcc750d70718ddedabf150536a5891" }, + "multicursor.nvim": { "branch": "1.0", "commit": "ffe2e402e85150516d096842f7be99fd1321a72b" }, + "neoconf.nvim": { "branch": "main", "commit": "a7b03bc23971ea9d569da70e21817d9d49b78c19" }, + "nvim-dap": { "branch": "master", "commit": "7523676a4be17644587aa47e4d42f6f7646d4727" }, + "nvim-genghis": { "branch": "main", "commit": "cdf584d05ffc9d5c1f247079991552249e4f7487" }, + "nvim-jdtls": { "branch": "master", "commit": "b69924ca90014fef485ee153571bdcbc1ece8c2e" }, + "nvim-lint": { "branch": "master", "commit": "0864f81c681e15d9bdc1156fe3a17bd07db5a3ed" }, + "nvim-lspconfig": { "branch": "master", "commit": "d9879110d0422a566fa01d732556f4d5515e1738" }, "nvim-luadev": { "branch": "master", "commit": "3ba0c02c378503739f1fdb95cff3ea2aad48db3e" }, "nvim-metals": { "branch": "main", "commit": "db6c9ffb32ec698b96d11cba1317dccc26f5c16d" }, "nvim-quick-switcher": { "branch": "main", "commit": "b56ba55cff165ae1551836a79313933bf4d43ae2" }, "nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" }, - "nvim-treesitter-context": { "branch": "master", "commit": "dca8726fea2c14e1ce6adbaa76a04816fbfaff61" }, + "nvim-treesitter-context": { "branch": "master", "commit": "41847d3dafb5004464708a3db06b14f12bde548a" }, "nvim-treesitter-textobjects": { "branch": "master", "commit": "71385f191ec06ffc60e80e6b0c9a9d5daed4824c" }, - "nvim-web-devicons": { "branch": "master", "commit": "4ae47f4fb18e85b80e84b729974fe65483b06aaf" }, + "nvim-web-devicons": { "branch": "master", "commit": "6e51ca170563330e063720449c21f43e27ca0bc1" }, "playground": { "branch": "master", "commit": "ba48c6a62a280eefb7c85725b0915e021a1a0749" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, "popup.nvim": { "branch": "master", "commit": "b7404d35d5d3548a82149238289fa71f7f6de4ac" }, "shipwright.nvim": { "branch": "master", "commit": "e596ab48328c31873f4f4d2e070243bf9de16ff3" }, "splice.vim": { "branch": "master", "commit": "815a28e687fdf78b67e9b9cd4c21277bbe658873" }, "suda.vim": { "branch": "master", "commit": "9adda7d195222d4e2854efb2a88005a120296c47" }, + "table-nvim": { "branch": "main", "commit": "c044fd37169eb10376962b0d0cec5f94d58ca626" }, "telescope-fzf-native.nvim": { "branch": "main", "commit": "1f08ed60cafc8f6168b72b80be2b2ea149813e55" }, "telescope-lsp-handlers.nvim": { "branch": "trunk", "commit": "de02085d6af1633942549a238bc7a5524fa9b201" }, "telescope.nvim": { "branch": "master", "commit": "b4da76be54691e854d3e0e02c36b0245f945c2c7" }, - "trouble.nvim": { "branch": "main", "commit": "85bedb7eb7fa331a2ccbecb9202d8abba64d37b3" }, + "trouble.nvim": { "branch": "main", "commit": "3fb3bd737be8866e5f3a170abc70b4da8b5dd45a" }, "vim-caddyfile": { "branch": "master", "commit": "6d60d5af0d73f20b88ec388a9d70188d55ed8223" }, "vim-characterize": { "branch": "master", "commit": "a8bffac6cead6b2869d939ecad06312b187a4c79" }, "vim-commentary": { "branch": "master", "commit": "64a654ef4a20db1727938338310209b6a63f60c9" }, @@ -57,12 +57,14 @@ "vim-markdown-composer": { "branch": "master", "commit": "e6f99bc20cfcb277c63041b1f766e6d5940bcc76" }, "vim-mkdir": { "branch": "master", "commit": "f0ba7a7dc190a0cedf1d827958c99f3718109cf0" }, "vim-nftables": { "branch": "master", "commit": "26f8a506c6f3e41f1e4a8d6aa94c9a79a666bbff" }, - "vim-pass": { "branch": "master", "commit": "bb3ab598c4730e538eb3cefa39fbb8ed5775072c" }, + "vim-pass": { "branch": "master", "commit": "601bdc138c736b36c695eebe68e31ce82bafdff0" }, "vim-repeat": { "branch": "master", "commit": "65846025c15494983dafe5e3b46c8f88ab2e9635" }, + "vim-sleuth": { "branch": "master", "commit": "be69bff86754b1aa5adcbb527d7fcd1635a84080" }, "vim-surround": { "branch": "master", "commit": "3d188ed2113431cf8dac77be61b842acb64433d9" }, "vim-textobj-comment": { "branch": "master", "commit": "58ae4571b76a5bf74850698f23d235eef991dd4b" }, "vim-textobj-user": { "branch": "master", "commit": "41a675ddbeefd6a93664a4dc52f302fe3086a933" }, - "vimtex": { "branch": "master", "commit": "dc90feacb86f7b85b0b791d8073eefc769a23725" }, + "vim-theme-chroma": { "branch": "lush", "commit": "0ad7be22f2244f99a877edfd6261eaba47952b6f" }, + "vimtex": { "branch": "master", "commit": "77f31bd02cec678823c8614e6400db97390b5ce7" }, "which-key.nvim": { "branch": "main", "commit": "370ec46f710e058c9c1646273e6b225acf47cbed" }, "workspace-diagnostics.nvim": { "branch": "main", "commit": "60f9175b2501ae3f8b1aba9719c0df8827610c8e" } } diff --git a/lua/plugins/cmp.lua b/lua/plugins/cmp.lua deleted file mode 100644 index d956bb1..0000000 --- a/lua/plugins/cmp.lua +++ /dev/null @@ -1,122 +0,0 @@ -return {} ---return { --- { --- "hrsh7th/nvim-cmp", --- -- load cmp on InsertEnter --- event = "InsertEnter", --- -- these dependencies will only be loaded when cmp loads --- -- dependencies are always lazy-loaded unless specified otherwise --- dependencies = { --- "hrsh7th/cmp-nvim-lsp", --- "hrsh7th/cmp-buffer", --- "hrsh7th/cmp-path", --- "hrsh7th/cmp-cmdline", --- -- 'hrsh7th/cmp-vsnip', --- -- 'hrsh7th/vim-vsnip', --- -- 'hrsh7th/vim-vsnip-integ', --- "L3MON4D3/LuaSnip", --- "saadparwaiz1/cmp_luasnip", --- "onsails/lspkind.nvim", --- { --- "zbirenbaum/copilot-cmp", --- dependencies = { --- "hrsh7th/nvim-cmp", --- { --- "zbirenbaum/copilot.lua", --- opts = { --- suggestion = { enabled = false }, --- panel = { enabled = false }, --- filetypes = { --- mail = false, --- text = false, --- }, --- }, --- }, --- }, --- config = function() --- require("copilot_cmp").setup() --- end, --- }, --- }, --- config = function() --- local cmp = require("cmp") --- local lspkind = require("lspkind") --- local luasnip = require("luasnip") --- local has_words_before = function() --- unpack = unpack or table.unpack --- local line, col = unpack(vim.api.nvim_win_get_cursor(0)) --- return col ~= 0 --- and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil --- end --- --- cmp.setup({ --- snippet = { --- expand = function(args) --- --vim.fn["vsnip#anonymous"](args.body) --- require("luasnip").lsp_expand(args.body) --- end, --- }, --- mapping = { --- [""] = cmp.mapping.confirm({ select = true }), --- [""] = cmp.mapping.scroll_docs(-4), --- [""] = cmp.mapping.scroll_docs(4), --- [""] = cmp.mapping.complete(), --- [""] = cmp.mapping.abort(), --- [""] = cmp.mapping.confirm({ --- behavior = cmp.ConfirmBehavior.Replace, --- select = true, --- }), --- [""] = cmp.mapping({ --- i = function(fallback) --- if cmp.visible() and cmp.get_active_entry() then --- cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false }) --- else --- fallback() --- end --- end, --- s = cmp.mapping.confirm({ select = true }), --- c = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }), --- }), --- [""] = cmp.mapping(function(fallback) --- if cmp.visible() then --- cmp.select_next_item() --- -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() --- -- they way you will only jump inside the snippet region --- elseif luasnip.expand_or_jumpable() then --- luasnip.expand_or_jump() --- elseif has_words_before() then --- cmp.complete() --- else --- fallback() --- end --- end, { "i", "s" }), --- [""] = cmp.mapping(function(fallback) --- if cmp.visible() then --- cmp.select_prev_item() --- elseif luasnip.jumpable(-1) then --- luasnip.jump(-1) --- else --- fallback() --- end --- end, { "i", "s" }), --- }, --- sources = cmp.config.sources({ --- { name = "nvim_lsp" }, --- { name = "luasnip" }, --- { name = "copilot" }, --- }, { --- { name = "buffer", option = { keyword_pattern = [[\k\+]] } }, --- --{ name = 'path' }, --- }), --- formatting = { --- format = lspkind.cmp_format({ --- mode = "symbol_text", -- show only symbol annotations --- maxwidth = 80, -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters) --- ellipsis_char = "…", -- when popup menu exceed maxwidth, the truncated part would show ellipsis_char instead (must define maxwidth first) --- symbol_map = { Copilot = "" }, --- }), --- }, --- }) --- end, --- }, ---} diff --git a/lua/plugins/conform.lua b/lua/plugins/conform.lua index cf939f5..93322cb 100644 --- a/lua/plugins/conform.lua +++ b/lua/plugins/conform.lua @@ -17,6 +17,7 @@ return { opts = { -- Define your formatters formatters_by_ft = { + xml = { "xmlformatter" }, lua = { "stylua" }, python = { "isort", "black" }, javascript = { "prettierd", "prettier", stop_after_first = true }, diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index fa3cc12..d2f1ef8 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -5,8 +5,9 @@ return { "tpope/vim-surround", "tpope/vim-characterize", "tpope/vim-commentary", + "tpope/vim-sleuth", - { "chrisgrieser/nvim-genghis", dependencies = "stevearc/dressing.nvim" }, + { "chrisgrieser/nvim-genghis", dependencies = "stevearc/dressing.nvim", opts = { trashCmd = "rm" } }, -- ic / ac { @@ -21,13 +22,6 @@ return { "nvim-telescope/telescope.nvim", dependencies = { "nvim-lua/plenary.nvim" }, }, - - { - "ray-x/lsp_signature.nvim", - config = function() - require("lsp_signature").setup({}) - end, - }, "kyazdani42/nvim-web-devicons", "folke/trouble.nvim", "folke/lsp-colors.nvim", diff --git a/lua/plugins/markdown.lua b/lua/plugins/markdown.lua index 4666219..c5201d3 100644 --- a/lua/plugins/markdown.lua +++ b/lua/plugins/markdown.lua @@ -1,7 +1,31 @@ return { - "euclio/vim-markdown-composer", - build = "cargo build --release", - enabled = function() - return vim.fn.executable("cargo") - end, + { + "euclio/vim-markdown-composer", + build = "cargo build --release", + enabled = function() + return vim.fn.executable("cargo") + end, + + }, + { + 'SCJangra/table-nvim', + ft = 'markdown', + opts = { + mappings = { + next = '', -- Go to next cell. + prev = '', -- Go to previous cell. + insert_row_up = 'k', -- Insert a row above the current row. + insert_row_down = 'j', -- Insert a row below the current row. + move_row_up = 'K', -- Move the current row up. + move_row_down = 'J', -- Move the current row down. + insert_column_left = 'h', -- Insert a column to the left of current column. + insert_column_right = 'l', -- Insert a column to the right of current column. + move_column_left = 'H', -- Move the current column to the left. + move_column_right = 'L', -- Move the current column to the right. + insert_table = 't', -- Insert a new table. + insert_table_alt = 'T', -- Insert a new table that is not surrounded by pipes. + delete_column = 'd', -- Delete the column under cursor. + } + }, + } } diff --git a/lua/plugins/tabline.lua b/lua/plugins/tabline.lua index 30beb64..49e3ae6 100644 --- a/lua/plugins/tabline.lua +++ b/lua/plugins/tabline.lua @@ -40,7 +40,7 @@ return { local wincount = #vim.tbl_filter(function(i) return i == "leaf" - end, vim.tbl_flatten(vim.fn.winlayout(context.tabnr))) + end, vim.iter(vim.fn.winlayout(context.tabnr)):flatten():totable()) return name .. (wincount > 1 and " +" .. (wincount - 1) or "") diff --git a/lua/plugins/ui.lua b/lua/plugins/ui.lua index d209a3f..7d5f458 100644 --- a/lua/plugins/ui.lua +++ b/lua/plugins/ui.lua @@ -13,12 +13,64 @@ return { }, { "lukas-reineke/indent-blankline.nvim", + dependencies = { "nvim-treesitter/nvim-treesitter" }, main = "ibl", - tag = "v3.5.4", - opts = { - whitespace = { highlight = { "Whitespace", "CursorLine" }, remove_blankline_trail = false }, - indent = { highlight = { "Whitespace", "CursorLine" }, char = "" }, - scope = { enabled = true }, - }, + --tag = "v3.5.4", + --opts = { + -- whitespace = { highlight = { "Whitespace", "CursorLine" }, remove_blankline_trail = false }, + -- indent = { highlight = { "Whitespace", "CursorLine" }, char = "" }, + -- scope = { enabled = true }, + --}, + config = function() + local highlightLines = { + "RainbowRed", + "RainbowYellow", + "RainbowBlue", + "RainbowOrange", + "RainbowGreen", + "RainbowViolet", + "RainbowCyan", + } + local highlightDimLines = { + "RainbowDimRed", + "RainbowDimYellow", + "RainbowDimBlue", + "RainbowDimOrange", + "RainbowDimGreen", + "RainbowDimViolet", + "RainbowDimCyan", + } + local hooks = require 'ibl.hooks' + + hooks.register(hooks.type.HIGHLIGHT_SETUP, function() + vim.api.nvim_set_hl(0, "RainbowRed", { fg = "#E06C75" }) + vim.api.nvim_set_hl(0, "RainbowYellow", { fg = "#E5C07B" }) + vim.api.nvim_set_hl(0, "RainbowBlue", { fg = "#61AFEF" }) + vim.api.nvim_set_hl(0, "RainbowOrange", { fg = "#D19A66" }) + vim.api.nvim_set_hl(0, "RainbowGreen", { fg = "#98C379" }) + vim.api.nvim_set_hl(0, "RainbowViolet", { fg = "#C678DD" }) + vim.api.nvim_set_hl(0, "RainbowCyan", { fg = "#56B6C2" }) + vim.api.nvim_set_hl(0, "RainbowDimRed", { fg = "#733338" }) + vim.api.nvim_set_hl(0, "RainbowDimYellow", { fg = "#7B6335" }) + vim.api.nvim_set_hl(0, "RainbowDimBlue", { fg = "#2B587D" }) + vim.api.nvim_set_hl(0, "RainbowDimOrange", { fg = "#674D35" }) + vim.api.nvim_set_hl(0, "RainbowDimGreen", { fg = "#4C613D" }) + vim.api.nvim_set_hl(0, "RainbowDimViolet", { fg = "#663774" }) + vim.api.nvim_set_hl(0, "RainbowDimCyan", { fg = "#32555A" }) + end) + + require('ibl').setup { + indent = { + highlight = highlightDimLines, + char = '┊', + tab_char = '┊', + }, + scope = { + highlight = highlightLines, + char = '▎', + } + } + vim.api.nvim_set_hl(0, "@ibl.whitespace.char.1", {}) + end, }, } diff --git a/lua/profile.lua b/lua/profile.lua new file mode 100644 index 0000000..0fafc37 --- /dev/null +++ b/lua/profile.lua @@ -0,0 +1,143 @@ +local autocmd = require("profile.autocmd") +local clock = require("profile.clock") +local instrument = require("profile.instrument") +local util = require("profile.util") +local M = {} + +local event_defaults = { + pid = 1, + tid = 1, +} + +---Call this at the top of your init.vim to get durations for autocmds. If you +---don't, autocmds will show up as 'instant' events in the profile +M.instrument_autocmds = function() + autocmd.instrument_start() +end + +---Instrument matching modules +---@param name string Name of module or glob pattern (e.g. "telescope*") +M.instrument = function(name) + instrument(name) +end + +---Mark matching modules to be ignored by profiling +---@param name string Name of module or glob pattern (e.g. "telescope*") +M.ignore = function(name) + instrument.ignore(name) +end + +---@param sample_rate number Float between 0 and 1 +M.set_sample_rate = function(sample_rate) + instrument.set_sample_rate(sample_rate) +end + +---Start collecting data for a profile +---@param ... string Names or patterns of modules to instrument (if instrument() not called before this) +M.start = function(...) + for _, pattern in ipairs({ ... }) do + instrument(pattern) + end + autocmd.instrument_auto() + instrument.clear_events() + clock.reset() + instrument.recording = true + vim.api.nvim_exec_autocmds("User", { pattern = "ProfileStart", modeline = false }) +end + +---@return boolean +M.is_recording = function() + return instrument.recording +end + +---@param filename? string If present, write the profile data to this file +M.stop = function(filename) + instrument.recording = false + vim.api.nvim_exec_autocmds("User", { pattern = "ProfileStop", modeline = false }) + if filename then + M.export(filename) + end +end + +---@private +---@param name string Name of the function +---@param ... any Arguments to the function +M.log_start = function(name, ...) + if not instrument.recording then + return + end + instrument.add_event({ + name = name, + args = util.format_args(...), + cat = "function,manual", + ph = "B", + ts = clock(), + }) +end + +---@private +---@param name string Name of the function +---@param ... any Arguments to the function +M.log_end = function(name, ...) + if not instrument.recording then + return + end + instrument.add_event({ + name = name, + args = util.format_args(...), + cat = "function,manual", + ph = "E", + ts = clock(), + }) +end + +---@private +---@param name string Name of the function +---@param ... any Arguments to the function +M.log_instant = function(name, ...) + if not instrument.recording then + return + end + instrument.add_event({ + name = name, + args = util.format_args(...), + cat = "", + ph = "i", + ts = clock(), + s = "g", + }) +end + +---Print out a list of all modules that have been instrumented +M.print_instrumented_modules = function() + instrument.print_modules() +end + +---Write the trace to a file +---@param filename string +M.export = function(filename) + local file = assert(io.open(filename, "w")) + local events = instrument.get_events() + file:write("[") + for i, event in ipairs(events) do + local e = vim.tbl_extend("keep", event, event_defaults) + local ok, jse = pcall(vim.json.encode, e) + if not ok and e.args then + e.args = nil + ok, jse = pcall(vim.json.encode, e) + end + if ok then + file:write(jse) + if i < #events then + file:write(",\n") + end + else + local err = string.format("Could not encode event: %s\n%s", jse, vim.inspect(e)) + vim.api.nvim_echo({ { err, "Error" } }, true, {}) + end + end + file:write("]") + file:close() +end + +return M diff --git a/lua/profile/autocmd.lua b/lua/profile/autocmd.lua new file mode 100644 index 0000000..f7a5ee1 --- /dev/null +++ b/lua/profile/autocmd.lua @@ -0,0 +1,154 @@ +local M = {} + +local autocmds = { + "BufAdd", + "BufDelete", + "BufEnter", + "BufFilePost", + "BufFilePre", + "BufHidden", + "BufLeave", + "BufModifiedSet", + "BufNew", + "BufNewFile", + "BufRead", + -- "BufReadCmd", + "BufReadPre", + "BufUnload", + "BufWinEnter", + "BufWinLeave", + "BufWipeout", + "BufWrite", + -- "BufWriteCmd", + "BufWritePost", + "ChanInfo", + "ChanOpen", + "CmdUndefined", + "CmdlineChanged", + "CmdlineEnter", + "CmdlineLeave", + "CmdwinEnter", + "CmdwinLeave", + "ColorScheme", + "ColorSchemePre", + "CompleteChanged", + "CompleteDonePre", + "CompleteDone", + "CursorHold", + "CursorHoldI", + "CursorMoved", + "CursorMovedI", + "DiffUpdated", + "DirChanged", + -- "FileAppendCmd", + "FileAppendPost", + "FileAppendPre", + "FileChangedRO", + "ExitPre", + "FileChangedShell", + "FileChangedShellPost", + -- "FileReadCmd", + "FileReadPost", + "FileReadPre", + "FileType", + -- "FileWriteCmd", + "FileWritePost", + "FileWritePre", + "FilterReadPost", + "FilterReadPre", + "FilterWritePost", + "FilterWritePre", + "FocusGained", + "FocusLost", + "FuncUndefined", + "UIEnter", + "UILeave", + "InsertChange", + "InsertCharPre", + "TextYankPost", + "InsertEnter", + "InsertLeavePre", + "InsertLeave", + "LspAttach", + "LspDetach", + "LspTokenUpdate", + "MenuPopup", + "OptionSet", + "QuickFixCmdPre", + "QuickFixCmdPost", + "QuitPre", + "RemoteReply", + "SessionLoadPost", + "ShellCmdPost", + "Signal", + "ShellFilterPost", + "SourcePre", + "SourcePost", + -- "SourceCmd", + "SpellFileMissing", + "StdinReadPost", + "StdinReadPre", + "SwapExists", + "Syntax", + "TabEnter", + "TabLeave", + "TabNew", + "TabNewEntered", + "TabClosed", + "TermOpen", + "TermEnter", + "TermLeave", + "TermClose", + "TermResponse", + "TextChanged", + "TextChangedI", + "TextChangedP", + "User", + "VimEnter", + "VimLeave", + "VimLeavePre", + "VimResized", + "VimResume", + "VimSuspend", + "WinClosed", + "WinEnter", + "WinLeave", + "WinNew", + "WinScrolled", +} + +---@param groupname string +---@param fn string +local function create(groupname, fn) + local aug = vim.api.nvim_create_augroup(groupname, {}) + for _, autocmd in ipairs(autocmds) do + vim.api.nvim_create_autocmd(autocmd, { + desc = "profile.nvim " .. fn, + pattern = "*", + group = aug, + callback = function(args) + require("profile")[fn](autocmd, { match = args.match }) + end, + }) + end +end + +M.instrument_start = function() + create("lua_profile_start", "log_start") +end + +M.instrument_auto = function() + if vim.fn.exists("#lua_profile_start") ~= 0 then + create("lua_profile_end", "log_end") + else + create("lua_profile", "log_instant") + end +end + +M.clear = function() + vim.api.nvim_create_augroup("lua_profile", {}) + vim.api.nvim_create_augroup("lua_profile_start", {}) + vim.api.nvim_create_augroup("lua_profile_end", {}) +end + +return M diff --git a/lua/profile/clock.lua b/lua/profile/clock.lua new file mode 100644 index 0000000..0b53156 --- /dev/null +++ b/lua/profile/clock.lua @@ -0,0 +1,14 @@ +local hrtime = vim.loop.hrtime + +local start = hrtime() + +return setmetatable({ + reset = function() + start = hrtime() + end, +}, { + __call = function() + -- Microseconds + return (hrtime() - start) / 1e3 + end, +}) diff --git a/lua/profile/instrument.lua b/lua/profile/instrument.lua new file mode 100644 index 0000000..a0c0b69 --- /dev/null +++ b/lua/profile/instrument.lua @@ -0,0 +1,209 @@ +local clock = require("profile.clock") +local util = require("profile.util") +local M = {} + +local rawrequire = require +local events = {} +local ignore_list = { + "^_G$", + "^bit$", + "^coroutine$", + "^debug$", + "^ffi$", + "^io$", + "^jit.*$", + "^luv$", + "^math$", + "^os$", + "^package$", + "^string$", + "^table$", + "^vim%.inspect$", + "^profile.*$", + "^lspconfig%.util%.script_path$", + "^plenary%.async_lib.*$", +} + +local instrument_list = {} +local wrapped_modules = {} +local wrapped_functions = {} +M.recording = false +M.sample_rate = 1 +local exposed_globals = { + ["vim"] = vim, + ["vim.fn"] = vim.fn, + ["vim.api"] = vim.api, +} + +local function all_modules() + return vim.tbl_extend("keep", package.loaded, exposed_globals) +end + +local function should_instrument(name) + -- Don't double-wrap + if wrapped_functions[name] then + return false + elseif wrapped_modules[name] then + return false + else + for _, pattern in ipairs(ignore_list) do + if string.match(name, pattern) then + return false + end + end + return true + end +end + +local function wrap_function(name, fn) + return function(...) + if M.sample_rate < 1 and math.random() > M.sample_rate then + return fn(...) + end + local arg_string = util.format_args(...) + local start = clock() + local function handle_result(...) + local delta = clock() - start + M.add_event({ + name = name, + args = arg_string, + cat = "function", + ph = "X", + ts = start, + dur = delta, + }) + return ... + end + return handle_result(fn(...)) + end +end + +local function patch_function(modname, mod, name) + local fn = mod[name] + if type(fn) == "function" then + local fnname = string.format("%s.%s", modname, name) + if should_instrument(fnname) then + wrapped_functions[fnname] = fn + mod[name] = wrap_function(fnname, fn) + end + end +end + +local function maybe_wrap_function(name) + local parent_mod_name, fn = util.split_path(name) + local mod = M.get_module(parent_mod_name) + if mod then + patch_function(parent_mod_name, mod, fn) + end +end + +local function wrap_module(name, mod) + name = util.normalize_module_name(name) + if type(mod) ~= "table" or not should_instrument(name) then + return + end + wrapped_modules[name] = true + for k in pairs(mod) do + -- Do not wrap module functions that start with '_' + -- Those have to be explicitly passed to instrument() + if type(k) == "string" and not util.startswith(k, "_") then + patch_function(name, mod, k) + end + end +end + +local function should_profile_module(name) + for _, pattern in ipairs(instrument_list) do + if string.match(name, pattern) then + return true + end + end + return false +end + +M.hook_require = function(module_name) + if rawrequire ~= require then + return + end + local wrapped_require = wrap_function("require", rawrequire) + _G.require = function(name) + -- Don't time the require if the file is already loaded + if package.loaded[name] or not should_profile_module(name) then + return rawrequire(name) + end + local mod = wrapped_require(name) + wrap_module(name, mod) + return mod + end +end + +M.clear_events = function() + events = {} +end + +M.add_event = function(event) + if M.recording then + table.insert(events, event) + end +end + +M.get_module = function(name) + local ok, mod = pcall(require, name) + if ok then + if type(mod) == "table" then + return mod + else + return nil + end + else + mod = _G + local paths = vim.split(name, ".", { plain = true }) + for _, token in ipairs(paths) do + mod = mod[token] + if not mod then + break + end + end + return type(mod) == "table" and mod or nil + end +end + +M.get_events = function() + return events +end + +M.ignore = function(name) + table.insert(ignore_list, util.path_glob_to_regex(name)) +end + +M.print_modules = function() + for module, _ in pairs(wrapped_modules) do + print(module) + end +end + +local function instrument(_, name) + local pattern = util.path_glob_to_regex(name) + if not vim.tbl_contains(instrument_list, pattern) then + table.insert(instrument_list, pattern) + end + M.hook_require(name) + for modname, mod in pairs(all_modules()) do + if string.match(modname, pattern) then + wrap_module(modname, mod) + end + end + maybe_wrap_function(name) +end + +---@param sample_rate number Float between 0 and 1 +M.set_sample_rate = function(sample_rate) + if sample_rate <= 0 or sample_rate > 1 then + error("sample_rate must be between 0 (exclusive) and 1 (inclusive)") + end + M.sample_rate = sample_rate +end + +return setmetatable(M, { + __call = instrument, +}) diff --git a/lua/profile/util.lua b/lua/profile/util.lua new file mode 100644 index 0000000..b1648d3 --- /dev/null +++ b/lua/profile/util.lua @@ -0,0 +1,83 @@ +local M = {} + +local MAX_ARG_LEN = 200 +local tbl_isarray = vim.isarray or vim.tbl_isarray or vim.tbl_islist +local pack_len = vim.F.pack_len +local split = vim.split + +---@param glob string +---@return string +M.path_glob_to_regex = function(glob) + local pattern = string.gsub(glob, "%.", "[%./]") + pattern = string.gsub(pattern, "*", ".*") + return "^" .. pattern .. "$" +end + +---@param name string +---@return string +M.normalize_module_name = function(name) + local ret = string.gsub(name, "/", ".") + return ret +end + +---@param haystack string +---@param prefix string +---@return boolean +M.startswith = function(haystack, prefix) + return string.find(haystack, prefix) == 1 +end + +---@param path string +---@return string module +---@return string tail +M.split_path = function(path) + local pieces = split(path, ".", { plain = true }) + if #pieces == 1 then + return "_G", path + end + local mod = table.concat(pack_len(unpack(pieces, 1, #pieces - 1)), ".") + return mod, pieces[#pieces] +end + +local function sanitize(table) + local clean = {} + local iterfn + if tbl_isarray(table) then + iterfn = ipairs + else + iterfn = pairs + end + for i, v in iterfn(table) do + local t = type(v) + if t == "string" then + if string.len(v) > MAX_ARG_LEN then + clean[tostring(i)] = string.sub(v, 1, MAX_ARG_LEN - 3) .. "..." + else + clean[tostring(i)] = v + end + elseif t == "nil" or t == "boolean" or t == "number" then + clean[tostring(i)] = v + end + end + -- If no args, then return nil + if next(clean) == nil then + return nil + else + return clean + end +end + +---@param ... any[] +---@return any +M.format_args = function(...) + local args = pack_len(...) + if args.n == 0 then + return nil + elseif args.n == 1 and type(args[1]) == "table" then + return sanitize(args[1]) + else + return sanitize(args) + end +end + +return M diff --git a/plugin/on-attach.lua b/plugin/on-attach.lua index 3d52187..e4bdd11 100644 --- a/plugin/on-attach.lua +++ b/plugin/on-attach.lua @@ -25,6 +25,7 @@ local on_attach = function(args) { 'D', vim.lsp.buf.type_definition, "Go to type definition" }, { 'rn', vim.lsp.buf.rename, "Rename" }, { '', vim.lsp.buf.code_action, "Code action" }, + { '', vim.lsp.buf.code_action, "Code action" }, { '', vim.lsp.codelens.run, "Run code lens" }, { 'gr', function() require('telescope.builtin').lsp_references() end,"Go to references" }, { '', vim.diagnostic.open_float, "Open diagnostics" }, diff --git a/plugin/osc52.lua b/plugin/osc52.lua deleted file mode 100644 index fd032cc..0000000 --- a/plugin/osc52.lua +++ /dev/null @@ -1,11 +0,0 @@ -vim.g.clipboard = { - name = "OSC 52", - copy = { - ["+"] = require("vim.ui.clipboard.osc52").copy("+"), - ["*"] = require("vim.ui.clipboard.osc52").copy("*"), - }, - paste = { - ["+"] = require("vim.ui.clipboard.osc52").paste("+"), - ["*"] = require("vim.ui.clipboard.osc52").paste("*"), - }, -}