Refactor: Add parsers.get_buf_lang

This commit is contained in:
Stephan Seitz 2020-07-10 21:47:50 +02:00 committed by Thomas Vigouroux
parent 1849f30bb5
commit a4e2692c7b
5 changed files with 18 additions and 11 deletions

View file

@ -104,7 +104,7 @@ end
local function enable_module(mod, bufnr, lang)
local bufnr = bufnr or api.nvim_get_current_buf()
local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, 'ft'))
local lang = lang or parsers.get_buf_lang(bufnr)
if not parsers.list[lang] then
return
@ -157,7 +157,7 @@ end
local function disable_module(mod, bufnr, lang)
local bufnr = bufnr or api.nvim_get_current_buf()
local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, 'ft'))
local lang = lang or parsers.get_buf_lang(bufnr)
if not lang then
return
end

View file

@ -54,7 +54,7 @@ hlmap["include"] = "TSInclude"
function M.attach(bufnr, lang)
local bufnr = bufnr or api.nvim_get_current_buf()
local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, 'ft'))
local lang = parsers.get_buf_lang(bufnr, lang)
local config = configs.get_module('highlight')
for k, v in pairs(config.custom_captures) do

View file

@ -43,7 +43,7 @@ end
function M.iter_locals(bufnr, root, query_kind)
query_kind = query_kind or 'locals'
local lang = parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, "ft"))
local lang = parsers.get_buf_lang(bufnr)
if not lang then return end
local query = queries.get_query(lang, query_kind)

View file

@ -257,14 +257,14 @@ end
function M.has_parser(lang)
local buf = api.nvim_get_current_buf()
local lang = lang or M.ft_to_lang(api.nvim_buf_get_option(buf, 'ft'))
local lang = M.get_buf_lang(buf) or lang
if not lang or #lang == 0 then return false end
return #api.nvim_get_runtime_file('parser/' .. lang .. '.*', false) > 0
end
function M.get_parser(bufnr, lang)
local buf = bufnr or api.nvim_get_current_buf()
local lang = lang or M.ft_to_lang(api.nvim_buf_get_option(buf, 'ft'))
local lang = lang or M.get_buf_lang(buf)
if M.has_parser(lang) then
if not M[buf] then
@ -277,4 +277,12 @@ function M.get_parser(bufnr, lang)
end
end
-- get language of given buffer
-- @param optional buffer number or current buffer
-- @returns language string of buffer
function M.get_buf_lang(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
return M.ft_to_lang(api.nvim_buf_get_option(bufnr, "ft"))
end
return M

View file

@ -11,9 +11,8 @@ local M = {}
function M.select_textobject(query_string)
local bufnr = vim.api.nvim_get_current_buf()
local ft = api.nvim_buf_get_option(bufnr, "ft")
if not ft then return end
local lang = parsers.ft_to_lang(ft)
local lang = parsers.get_buf_lang(bufnr)
if not lang then return end
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1
@ -77,7 +76,7 @@ end
function M.attach(bufnr, lang)
local buf = bufnr or api.nvim_get_current_buf()
local config = configs.get_module("textobjects")
local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, "ft"))
local lang = lang or parsers.get_buf_lang(buf)
for mapping, query in pairs(config.keymaps) do
if type(query) == 'table' then
@ -96,7 +95,7 @@ end
function M.detach(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
local config = configs.get_module("textobjects")
local lang = parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, "ft"))
local lang = parsers.get_buf_lang(bufnr)
for mapping, query in pairs(config.keymaps) do
if type(query) == 'table' then