--{{ salt['pillar.get']('salt_managed', default='Salt Managed') }}
{%- from "neovim/map.jinja" import neovim with context %}

-- NeoVIM global configs
vim.cmd.colorscheme('{{ neovim.config.use_theme }}')
vim.cmd.filetype('plugin on')
{%- if neovim.config.use_syntax %}
vim.cmd.syntax('on')
{%- endif %}

{% macro cfg(command, settings) -%}
{%- if command == "globals" %}{% set t = "g" %}{% endif %}
{%- if command == "options" %}{% set t = "o" %}{% endif %}
{% for k, v in settings.items() %}
vim.{{ t }}["{{ k }}"]{% if v is defined %}={% if v is integer or v is boolean %}{{ v|lower }}{% else %}"{{ v }}"{% endif %}{% endif %}
{%- endfor %}
{% endmacro -%}

-- NeoVIM globals
{{- cfg("globals", neovim.config.globals) }}

-- NeoVIM options
{{- cfg("options", neovim.config.options) }}

-- NeoVIM functions
function RemoveComments()
  vim.cmd.normal("g/^#/d")
  vim.cmd.normal("g/^$/d")
end

function Reformat()
  vim.cmd.normal("gg=G")
end

vim.api.nvim_command("command! Reformat call luaeval('Reformat()')")
vim.api.nvim_command("command! RemoveComments call luaeval('RemoveComments()')")

-- NeoVIM keymaps
vim.keymap.set("n", "<F9>", ":!%:p")

vim.keymap.set("v", "<C-a>", "g<C-a>")
vim.keymap.set("v", "<C-x>", "g<C-x>")
vim.keymap.set("v", "g<C-a>", "<C-a>")
vim.keymap.set("v", "g<C-x>", "<C-x>")

-- NeoVIM functions
{% if "nvim-tree" in neovim.plugins.keys() -%}
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

vim.opt.termguicolors = true

require("nvim-tree").setup({
  sort = {
    sorter = "case_sensitive",
  },
  view = {
    width = 30,
  },
  renderer = {
    group_empty = true,
  },
  filters = {
    dotfiles = true,
    git_ignored = false,
  },
})

vim.keymap.set("n", "<C-b>", ":NvimTreeToggle<CR>")

vim.api.nvim_create_autocmd("BufEnter", {
  nested = true,
  callback = function()
    if #vim.api.nvim_list_wins() == 1 and require("nvim-tree.utils").is_nvim_tree_buf() then
      vim.cmd "quit"
    end
  end
})
{% endif -%}

{% if "fzf-lua" in neovim.plugins.keys() -%}
vim.keymap.set("n", "<C-s>", ":FzfLua<CR>")
{% endif -%}