changeset 718:451632dcb1dd

feat: add lsp-format-modifications
author zegervdv <zegervdv@me.com>
date Sat, 03 Dec 2022 22:22:10 +0100
parents 164683291d2f
children 08baa6419e7d
files .chezmoitemplates/init.lua
diffstat 1 files changed, 18 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/.chezmoitemplates/init.lua	Sat Dec 03 11:28:59 2022 +0100
+++ b/.chezmoitemplates/init.lua	Sat Dec 03 22:22:10 2022 +0100
@@ -443,6 +443,10 @@
       end,
     }
     use {
+      local_plugin 'lsp-format-modifications.nvim',
+      requires = { 'nvim-lua/plenary.nvim' },
+    }
+    use {
       'j-hui/fidget.nvim',
       config = function()
         require('fidget').setup {
@@ -1114,7 +1118,7 @@
   }
 end
 
-local on_attach = function(client)
+local on_attach = function(client, bufnr)
   local nmap = function(lhs, rhs, opts) return vim.keymap.set('n', lhs, rhs, opts) end
 
   nmap('gp', require('goto-preview').goto_preview_definition, { silent = true, buffer = 0 })
@@ -1128,10 +1132,21 @@
   nmap('g0', vim.lsp.buf.document_symbol, { silent = true, buffer = 0 })
   nmap('ga', vim.lsp.buf.code_action, { silent = true, buffer = 0 })
 
-  if client.supports_method 'textDocument/formatting' then nmap('<c-p>', function() lsp_formatting(0) end) end
-
   vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, { border = 'single' })
   vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(vim.lsp.handlers.hover, { border = 'single' })
+
+  if client.supports_method 'textDocument/rangeFormatting' then
+    local root = vim.fs.find({ '.git', '.hg' }, { path = client.config.root_dir })
+    local vcs = 'git'
+    if root then vcs = vim.fs.basename(root[1]):sub(2) end
+
+    vim.notify('Enabled modification formatting via ' .. vcs .. ' using ' .. client.name, vim.log.levels.INFO)
+    local lsp_format_modifications = require 'lsp-format-modifications'
+    lsp_format_modifications.attach(client, bufnr, { format_on_save = false, vcs = 'hg' })
+    nmap('<c-p>', function() lsp_format_modifications.format_modifications_current_buffer() end)
+  elseif client.supports_method 'textDocument/formatting' then
+    nmap('<c-p>', function() lsp_formatting(0) end)
+  end
 end
 
 local capabilities = require('cmp_nvim_lsp').default_capabilities()