-- 自动安装 Packer.nvim
-- 插件安装目录
-- ~/.local/share/nvim/site/pack/packer/
local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
local paccker_bootstrap
if fn.empty(fn.glob(install_path)) > 0 then
    vim.notify("正在安装Pakcer.nvim,请稍后...")
    paccker_bootstrap = fn.system({"git", "clone", "--depth", "1", "https://github.com/wbthomason/packer.nvim",
    -- "https://gitcode.net/mirrors/wbthomason/packer.nvim",
                                   install_path})

    -- https://github.com/wbthomason/packer.nvim/issues/750
    local rtp_addition = vim.fn.stdpath("data") .. "/site/pack/*/start/*"
    if not string.find(vim.o.runtimepath, rtp_addition) then
        vim.o.runtimepath = rtp_addition .. "," .. vim.o.runtimepath
    end
    vim.notify("Pakcer.nvim 安装完毕")
end

-- Use a protected call so we don't error out on first use
local status_ok, packer = pcall(require, "packer")
if not status_ok then
    vim.notify("没有安装 packer.nvim")
    return
end

packer.startup({
    function(use)
        -- Packer 可以管理自己本身
        use 'wbthomason/packer.nvim'
        -- 你的插件列表...
        use {
            'nvim-lualine/lualine.nvim',
            requires = {
                'nvim-tree/nvim-web-devicons',
                opt = true
            }
        }
        use('nvim-tree/nvim-web-devicons')
        -- tokyonight 主题
        use("folke/tokyonight.nvim")
        -- nvim-tree  文件侧边栏
        use({
            "kyazdani42/nvim-tree.lua",
            requires = "kyazdani42/nvim-web-devicons"
        })
        -- 文件搜索
        use {
            'nvim-telescope/telescope.nvim',
            requires = {"nvim-lua/plenary.nvim"}
        }
        -- dashboard-nvim  vim 主页
        use {
            'glepnir/dashboard-nvim',
            requires = {'nvim-tree/nvim-web-devicons'}
        }
        use 'voldikss/vim-floaterm'
        -- project
        use("ahmedkhalf/project.nvim")
        -- treesitter (新增)
        use {
            'nvim-treesitter/nvim-treesitter',
            run = ':TSUpdate'
        }
        --------------------- LSP --------------------
        use {
          'neoclide/coc.nvim',
          branch = 'release'
        }
        -- Lspconfig
        use({"neovim/nvim-lspconfig"})
         use("hrsh7th/nvim-cmp")
        -- snippet 引擎
        use("hrsh7th/vim-vsnip")
        -- 补全源
        use("hrsh7th/cmp-vsnip")
        use("hrsh7th/cmp-nvim-lsp") -- { name = nvim_lsp }
        use("hrsh7th/cmp-buffer") -- { name = 'buffer' },
        use("hrsh7th/cmp-path") -- { name = 'path' }
        use("hrsh7th/cmp-cmdline") -- { name = 'cmdline' }

        -- 常见编程语言代码段
        use("rafamadriz/friendly-snippets")
        -- 括号自动补全
        use {
            "windwp/nvim-autopairs",
            config = function()
                require("nvim-autopairs").setup {}
            end
        }
        -- 代码格式化
        use('jose-elias-alvarez/null-ls.nvim')
        use('MunifTanjim/prettier.nvim')
        -- 补全引擎
        -- use("hrsh7th/nvim-cmp")
        -- snippet 引擎
        -- use("hrsh7th/vim-vsnip")
        -- 补全源
        -- use("hrsh7th/cmp-vsnip")
        -- use("hrsh7th/cmp-nvim-lsp") -- { name = nvim_lsp }
        -- use("hrsh7th/cmp-buffer") -- { name = 'buffer' },
        -- use("hrsh7th/cmp-path") -- { name = 'path' }
        -- use("hrsh7th/cmp-cmdline") -- { name = 'cmdline' }

        -- 常见编程语言代码段
        -- use("rafamadriz/friendly-snippets")

    end,
    config = {
        -- 并发数限制
        max_jobs = 16,
        -- 自定义源
        git = {
            -- default_url_format = "https://hub.fastgit.xyz/%s",
            -- default_url_format = "https://mirror.ghproxy.com/https://github.com/%s"
            -- default_url_format = "https://gitcode.net/mirrors/%s"
            -- default_url_format = "https://gitclone.com/github.com/%s",
        },
        display = {
            open_fn = function()
                return require("packer.util").float({
                    border = "single"
                })
            end
        }
    }
})

-- 每次保存 plugins.lua 自动安装插件
pcall(vim.cmd, [[
    augroup packer_user_config
    autocmd!
    autocmd BufWritePost plugins.lua source <afile> | PackerSync
    augroup end
  ]])