Skip to content

Commit 464bab1

Browse files
phanenibhagwan
authored andcommitted
fix: memoize has_ts_parser call
fix: duplicated entry
1 parent 7c21f30 commit 464bab1

5 files changed

Lines changed: 19 additions & 20 deletions

File tree

lua/fzf-lua/data/colorschemes.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@
146146
{ "name": "carbonfox", "disp_name": "Carbonfox" }
147147
]
148148
},
149-
"embark-theme": {
150-
"disp_name": "Embark",
151-
"url": "embark-theme/vim",
152-
"colorschemes": ["embark"]
153-
},
154149
"github-nvim-theme": {
155150
"disp_name": "Github",
156151
"url": "projekt0n/github-nvim-theme",

lua/fzf-lua/previewer/builtin.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ local ts_attach = function(bufnr, ft)
966966
-- ts is already attach, see $VIMRUNTIME/lua/vim/treesitter/highlighter.lua
967967
if vim.b[bufnr].ts_highlight then return true end
968968
local lang = vim.treesitter.language.get_lang(ft)
969-
local loaded = lang and utils.has_ts_parser(lang)
969+
local loaded = lang and utils.has_ts_parser(lang, "highlights")
970970
if lang and loaded then
971971
local ok, err = pcall(vim.treesitter.start, bufnr, lang)
972972
if not ok then
@@ -997,7 +997,7 @@ function Previewer.base:update_ts_context()
997997
-- https://github.com/neovim/neovim/commit/45e606b1fddbfeee8fe28385b5371ca6f2fba71b
998998
-- For more info see #1922
999999
local lang = vim.treesitter.language.get_lang(ft)
1000-
if not utils.has_ts_parser(lang) then
1000+
if not utils.has_ts_parser(lang, "context") then
10011001
TSContext.close(self.win.preview_winid)
10021002
return
10031003
end

lua/fzf-lua/providers/buffers.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ M.treesitter = function(opts)
466466
local ts = vim.treesitter
467467
local ft = vim.bo[bufnr0].ft
468468
local lang = ts.language.get_lang(ft) or ft
469-
if not utils.has_ts_parser(lang) then
470-
utils.info("No treesitter parser found for '%s' (bufnr=%d)", bufname0, bufnr0)
469+
if not utils.has_ts_parser(lang, "locals") then
470+
utils.info("No treesitter parser or no 'locals.scm' found for '%s' (bufnr=%d)", bufname0, bufnr0)
471471
return
472472
end
473473

lua/fzf-lua/utils.lua

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function _G.dump(...)
77
end
88

99
local uv = vim.uv or vim.loop
10+
local memoize = vim.func and vim.func._memoize
1011

1112
local M = {}
1213

@@ -1467,17 +1468,20 @@ end
14671468

14681469
--- Checks if treesitter parser for language is installed
14691470
---@param lang string
1471+
---@param query_name string
14701472
---@return boolean
1471-
function M.has_ts_parser(lang)
1472-
if M.__HAS_NVIM_011 then
1473-
return vim.treesitter.language.add(lang)
1474-
-- ensure the language has a highlights parser or we get
1475-
-- no highlights for langugaes like json/jsonc/toml/etc
1476-
and #vim.treesitter.query.get_files(lang, "highlights") > 0
1477-
or false
1478-
else
1479-
return (pcall(vim.treesitter.language.add, lang))
1480-
end
1473+
function M.has_ts_parser(lang, query_name)
1474+
-- ensure the language has a highlights parser or we get
1475+
-- no highlights for langugaes like json/jsonc/toml/etc
1476+
return (M.__HAS_NVIM_011 and vim.treesitter.language.add(lang)
1477+
or (pcall(vim.treesitter.language.add, lang)))
1478+
and #vim.treesitter.query.get_files(lang, query_name) > 0 or false
1479+
end
1480+
1481+
-- query.get_files, which looks expensive recursively check modeline
1482+
-- also call nvim_get_runtime_file -> shell scripts
1483+
if memoize then
1484+
M.has_ts_parser = memoize("concat-2", M.has_ts_parser, false)
14811485
end
14821486

14831487
--- Wrapper around vim.lsp.jump_to_location which was deprecated in v0.11

lua/fzf-lua/win.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ function FzfWin:treesitter_attach()
10291029

10301030
local lang = vim.treesitter.language.get_lang(ft)
10311031
if not lang then return end
1032-
local loaded = lang and utils.has_ts_parser(lang)
1032+
local loaded = lang and utils.has_ts_parser(lang, "highlights")
10331033
if not loaded then return end
10341034

10351035
-- NOTE: if the line contains unicode characters `#line > win_width`

0 commit comments

Comments
 (0)