From b2e2b15b4532186cf11fcdb7dc1d6c1714f94e9f Mon Sep 17 00:00:00 2001
From: siduck <siduck@tutanota.com>
Date: Sun, 28 Jan 2024 21:33:38 +0530
Subject: [PATCH] fix editorConfig indent opts not applying on certain fts
 #2633

---
 lua/core/init.lua                 | 25 +++++++++++++++++++-
 lua/core/utils.lua                | 30 +-----------------------
 lua/plugins/configs/lspconfig.lua |  5 +---
 lua/plugins/init.lua              | 38 ++++---------------------------
 4 files changed, 31 insertions(+), 67 deletions(-)

diff --git a/lua/core/init.lua b/lua/core/init.lua
index e1b9558..50045cf 100644
--- a/lua/core/init.lua
+++ b/lua/core/init.lua
@@ -11,7 +11,7 @@ opt.showmode = false
 
 opt.clipboard = "unnamedplus"
 opt.cursorline = true
-opt.cursorlineopt = 'number'
+opt.cursorlineopt = "number"
 
 -- Indenting
 opt.expandtab = true
@@ -104,6 +104,29 @@ autocmd("BufWritePost", {
   end,
 })
 
+-- user event that loads after UIEnter + only if file buf is there
+vim.api.nvim_create_autocmd({ "UIEnter", "BufReadPost", "BufNewFile" }, {
+  group = vim.api.nvim_create_augroup("NvFilePost", { clear = true }),
+  callback = function(args)
+    local file = vim.api.nvim_buf_get_name(args.buf)
+    local buftype = vim.api.nvim_buf_get_option(args.buf, "buftype")
+
+    if not vim.g.ui_entered and args.event == "UIEnter" then
+      vim.g.ui_entered = true
+    end
+
+    if file ~= "" and buftype ~= "nofile" and vim.g.ui_entered then
+      vim.api.nvim_exec_autocmds("User", { pattern = "FilePost", modeline = false })
+      vim.api.nvim_del_augroup_by_name "NvFilePost"
+
+      vim.schedule(function()
+        vim.api.nvim_exec_autocmds("FileType", {})
+        require("editorconfig").config(args.buf)
+      end, 0)
+    end
+  end,
+})
+
 -------------------------------------- commands ------------------------------------------
 local new_cmd = vim.api.nvim_create_user_command
 
diff --git a/lua/core/utils.lua b/lua/core/utils.lua
index 96fe103..6f4dfae 100644
--- a/lua/core/utils.lua
+++ b/lua/core/utils.lua
@@ -7,7 +7,7 @@ M.load_config = function()
 
   if chadrc_path then
     local chadrc = dofile(chadrc_path)
-    
+
     config.mappings = M.remove_disabled_keys(chadrc.mappings, config.mappings)
     config = merge_tb("force", config, chadrc)
     config.mappings.disabled = nil
@@ -87,32 +87,4 @@ M.load_mappings = function(section, mapping_opt)
   end)
 end
 
-M.lazy_load = function(plugin)
-  vim.api.nvim_create_autocmd({ "BufRead", "BufWinEnter", "BufNewFile" }, {
-    group = vim.api.nvim_create_augroup("BeLazyOnFileOpen" .. plugin, {}),
-    callback = function()
-      local file = vim.fn.expand "%"
-      local condition = file ~= "NvimTree_1" and file ~= "[lazy]" and file ~= ""
-
-      if condition then
-        vim.api.nvim_del_augroup_by_name("BeLazyOnFileOpen" .. plugin)
-
-        -- dont defer for treesitter as it will show slow highlighting
-        -- This deferring only happens only when we do "nvim filename"
-        if plugin ~= "nvim-treesitter" then
-          vim.schedule(function()
-            require("lazy").load { plugins = plugin }
-
-            if plugin == "nvim-lspconfig" then
-              vim.cmd "silent! do FileType"
-            end
-          end, 0)
-        else
-          require("lazy").load { plugins = plugin }
-        end
-      end
-    end,
-  })
-end
-
 return M
diff --git a/lua/plugins/configs/lspconfig.lua b/lua/plugins/configs/lspconfig.lua
index 5c6027c..55dac4d 100644
--- a/lua/plugins/configs/lspconfig.lua
+++ b/lua/plugins/configs/lspconfig.lua
@@ -4,7 +4,7 @@ local M = {}
 M.on_attach = function(client, bufnr)
   local utils = require "core.utils"
   local conf = require("nvconfig").ui.lsp
- 
+
   -- semanticTokens
   if not conf.semantic_tokens and client.supports_method "textDocument/semanticTokens" then
     client.server_capabilities.semanticTokensProvider = nil
@@ -14,9 +14,6 @@ M.on_attach = function(client, bufnr)
     require("nvchad.signature").setup(client, bufnr)
   end
 
-  client.server_capabilities.documentFormattingProvider = false
-  client.server_capabilities.documentRangeFormattingProvider = false
-
   utils.load_mappings("lspconfig", { buffer = bufnr })
 end
 
diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua
index fe74af7..26e9ba0 100644
--- a/lua/plugins/init.lua
+++ b/lua/plugins/init.lua
@@ -23,9 +23,7 @@ local default_plugins = {
 
   {
     "NvChad/nvim-colorizer.lua",
-    init = function()
-      require("core.utils").lazy_load "nvim-colorizer.lua"
-    end,
+    event = "User FilePost",
     config = function(_, opts)
       require("colorizer").setup(opts)
 
@@ -49,9 +47,7 @@ local default_plugins = {
 
   {
     "lukas-reineke/indent-blankline.nvim",
-    init = function()
-      require("core.utils").lazy_load "indent-blankline.nvim"
-    end,
+    event = "User FilePost",
     opts = function()
       return require("plugins.configs.others").blankline
     end,
@@ -67,9 +63,7 @@ local default_plugins = {
 
   {
     "nvim-treesitter/nvim-treesitter",
-    init = function()
-      require("core.utils").lazy_load "nvim-treesitter"
-    end,
+    event = { "BufReadPost", "BufNewFile" },
     cmd = { "TSInstall", "TSBufEnable", "TSBufDisable", "TSModuleInfo" },
     build = ":TSUpdate",
     opts = function()
@@ -85,27 +79,7 @@ local default_plugins = {
   -- git stuff
   {
     "lewis6991/gitsigns.nvim",
-    ft = { "gitcommit", "diff" },
-    init = function()
-      -- load gitsigns only when a git file is opened
-      vim.api.nvim_create_autocmd({ "BufRead" }, {
-        group = vim.api.nvim_create_augroup("GitSignsLazyLoad", { clear = true }),
-        callback = function()
-          vim.fn.jobstart({"git", "-C", vim.loop.cwd(), "rev-parse"},
-            {
-              on_exit = function(_, return_code)
-                if return_code == 0 then
-                  vim.api.nvim_del_augroup_by_name "GitSignsLazyLoad"
-                  vim.schedule(function()
-                    require("lazy").load { plugins = { "gitsigns.nvim" } }
-                  end)
-                end
-              end
-            }
-          )
-        end,
-      })
-    end,
+    event = "User FilePost",
     opts = function()
       return require("plugins.configs.others").gitsigns
     end,
@@ -139,9 +113,7 @@ local default_plugins = {
 
   {
     "neovim/nvim-lspconfig",
-    init = function()
-      require("core.utils").lazy_load "nvim-lspconfig"
-    end,
+    event = "User FilePost",
     config = function()
       require("plugins.configs.lspconfig").defaults()
     end,