Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/cmdline/highlight_spec.lua

Raw
describe('highlight.apply', function()
  local highlight

  before_each(function()
    package.loaded['bugabinga.cmdline.highlight'] = nil
    highlight = require('bugabinga.cmdline.highlight')
  end)

  after_each(function()
    for _, buf in ipairs(vim.api.nvim_list_bufs()) do
      vim.api.nvim_buf_delete(buf, { force = true })
    end
  end)

  local function make_buf_with_line()
    local buf = vim.api.nvim_create_buf(false, true)
    vim.api.nvim_buf_set_lines(buf, 0, -1, false, { '                    ' })
    return buf
  end

  describe('apply', function()
    it('applies treesitter highlights for lua lang', function()
      local buf = make_buf_with_line()
      local ns = vim.api.nvim_create_namespace 'test'
      local text = 'local x = 10'
      local row = 0
      local col_offset = 2
      highlight.apply(buf, ns, text, 'lua', row, col_offset)
      local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
      assert.is_true(#extmarks > 0)
    end)

    it('applies treesitter highlights for vim lang', function()
      local buf = make_buf_with_line()
      local ns = vim.api.nvim_create_namespace 'test'
      local text = ':function Foo() end'
      local row = 0
      local col_offset = 2
      highlight.apply(buf, ns, text, 'vim', row, col_offset)
      local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
      assert.is_true(#extmarks > 0)
    end)

    it('applies treesitter highlights for bash lang if parser available', function()
      local buf = make_buf_with_line()
      local ns = vim.api.nvim_create_namespace 'test'
      local text = 'ls -la'
      local row = 0
      local col_offset = 2
      highlight.apply(buf, ns, text, 'bash', row, col_offset)
      local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
      if #extmarks == 0 then
        vim.notify('bash parser not available, skipping', vim.log.levels.WARN)
      end
      assert.is_true(#extmarks >= 0)
    end)

    it('does not crash when lang is nil', function()
      local buf = make_buf_with_line()
      local ns = vim.api.nvim_create_namespace 'test'
      local text = 'hello world'
      local row = 0
      local col_offset = 2
      local ok = pcall(highlight.apply, buf, ns, text, nil, row, col_offset)
      assert.is_true(ok)
    end)

    it('does not crash when lang has no parser', function()
      local buf = make_buf_with_line()
      local ns = vim.api.nvim_create_namespace 'test'
      local text = 'hello world'
      local row = 0
      local col_offset = 2
      local ok = pcall(highlight.apply, buf, ns, text, 'nonexistent_lang_xyz', row, col_offset)
      assert.is_true(ok)
    end)

    it('applies highlights with correct col_offset applied', function()
      local buf = make_buf_with_line()
      local ns = vim.api.nvim_create_namespace 'test'
      local text = 'print("hi")'
      local row = 0
      local col_offset = 5
      highlight.apply(buf, ns, text, 'lua', row, col_offset)
      local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
      assert.is_true(#extmarks > 0)
      for _, extmark in ipairs(extmarks) do
        local _, mark_col = extmark[2], extmark[3]
        assert.is_true(mark_col >= col_offset)
      end
    end)

    it('clears namespace before applying new highlights', function()
      local buf = make_buf_with_line()
      local ns = vim.api.nvim_create_namespace 'test'
      local text = 'local x = 10'
      local row = 0
      local col_offset = 2
      highlight.apply(buf, ns, text, 'lua', row, col_offset)
      local extmarks_first = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
      local first_count = #extmarks_first
      highlight.apply(buf, ns, text, 'lua', row, col_offset)
      local extmarks_second = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
      assert.equal(first_count, #extmarks_second)
    end)

    it('handles nil lang gracefully', function()
      local buf = make_buf_with_line()
      local ns = vim.api.nvim_create_namespace 'test'
      local text = 'local x = 10'
      local row = 0
      local col_offset = 2
      local ok = pcall(highlight.apply, buf, ns, text, nil, row, col_offset)
      assert.is_true(ok)
    end)
  end)
end)