summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/highlight.lua
blob: 04f21eb1e86d0808e5e4be7b16e6a84ff2aeb1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local api = vim.api
local ts = vim.treesitter
local queries = require'nvim-treesitter.query'

local M = {
  highlighters = {}
}

function M.attach(bufnr, ft)
  local buf = bufnr or api.nvim_get_current_buf()
  local ft = ft or api.nvim_buf_get_option(buf, 'ft')

  local query = queries.get_query(ft, "highlights")
  if not query then return end

  M.highlighters[buf] = ts.TSHighlighter.new(query, buf, ft)
end

function M.detach(bufnr)
  local buf = bufnr or api.nvim_get_current_buf()
  if M.highlighters[buf] then
    M.highlighters[buf]:set_query("")
    M.highlighters[buf] = nil
  end
  api.nvim_buf_set_option(buf, 'syntax', 'on')
end

function M.checkhealth(lang)
  local health_start = vim.fn["health#report_start"]
  local health_ok = vim.fn['health#report_ok']
  local health_info = vim.fn['health#report_info']
  local health_warn = vim.fn['health#report_warn']
  local health_error = vim.fn['health#report_error']

  if not queries.get_query(lang, "highlights") then
    health_warn("No `highlights.scm` query found for " .. lang, {
      "Open an issue at https://github.com/nvim-treesitter/nvim-treesitter"
    })
  else
    health_ok("`highlights.scm` found.")
  end
end

return M