Skip to content

Commit

Permalink
feat: Item.get_lang and Item.get_ft
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 86aceb8 commit 498da6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
19 changes: 19 additions & 0 deletions lua/trouble/item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ function M.new(opts)
return setmetatable(self, M)
end

---@return string?
function M:get_ft()
if self.buf and vim.api.nvim_buf_is_loaded(self.buf) then
return vim.bo[self.buf].filetype
end
local ft = self.cache.ft
if ft == nil then
ft = vim.filetype.match({ filename = self.filename })
self.cache.ft = ft or false -- cache misses too
end
return ft
end

function M:get_lang()
local ft = self:get_ft()
return ft and ft ~= "" and vim.treesitter.language.get_lang(ft) or nil
end

function M:__index(k)
if type(k) ~= "string" then
return
Expand All @@ -45,6 +63,7 @@ function M:__index(k)
end

local obj = self

local start = 1
while type(obj) == "table" do
local dot = k:find(".", start, true)
Expand Down
23 changes: 8 additions & 15 deletions lua/trouble/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,14 @@ function M:at(row)
return self._locations[row] or {}
end

function M.get_lang(buf)
local ret = Cache.langs[buf]
if ret ~= nil then
return ret
---@param item trouble.Item
function M.get_lang(item)
local lang = Cache.langs[item.filename]
if lang == nil then
lang = item:get_lang()
Cache.langs[item.filename] = lang or false -- cache misses too
end
local ft = vim.api.nvim_buf_is_loaded(buf) and not vim.b[buf].trouble_preview and vim.bo[buf].filetype
or vim.filetype.match({ buf = buf })
if ft then
local lang = vim.treesitter.language.get_lang(ft)
if lang then
Cache.langs[buf] = lang
return lang
end
end
Cache.langs[buf] = false
return lang
end

---@param node trouble.Node
Expand Down Expand Up @@ -209,7 +202,7 @@ function M:item(node, section, indent)
local offset ---@type number? start column of the first line
local first ---@type string? first line
if ff.hl == "ts" then
local lang = M.get_lang(item.buf)
local lang = M.get_lang(item)
if lang then
ff.hl = "ts." .. lang
else
Expand Down

0 comments on commit 498da6b

Please sign in to comment.