fix: add ts_parsers.ft_to_lang to detect lang (#373)
Add detection of a proper buffer language by `ts_parsers.ft_to_lang` as `ts_parsers.has_parser` works with `language` but not with `filetype` https://github.com/nvim-treesitter/nvim-treesitter/issues/796
This commit is contained in:
@@ -49,31 +49,40 @@ utils.job_maker = function(cmd, bufnr, opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function has_filetype(ft)
|
||||||
|
return ft and ft ~= ''
|
||||||
|
end
|
||||||
|
|
||||||
--- Attach default highlighter which will choose between regex and ts
|
--- Attach default highlighter which will choose between regex and ts
|
||||||
utils.highlighter = function(bufnr, ft)
|
utils.highlighter = function(bufnr, ft)
|
||||||
if ft and ft ~= '' then
|
if not(utils.ts_highlighter(bufnr, ft)) then
|
||||||
if has_ts and ts_parsers.has_parser(ft) then
|
utils.regex_highlighter(bufnr, ft)
|
||||||
ts_highlight.attach(bufnr, ft)
|
|
||||||
else
|
|
||||||
vim.cmd(':ownsyntax ' .. ft)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Attach regex highlighter
|
--- Attach regex highlighter
|
||||||
utils.regex_highlighter = function(_, ft)
|
utils.regex_highlighter = function(_, ft)
|
||||||
if ft and ft ~= '' then
|
if has_filetype(ft) then
|
||||||
vim.cmd(':ownsyntax ' .. ft)
|
vim.cmd(':ownsyntax ' .. ft)
|
||||||
|
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Attach ts highlighter
|
-- Attach ts highlighter
|
||||||
utils.ts_highlighter = function(bufnr, ft)
|
utils.ts_highlighter = function(bufnr, ft)
|
||||||
if ft and ft ~= '' then
|
if has_filetype(ft) then
|
||||||
if has_ts and ts_parsers.has_parser(ft) then
|
local lang = ts_parsers.ft_to_lang(ft);
|
||||||
ts_highlight.attach(bufnr, ft)
|
if has_ts and ts_parsers.has_parser(lang) then
|
||||||
end
|
ts_highlight.attach(bufnr, lang)
|
||||||
|
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
return utils
|
return utils
|
||||||
|
|||||||
Reference in New Issue
Block a user