From 2b1d2ed5be432c50a103d5d55733226a0db925b2 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 26 Aug 2025 18:23:14 +0100 Subject: [PATCH] Update snippets --- init.lua | 2 +- lua/autocmd.lua | 8 +++ lua/keymap.lua | 29 +++++++++-- lua/plugins/blink.lua | 3 -- lua/plugins/copilot.lua | 20 +++++++- lua/plugins/lint.lua | 7 +-- lua/plugins/snippets.lua | 15 ++---- lua/snippets/all.json | 1 - lua/snippets/all.lua | 1 + lua/snippets/javascript-vue.json | 9 ---- lua/snippets/javascript.lua | 45 +++++++++++++++++ lua/snippets/package.json | 26 ---------- lua/snippets/php.json | 78 ----------------------------- lua/snippets/php.lua | 84 ++++++++++++++++++++++++++++++++ lua/visuals.lua | 2 + 15 files changed, 193 insertions(+), 137 deletions(-) delete mode 100644 lua/snippets/all.json create mode 100644 lua/snippets/all.lua delete mode 100644 lua/snippets/javascript-vue.json create mode 100644 lua/snippets/javascript.lua delete mode 100644 lua/snippets/package.json delete mode 100644 lua/snippets/php.json create mode 100644 lua/snippets/php.lua diff --git a/init.lua b/init.lua index cbd144c..168ee87 100644 --- a/init.lua +++ b/init.lua @@ -19,7 +19,7 @@ require('helpers').edit_cf('v', '/init.lua') TODO: Neovim configurations I want to add: - [x] Clearer line numbers (weirdly not as simple as it sounds) - [ ] Debugging keymap that works like PHPStorm -- [ ] Improve snippet expansion (an get rid of that stupid #endsection snippet) +- [ ] Improve snippet expansion (and get rid of that stupid #endsection snippet) - [ ] Better organising of buffers when opening side panels (like AI chat and test summary) - [ ] Close test panel when opening debugger and vice versa using edgy - [ ] Close terminal from inside terminal buffer diff --git a/lua/autocmd.lua b/lua/autocmd.lua index 8ad9f23..1133bb3 100644 --- a/lua/autocmd.lua +++ b/lua/autocmd.lua @@ -26,6 +26,14 @@ vim.api.nvim_create_autocmd('InsertLeave', { command = 'silent! wa', -- Save all files when leaving insert mode }) +vim.api.nvim_create_autocmd('SwapExists', { + desc = 'Always choose to delete the swap file, files are saved automatically', + group = vim.api.nvim_create_augroup('NoSwaps', { clear = true }), + callback = function(args) + vim.cmd("let v:swapchoice = 'd'") + end, +}) + -- vim.api.nvim_create_autocmd('TextChanged', { -- group = autosave_group, -- pattern = '*', diff --git a/lua/keymap.lua b/lua/keymap.lua index 49dacae..ec87663 100644 --- a/lua/keymap.lua +++ b/lua/keymap.lua @@ -1,5 +1,5 @@ --- [[ Basic Keymaps ]] --- See `:help vim.keymap.set()` +-- Basic Keymaps +-- See `:help vim.keymap.set()` -- Swap : and ; around vim.keymap.set({ 'n', 'v' }, ':', ';') @@ -12,6 +12,10 @@ vim.keymap.set('n', '', 'nohlsearch') -- Diagnostic keymaps vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagnostic [Q]uickfix list' }) +vim.keymap.set({ 'n', 'v' }, 'c', '"zc', { desc = 'Change without copying to clipboard' }) +vim.keymap.set({ 'n', 'v' }, 'x', '"zx', { desc = 'Cut without copying to clipboard' }) +vim.keymap.set('v', 'p', '"zdP', { desc = 'Paste over selection without yanking replaced text' }) + -- Exit terminal mode in the builtin terminal with a shortcut that is a bit easier -- for people to discover. Otherwise, you normally need to press , which -- is not what someone will guess without a bit more experience. @@ -235,6 +239,22 @@ vim.keymap.set('n', 'tc', function() require('neotest').output_panel.close() end, { desc = 'Close test panels' }) +-- vim.keymap.set('i', '', function() +-- local copilot = require 'copilot.suggestion' +-- local ls = require 'luasnip' +-- if copilot.is_visible() then +-- vim.api.nvim_echo({{'Accepting copilot suggestion'}}, false, {}) +-- copilot.accept() +-- copilot.dismiss() +-- elseif ls.jumpable(1) then +-- vim.api.nvim_echo({{'Jumping in snippet'}}, false, {}) +-- ls.jump() +-- else +-- vim.api.nvim_echo({{'Inserting tab'}}, false, {}) +-- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, true, true), 'n', true) +-- end +-- end, { desc = 'Luasnip accept copilot or jump forward' }) + -- Leaving this commented out, I will try the format command instead -- "A command to properly indent json code -- command! FormatJSON %!python -m json.tool @@ -243,8 +263,11 @@ end, { desc = 'Close test panels' }) -- directory if the file does not exist vim.keymap.set('n', 'es', function() local ft = vim.bo.filetype + if ft == 'vue' then + ft = 'javascript' + end local snippets_dir = vim.fn.stdpath 'config' .. '/lua/snippets' - local snippets_file = snippets_dir .. '/' .. ft .. '.json' + local snippets_file = snippets_dir .. '/' .. ft .. '.lua' vim.cmd('tabedit ' .. snippets_file) end, { desc = 'Edit snippets file' }) diff --git a/lua/plugins/blink.lua b/lua/plugins/blink.lua index 7ffe48f..0ae36fa 100644 --- a/lua/plugins/blink.lua +++ b/lua/plugins/blink.lua @@ -4,9 +4,6 @@ return { event = 'VimEnter', version = '1.*', dependencies = { - -- Compatibility for Avante with Blink (it typically only works with cmp) - -- 'Kaiser-Yang/blink-cmp-avante', - -- Snippet Engine 'L3MON4D3/LuaSnip', 'folke/lazydev.nvim', }, diff --git a/lua/plugins/copilot.lua b/lua/plugins/copilot.lua index 8e490ed..cd65cef 100644 --- a/lua/plugins/copilot.lua +++ b/lua/plugins/copilot.lua @@ -1,3 +1,21 @@ return { - 'github/copilot.vim', + 'zbirenbaum/copilot.lua', + event = 'InsertEnter', + config = function() + require('copilot').setup { + suggestion = { + enabled = true, + auto_trigger = true, + keymap = { + accept = '', + }, + }, + filetypes = { + yaml = true, + markdown = true, + gitcommit = true, + gitrebase = true, + }, + } + end, } diff --git a/lua/plugins/lint.lua b/lua/plugins/lint.lua index 153466c..fcb121f 100644 --- a/lua/plugins/lint.lua +++ b/lua/plugins/lint.lua @@ -4,9 +4,10 @@ return { event = { 'BufReadPre', 'BufNewFile' }, config = function() local lint = require 'lint' - lint.linters_by_ft = { - markdown = { 'markdownlint' }, - } + -- lint.linters_by_ft = { + -- markdown = { 'markdownlint' }, + -- } + lint.linters_by_ft['markdown'] = nil lint.linters_by_ft['javascript'] = nil lint.linters_by_ft['vue'] = nil lint.linters_by_ft['html'] = nil diff --git a/lua/plugins/snippets.lua b/lua/plugins/snippets.lua index 30f5a6c..5db80d0 100644 --- a/lua/plugins/snippets.lua +++ b/lua/plugins/snippets.lua @@ -11,21 +11,12 @@ return { return 'make install_jsregexp' end)(), config = function() + local ls = require 'luasnip' + ls.filetype_extend('vue', { 'javascript' }) local snippets_dir = vim.fn.stdpath 'config' .. '/lua/snippets' - require('luasnip.loaders.from_vscode').lazy_load { + require('luasnip.loaders.from_lua').load { paths = { snippets_dir }, } end, - dependencies = { - -- `friendly-snippets` contains a variety of premade snippets. - -- See the README about individual language/framework/plugin snippets: - -- https://github.com/rafamadriz/friendly-snippets - { - 'rafamadriz/friendly-snippets', - config = function() - require('luasnip.loaders.from_vscode').lazy_load() - end, - }, - }, opts = {}, } diff --git a/lua/snippets/all.json b/lua/snippets/all.json deleted file mode 100644 index 0967ef4..0000000 --- a/lua/snippets/all.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/lua/snippets/all.lua b/lua/snippets/all.lua new file mode 100644 index 0000000..a564707 --- /dev/null +++ b/lua/snippets/all.lua @@ -0,0 +1 @@ +return {} diff --git a/lua/snippets/javascript-vue.json b/lua/snippets/javascript-vue.json deleted file mode 100644 index e331a1f..0000000 --- a/lua/snippets/javascript-vue.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Log function": { - "prefix": "du", - "body": [ - "console.log($0);" - ], - "description": "Log variable" - } -} diff --git a/lua/snippets/javascript.lua b/lua/snippets/javascript.lua new file mode 100644 index 0000000..375224f --- /dev/null +++ b/lua/snippets/javascript.lua @@ -0,0 +1,45 @@ +local ls = require 'luasnip' +local s = ls.snippet +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node + +return { + s('du', { t 'console.log(', i(0), t ');' }), + + s('vue', { + t { '', '', '', '', '', '', '' }, + }), + + s('fun', { + t 'function ', + i(1), + t '(', + i(2), + t ') {', + t { '', ' ' }, + i(0), + t { '', '}' }, + }), + + s('afun', { + t 'async function ', + i(1), + t '(', + i(2), + t ') {', + t { '', ' ' }, + i(0), + t { '', '}' }, + }), + + s('()', { + t '() => {', + t { '', ' ' }, + i(0), + t { '', '}' }, + }), +} diff --git a/lua/snippets/package.json b/lua/snippets/package.json deleted file mode 100644 index d9d5fa8..0000000 --- a/lua/snippets/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "example-snippets", - "contributes": { - "snippets": [ - { - "language": [ - "all" - ], - "path": "./snippets/all.json" - }, - { - "language": [ - "php" - ], - "path": "./php.json" - }, - { - "language": [ - "javascript", - "vue" - ], - "path": "./javascript-vue.json" - } - ] - } -} diff --git a/lua/snippets/php.json b/lua/snippets/php.json deleted file mode 100644 index 03231a4..0000000 --- a/lua/snippets/php.json +++ /dev/null @@ -1,78 +0,0 @@ -{ - "Dump function": { - "prefix": "du", - "body": [ - "dump($0);" - ], - "description": "Dump variable" - }, - "Type dump function": { - "prefix": "dt", - "body": [ - "\\PhpStan\\dumpType($0);" - ], - "description": "Dump PHPStan type" - }, - "Test case": { - "prefix": "test", - "body": [ - "test($1, function () {", - " $0", - "});" - ], - "description": "Test case" - }, - "Create doc block": { - "prefix": "/**", - "body": [ - "/**", - " * $0", - " */" - ] - }, - "DocBlock property": { - "prefix": "@p", - "body": "@property $1 $$0", - "description": "DocBlock property" - }, - "DocBlock boolean property": { - "prefix": "@pb", - "body": "@property bool $$0", - "description": "DocBlock boolean property" - }, - "DocBlock string property": { - "prefix": "@ps", - "body": "@property string $$0", - "description": "DocBlock string property" - }, - "DocBlock int property": { - "prefix": "@pi", - "body": "@property int $$0", - "description": "DocBlock int property" - }, - "DocBlock Collection property": { - "prefix": "@pc", - "body": "@property \\Illuminate\\Support\\Collection $$0", - "description": "DocBlock int property" - }, - "DocBlock date property": { - "prefix": "@pd", - "body": "@property \\Illuminate\\Support\\Carbon $$0", - "description": "DocBlock date property" - }, - "DocBlock model properties": { - "prefix": "@pp", - "body": [ - "/**", - " * @property int $$id", - " * $1", - " * @property \\Illuminate\\Support\\Carbon $$created_at", - " * @property \\Illuminate\\Support\\Carbon $$updated_at", - " *", - " * Relationships", - " * $2", - " */" - ], - "description": "DocBlock model properties" - } -} diff --git a/lua/snippets/php.lua b/lua/snippets/php.lua new file mode 100644 index 0000000..bf871b3 --- /dev/null +++ b/lua/snippets/php.lua @@ -0,0 +1,84 @@ +local ls = require("luasnip") +local s = ls.snippet +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node + +local function psr_namespace(args, snip) + local path = snip.env.TM_FILENAME_FULL or "" + local root = "/src/" + local ns = path:match(root .. "(.*)/[^/]+%.php$") + if ns then + return ns:gsub("/", "\\") + else + return "App" + end +end + +local function class_name(args, snip) + local filename = snip.env.TM_FILENAME or "" + return filename:match("([^%.]+)") or "ClassName" +end + +return { + s("du", { t("dump("), i(1), t(");") }), + s("dt", { t("\\PhpStan\\dumpType("), i(1), t(");") }), + s("ql", { + t("\\Illuminate\\Support\\Facades\\DB::listen(function (\\Illuminate\\Database\\Events\\QueryExecuted $e) {"), + t({"", " dump($e->sql, $e->bindings);"}), + t({"", "});"}) + }), + s("test", { + t("test("), i(1), t(", function () {"), + t({"", " "}), i(2), + t({"", ");"}) + }), + s("/**", { + t("/**"), + t({"", " * "}), i(1), + t({"", " */"}) + }), + s("@p", { t("@property "), i(1), t(" $"), i(2) }), + s("@pb", { t("@property bool $"), i(1) }), + s("@ps", { t("@property string $"), i(1) }), + s("@pi", { t("@property int $"), i(1) }), + s("@pc", { t("@property \\Illuminate\\Support\\Collection $"), i(2) }), + s("@pd", { t("@property \\Illuminate\\Support\\Carbon $"), i(1) }), + s("@pp", { + t("/**"), + t({"", " * @property int $id"}), + t({"", " * "}), i(1), + t({"", " * @property \\Illuminate\\Support\\Carbon $created_at"}), + t({"", " * @property \\Illuminate\\Support\\Carbon $updated_at"}), + t({"", " *", " * Relationships"}), + t({"", " * "}), i(2), + t({"", " */"}) + }), + s("php", { + t("