54 lines
1.1 KiB
Lua
54 lines
1.1 KiB
Lua
---@class lib.prettier
|
|
local M = {}
|
|
|
|
---@alias ConformCtx {buf: number, filename: string, dirname: string}
|
|
|
|
M.supported = {
|
|
'css',
|
|
'graphql',
|
|
'handlebars',
|
|
'html',
|
|
'javascript',
|
|
'javascriptreact',
|
|
'json',
|
|
'jsonc',
|
|
'less',
|
|
'markdown',
|
|
'markdown.mdx',
|
|
'scss',
|
|
'typescript',
|
|
'typescriptreact',
|
|
'vue',
|
|
'yaml',
|
|
}
|
|
|
|
---Check if a parser exists for the given context.
|
|
---@param ctx ConformCtx
|
|
function M.has_parser(ctx)
|
|
local ft = vim.bo[ctx.buf].filetype --[[@as string]]
|
|
|
|
-- Default filetypes are always supported.
|
|
if vim.tbl_contains(M.supported, ft) then
|
|
return true
|
|
end
|
|
|
|
-- Otherwise, check if a parser can be inferred.
|
|
local inferred = vim.fn.system({ 'prettier', '--file-info', ctx.filename })
|
|
|
|
---@type boolean, string?
|
|
local ok, parser = pcall(function()
|
|
return vim.fn.json_decode(inferred).inferredParser
|
|
end)
|
|
|
|
return ok and parser and parser ~= vim.NIL
|
|
end
|
|
|
|
---Check if a Prettier config file exists in the current context.
|
|
---@param ctx ConformCtx
|
|
function M.has_config(ctx)
|
|
vim.fn.system({ 'prettier', '--find-config-path', ctx.filename })
|
|
|
|
return vim.v.shell_error == 0
|
|
end
|
|
|
|
return M
|