-- Required to enable textobjects. See
-- https://github.com/nvim-treesitter/nvim-treesitter-textobjects/issues/100.
require'nvim-treesitter-textobjects'

local ts_langs = { "bash", "c", "cpp", "fish", "haskell", "java", "javascript", "jsdoc", "json", "lua", "nix", "python", "query", "rust", "teal", "toml", "tsx", "typescript", "verilog", "vim" }

require'nvim-treesitter.install'.compilers = { "gcc" }
require'nvim-treesitter.configs'.setup {
  ensure_installed = ts_langs,
  highlight        = { enable = true, },
  textobjects = {
    select = {
      enable    = true,
      lookahead = true,
      keymaps = {
	["ab"] = "@block.outer"      ,
	["ib"] = "@block.inner"      ,
	-- We use M,m for call/conditional as C/c would conflict with textobj-word-column
	["aM"] = "@call.outer"       ,
	["iM"] = "@call.inner"       ,
	["ao"] = "@class.outer"      ,
	["io"] = "@class.inner"      ,
	["at"] = "@comment.outer"    ,
	["am"] = "@conditional.outer",
	["im"] = "@conditional.inner",
	["af"] = "@function.outer"   ,
	["if"] = "@function.inner"   ,
	["al"] = "@loop.outer"       ,
	["il"] = "@loop.inner"       ,
	["ap"] = "@parameter.outer"  ,
	["ip"] = "@parameter.inner"  ,
	["is"] = "@scopename.inner"  ,
	["as"] = "@statement.outer"  ,
      },
    },
    swap = {
      enable        = true,
      swap_next     = {
	["<Leader>sb"] = "@block.outer"    ,
	["<Leader>sl"] = "@call.outer"     ,
	["<Leader>sc"] = "@class.outer"    ,
	["<Leader>sf"] = "@function.outer" ,
	["<Leader>sp"] = "@parameter.inner",
	["<Leader>ss"] = "@statement.outer",
      },
      swap_previous = {
	["<Leader>sB"] = "@block.outer"    ,
	["<Leader>sL"] = "@call.outer"     ,
	["<Leader>sC"] = "@class.outer"    ,
	["<Leader>sF"] = "@function.outer" ,
	["<Leader>sP"] = "@parameter.inner",
	["<Leader>sS"] = "@statement.outer",
      },
    },
    move = {
      enable    = true,
      set_jumps = true,
      goto_next_start = {
	["]m"] = "@function.outer",
	["]s"] = "@class.outer",
      },
      goto_next_end = {
	["]M"] = "@function.outer",
	["]S"] = "@class.outer",
      },
      goto_previous_start = {
	["[m"] = "@function.outer",
	["[s"] = "@class.outer",
      },
      goto_previous_end = {
	["[M"] = "@function.outer",
	["[S"] = "@class.outer",
      },
    },
  },
  query_linter = {
    enable           = true,
    use_virtual_text = true,
    lint_events      = { "BufWrite", "CursorHold" },
  },
  playground = {
    enable          = true,
    disable         = {},
    updatetime      = 25,
    persist_queries = false,
    keybindings = {
      toggle_query_editor       = 'o',
      toggle_hl_groups          = 'i',
      toggle_injected_languages = 't',
      toggle_anonymous_nodes    = 'a',
      toggle_language_display   = 'I',
      focus_language            = 'f',
      unfocus_language          = 'F',
      update                    = 'R',
      goto_node                 = '<CR>',
      show_help                 = '?',
    },
  }
}
