From fbcdd3af0c673d067ca5dde69d30fb8f45d164d6 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 3 Jul 2026 13:48:02 +0100 Subject: [PATCH] Formatting --- init.lua | 1 + lua/autocmd.lua | 8 +++++ lua/helpers.lua | 49 +++++++++++++++++++++++++++++++ lua/keymap.lua | 9 +++++- lua/plugins/conform.lua | 45 ---------------------------- lua/plugins/lsp.lua | 2 ++ lua/projects.lua | 65 +++++++++++++++++++++++++++++------------ 7 files changed, 114 insertions(+), 65 deletions(-) delete mode 100644 lua/plugins/conform.lua diff --git a/init.lua b/init.lua index 2daeadc..e5f265e 100644 --- a/init.lua +++ b/init.lua @@ -1,6 +1,7 @@ -- Set as the leader key -- See `:help mapleader` -- NOTE: Must happen before plugins are loaded (otherwise wrong leader will be used) +vim.lsp.log.set_level 'debug' vim.g.mapleader = ',' vim.g.maplocalleader = ',' diff --git a/lua/autocmd.lua b/lua/autocmd.lua index a903e26..92bdbe0 100644 --- a/lua/autocmd.lua +++ b/lua/autocmd.lua @@ -94,6 +94,13 @@ vim.api.nvim_create_autocmd({ 'User' }, { -- end, -- }) +vim.api.nvim_create_autocmd({ 'BufWritePre' }, { + pattern = { '*.lua' }, + callback = function(args) + require('helpers').format_buffer('stylua', args.buf) + end, +}) + local modes_group = vim.api.nvim_create_augroup('modes', { clear = true }) vim.api.nvim_create_autocmd('FocusLost', { group = modes_group, @@ -211,3 +218,4 @@ vim.api.nvim_create_autocmd('VimLeavePre', { }) require('helpers').edit_cf('a', '/lua/autocmd.lua') + diff --git a/lua/helpers.lua b/lua/helpers.lua index 3299aa4..1fa5ba7 100644 --- a/lua/helpers.lua +++ b/lua/helpers.lua @@ -104,5 +104,54 @@ helpers.open_file_modal = function(path, title) }) end +local function get_formatter_bin(formatter) + return vim.fn.stdpath 'data' .. '/mason/bin/' .. formatter +end + +local formatter_args = { + pint = function(file) + return { + get_formatter_bin 'pint', + '--stdin-filename', + file, + } + end, + eslint_d = function(file) + return { + get_formatter_bin 'eslint_d', + '--stdin', + '--stdin-filename', + file, + '--fix-to-stdout', + } + end, + stylua = function(file) + return { + get_formatter_bin 'stylua', + '--stdin-filepath', + file, + '-', + } + end, +} + +helpers.format_buffer = function(formatter, buf, cwd) + local file = vim.api.nvim_buf_get_name(buf) + local input = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), '\n') + local result = vim.system(formatter_args[formatter](file), { + text = true, + stdin = input, + cwd = cwd, + }):wait() + + if result.code ~= 0 then + vim.notify(result.stderr, vim.log.levels.ERROR) + return + end + + vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(result.stdout, '\n', { plain = true })) +end + return helpers -- nnoremap ev :tabedit $MYVIMRC + diff --git a/lua/keymap.lua b/lua/keymap.lua index 56c5e1b..7e1c73c 100644 --- a/lua/keymap.lua +++ b/lua/keymap.lua @@ -257,7 +257,14 @@ vim.keymap.set('n', 'sGS', 'Telescope git_stash', { desc = 'Sea vim.keymap.set('n', ']', 'cnext', { desc = 'Next item in quickfix list' }) vim.keymap.set('n', '[', 'cprevious', { desc = 'Previous item in quickfix list' }) vim.keymap.set('n', 'gd', 'Telescope lsp_definitions', { desc = 'Go to definition' }) -vim.keymap.set('n', 'r', 'e!', { desc = 'Reload buffer' }) +vim.keymap.set('n', 'r', function() + for _, c in ipairs(vim.lsp.get_clients { bufnr = 0 }) do + if c.name == 'phpantom_lsp' then + c:stop(true) + end + end + vim.cmd 'edit' +end, { desc = 'Reload buffer' }) local function open_test() require('neotest').summary.open() diff --git a/lua/plugins/conform.lua b/lua/plugins/conform.lua deleted file mode 100644 index 9c03047..0000000 --- a/lua/plugins/conform.lua +++ /dev/null @@ -1,45 +0,0 @@ -return { -- Autoformat - 'stevearc/conform.nvim', - event = { 'BufWritePre' }, - cmd = { 'ConformInfo' }, - keys = { - { - 'f', - function() - require('conform').format { async = true, lsp_format = 'fallback' } - end, - mode = '', - desc = '[F]ormat buffer', - }, - }, - config = function() - require('conform').setup({ - notify_on_error = false, - format_on_save = function(bufnr) - -- Disable "format_on_save lsp_fallback" for languages that don't - -- have a well standardized coding style. You can add additional - -- languages here or re-enable it for the disabled ones. - local disable_filetypes = { c = true, cpp = true, vue = true, js = true } - if disable_filetypes[vim.bo[bufnr].filetype] then - return nil - else - return { - timeout_ms = 500, - lsp_format = 'fallback', - } - end - end, - formatters_by_ft = { - lua = { 'stylua' }, - php = { 'pint' }, - blade = { 'blade-formatter' }, - -- Conform can also run multiple formatters sequentially - -- python = { "isort", "black" }, - -- - -- You can use 'stop_after_first' to run the first available formatter from the list - -- javascript = { "prettierd", "prettier", stop_after_first = true }, - }, - log_level = vim.log.levels.DEBUG - }) - end -} diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 6b64c6f..2601954 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -207,6 +207,8 @@ return { vim.list_extend(ensure_installed, { 'stylua', -- Used to format Lua code 'blade-formatter', + 'eslint_d', + 'pint', }) -- LSP servers and clients are able to communicate to each other what features they support. diff --git a/lua/projects.lua b/lua/projects.lua index 053bffb..efd090a 100644 --- a/lua/projects.lua +++ b/lua/projects.lua @@ -226,6 +226,34 @@ local PROJECTS = { }, } + vim.api.nvim_create_autocmd({ 'BufWritePre' }, { + pattern = { '*.php' }, + callback = function(args) + local cwd = vim.fn.getcwd() + if vim.fn.expand('%:p'):match(dir .. '/server/') then + cwd = dir .. '/server/' + end + local file = vim.api.nvim_buf_get_name(args.buf) + local input = table.concat(vim.api.nvim_buf_get_lines(args.buf, 0, -1, false), '\n') + local result = vim.system({ + vim.fn.stdpath 'data' .. '/mason/bin/pint', + '--stdin-filename', + file, + }, { + text = true, + stdin = input, + cwd = dir .. '/server/', + }):wait() + + if result.code ~= 0 then + vim.notify(result.stderr, vim.log.levels.ERROR) + return + end + + vim.api.nvim_buf_set_lines(args.buf, 0, -1, false, vim.split(result.stdout, '\n', { plain = true })) + end, + }) + create_bookmark('E', dir .. '/client/src/types/api/endpointMap.d.ts') create_bookmark('q', dir .. '/client/quasar.config.ts') create_bookmark('db', dir .. '/client/src/boot') @@ -251,28 +279,26 @@ local PROJECTS = { end, }) - vim.api.nvim_create_autocmd({ 'BufWritePost' }, { - pattern = { '.graphql' }, - callback = function() - vim.fn.jobstart('vendor/bin/sail artisan lighthouse:clear-cache', { stdout_buffered = true }) + vim.api.nvim_create_autocmd({ 'BufWritePre' }, { + pattern = { '*.js', '*.ts', '*.vue' }, + callback = function(args) + vim.env.ESLINT_D_PPID = vim.fn.getpid() + local cwd = vim.fn.getcwd() + if vim.fn.expand('%:p'):match(dir .. '/frontend2/') then + cwd = dir .. '/frontend2/' + end + require('helpers').format_buffer('eslint_d', args.buf, cwd) end, }) - end, - ['server'] = function(dir) - map('pl', ':cexpr system("vendor/bin/phpstan analyse --no-progress --error-format=raw --memory-limit=2G -vv") ', { desc = 'Run lint' }) - map('pd', function() - helpers.open_term { cmd = 'lazysql pgsql://homestead:password@localhost:5432/homestead' } - end, { desc = 'Open database manager' }) - laravel_keymaps() - laravel_makes() - map('yrn ', '!cd frontend && yarn ', { desc = 'Run yarn script' }, 'c') - map('pm', ':vendor/bin/sail composer migrate') - vim.api.nvim_create_autocmd({ 'BufWritePost' }, { - pattern = { '*.php', '.env', '.graphql' }, - callback = function() - vim.fn.jobstart('vendor/bin/sail artisan octane:reload', { stdout_buffered = true }) - vim.fn.jobstart('vendor/bin/sail artisan horizon:terminate', { stdout_buffered = true }) + vim.api.nvim_create_autocmd({ 'BufWritePre' }, { + pattern = { '*.php' }, + callback = function(args) + local cwd = vim.fn.getcwd() + if vim.fn.expand('%:p'):match(dir .. '/server/') then + cwd = dir .. '/server/' + end + require('helpers').format_buffer('pint', args.buf, cwd) end, }) @@ -334,3 +360,4 @@ end, {}) -- Keep your helper line require('helpers').edit_cf('w', '/lua/projects.lua') +