From 0f6fc5bd2f68fbaf0acad1001f0b6af7f9f7e7ab Mon Sep 17 00:00:00 2001
From: Nikos Papadakis <nikos@papadakis.xyz>
Date: Sat, 19 Aug 2023 00:12:43 +0300
Subject: [PATCH] nvim

---
 .../nvim/after/plugin/lspconfig.lua           | 21 +++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/nvim/dot-config/nvim/after/plugin/lspconfig.lua b/nvim/dot-config/nvim/after/plugin/lspconfig.lua
index 51b02c0..a792ad2 100644
--- a/nvim/dot-config/nvim/after/plugin/lspconfig.lua
+++ b/nvim/dot-config/nvim/after/plugin/lspconfig.lua
@@ -4,6 +4,14 @@ if not ok then
     return
 end
 
+local root_dir
+for dir in vim.fs.parents(vim.api.nvim_buf_get_name(0)) do
+    if vim.fn.isdirectory(dir .. "/.git") == 1 then
+        root_dir = dir
+        break
+    end
+end
+
 local border = {
     { "┌", "FloatBorder" },
     { "─", "FloatBorder" },
@@ -64,8 +72,16 @@ local on_attach = function(client, bufnr)
     end
 end
 
+local function prettier_command()
+    if vim.fs.basename(root_dir .. "node_modules/prettier") then
+        return "npx prettier"
+    else
+        return "prettier"
+    end
+end
+
 local prettier = {
-    formatCommand = [[prettier --stdin-filepath ${INPUT} ${--tab-width:tab_width}]],
+    formatCommand = prettier_command() .. [[ --stdin-filepath ${INPUT} ${--tab-width:tab_width}]],
     formatStdin = true,
 }
 
@@ -92,7 +108,7 @@ local shellcheck = {
 
 local servers = {
     efm = {
-        filetypes = { "typescript", "typescriptreact", "sh" },
+        filetypes = { "typescript", "typescriptreact", "sh", "php" },
         init_options = { documentFormatting = true },
         settings = {
             rootMarkers = { ".git/" },
@@ -100,6 +116,7 @@ local servers = {
                 typescript = { prettier, eslint },
                 typescriptreact = { prettier, eslint },
                 sh = { shellcheck },
+                php = { prettier },
             },
         },
     },