Browse Source

Added default formatters (#908)

* added default formatters for a couple of langs

* fix: sugesstions from tastyep

* allow users to change default formatters

* suggestion: mellow pointed out the telescope issue

* we don't need to use formatter.setup
Abouzar Parvan 4 years ago
parent
commit
de7ec62f5f

+ 16 - 0
ftplugin/c.lua

@@ -1,3 +1,19 @@
+O.formatters.filetype["c"] = {
+  function()
+    return {
+      exe = O.lang.c.formatter.exe,
+      args = O.lang.c.formatter.args,
+      stdin = not (O.lang.c.formatter.stdin ~= nil),
+    }
+  end,
+}
+O.formatters.filetype["cpp"] = O.formatters.filetype["c"]
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
+
 if require("lv-utils").check_lsp_client_active "clangd" then
   return
 end

+ 14 - 0
ftplugin/dart.lua

@@ -1,3 +1,17 @@
+O.formatters.filetype["dart"] = {
+  function()
+    return {
+      exe = O.lang.dart.formatter.exe,
+      args = O.lang.dart.formatter.args,
+      stdin = not (O.lang.dart.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
 if require("lv-utils").check_lsp_client_active "dartls" then
   return
 end

+ 15 - 0
ftplugin/go.lua

@@ -1,3 +1,18 @@
+O.formatters.filetype["go"] = {
+  function()
+    return {
+      exe = O.lang.go.formatter.exe,
+      args = O.lang.go.formatter.args,
+      stdin = not (O.lang.go.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
+
 if not require("lv-utils").check_lsp_client_active "gopls" then
   require("lspconfig").gopls.setup {
     cmd = { DATA_PATH .. "/lspinstall/go/gopls" },

+ 14 - 0
ftplugin/json.lua

@@ -1,3 +1,17 @@
+O.formatters.filetype["json"] = {
+  function()
+    return {
+      exe = O.lang.json.formatter.exe,
+      args = O.lang.json.formatter.args,
+      stdin = not (O.lang.json.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
 if require("lv-utils").check_lsp_client_active "jsonls" then
   return
 end

+ 3 - 17
ftplugin/lua.lua

@@ -1,11 +1,9 @@
 O.formatters.filetype["lua"] = {
-  -- prettier
   function()
     return {
-      exe = "stylua",
-      --  TODO: append to this for args don't overwrite
-      args = {},
-      stdin = false,
+      exe = O.lang.lua.formatter.exe,
+      args = O.lang.lua.formatter.args,
+      stdin = not (O.lang.lua.formatter.stdin ~= nil),
     }
   end,
 }
@@ -48,16 +46,4 @@ if not require("lv-utils").check_lsp_client_active "sumneko_lua" then
   }
 end
 
-if O.lang.lua.autoformat then
-  require("lv-utils").define_augroups {
-    _lua_autoformat = {
-      {
-        "BufWritePre",
-        "*.lua",
-        "lua vim.lsp.buf.formatting_sync(nil, 1000)",
-      },
-    },
-  }
-end
-
 vim.cmd "setl ts=2 sw=2"

+ 14 - 0
ftplugin/php.lua

@@ -1,3 +1,17 @@
+O.formatters.filetype["php"] = {
+  function()
+    return {
+      exe = O.lang.php.formatter.exe,
+      args = O.lang.php.formatter.args,
+      stdin = not (O.lang.php.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
 if require("lv-utils").check_lsp_client_active "intelephense" then
   return
 end

+ 15 - 0
ftplugin/python.lua

@@ -1,3 +1,18 @@
+O.formatters.filetype["python"] = {
+  function()
+    return {
+      exe = O.lang.python.formatter.exe,
+      args = O.lang.python.formatter.args,
+      stdin = not (O.lang.python.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
+
 local python_arguments = {}
 
 -- TODO: replace with path argument

+ 14 - 0
ftplugin/ruby.lua

@@ -1,3 +1,17 @@
+O.formatters.filetype["ruby"] = {
+  function()
+    return {
+      exe = O.lang.ruby.formatter.exe,
+      args = O.lang.ruby.formatter.args,
+      stdin = not (O.lang.ruby.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
 if require("lv-utils").check_lsp_client_active "solargraph" then
   return
 end

+ 15 - 1
ftplugin/rust.lua

@@ -1,3 +1,17 @@
+O.formatters.filetype["rust"] = {
+  function()
+    return {
+      exe = O.lang.rust.formatter.exe,
+      args = O.lang.rust.formatter.args,
+      stdin = not (O.lang.rust.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
 if require("lv-utils").check_lsp_client_active "rust_analyzer" then
   return
 end
@@ -81,7 +95,7 @@ else
     cmd = { DATA_PATH .. "/lspinstall/rust/rust-analyzer" },
     on_attach = require("lsp").common_on_attach,
     filetypes = { "rust" },
-    root_dir = require("lspconfig.util").root_pattern("Cargo.toml", "rust-project.json"),
+    root_dir = require("lspconfig.util").root_pattern("Carrust.toml", "rust-project.json"),
   }
 end
 

+ 15 - 1
ftplugin/sh.lua

@@ -1,3 +1,17 @@
+O.formatters.filetype["sh"] = {
+  function()
+    return {
+      exe = O.lang.sh.formatter.exe,
+      args = O.lang.sh.formatter.args,
+      stdin = not (O.lang.sh.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
 if not require("lv-utils").check_lsp_client_active "bashls" then
   -- npm i -g bash-language-server
   require("lspconfig").bashls.setup {
@@ -26,7 +40,7 @@ if not require("lv-utils").check_lsp_client_active "efm" then
     -- init_options = {initializationOptions},
     cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
     init_options = { documentFormatting = true, codeAction = false },
-    root_dir = require("lspconfig").util.root_pattern(".git/"),
+    root_dir = require("lspconfig").util.root_pattern ".git/",
     filetypes = { "sh" },
     settings = {
       rootMarkers = { ".git/" },

+ 2 - 3
ftplugin/tex.lua

@@ -33,7 +33,6 @@ vim.api.nvim_exec(
     ]],
   false
 )
-if (O.lang.latex.auto_save)
-then
-  vim.api.nvim_exec([[au FocusLost * :wa]],false)
+if O.lang.latex.auto_save then
+  vim.api.nvim_exec([[au FocusLost * :wa]], false)
 end

+ 14 - 0
ftplugin/yaml.lua

@@ -1,3 +1,17 @@
+O.formatters.filetype["yaml"] = {
+  function()
+    return {
+      exe = O.lang.yaml.formatter.exe,
+      args = O.lang.yaml.formatter.args,
+      stdin = not (O.lang.yaml.formatter.stdin ~= nil),
+    }
+  end,
+}
+
+require("formatter.config").set_defaults {
+  logging = false,
+  filetype = O.formatters.filetype,
+}
 if require("lv-utils").check_lsp_client_active "yamlls" then
   return
 end

+ 1 - 1
ftplugin/zsh.lua

@@ -24,7 +24,7 @@ if not require("lv-utils").check_lsp_client_active "efm" then
     -- init_options = {initializationOptions},
     cmd = { DATA_PATH .. "/lspinstall/efm/efm-langserver" },
     init_options = { documentFormatting = true, codeAction = false },
-    root_dir = require("lspconfig").util.root_pattern(".git/"),
+    root_dir = require("lspconfig").util.root_pattern ".git/",
     filetypes = { "zsh" },
     settings = {
       rootMarkers = { ".git/" },

+ 0 - 2
lua/core/formatter.lua

@@ -53,8 +53,6 @@ if not status_ok then
   return
 end
 
-formatter.setup {}
-
 if not O.format_on_save then
   vim.cmd [[if exists('#autoformat#BufWritePost')
 	:autocmd! autoformat

+ 1 - 7
lua/default-config.lua

@@ -258,11 +258,6 @@ O = {
         "typescript",
         "typescriptreact",
       },
-      formatter = {
-        exe = "prettier",
-        args = { "--write", "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
-        stdin = false,
-      },
     },
     terraform = {},
     tsserver = {
@@ -275,8 +270,7 @@ O = {
       },
       formatter = {
         exe = "prettier",
-        args = { "--write", "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
-        stdin = false,
+        args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
       },
     },
     vim = {},

+ 11 - 6
lua/lsp/tsserver-ls.lua

@@ -1,18 +1,23 @@
 vim.cmd "let proj = FindRootDirectory()"
-print(vim.api.nvim_get_var "proj")
 local root_dir = vim.api.nvim_get_var "proj"
+
+-- use the global prettier if you didn't find the local one
+local prettier_instance = root_dir .. "/node_modules/.bin/prettier"
+if vim.fn.executable(prettier_instance) ~= 1 then
+  prettier_instance = O.lang.tsserver.formatter.exe
+end
+
 O.formatters.filetype["javascriptreact"] = {
-  -- vim.cmd "let root_dir "
-  -- prettier
   function()
     return {
-      exe = root_dir .. "/node_modules/.bin/prettier",
-      --  TODO: append to this for args don't overwrite
+      exe = prettier_instance,
+      -- TODO: allow user to override this
       args = { "--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote" },
-      stdin = true,
+      stdin = true
     }
   end,
 }
+O.formatters.filetype["javascript"] = O.formatters.filetype["javascriptreact"]
 
 require("formatter.config").set_defaults {
   logging = false,