Browse Source

WIP: using formatter.nvim instead of neoformat (#781)

Abouzar Parvan 4 years ago
parent
commit
0f7c876e93

+ 66 - 3
lua/default-config.lua

@@ -88,7 +88,12 @@ O = {
   },
   },
 
 
   lang = {
   lang = {
-    cmake = {},
+    cmake = {
+      formatter = {
+        exe = "clang-format",
+        args = {},
+      },
+    },
     clang = {
     clang = {
       diagnostics = {
       diagnostics = {
         virtual_text = { spacing = 0, prefix = "" },
         virtual_text = { spacing = 0, prefix = "" },
@@ -97,12 +102,21 @@ O = {
       },
       },
       cross_file_rename = true,
       cross_file_rename = true,
       header_insertion = "never",
       header_insertion = "never",
+      filetypes = { "c", "cpp", "objc" },
+      formatter = {
+        exe = "clang-format",
+        args = {},
+      },
     },
     },
     css = {
     css = {
       virtual_text = true,
       virtual_text = true,
     },
     },
     dart = {
     dart = {
       sdk_path = "/usr/lib/dart/bin/snapshots/analysis_server.dart.snapshot",
       sdk_path = "/usr/lib/dart/bin/snapshots/analysis_server.dart.snapshot",
+      formatter = {
+        exe = "dart",
+        args = { "format" },
+      },
     },
     },
     docker = {},
     docker = {},
     efm = {},
     efm = {},
@@ -110,7 +124,12 @@ O = {
     emmet = { active = true },
     emmet = { active = true },
     elixir = {},
     elixir = {},
     graphql = {},
     graphql = {},
-    go = {},
+    go = {
+      formatter = {
+        exe = "gofmt",
+        args = {},
+      },
+    },
     html = {},
     html = {},
     java = {
     java = {
       java_tools = {
       java_tools = {
@@ -123,6 +142,10 @@ O = {
         signs = true,
         signs = true,
         underline = true,
         underline = true,
       },
       },
+      formatter = {
+        exe = "python",
+        args = { "-m", "json.tool" },
+      },
     },
     },
     kotlin = {},
     kotlin = {},
     latex = {},
     latex = {},
@@ -132,6 +155,11 @@ O = {
         signs = true,
         signs = true,
         underline = true,
         underline = true,
       },
       },
+      formatter = {
+        exe = "stylua",
+        args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0) },
+        stdin = false,
+      },
     },
     },
     php = {
     php = {
       format = {
       format = {
@@ -148,6 +176,11 @@ O = {
         underline = true,
         underline = true,
       },
       },
       filetypes = { "php", "phtml" },
       filetypes = { "php", "phtml" },
+      formatter = {
+        exe = "phpcbf",
+        args = { "--standard=PSR12", vim.api.nvim_buf_get_name(0) },
+        stdin = false,
+      },
     },
     },
     python = {
     python = {
       linter = "",
       linter = "",
@@ -162,6 +195,10 @@ O = {
         auto_search_paths = true,
         auto_search_paths = true,
         use_library_code_types = true,
         use_library_code_types = true,
       },
       },
+      formatter = {
+        exe = "yapf",
+        args = {},
+      },
     },
     },
     ruby = {
     ruby = {
       diagnostics = {
       diagnostics = {
@@ -170,6 +207,10 @@ O = {
         underline = true,
         underline = true,
       },
       },
       filetypes = { "rb", "erb", "rakefile", "ruby" },
       filetypes = { "rb", "erb", "rakefile", "ruby" },
+      formatter = {
+        exe = "rufo",
+        args = { "-x" },
+      },
     },
     },
     rust = {
     rust = {
       rust_tools = {
       rust_tools = {
@@ -177,6 +218,10 @@ O = {
         parameter_hints_prefix = "<-",
         parameter_hints_prefix = "<-",
         other_hints_prefix = "=>", -- prefix for all the other hints (type, chaining)
         other_hints_prefix = "=>", -- prefix for all the other hints (type, chaining)
       },
       },
+      formatter = {
+        exe = "rustfmt",
+        args = { "--emit=stdout" },
+      },
       linter = "",
       linter = "",
       diagnostics = {
       diagnostics = {
         virtual_text = { spacing = 0, prefix = "" },
         virtual_text = { spacing = 0, prefix = "" },
@@ -193,6 +238,11 @@ O = {
         signs = true,
         signs = true,
         underline = true,
         underline = true,
       },
       },
+      formatter = {
+        exe = "shfmt",
+        args = { "-w" },
+        stdin = false,
+      },
     },
     },
     svelte = {},
     svelte = {},
     tailwindcss = {
     tailwindcss = {
@@ -206,6 +256,10 @@ O = {
         "typescript",
         "typescript",
         "typescriptreact",
         "typescriptreact",
       },
       },
+      formatter = {
+        exe = "prettier",
+        args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
+      },
     },
     },
     terraform = {},
     terraform = {},
     tsserver = {
     tsserver = {
@@ -216,9 +270,18 @@ O = {
         signs = true,
         signs = true,
         underline = true,
         underline = true,
       },
       },
+      formatter = {
+        exe = "prettier",
+        args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
+      },
     },
     },
     vim = {},
     vim = {},
-    yaml = {},
+    yaml = {
+      formatter = {
+        exe = "prettier",
+        args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
+      },
+    },
   },
   },
 }
 }
 
 

+ 61 - 0
lua/lv-formatter/init.lua

@@ -0,0 +1,61 @@
+-- autoformat
+if O.format_on_save then
+  require("lv-utils").define_augroups {
+    autoformat = {
+      {
+        "BufWritePost",
+        "*",
+        "FormatWrite",
+      },
+    },
+  }
+end
+
+-- check if formatter has been defined for the language or not
+function formatter_exists(lang_formatter)
+  if lang_formatter == nil then
+    return false
+  end
+  if lang_formatter.exe == nil or lang_formatter.args == nil then
+    return false
+  end
+  return true
+end
+
+-- returns default formatter for given language
+function formatter_return(lang_formatter)
+  return {
+    exe = lang_formatter.exe,
+    args = lang_formatter.args,
+    stdin = not (lang_formatter.stdin ~= nil),
+  }
+end
+
+-- fill a table like this -> {rust: {exe:"sth",args:{"a","b"},stdin=true},go: {}...}
+local formatter_filetypes = {}
+for k, v in pairs(O.lang) do
+  if formatter_exists(v.formatter) then
+    local keys = v.filetypes
+    if keys == nil then
+      keys = {k}
+    end
+    for _, l in pairs(keys) do
+      formatter_filetypes[l] = {
+        function ()
+          return formatter_return(v.formatter)
+        end,
+      }
+    end
+  end
+end
+
+require("formatter").setup {
+  logging = false,
+  filetype = formatter_filetypes,
+}
+
+if not O.format_on_save then
+  vim.cmd [[if exists('#autoformat#BufWritePost')
+	:autocmd! autoformat
+	endif]]
+end

+ 0 - 23
lua/lv-neoformat/init.lua

@@ -1,23 +0,0 @@
--- autoformat
-if O.format_on_save then
-  require("lv-utils").define_augroups {
-    autoformat = {
-      {
-        "BufWritePre",
-        "*",
-        [[try | undojoin | Neoformat | catch /^Vim\%((\a\+)\)\=:E790/ | finally | silent Neoformat | endtry]],
-      },
-    },
-  }
-end
-
-vim.g.neoformat_run_all_formatters = 0
-
-vim.g.neoformat_enabled_python = { "autopep8", "yapf", "docformatter" }
-vim.g.neoformat_enabled_javascript = { "prettier" }
-
-if not O.format_on_save then
-  vim.cmd [[if exists('#autoformat#BufWritePre')
-	:autocmd! autoformat
-	endif]]
-end

+ 1 - 1
lua/lv-utils/init.lua

@@ -4,7 +4,7 @@ function lv_utils.reload_lv_config()
   vim.cmd "source ~/.config/nvim/lv-config.lua"
   vim.cmd "source ~/.config/nvim/lv-config.lua"
   vim.cmd "source ~/.config/nvim/lua/plugins.lua"
   vim.cmd "source ~/.config/nvim/lua/plugins.lua"
   vim.cmd "source ~/.config/nvim/lua/settings.lua"
   vim.cmd "source ~/.config/nvim/lua/settings.lua"
-  vim.cmd "source ~/.config/nvim/lua/lv-neoformat/init.lua"
+  vim.cmd "source ~/.config/nvim/lua/lv-formatter/init.lua"
   vim.cmd ":PackerCompile"
   vim.cmd ":PackerCompile"
   vim.cmd ":PackerInstall"
   vim.cmd ":PackerInstall"
 end
 end

+ 3 - 3
lua/plugins.lua

@@ -75,11 +75,11 @@ return require("packer").startup(function(use)
   -- Treesitter
   -- Treesitter
   use { "nvim-treesitter/nvim-treesitter" }
   use { "nvim-treesitter/nvim-treesitter" }
 
 
-  -- Neoformat
+  -- Formatter.nvim
   use {
   use {
-    "sbdchd/neoformat",
+    "mhartington/formatter.nvim",
     config = function()
     config = function()
-      require "lv-neoformat"
+      require "lv-formatter"
     end,
     end,
     event = "BufRead",
     event = "BufRead",
   }
   }

+ 6 - 0
utils/installer/lv-config.example.lua

@@ -40,6 +40,12 @@ O.lang.python.analysis.use_library_code_types = true
 -- javascript
 -- javascript
 O.lang.tsserver.linter = nil
 O.lang.tsserver.linter = nil
 
 
+-- rust
+O.lang.rust.formatter = {
+  exe = "rustfmt",
+  args = {"--emit=stdout"},
+}
+
 -- Additional Plugins
 -- Additional Plugins
 -- O.user_plugins = {
 -- O.user_plugins = {
 --     {"folke/tokyonight.nvim"}, {
 --     {"folke/tokyonight.nvim"}, {