local fn = vim.fn

local install_path = fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim"
if fn.empty(fn.glob(install_path)) > 0 then
  PACKER_BOOTSTRAP = fn.system {
    "git",
    "clone",
    "--depth",
    "1",
    "https://github.com/wbthomason/packer.nvim",
    install_path,
  }
  print "Installing packer close and reopen Neovim..."
  vim.cmd [[packadd packer.nvim]]
end

vim.cmd [[
  augroup packer_user_config
    autocmd!
    autocmd BufWritePost plugins.lua source <afile> | PackerSync
  augroup end
]]

local status_ok, packer = pcall(require, "packer")
if not status_ok then
  return
end

packer.init {
  display = {
    open_fn = function()
      return require("packer.util").float { border = "rounded" }
    end,
  },
}

return packer.startup(function(use)
  -- Utilities
  use { "wbthomason/packer.nvim", opt = true }
  use { "mbbill/undotree" }
  use { "nvim-lua/plenary.nvim" }
  use { "nvim-telescope/telescope.nvim" }
  use {
    "nvim-treesitter/nvim-treesitter",
    event = "BufEnter",
    run = ":TSUpdate",
    config = [[require('config.treesitter')]]
  }
  use { "tpope/vim-fugitive", event = "User InGitRepo" }

  -- Editing
  use { "godlygeek/tabular" }
  use { "kana/vim-textobj-user" }
  use { "mg979/vim-visual-multi", branch = "master" }
  use { "terryma/vim-expand-region" }
  use { "tommcdo/vim-exchange", event = "VimEnter" }
  use { "tpope/vim-abolish" }
  use { "tpope/vim-commentary", event = "VimEnter" }
  use { "tpope/vim-repeat", event = "VimEnter" }
  use { "tpope/vim-surround", event = "VimEnter" }
  use { "windwp/nvim-autopairs" }

  -- UI
  use { "junegunn/goyo.vim" }
  use { "junegunn/limelight.vim" }
  use { "markonm/traces.vim" }

  -- Searching
  use { "dyng/ctrlsf.vim" }

  -- LSP
  use { "neovim/nvim-lspconfig" }
  use { "williamboman/nvim-lsp-installer" }
  use { "jose-elias-alvarez/null-ls.nvim" }

  if PACKER_BOOTSTRAP then
    require("packer").sync()
  end
end)

