GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — nvim dev api nvim dev api
ES edited this page
插件作者 95% 时间在 Lua 层。下面全部挂在
最常用、最底层。所有
浮窗配置关键字段: 虚拟文本、内联诊断、sign、高亮都走它。 local ns = vim.api.nvim_create_namespace('myplug') vim.api.nvim_buf_set_extmark(buf, ns, row, col, { virt_text = { {'TODO', 'WarningMsg'} }, -- 行尾虚拟文本 virt_lines = { {{'-- above line', 'Comment'}} }, -- 虚拟行 sign_text = 'E', sign_hl_group = 'Error', -- sign 列图标 hl_group = 'Search', -- 范围高亮 end_row = row, end_col = col + 10, }) vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1) -- 直接清除 -- autocmd local aug = vim.api.nvim_create_augroup('MyPlug', { clear = true }) vim.api.nvim_create_autocmd('BufWritePost', { group = aug, pattern = '*.ts', callback = function(args) print('saved', args.file) end, }) -- 用户命令 vim.api.nvim_create_user_command('MyCmd', function(o) print(o.args, o.bang) end, { nargs = '?', bang = true, desc = 'my command' }) -- 高亮 vim.api.nvim_set_hl(0, 'MyHl', { fg = '#ff6ac1', bold = true })
vim.fn.expand('%:p') -- Vimscript 函数 vim.fn.glob('*.lua', nil, true) vim.cmd('edit foo.lua') -- Ex 命令字符串 vim.cmd.edit('foo.lua') -- 新式调用语法 vim.cmd.colorscheme('tokyonight') -- 变量 vim.g.mapleader = ' ' -- 全局 vim.b.my_state = 1 -- 当前 buffer vim.b[bufnr].my_state = 1 -- 指定 buffer vim.w[win].my_state = 1 -- window vim.env.FOO = 'bar' -- 进程环境变量 -- 选项(三套 API,按需选) vim.o.number = true -- 普通值(get/set) vim.bo.filetype = 'lua' -- buf 局部 vim.wo[win].number = false -- win 局部 vim.opt.list:append({ tab = '»·' }) -- list/set 语义 vim.opt_local.wrap = false -- setlocal vim.keymap.set('n', '<leader>w', '<cmd>write<cr>', { desc = 'save', silent = true }) vim.keymap.set({'n', 'v'}, 'gf', function() vim.lsp.buf.format() end) vim.keymap.set('n', '<leader>x', '<cmd>MyCmd<cr>', { buffer = buf }) -- buffer-local vim.keymap.del('n', '<leader>w') -- 全局注册 server 配置 vim.lsp.config('lua_ls', { cmd = { 'lua-language-server' }, root_markers = { '.luarc.json', '.git' }, settings = { Lua = { diagnostics = { globals = { 'vim' } } } }, }) -- 按需启用 vim.lsp.enable({ 'lua_ls', 'tsserver' })
子模块: local parser = vim.treesitter.get_parser(bufnr, 'typescript') local tree = parser:parse()[1] local root = tree:root() -- 查询 local query = vim.treesitter.query.parse('typescript', [[ (function_declaration name: (identifier) @func) ]]) for id, node in query:iter_captures(root, bufnr, 0, -1) do local name = query.captures[id] local text = vim.treesitter.get_node_text(node, bufnr) end -- 光标下节点 local node = vim.treesitter.get_node() 子模块: -- 发布诊断(给自己插件用) local ns = vim.api.nvim_create_namespace('myplug.diag') vim.diagnostic.set(ns, bufnr, { { lnum = 0, col = 0, severity = vim.diagnostic.severity.WARN, message = 'oops' }, }) -- 读取 / 跳转 vim.diagnostic.get(bufnr, { severity = vim.diagnostic.severity.ERROR }) vim.diagnostic.jump({ count = 1 }) -- next vim.diagnostic.open_float() -- 全局配置 vim.diagnostic.config({ virtual_text = false, virtual_lines = true, -- 0.11 新:多行展示 severity_sort = true, signs = { text = { [vim.diagnostic.severity.ERROR] = 'E' } }, }) -- vim.uv:libuv 全绑定 local timer = vim.uv.new_timer() timer:start(100, 0, vim.schedule_wrap(function() vim.notify('tick') timer:close() end)) -- vim.system:子进程推荐 API vim.system({ 'git', 'status', '--porcelain' }, { text = true }, function(obj) vim.schedule(function() print(obj.code, obj.stdout) end) end) -- 同步版 local result = vim.system({'ls', '-la'}, { text = true }):wait() -- 主循环调度 vim.schedule(function() ... end) -- 下一个安全点执行 vim.schedule_wrap(cb) -- 包装一个 cb vim.defer_fn(fn, 100) -- 100ms 后 vim.wait(100, function() return done end, 10) -- 忙等(尽量别用) 关键:libuv 回调里不能直接调 -- 用户级调用 vim.ui.input({ prompt = 'Name: ', default = 'foo' }, function(val) if val then print('got', val) end end) vim.ui.select({ 'a', 'b', 'c' }, { prompt = 'Pick:' }, function(choice) print(choice) end) vim.ui.open('https://google.com') -- 走系统默认打开器 -- 插件级覆盖(dressing / telescope 都这么干) vim.ui.select = function(items, opts, on_choice) ... end
-- iter:链式迭代器 vim.iter({1,2,3,4}) :filter(function(x) return x > 1 end) :map(function(x) return x * 2 end) :totable() -- { 4, 6, 8 } -- fs vim.fs.find('.git', { upward = true, path = vim.fn.expand('%:p') }) vim.fs.root(0, { 'package.json', '.git' }) -- 0.10+ 根目录查找 vim.fs.dirname('/a/b/c.lua') vim.fs.normalize('~/x/../y') -- filetype vim.filetype.add({ extension = { tsx = 'typescriptreact' }, pattern = { ['.*%.env%..*'] = 'sh' }, }) -- validate vim.validate('name', name, 'string') vim.validate('count', count, 'number', true) -- 可选 -- inspect / print vim.print(obj) -- = print(vim.inspect(obj)) vim.inspect({a=1, b={2,3}}) -- tbl_* vim.tbl_deep_extend('force', defaults, opts) vim.tbl_keys(t) / vim.tbl_values(t) / vim.tbl_map(fn, t) / vim.tbl_filter(fn, t) vim.islist(t) -- version local v = vim.version.parse('1.2.3') vim.version.lt(v1, v2) vim.snippet.expand('console.log($1);$0') vim.snippet.jump(1) -- 下一个 tabstop vim.snippet.stop() vim.snippet.active({ direction = 1 }) vim.pack.add({ 'https://github.com/folke/tokyonight.nvim', { src = 'https://github.com/nvim-telescope/telescope.nvim', version = '0.1.8' }, }) vim.pack.update() -- 升级 vim.pack.del({ 'tokyonight.nvim' }) Lockfile:
核心规则:
-- lua/myplug/init.lua local M = {} ---@class MyPlugConfig ---@field border string @default 'rounded' ---@field debug boolean @default false local defaults = { border = 'rounded', debug = false } ---@param opts MyPlugConfig? function M.setup(opts) M.config = vim.tbl_deep_extend('force', defaults, opts or {}) vim.validate('border', M.config.border, 'string') vim.api.nvim_create_user_command('MyPlug', function() M.hello() end, {}) end function M.hello() vim.notify('hi from myplug', vim.log.levels.INFO) end return M -- lua/myplug/health.lua local M = {} function M.check() vim.health.start('myplug') if vim.fn.executable('rg') == 1 then vim.health.ok('rg found') else vim.health.error('ripgrep not installed') end end return M 调用 常用调试:
Clone this wiki locally | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |