mirror of
				https://github.com/NotAShelf/nvf.git
				synced 2025-11-03 20:22:21 +00:00 
			
		
		
		
	lsp: prefer conform for format on save
This commit is contained in:
		
					parent
					
						
							
								22f5704527
							
						
					
				
			
			
				commit
				
					
						4f0cc5725a
					
				
			
		
					 1 changed files with 54 additions and 66 deletions
				
			
		| 
						 | 
					@ -6,6 +6,7 @@
 | 
				
			||||||
}: let
 | 
					}: let
 | 
				
			||||||
  inherit (lib.generators) mkLuaInline;
 | 
					  inherit (lib.generators) mkLuaInline;
 | 
				
			||||||
  inherit (lib.modules) mkIf;
 | 
					  inherit (lib.modules) mkIf;
 | 
				
			||||||
 | 
					  inherit (lib.lists) optional;
 | 
				
			||||||
  inherit (lib.strings) optionalString;
 | 
					  inherit (lib.strings) optionalString;
 | 
				
			||||||
  inherit (lib.trivial) boolToString;
 | 
					  inherit (lib.trivial) boolToString;
 | 
				
			||||||
  inherit (lib.nvim.binds) addDescriptionsToMappings;
 | 
					  inherit (lib.nvim.binds) addDescriptionsToMappings;
 | 
				
			||||||
| 
						 | 
					@ -14,7 +15,10 @@
 | 
				
			||||||
  usingNvimCmp = config.vim.autocomplete.nvim-cmp.enable;
 | 
					  usingNvimCmp = config.vim.autocomplete.nvim-cmp.enable;
 | 
				
			||||||
  usingBlinkCmp = config.vim.autocomplete.blink-cmp.enable;
 | 
					  usingBlinkCmp = config.vim.autocomplete.blink-cmp.enable;
 | 
				
			||||||
  self = import ./module.nix {inherit config lib pkgs;};
 | 
					  self = import ./module.nix {inherit config lib pkgs;};
 | 
				
			||||||
 | 
					  conformCfg = config.vim.formatter.conform-nvim;
 | 
				
			||||||
 | 
					  conformFormatOnSave = conformCfg.enable && conformCfg.setupOpts.format_on_save != null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  augroup = "nvf_lsp";
 | 
				
			||||||
  mappingDefinitions = self.options.vim.lsp.mappings;
 | 
					  mappingDefinitions = self.options.vim.lsp.mappings;
 | 
				
			||||||
  mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
 | 
					  mappings = addDescriptionsToMappings cfg.mappings mappingDefinitions;
 | 
				
			||||||
  mkBinding = binding: action:
 | 
					  mkBinding = binding: action:
 | 
				
			||||||
