From cb4e930158099b6defbe3675db4a4e6187fa669e Mon Sep 17 00:00:00 2001 From: nikkitschierske Date: Fri, 15 Dec 2023 13:20:18 +0100 Subject: [PATCH] idk --- init.lua | 35 ++++++++++++++++++++++++++++------- lazy-lock.json | 1 + 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/init.lua b/init.lua index 362c23ee..0cba5868 100644 --- a/init.lua +++ b/init.lua @@ -71,6 +71,8 @@ require('lazy').setup({ -- REMOVE "github/copilot.vim", + -- "mhartington/formatter.nvim", + 'stevearc/conform.nvim', -- Git related plugins -- 'tpope/vim-fugitive', @@ -117,7 +119,7 @@ require('lazy').setup({ }, -- Useful plugin to show you pending keybinds. - { 'folke/which-key.nvim', opts = {} }, + { 'folke/which-key.nvim', opts = {} }, { -- Adds git related signs to the gutter, as well as utilities for managing changes 'lewis6991/gitsigns.nvim', @@ -324,7 +326,7 @@ vim.g.copilot_no_tab_map = true -- keymap for opening lazygit vim.keymap.set('n', 'g', ':LazyGit', { desc = 'Open lazygit' }) - +vim.keymap.set('n', 'f', ':Format', { desc = 'Format current buffer' }) -- Keymaps for better default experience -- See `:help vim.keymap.set()` @@ -540,11 +542,6 @@ local on_attach = function(_, bufnr) nmap('wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, '[W]orkspace [L]ist Folders') - - -- Create a command `:Format` local to the LSP buffer - vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) - vim.lsp.buf.format() - end, { desc = 'Format current buffer with LSP' }) end -- document existing key chains @@ -675,3 +672,27 @@ cmp.setup { -- The line beneath this is called `modeline`. See `:help modeline` -- vim: ts=2 sts=2 sw=2 et +-- +require("conform").setup({ + formatters_by_ft = { + lua = { "stylua" }, + -- Conform will run multiple formatters sequentially + python = { "isort", "black" }, + -- Use a sub-list to run only the first available formatter + javascript = { { "prettierd", "prettier" } }, + }, +}) + +vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" + +vim.api.nvim_create_user_command("Format", function(args) + local range = nil + if args.count ~= -1 then + local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1] + range = { + start = { args.line1, 0 }, + ["end"] = { args.line2, end_line:len() }, + } + end + require("conform").format({ async = true, lsp_fallback = true, range = range }) +end, { range = true }) diff --git a/lazy-lock.json b/lazy-lock.json index 4fbc5318..685f10f4 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -5,6 +5,7 @@ "cmp-nvim-lsp": { "branch": "main", "commit": "5af77f54de1b16c34b23cba810150689a3a90312" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, + "conform.nvim": { "branch": "master", "commit": "8b407bb6175846cdc4c499e2a8d28109615a2089" }, "copilot.vim": { "branch": "release", "commit": "5b19fb001d7f31c4c7c5556d7a97b243bd29f45f" }, "fidget.nvim": { "branch": "main", "commit": "7b9c383438a2e490e37d57b07ddeae3ab4f4cf69" }, "friendly-snippets": { "branch": "main", "commit": "53d3df271d031c405255e99410628c26a8f0d2b0" },