| 
						 | 
					@ -29,10 +33,12 @@ in {
 | 
				
			||||||
        sourcePlugins = ["cmp-nvim-lsp"];
 | 
					        sourcePlugins = ["cmp-nvim-lsp"];
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      augroups = [{name = augroup;}];
 | 
				
			||||||
      autocmds =
 | 
					      autocmds =
 | 
				
			||||||
        if cfg.inlayHints.enable
 | 
					        (optional cfg.inlayHints.enable {
 | 
				
			||||||
        then [
 | 
					          group = augroup;
 | 
				
			||||||
          {
 | 
					          event = ["LspAttach"];
 | 
				
			||||||
 | 
					          desc = "LSP on-attach enable inlay hints autocmd";
 | 
				
			||||||
          callback = mkLuaInline ''
 | 
					          callback = mkLuaInline ''
 | 
				
			||||||
            function(event)
 | 
					            function(event)
 | 
				
			||||||
              local bufnr = event.buf
 | 
					              local bufnr = event.buf
 | 
				
			||||||
| 
						 | 
					@ -42,11 +48,44 @@ in {
 | 
				
			||||||
              end
 | 
					              end
 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
          '';
 | 
					          '';
 | 
				
			||||||
            desc = "LSP on-attach enable inlay hints autocmd";
 | 
					        })
 | 
				
			||||||
            event = ["LspAttach"];
 | 
					        ++ (optional (!conformFormatOnSave) {
 | 
				
			||||||
          }
 | 
					          group = augroup;
 | 
				
			||||||
        ]
 | 
					          event = ["BufWritePre"];
 | 
				
			||||||
        else [];
 | 
					          desc = "LSP on-attach create format on save autocmd";
 | 
				
			||||||
 | 
					          callback = mkLuaInline ''
 | 
				
			||||||
 | 
					            function(ev)
 | 
				
			||||||
 | 
					              if vim.b.disableFormatSave or not vim.g.formatsave then
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					              end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              local bufnr = ev.buf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              ${optionalString cfg.null-ls.enable ''
 | 
				
			||||||
 | 
					              -- prefer null_ls formatter
 | 
				
			||||||
 | 
					              do
 | 
				
			||||||
 | 
					                local clients = vim.lsp.get_clients({
 | 
				
			||||||
 | 
					                  bufnr = bufnr,
 | 
				
			||||||
 | 
					                  name = "null-ls",
 | 
				
			||||||
 | 
					                  method = "textDocument/formatting",
 | 
				
			||||||
 | 
					                })
 | 
				
			||||||
 | 
					                if clients[1] then
 | 
				
			||||||
 | 
					                  vim.lsp.buf.format({ bufnr = bufnr, id = clients[1].id })
 | 
				
			||||||
 | 
					                  return
 | 
				
			||||||
 | 
					                end
 | 
				
			||||||
 | 
					              end
 | 
				
			||||||
 | 
					            ''}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              local clients = vim.lsp.get_clients({
 | 
				
			||||||
 | 
					                bufnr = bufnr,
 | 
				
			||||||
 | 
					                method = "textDocument/formatting",
 | 
				
			||||||
 | 
					              })
 | 
				
			||||||
 | 
					              if clients[1] then
 | 
				
			||||||
 | 
					                vim.lsp.buf.format({ bufnr = bufnr, id = clients[1].id })
 | 
				
			||||||
 | 
					              end
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					          '';
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      pluginRC.lsp-setup = ''
 | 
					      pluginRC.lsp-setup = ''
 | 
				
			||||||
        vim.g.formatsave = ${boolToString cfg.formatOnSave};
 | 
					        vim.g.formatsave = ${boolToString cfg.formatOnSave};
 | 
				
			||||||
| 
						 | 
					@ -74,60 +113,9 @@ in {
 | 
				
			||||||
          ${mkBinding mappings.toggleFormatOnSave "function() vim.b.disableFormatSave = not vim.b.disableFormatSave end"}
 | 
					          ${mkBinding mappings.toggleFormatOnSave "function() vim.b.disableFormatSave = not vim.b.disableFormatSave end"}
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        -- Enable formatting
 | 
					 | 
				
			||||||
        local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        format_callback = function(client, bufnr)
 | 
					 | 
				
			||||||
          if vim.g.formatsave then
 | 
					 | 
				
			||||||
            vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
 | 
					 | 
				
			||||||
            vim.api.nvim_create_autocmd("BufWritePre", {
 | 
					 | 
				
			||||||
              group = augroup,
 | 
					 | 
				
			||||||
              buffer = bufnr,
 | 
					 | 
				
			||||||
              callback = function()
 | 
					 | 
				
			||||||
                if vim.b.disableFormatSave then
 | 
					 | 
				
			||||||
                  return
 | 
					 | 
				
			||||||
                end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                ${
 | 
					 | 
				
			||||||
          if config.vim.lsp.null-ls.enable
 | 
					 | 
				
			||||||
          then ''
 | 
					 | 
				
			||||||
            local function is_null_ls_formatting_enabled(bufnr)
 | 
					 | 
				
			||||||
                local file_type = vim.api.nvim_buf_get_option(bufnr, "filetype")
 | 
					 | 
				
			||||||
                local generators = require("null-ls.generators").get_available(
 | 
					 | 
				
			||||||
                    file_type,
 | 
					 | 
				
			||||||
                    require("null-ls.methods").internal.FORMATTING
 | 
					 | 
				
			||||||
                )
 | 
					 | 
				
			||||||
                return #generators > 0
 | 
					 | 
				
			||||||
            end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if is_null_ls_formatting_enabled(bufnr) then
 | 
					 | 
				
			||||||
               vim.lsp.buf.format({
 | 
					 | 
				
			||||||
                  bufnr = bufnr,
 | 
					 | 
				
			||||||
                  filter = function(client)
 | 
					 | 
				
			||||||
                    return client.name == "null-ls"
 | 
					 | 
				
			||||||
                  end
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
            else
 | 
					 | 
				
			||||||
                vim.lsp.buf.format({
 | 
					 | 
				
			||||||
                  bufnr = bufnr,
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
            end
 | 
					 | 
				
			||||||
          ''
 | 
					 | 
				
			||||||
          else "
 | 
					 | 
				
			||||||
              vim.lsp.buf.format({
 | 
					 | 
				
			||||||
                bufnr = bufnr,
 | 
					 | 
				
			||||||
              })
 | 
					 | 
				
			||||||
        "
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
              end,
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
          end
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        ${optionalString config.vim.ui.breadcrumbs.enable ''local navic = require("nvim-navic")''}
 | 
					        ${optionalString config.vim.ui.breadcrumbs.enable ''local navic = require("nvim-navic")''}
 | 
				
			||||||
        default_on_attach = function(client, bufnr)
 | 
					        default_on_attach = function(client, bufnr)
 | 
				
			||||||
          attach_keymaps(client, bufnr)
 | 
					          attach_keymaps(client, bufnr)
 | 
				
			||||||
          format_callback(client, bufnr)
 | 
					 | 
				
			||||||
          ${optionalString config.vim.ui.breadcrumbs.enable ''
 | 
					          ${optionalString config.vim.ui.breadcrumbs.enable ''
 | 
				
			||||||
          -- let navic attach to buffers
 | 
					          -- let navic attach to buffers
 | 
				
			||||||
          if client.server_capabilities.documentSymbolProvider then
 | 
					          if client.server_capabilities.documentSymbolProvider then
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue